Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +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 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
| 21 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 26 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangList; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangStatusType; |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 34 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 35 | |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.core.Is.is; |
| 38 | |
| 39 | /** |
| 40 | * Test cases for testing container listener. |
| 41 | */ |
| 42 | public class ContainerListenerTest { |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 48 | |
| 49 | /** |
| 50 | * Checks container statement as sub-statement of module. |
| 51 | */ |
| 52 | @Test |
| 53 | public void processModuleSubStatementContainer() throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementContainer.yang"); |
| 56 | |
| 57 | assertThat((node instanceof YangModule), is(true)); |
| 58 | |
| 59 | // Check whether the node type is set properly to module. |
| 60 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 61 | |
| 62 | // Check whether the module name is set correctly. |
| 63 | YangModule yangNode = (YangModule) node; |
| 64 | assertThat(yangNode.getName(), is("Test")); |
| 65 | |
| 66 | // Check whether the container is child of module |
| 67 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 68 | assertThat(yangContainer.getName(), is("valid")); |
| 69 | } |
| 70 | |
| 71 | /** |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 72 | * Checks if container identifier in module is duplicate. |
| 73 | */ |
| 74 | @Test(expected = ParserException.class) |
| 75 | public void processModuleDuplicateContainer() throws IOException, ParserException { |
| 76 | |
| 77 | YangNode node = manager.getDataModel("src/test/resources/ModuleDuplicateContainer.yang"); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if container identifier in container is duplicate. |
| 82 | */ |
| 83 | @Test(expected = ParserException.class) |
| 84 | public void processContainerDuplicateContainer() throws IOException, ParserException { |
| 85 | |
| 86 | YangNode node = manager.getDataModel("src/test/resources/ContainerDuplicateContainer.yang"); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks if container identifier in list is duplicate. |
| 91 | */ |
| 92 | @Test(expected = ParserException.class) |
| 93 | public void processListDuplicateContainer() throws IOException, ParserException { |
| 94 | |
| 95 | YangNode node = manager.getDataModel("src/test/resources/ListDuplicateContainer.yang"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Checks if container identifier collides with list at same level. |
| 100 | */ |
| 101 | @Test(expected = ParserException.class) |
| 102 | public void processDuplicateContainerAndList() throws IOException, ParserException { |
| 103 | |
| 104 | YangNode node = manager.getDataModel("src/test/resources/DuplicateContainerAndList.yang"); |
| 105 | } |
| 106 | |
| 107 | /** |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 108 | * Checks container statement as sub-statement of container. |
| 109 | */ |
| 110 | @Test |
| 111 | public void processContainerSubStatementContainer() throws IOException, ParserException { |
| 112 | |
| 113 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementContainer.yang"); |
| 114 | |
| 115 | assertThat((node instanceof YangModule), is(true)); |
| 116 | |
| 117 | // Check whether the node type is set properly to module. |
| 118 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 119 | |
| 120 | // Check whether the module name is set correctly. |
| 121 | YangModule yangNode = (YangModule) node; |
| 122 | assertThat(yangNode.getName(), is("Test")); |
| 123 | |
| 124 | // Check whether the container is child of module |
| 125 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 126 | assertThat(yangContainer.getName(), is("ospf")); |
| 127 | |
| 128 | // Check whether the container is child of container |
| 129 | YangContainer yangContainer1 = (YangContainer) yangContainer.getChild(); |
| 130 | assertThat(yangContainer1.getName(), is("valid")); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks container statement as sub-statement of list. |
| 135 | */ |
| 136 | @Test |
| 137 | public void processListSubStatementContainer() throws IOException, ParserException { |
| 138 | |
| 139 | YangNode node = manager.getDataModel("src/test/resources/ListSubStatementContainer.yang"); |
| 140 | |
| 141 | assertThat((node instanceof YangModule), is(true)); |
| 142 | |
| 143 | // Check whether the node type is set properly to module. |
| 144 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 145 | |
| 146 | // Check whether the module name is set correctly. |
| 147 | YangModule yangNode = (YangModule) node; |
| 148 | assertThat(yangNode.getName(), is("Test")); |
| 149 | |
| 150 | // Check whether the list is child of module |
| 151 | YangList yangList1 = (YangList) yangNode.getChild(); |
| 152 | assertThat(yangList1.getName(), is("ospf")); |
| 153 | |
| 154 | ListIterator<String> keyList = yangList1.getKeyList().listIterator(); |
| 155 | assertThat(keyList.next(), is("process-id")); |
| 156 | |
| 157 | // Check whether the list is child of list |
| 158 | YangContainer yangContainer = (YangContainer) yangList1.getChild(); |
| 159 | assertThat(yangContainer.getName(), is("interface")); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Checks container with all its sub-statements. |
| 164 | */ |
| 165 | @Test |
| 166 | public void processContainerSubStatements() throws IOException, ParserException { |
| 167 | |
| 168 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatements.yang"); |
| 169 | |
| 170 | assertThat((node instanceof YangModule), is(true)); |
| 171 | |
| 172 | // Check whether the node type is set properly to module. |
| 173 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 174 | |
| 175 | // Check whether the module name is set correctly. |
| 176 | YangModule yangNode = (YangModule) node; |
| 177 | assertThat(yangNode.getName(), is("Test")); |
| 178 | |
| 179 | // Check whether the container is child of module |
| 180 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 181 | |
| 182 | // Check whether container properties as set correctly. |
| 183 | assertThat(yangContainer.getName(), is("ospf")); |
| 184 | |
| 185 | assertThat(yangContainer.isConfig(), is(true)); |
| 186 | assertThat(yangContainer.getPresence(), is("\"ospf logs\"")); |
| 187 | assertThat(yangContainer.getDescription(), is("\"container description\"")); |
| 188 | assertThat(yangContainer.getStatus(), is(YangStatusType.CURRENT)); |
| 189 | assertThat(yangContainer.getReference(), is("\"container reference\"")); |
| 190 | |
| 191 | // Check whether leaf properties as set correctly. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 192 | ListIterator<YangLeaf> leafIterator = yangContainer.getListOfLeaf().listIterator(); |
| 193 | YangLeaf leafInfo = leafIterator.next(); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 194 | |
| 195 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 196 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 197 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 198 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 199 | assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT)); |
| 200 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Checks cardinality of sub-statements of container. |
| 205 | */ |
| 206 | @Test |
| 207 | public void processContainerSubStatementCardinality() throws IOException, ParserException { |
| 208 | thrown.expect(ParserException.class); |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 209 | thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"container valid\"."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 210 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang"); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Checks container as root node. |
| 215 | */ |
| 216 | @Test |
| 217 | public void processContainerRootNode() throws IOException, ParserException { |
| 218 | thrown.expect(ParserException.class); |
| 219 | thrown.expectMessage("no viable alternative at input 'container'"); |
| 220 | YangNode node = manager.getDataModel("src/test/resources/ContainerRootNode.yang"); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Checks invalid identifier for container statement. |
| 225 | */ |
| 226 | @Test |
| 227 | public void processContainerInvalidIdentifier() throws IOException, ParserException { |
| 228 | thrown.expect(ParserException.class); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 229 | thrown.expectMessage("YANG file error : container name 1valid is not valid."); |
Vidyashree Rama | 50b73a0 | 2016-02-13 22:22:12 +0530 | [diff] [blame] | 230 | YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang"); |
| 231 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 232 | } |