Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 22 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 24 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | |
| 28 | import static org.hamcrest.core.Is.is; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 29 | import static org.hamcrest.core.IsNull.notNullValue; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 30 | import static org.junit.Assert.assertThat; |
| 31 | |
| 32 | /** |
| 33 | * Test cases for testing submodule listener functionality. |
| 34 | */ |
| 35 | public class SubModuleListenerTest { |
| 36 | |
| 37 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 38 | |
| 39 | /** |
| 40 | * Checks if the sub module listeners updates the data model tree. |
| 41 | */ |
| 42 | @Test |
| 43 | public void processSubModuleValidEntry() throws IOException, ParserException { |
| 44 | |
| 45 | YangNode node = manager.getDataModel("src/test/resources/SubModuleValidEntry.yang"); |
| 46 | |
| 47 | // Check whether the data model tree returned is of type module. |
| 48 | assertThat((node instanceof YangSubModule), is(true)); |
| 49 | |
| 50 | // Check whether the node type is set properly to module. |
| 51 | assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE)); |
| 52 | |
| 53 | YangSubModule yangNode = (YangSubModule) node; |
| 54 | // Check whether the module name is set correctly. |
| 55 | assertThat(yangNode.getName(), is("Test")); |
| 56 | // Checks for the version value in data model tree. |
| 57 | assertThat(yangNode.getVersion(), is((byte) 1)); |
| 58 | // Checks identifier of belongsto in data model tree. |
| 59 | assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS")); |
| 60 | // Checks for the version value in data model tree. |
| 61 | assertThat(yangNode.getBelongsTo().getPrefix(), is("On1")); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Checks if the yang version and belongs to can come in any order in sub |
| 66 | * module. |
| 67 | */ |
| 68 | @Test |
| 69 | public void processSubModuleOrder() throws IOException, ParserException { |
| 70 | |
| 71 | YangNode node = manager.getDataModel("src/test/resources/SubModuleOrder.yang"); |
| 72 | |
| 73 | // Check whether the data model tree returned is of type module. |
| 74 | assertThat((node instanceof YangSubModule), is(true)); |
| 75 | |
| 76 | // Check whether the node type is set properly to module. |
| 77 | assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE)); |
| 78 | |
| 79 | YangSubModule yangNode = (YangSubModule) node; |
| 80 | // Check whether the module name is set correctly. |
| 81 | assertThat(yangNode.getName(), is("Test")); |
| 82 | // Checks for the version value in data model tree. |
| 83 | assertThat(yangNode.getVersion(), is((byte) 1)); |
| 84 | // Checks identifier of belongsto in data model tree. |
| 85 | assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS")); |
| 86 | // Checks for the version value in data model tree. |
| 87 | assertThat(yangNode.getBelongsTo().getPrefix(), is("On1")); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks if yang version is optional. |
| 92 | */ |
| 93 | @Test |
| 94 | public void processSubModuleWithoutVersion() throws IOException, ParserException { |
| 95 | |
| 96 | YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutVersion.yang"); |
| 97 | |
| 98 | // Check whether the data model tree returned is of type module. |
| 99 | assertThat((node instanceof YangSubModule), is(true)); |
| 100 | |
| 101 | // Check whether the node type is set properly to module. |
| 102 | assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE)); |
| 103 | |
| 104 | YangSubModule yangNode = (YangSubModule) node; |
| 105 | // Check whether the module name is set correctly. |
| 106 | assertThat(yangNode.getName(), is("Test")); |
| 107 | // Checks identifier of belongsto in data model tree. |
| 108 | assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS")); |
| 109 | // Checks for the version value in data model tree. |
| 110 | assertThat(yangNode.getBelongsTo().getPrefix(), is("On1")); |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 111 | //Checks the revision with current date is created for empty revision statement. |
| 112 | assertThat(((YangSubModule) node).getRevision().getRevDate(), notNullValue()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Checks if sub module name is correct. |
| 117 | */ |
| 118 | @Test(expected = ParserException.class) |
| 119 | public void processSubModuleInvalidName() throws IOException, ParserException { |
| 120 | |
| 121 | YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang"); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Checks if sub module has invalid modules construct eg namespace. |
| 126 | */ |
| 127 | @Test(expected = ParserException.class) |
| 128 | public void processSubModuleWithNamespace() throws IOException, ParserException { |
| 129 | |
| 130 | YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang"); |
| 131 | } |
| 132 | } |