Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.text.ParseException; |
| 21 | import java.text.SimpleDateFormat; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 27 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 28 | import static org.hamcrest.core.Is.is; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | |
| 31 | /** |
| 32 | * Test cases for testing include listener functionality. |
| 33 | */ |
| 34 | public class IncludeListenerTest { |
| 35 | |
| 36 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 37 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
| 38 | private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Checks if include listener with ; is valid and updates the data |
| 42 | * model tree. |
| 43 | */ |
| 44 | @Test |
| 45 | public void processIncludeWithStmtend() throws IOException, ParserException { |
| 46 | |
| 47 | YangNode node = manager.getDataModel("src/test/resources/IncludeWithStmtend.yang"); |
| 48 | |
| 49 | // Checks for the sub module name in data model tree. |
| 50 | assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut")); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Checks if include listener with braces and without revision date is valid |
| 55 | * and updates the data model tree. |
| 56 | */ |
| 57 | @Test |
| 58 | public void processIncludeWithEmptyBody() throws IOException, ParserException { |
| 59 | |
| 60 | YangNode node = manager.getDataModel("src/test/resources/IncludeWithEmptyBody.yang"); |
| 61 | |
| 62 | // Checks for the sub module name in data model tree. |
| 63 | assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut")); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Checks if include listener with braces and with revision date is valid |
| 68 | * and updates the data model tree. |
| 69 | */ |
| 70 | @Test |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 71 | public void processIncludeWithDate() throws IOException, ParserException, ParseException { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 72 | |
| 73 | YangNode node = manager.getDataModel("src/test/resources/IncludeWithDate.yang"); |
| 74 | |
| 75 | // Checks for the sub module name in data model tree. |
| 76 | assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut")); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 77 | assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03"))); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if include has more than one occurrence. |
| 82 | */ |
| 83 | @Test |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 84 | public void processIncludeMultiInstance() throws IOException, ParserException, ParseException { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 85 | |
| 86 | YangNode node = manager.getDataModel("src/test/resources/IncludeMultiInstance.yang"); |
| 87 | |
| 88 | // Checks for the sub module name in data model tree. |
| 89 | assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut")); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 90 | assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03"))); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 91 | assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn")); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 92 | assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is(simpleDateFormat.parse("2014-02-03"))); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Checks if include and import can come in any order. |
| 97 | */ |
| 98 | @Test |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 99 | public void processIncludeImportAnyOrder() throws IOException, ParserException, ParseException { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 100 | |
| 101 | YangNode node = manager.getDataModel("src/test/resources/IncludeImportAnyOrder.yang"); |
| 102 | |
| 103 | // Checks for the sub module name in data model tree. |
| 104 | assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut")); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 105 | assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03"))); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 106 | assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn")); |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 107 | assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is(simpleDateFormat.parse("2014-02-03"))); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Checks if syntax of Include is not correct. |
| 112 | */ |
| 113 | @Test(expected = ParserException.class) |
| 114 | public void processIncludeInvalidSyntax() throws IOException, ParserException { |
| 115 | |
| 116 | YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidSyntax.yang"); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Checks if syntax of revision date in Include is not correct. |
| 121 | */ |
| 122 | @Test(expected = ParserException.class) |
| 123 | public void processIncludeInvalidDateSyntax() throws IOException, ParserException { |
| 124 | |
| 125 | YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidDateSyntax.yang"); |
| 126 | } |
| 127 | } |