janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.linker; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.ListIterator; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.rules.ExpectedException; |
| 24 | import org.onosproject.yangutils.datamodel.ResolvableStatus; |
| 25 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 26 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 27 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 29 | import org.onosproject.yangutils.datamodel.YangList; |
| 30 | import org.onosproject.yangutils.datamodel.YangModule; |
| 31 | import org.onosproject.yangutils.datamodel.YangNode; |
| 32 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 33 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 34 | import org.onosproject.yangutils.datamodel.YangUses; |
| 35 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 36 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.core.Is.is; |
| 40 | |
| 41 | /** |
| 42 | * Test cases for testing uses intra file linking. |
| 43 | */ |
| 44 | public class IntraFileUsesLinkingTest { |
| 45 | |
| 46 | @Rule |
| 47 | public ExpectedException thrown = ExpectedException.none(); |
| 48 | |
| 49 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 50 | |
| 51 | /** |
| 52 | * Checks self resolution when grouping and uses are siblings. |
| 53 | * Grouping followed by uses. |
| 54 | */ |
| 55 | @Test |
| 56 | public void processSelfResolutionWhenUsesAndGroupingAtRootLevel() |
| 57 | throws IOException, ParserException { |
| 58 | |
| 59 | YangNode node = manager.getDataModel("src/test/resources/SelfResolutionWhenUsesAndGroupingAtRootLevel.yang"); |
| 60 | |
| 61 | // Check whether the data model tree returned is of type module. |
| 62 | assertThat((node instanceof YangModule), is(true)); |
| 63 | |
| 64 | // Check whether the node type is set properly to module. |
| 65 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 66 | |
| 67 | // Check whether the module name is set correctly. |
| 68 | YangModule yangNode = (YangModule) node; |
| 69 | assertThat(yangNode.getName(), is("Test")); |
| 70 | |
| 71 | ListIterator<YangLeaf> leafIterator; |
| 72 | YangLeaf leafInfo; |
| 73 | |
| 74 | // Check whether grouping is the sibling of module's child. |
| 75 | assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true)); |
| 76 | |
| 77 | YangGrouping grouping = (YangGrouping) yangNode.getChild().getNextSibling(); |
| 78 | leafIterator = grouping.getListOfLeaf().listIterator(); |
| 79 | leafInfo = leafIterator.next(); |
| 80 | |
| 81 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 82 | assertThat(leafInfo.getName(), is("hello")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 83 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 84 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 85 | |
| 86 | // Check whether uses is module's child. |
| 87 | assertThat((yangNode.getChild() instanceof YangUses), is(true)); |
| 88 | YangUses uses = (YangUses) yangNode.getChild(); |
| 89 | |
| 90 | // Check whether uses get resolved. |
| 91 | assertThat(uses.getResolvableStatus(), |
| 92 | is(ResolvableStatus.RESOLVED)); |
| 93 | |
| 94 | leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 95 | leafInfo = leafIterator.next(); |
| 96 | |
| 97 | // Check whether the information in the leaf is correct under module. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 98 | assertThat(leafInfo.getName(), is("hello")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 99 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 100 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Checks self resolution when grouping and uses are siblings. |
| 106 | * Grouping has a child node. |
| 107 | */ |
| 108 | @Test |
| 109 | public void processSelfResolutionWhenUsesAndGroupingAtRootLevelGroupingWithChild() |
| 110 | throws IOException, ParserException { |
| 111 | |
| 112 | YangNode node = manager.getDataModel( |
| 113 | "src/test/resources/SelfResolutionWhenUsesAndGroupingAtRootLevelGroupingWithChild.yang"); |
| 114 | |
| 115 | // Check whether the data model tree returned is of type module. |
| 116 | assertThat((node instanceof YangModule), is(true)); |
| 117 | |
| 118 | // Check whether the node type is set properly to module. |
| 119 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 120 | |
| 121 | // Check whether the module name is set correctly. |
| 122 | YangModule yangNode = (YangModule) node; |
| 123 | assertThat(yangNode.getName(), is("Test")); |
| 124 | |
| 125 | ListIterator<YangLeaf> leafIterator; |
| 126 | YangLeaf leafInfo; |
| 127 | |
| 128 | // Check whether grouping is the sibling of module's child. |
| 129 | assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true)); |
| 130 | |
| 131 | YangGrouping grouping = (YangGrouping) yangNode.getChild().getNextSibling(); |
| 132 | leafIterator = grouping.getListOfLeaf().listIterator(); |
| 133 | leafInfo = leafIterator.next(); |
| 134 | |
| 135 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 136 | assertThat(leafInfo.getName(), is("treat")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 137 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 138 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 139 | |
| 140 | // Check whether container is the child of grouping. |
| 141 | assertThat((grouping.getChild() instanceof YangContainer), is(true)); |
| 142 | YangContainer container = (YangContainer) grouping.getChild(); |
| 143 | |
| 144 | // Check whether the container name is set correctly which is under grouping. |
| 145 | assertThat(container.getName(), is("test")); |
| 146 | |
| 147 | leafIterator = container.getListOfLeaf().listIterator(); |
| 148 | leafInfo = leafIterator.next(); |
| 149 | |
| 150 | // Check whether the information in the leaf is correct under container which is under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 151 | assertThat(leafInfo.getName(), is("leaf2")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 152 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 153 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 154 | |
| 155 | // Check whether uses is module's child. |
| 156 | assertThat((yangNode.getChild() instanceof YangUses), is(true)); |
| 157 | YangUses uses = (YangUses) yangNode.getChild(); |
| 158 | |
| 159 | // Check whether uses get resolved. |
| 160 | assertThat(uses.getResolvableStatus(), |
| 161 | is(ResolvableStatus.RESOLVED)); |
| 162 | |
| 163 | leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 164 | leafInfo = leafIterator.next(); |
| 165 | |
| 166 | // Check whether the information in the leaf is correct under module. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 167 | assertThat(leafInfo.getName(), is("treat")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 168 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 169 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 170 | |
| 171 | // Check whether container is the child of module. |
| 172 | assertThat((grouping.getNextSibling() instanceof YangContainer), is(true)); |
| 173 | container = (YangContainer) grouping.getNextSibling(); |
| 174 | |
| 175 | // Check whether the container name is set correctly which is under module. |
| 176 | assertThat(container.getName(), is("test")); |
| 177 | |
| 178 | leafIterator = container.getListOfLeaf().listIterator(); |
| 179 | leafInfo = leafIterator.next(); |
| 180 | |
| 181 | // Check whether the information in the leaf is correct under container which is under module. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 182 | assertThat(leafInfo.getName(), is("leaf2")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 183 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 184 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks self resolution when grouping in rpc and uses in output of the same rpc. |
| 189 | * Uses is followed by grouping. |
| 190 | */ |
| 191 | @Test |
| 192 | public void processSelfResolutionGroupingInRpcAndUsesInOutput() |
| 193 | throws IOException, ParserException { |
| 194 | |
| 195 | YangNode node = manager |
| 196 | .getDataModel("src/test/resources/SelfResolutionGroupingInRpcAndUsesInOutput.yang"); |
| 197 | |
| 198 | // Check whether the data model tree returned is of type module. |
| 199 | assertThat((node instanceof YangModule), is(true)); |
| 200 | |
| 201 | // Check whether the node type is set properly to module. |
| 202 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 203 | |
| 204 | // Check whether the module name is set correctly. |
| 205 | YangModule yangNode = (YangModule) node; |
| 206 | assertThat(yangNode.getName(), is("rock")); |
| 207 | |
| 208 | ListIterator<YangLeaf> leafIterator; |
| 209 | YangLeaf leafInfo; |
| 210 | |
| 211 | // Check whether grouping is the child of rpc. |
| 212 | assertThat((yangNode.getChild().getChild() instanceof YangGrouping), is(true)); |
| 213 | YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild(); |
| 214 | |
| 215 | // Check whether the grouping name is set correctly. |
| 216 | assertThat(grouping.getName(), is("hello")); |
| 217 | |
| 218 | // Check whether list is the child of grouping. |
| 219 | assertThat((grouping.getChild() instanceof YangList), is(true)); |
| 220 | YangList yangListNode = (YangList) grouping.getChild(); |
| 221 | |
| 222 | // Check whether the list name is set correctly. |
| 223 | assertThat(yangListNode.getName(), is("valid")); |
| 224 | |
| 225 | leafIterator = yangListNode.getListOfLeaf().listIterator(); |
| 226 | leafInfo = leafIterator.next(); |
| 227 | |
| 228 | // Check whether the information in the leaf is correct under list which is under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 229 | assertThat(leafInfo.getName(), is("invalid-interval")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 230 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 231 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 232 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 233 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 234 | |
| 235 | // Check whether uses is input's child. |
| 236 | assertThat((yangNode.getChild().getChild().getNextSibling().getChild() instanceof YangUses), is(true)); |
| 237 | YangUses uses = (YangUses) yangNode.getChild().getChild().getNextSibling().getChild(); |
| 238 | |
| 239 | // Check whether uses get resolved. |
| 240 | assertThat(uses.getResolvableStatus(), |
| 241 | is(ResolvableStatus.RESOLVED)); |
| 242 | |
| 243 | // Check whether list is the sibling of uses which has been deep copied from grouping. |
| 244 | assertThat((yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling() instanceof YangList), |
| 245 | is(true)); |
| 246 | YangList yangList = (YangList) yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling(); |
| 247 | |
| 248 | // Check whether the list name is set correctly. |
| 249 | assertThat(yangList.getName(), is("valid")); |
| 250 | |
| 251 | leafIterator = yangList.getListOfLeaf().listIterator(); |
| 252 | leafInfo = leafIterator.next(); |
| 253 | |
| 254 | // Check whether the information in the leaf is correct under list which is deep copied. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 255 | assertThat(leafInfo.getName(), is("invalid-interval")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 256 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 257 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 258 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 259 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 260 | |
| 261 | // Check whether uses is output's child. |
| 262 | assertThat((yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild() instanceof YangUses), |
| 263 | is(true)); |
| 264 | YangUses usesInOuput = (YangUses) yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild(); |
| 265 | |
| 266 | // Check whether uses get resolved. |
| 267 | assertThat(usesInOuput.getResolvableStatus(), |
| 268 | is(ResolvableStatus.RESOLVED)); |
| 269 | |
| 270 | // Check whether list is the sibling of uses which has been deep copied from grouping. |
| 271 | assertThat((yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling() instanceof YangList), |
| 272 | is(true)); |
| 273 | |
| 274 | YangList yangListInOutput = (YangList) yangNode.getChild().getChild().getNextSibling().getNextSibling() |
| 275 | .getChild().getNextSibling(); |
| 276 | |
| 277 | // Check whether the list name is set correctly. |
| 278 | assertThat(yangListInOutput.getName(), is("valid")); |
| 279 | |
| 280 | leafIterator = yangListInOutput.getListOfLeaf().listIterator(); |
| 281 | leafInfo = leafIterator.next(); |
| 282 | |
| 283 | // Check whether the information in the leaf is correct under list which is deep copied. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 284 | assertThat(leafInfo.getName(), is("invalid-interval")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 285 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 286 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 287 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 288 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Checks the failure scenario when uses is referring to its own grouping directly. |
| 293 | */ |
| 294 | @Test |
| 295 | public void processSelfResolutionGroupingReferencingItselfFailureScenerio() |
| 296 | throws IOException, ParserException { |
| 297 | |
| 298 | thrown.expect(ParserException.class); |
| 299 | thrown.expectMessage( |
| 300 | "YANG file error: Duplicate input identifier detected, same as leaf \"zip-code\""); |
| 301 | YangNode node = manager |
| 302 | .getDataModel("src/test/resources/SelfResolutionGroupingReferencingItselfFailureScenerio.yang"); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Checks the when multiple uses are present and are referred to the grouping at different levels. |
| 308 | */ |
| 309 | @Test |
| 310 | public void processSelfResolutionGroupingWithMultipleUses() |
| 311 | throws IOException, ParserException { |
| 312 | |
| 313 | YangNode node = manager |
| 314 | .getDataModel("src/test/resources/SelfResolutionGroupingWithMultipleUses.yang"); |
| 315 | |
| 316 | // Check whether the data model tree returned is of type module. |
| 317 | assertThat((node instanceof YangModule), is(true)); |
| 318 | |
| 319 | // Check whether the node type is set properly to module. |
| 320 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 321 | |
| 322 | // Check whether the module name is set correctly. |
| 323 | YangModule yangNode = (YangModule) node; |
| 324 | assertThat(yangNode.getName(), is("Test")); |
| 325 | |
| 326 | ListIterator<YangLeaf> leafIterator; |
| 327 | YangLeaf leafInfo; |
| 328 | |
| 329 | // Check whether grouping is the child of container. |
| 330 | assertThat((yangNode.getChild().getChild() instanceof YangGrouping), is(true)); |
| 331 | YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild(); |
| 332 | |
| 333 | // Check whether the grouping name is set correctly. |
| 334 | assertThat(grouping.getName(), is("endpoint")); |
| 335 | |
| 336 | // Check whether uses is endpoint-grouping's child. |
| 337 | assertThat((grouping.getChild() instanceof YangUses), is(true)); |
| 338 | YangUses firstUses = (YangUses) grouping.getChild(); |
| 339 | |
| 340 | // Check whether uses get resolved. |
| 341 | assertThat(firstUses.getResolvableStatus(), |
| 342 | is(ResolvableStatus.RESOLVED)); |
| 343 | |
| 344 | // Check whether container is the sibling of uses. |
| 345 | assertThat((firstUses.getNextSibling() instanceof YangContainer), is(true)); |
| 346 | YangContainer yangContainer = (YangContainer) firstUses.getNextSibling(); |
| 347 | |
| 348 | // Check whether the container name is set correctly. |
| 349 | assertThat(yangContainer.getName(), is("design")); |
| 350 | |
| 351 | leafIterator = yangContainer.getListOfLeaf().listIterator(); |
| 352 | leafInfo = leafIterator.next(); |
| 353 | |
| 354 | // Check whether the information in the leaf is correct under design-container. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 355 | assertThat(leafInfo.getName(), is("ink")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 356 | assertThat(leafInfo.getDataType().getDataTypeName(), is("int32")); |
| 357 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32)); |
| 358 | |
| 359 | // Check whether uses is design-container's child. |
| 360 | assertThat((yangContainer.getChild() instanceof YangUses), is(true)); |
| 361 | YangUses secondUses = (YangUses) yangContainer.getChild(); |
| 362 | |
| 363 | // Check whether uses get resolved. |
| 364 | assertThat(secondUses.getResolvableStatus(), |
| 365 | is(ResolvableStatus.RESOLVED)); |
| 366 | |
| 367 | // Check whether container is the sibling of uses. |
| 368 | assertThat((secondUses.getNextSibling() instanceof YangContainer), is(true)); |
| 369 | YangContainer yangContainer2 = (YangContainer) secondUses.getNextSibling(); |
| 370 | assertThat(yangContainer2.getName(), is("correct")); |
| 371 | |
| 372 | leafIterator = yangContainer2.getListOfLeaf().listIterator(); |
| 373 | leafInfo = leafIterator.next(); |
| 374 | |
| 375 | // Check whether the information in the leaf is correct under correct-container. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 376 | assertThat(leafInfo.getName(), is("newone")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 377 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 378 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 379 | |
| 380 | // Check whether uses is correct container's child. |
| 381 | assertThat((yangContainer2.getChild() instanceof YangUses), is(true)); |
| 382 | YangUses thirdUses = (YangUses) yangContainer2.getChild(); |
| 383 | |
| 384 | // Check whether uses get resolved. |
| 385 | assertThat(thirdUses.getResolvableStatus(), |
| 386 | is(ResolvableStatus.RESOLVED)); |
| 387 | |
| 388 | // Check whether container is the sibling of uses. |
| 389 | assertThat((thirdUses.getNextSibling() instanceof YangContainer), is(true)); |
| 390 | |
| 391 | YangContainer yangContainer3 = (YangContainer) thirdUses.getNextSibling(); |
| 392 | assertThat(yangContainer3.getName(), is("value")); |
| 393 | |
| 394 | leafIterator = yangContainer3.getListOfLeaf().listIterator(); |
| 395 | leafInfo = leafIterator.next(); |
| 396 | |
| 397 | // Check whether the information in the leaf is correct under container |
| 398 | // which has been deep copied from grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 399 | assertThat(leafInfo.getName(), is("zip-code")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 400 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 401 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 402 | |
| 403 | // Check whether uses is the sibling of container-design. |
| 404 | assertThat((yangContainer.getNextSibling() instanceof YangUses), is(true)); |
| 405 | YangUses fourthUses = (YangUses) yangContainer.getNextSibling(); |
| 406 | |
| 407 | // Check whether uses get resolved. |
| 408 | assertThat(fourthUses.getResolvableStatus(), |
| 409 | is(ResolvableStatus.RESOLVED)); |
| 410 | |
| 411 | // Check whether uses is the sibling of previous uses. |
| 412 | assertThat((fourthUses.getNextSibling() instanceof YangUses), is(true)); |
| 413 | YangUses fifthUses = (YangUses) fourthUses.getNextSibling(); |
| 414 | |
| 415 | // Check whether uses get resolved. |
| 416 | assertThat(fifthUses.getResolvableStatus(), |
| 417 | is(ResolvableStatus.RESOLVED)); |
| 418 | |
| 419 | // Check whether list is the sibling of uses. |
| 420 | assertThat((fifthUses.getNextSibling() instanceof YangList), is(true)); |
| 421 | YangList yangList = (YangList) fifthUses.getNextSibling(); |
| 422 | assertThat(yangList.getName(), is("valid")); |
| 423 | |
| 424 | leafIterator = yangList.getListOfLeaf().listIterator(); |
| 425 | leafInfo = leafIterator.next(); |
| 426 | |
| 427 | // Check whether the information in the leaf is correct under list which has been deep copied from grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 428 | assertThat(leafInfo.getName(), is("invalid-interval")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 429 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 430 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 431 | assertThat(leafInfo.getUnits(), is("\"seconds\"")); |
| 432 | assertThat(leafInfo.getReference(), is("\"RFC 6020\"")); |
| 433 | |
| 434 | // Check whether typedef is the sibling of list. |
| 435 | assertThat((yangList.getNextSibling() instanceof YangTypeDef), is(true)); |
| 436 | YangTypeDef yangTypeDef = (YangTypeDef) yangList.getNextSibling(); |
| 437 | assertThat(yangTypeDef.getName(), is("my-type")); |
| 438 | |
| 439 | leafIterator = grouping.getListOfLeaf().listIterator(); |
| 440 | leafInfo = leafIterator.next(); |
| 441 | |
| 442 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 443 | assertThat(leafInfo.getName(), is("zip-code")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 444 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 445 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 446 | |
| 447 | // Check whether uses is endpoint-grouping's sibling. |
| 448 | assertThat((grouping.getNextSibling() instanceof YangUses), is(true)); |
| 449 | YangUses endpointUses = (YangUses) grouping.getNextSibling(); |
| 450 | |
| 451 | // Check whether uses get resolved. |
| 452 | assertThat(endpointUses.getResolvableStatus(), |
| 453 | is(ResolvableStatus.RESOLVED)); |
| 454 | |
| 455 | assertThat((endpointUses.getNextSibling().getNextSibling().getNextSibling().getNextSibling().getNextSibling() |
| 456 | .getNextSibling() instanceof YangUses), is(true)); |
| 457 | |
| 458 | YangUses yangUsesInEndpoint = (YangUses) endpointUses.getNextSibling().getNextSibling().getNextSibling() |
| 459 | .getNextSibling().getNextSibling().getNextSibling(); |
| 460 | assertThat(yangUsesInEndpoint.getResolvableStatus(), |
| 461 | is(ResolvableStatus.RESOLVED)); |
| 462 | |
| 463 | assertThat((yangUsesInEndpoint.getNextSibling() instanceof YangContainer), is(true)); |
| 464 | YangContainer yangContainerInEndPoint = (YangContainer) yangUsesInEndpoint.getNextSibling(); |
| 465 | |
| 466 | assertThat(yangContainerInEndPoint.getName(), is("design")); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Checks the failure scenario when uses is present under the same node many times. |
| 471 | */ |
| 472 | @Test |
| 473 | public void processSelfResolutionGroupingHavingSameUsesManyTimes() |
| 474 | throws IOException, ParserException { |
| 475 | |
| 476 | thrown.expect(ParserException.class); |
| 477 | thrown.expectMessage( |
| 478 | "YANG file error: Duplicate input identifier detected, same as uses \"failure\""); |
| 479 | YangNode node = manager |
| 480 | .getDataModel("src/test/resources/SelfResolutionGroupingHavingSameUsesManyTimes.yang"); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Checks the rpc having both typedef and grouping. |
| 485 | * It also checks that the grouping under different nodes will not give any problem in resolving uses. |
| 486 | */ |
| 487 | @Test |
| 488 | public void processSelfResolutionRpcWithOneTypedefAndTwoGroupingUnderDifferentNode() |
| 489 | throws IOException, ParserException { |
| 490 | |
| 491 | YangNode node = manager |
| 492 | .getDataModel( |
| 493 | "src/test/resources/SelfResolutionRpcWithOneTypedefAndTwoGroupingUnderDifferentNode.yang"); |
| 494 | |
| 495 | // Check whether the data model tree returned is of type module. |
| 496 | assertThat((node instanceof YangModule), is(true)); |
| 497 | |
| 498 | // Check whether the node type is set properly to module. |
| 499 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 500 | |
| 501 | // Check whether the module name is set correctly. |
| 502 | YangModule yangNode = (YangModule) node; |
| 503 | assertThat(yangNode.getName(), is("rock")); |
| 504 | |
| 505 | ListIterator<YangLeaf> leafIterator; |
| 506 | YangLeaf leafInfo; |
| 507 | |
| 508 | // Check whether grouping is the child of input. |
| 509 | assertThat((yangNode.getChild().getChild().getChild() instanceof YangGrouping), is(true)); |
| 510 | YangGrouping groupingUnderInput = (YangGrouping) yangNode.getChild().getChild().getChild(); |
| 511 | |
| 512 | // Check whether the grouping name is set correctly. |
| 513 | assertThat(groupingUnderInput.getName(), is("creative")); |
| 514 | |
| 515 | leafIterator = groupingUnderInput.getListOfLeaf().listIterator(); |
| 516 | leafInfo = leafIterator.next(); |
| 517 | |
| 518 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 519 | assertThat(leafInfo.getName(), is("carry")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 520 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 521 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 522 | |
| 523 | // Check whether grouping is the child of output. |
| 524 | assertThat((yangNode.getChild().getChild().getNextSibling().getChild() instanceof YangGrouping), is(true)); |
| 525 | YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild().getNextSibling().getChild(); |
| 526 | assertThat(grouping.getName(), is("creative")); |
| 527 | |
| 528 | // Check whether typedef is the sibling of grouping. |
| 529 | assertThat((grouping.getNextSibling() instanceof YangTypeDef), is(true)); |
| 530 | |
| 531 | YangTypeDef typedef = (YangTypeDef) grouping.getNextSibling(); |
| 532 | assertThat(typedef.getName(), is("my-type")); |
| 533 | |
| 534 | // Check whether uses is the sibling of typedef. |
| 535 | assertThat((typedef.getNextSibling() instanceof YangUses), is(true)); |
| 536 | |
| 537 | // Check whether uses get resolved. |
| 538 | YangUses uses = (YangUses) typedef.getNextSibling(); |
| 539 | assertThat(uses.getName(), is("creative")); |
| 540 | assertThat(uses.getResolvableStatus(), |
| 541 | is(ResolvableStatus.RESOLVED)); |
| 542 | |
| 543 | // Check whether list is the sibling of uses. |
| 544 | assertThat((uses.getNextSibling() instanceof YangList), is(true)); |
| 545 | YangList list = (YangList) uses.getNextSibling(); |
| 546 | assertThat(list.getName(), is("valid")); |
| 547 | |
| 548 | leafIterator = list.getListOfLeaf().listIterator(); |
| 549 | leafInfo = leafIterator.next(); |
| 550 | |
| 551 | // Check whether the information in the leaf is correct under list. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 552 | assertThat(leafInfo.getName(), is("invalid-interval")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 553 | assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16")); |
| 554 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16)); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Checks the failure scenario when uses is cannot resolve its grouping. |
| 559 | */ |
| 560 | @Test |
| 561 | public void processSelfResolutionNestedGroupingWithUnresolvedUses() |
| 562 | throws IOException, ParserException { |
| 563 | |
| 564 | thrown.expect(ParserException.class); |
| 565 | thrown.expectMessage( |
| 566 | "YANG file error: Unable to find base typedef/grouping for given type/uses"); |
| 567 | |
| 568 | YangNode node = manager |
| 569 | .getDataModel("src/test/resources/SelfResolutionNestedGroupingWithUnresolvedUses.yang"); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Checks self resolution when typedef hierarchical references are present |
| 574 | * with last type is unresolved. |
| 575 | */ |
| 576 | @Test |
| 577 | public void processSelfFileLinkingWithGroupingHierarchicalRefUnresolved() |
| 578 | throws IOException, ParserException { |
| 579 | |
| 580 | YangNode node = manager |
| 581 | .getDataModel("src/test/resources/SelfFileLinkingWithGroupingHierarchicalRefUnresolved.yang"); |
| 582 | |
| 583 | // Check whether the data model tree returned is of type module. |
| 584 | assertThat((node instanceof YangModule), is(true)); |
| 585 | |
| 586 | // Check whether the node type is set properly to module. |
| 587 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 588 | |
| 589 | // Check whether the module name is set correctly. |
| 590 | YangModule yangNode = (YangModule) node; |
| 591 | assertThat(yangNode.getName(), is("Test")); |
| 592 | |
| 593 | // Check whether container is the sibling of grouping. |
| 594 | assertThat((yangNode.getChild().getNextSibling() instanceof YangContainer), is(true)); |
| 595 | YangContainer containerWithUses = (YangContainer) yangNode.getChild().getNextSibling(); |
| 596 | assertThat(containerWithUses.getName(), is("test")); |
| 597 | |
| 598 | // Check whether uses is the child of container. |
| 599 | assertThat((containerWithUses.getChild() instanceof YangUses), is(true)); |
| 600 | YangUses uses = (YangUses) containerWithUses.getChild(); |
| 601 | assertThat(uses.getName(), is("create")); |
| 602 | |
| 603 | // Check whether uses is getting resolved. |
| 604 | assertThat(uses.getResolvableStatus(), |
| 605 | is(ResolvableStatus.RESOLVED)); |
| 606 | |
| 607 | // Check whether grouping is the child of module. |
| 608 | assertThat((yangNode.getChild() instanceof YangGrouping), is(true)); |
| 609 | YangGrouping groupingWithUses = (YangGrouping) yangNode.getChild(); |
| 610 | assertThat(groupingWithUses.getName(), is("create")); |
| 611 | |
| 612 | // Check whether uses with prefix from from other file, is the child of grouping. |
| 613 | assertThat((groupingWithUses.getChild() instanceof YangUses), is(true)); |
| 614 | YangUses uses1 = (YangUses) groupingWithUses.getChild(); |
| 615 | assertThat(uses1.getName(), is("valid")); |
| 616 | |
| 617 | // Check whether this uses is getting intra-file-resolved. |
| 618 | assertThat(uses1.getResolvableStatus(), |
| 619 | is(ResolvableStatus.INTRA_FILE_RESOLVED)); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Checks self resolution when uses has prefix of self module. |
| 624 | */ |
| 625 | @Test |
| 626 | public void processSelfFileLinkingWithGroupingWithSelfModulePrefix() |
| 627 | throws IOException, ParserException { |
| 628 | |
| 629 | YangNode node = manager.getDataModel("src/test/resources/SelfFileLinkingWithGroupingWithSelfModulePrefix.yang"); |
| 630 | |
| 631 | // Check whether the data model tree returned is of type module. |
| 632 | assertThat((node instanceof YangModule), is(true)); |
| 633 | |
| 634 | // Check whether the node type is set properly to module. |
| 635 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 636 | |
| 637 | // Check whether the module name is set correctly. |
| 638 | YangModule yangNode = (YangModule) node; |
| 639 | assertThat(yangNode.getName(), is("Test")); |
| 640 | |
| 641 | // Check whether container is the sibling of grouping. |
| 642 | YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling(); |
| 643 | |
| 644 | // Check whether list is the child of container. |
| 645 | YangList yangList = (YangList) yangContainer.getChild(); |
| 646 | |
| 647 | // Check whether uses is the child of list. |
| 648 | assertThat((yangList.getChild() instanceof YangUses), is(true)); |
| 649 | YangUses yangUses1 = (YangUses) yangList.getChild(); |
| 650 | assertThat(yangUses1.getName(), is("FirstClass")); |
| 651 | |
| 652 | // Check whether uses is getting resolved. |
| 653 | assertThat(yangUses1.getResolvableStatus(), |
| 654 | is(ResolvableStatus.RESOLVED)); |
| 655 | |
| 656 | // Check whether grouping is the sibling of uses. |
| 657 | YangGrouping yangGrouping1 = (YangGrouping) yangUses1.getNextSibling(); |
| 658 | assertThat(yangGrouping1.getName(), is("FirstClass")); |
| 659 | |
| 660 | // Check whether uses is the child of grouping. |
| 661 | YangUses yangUses2 = (YangUses) yangGrouping1.getChild(); |
| 662 | assertThat(yangUses2.getName(), is("PassingClass")); |
| 663 | |
| 664 | // Check the uses gets resolved. |
| 665 | assertThat(yangUses2.getResolvableStatus(), |
| 666 | is(ResolvableStatus.RESOLVED)); |
| 667 | |
| 668 | // Check whether grouping is the sibling of list. |
| 669 | YangGrouping yangGrouping2 = (YangGrouping) yangList.getNextSibling(); |
| 670 | assertThat(yangGrouping2.getName(), is("PassingClass")); |
| 671 | |
| 672 | // Check uses is the child of that grouping which has prefix of the same module. |
| 673 | YangUses yangUses3 = (YangUses) yangGrouping2.getChild(); |
| 674 | assertThat(yangUses3.getName(), is("Percentage")); |
| 675 | |
| 676 | // Check uses is getting resolved. |
| 677 | assertThat(yangUses3.getResolvableStatus(), |
| 678 | is(ResolvableStatus.RESOLVED)); |
| 679 | |
| 680 | // Check grouping is the child of module. |
| 681 | YangGrouping yangGrouping3 = (YangGrouping) node.getChild(); |
| 682 | |
| 683 | ListIterator<YangLeaf> leafIterator; |
| 684 | YangLeaf leafInfo; |
| 685 | leafIterator = yangGrouping3.getListOfLeaf().listIterator(); |
| 686 | leafInfo = leafIterator.next(); |
| 687 | |
| 688 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 689 | assertThat(leafInfo.getName(), is("hello")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 690 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 691 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 692 | |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Checks self resolution when some type uses prefix of self module |
| 697 | * some uses external prefix. |
| 698 | */ |
| 699 | @Test |
| 700 | public void processSelfFileLinkingWithGroupingWithSelfAndExternalPrefixMix() |
| 701 | throws IOException, ParserException { |
| 702 | |
| 703 | YangNode node = manager |
| 704 | .getDataModel("src/test/resources/SelfFileLinkingWithGroupingWithSelfAndExternalPrefixMix.yang"); |
| 705 | |
| 706 | // Check whether the data model tree returned is of type module. |
| 707 | assertThat((node instanceof YangModule), is(true)); |
| 708 | |
| 709 | // Check whether the node type is set properly to module. |
| 710 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 711 | |
| 712 | // Check whether the module name is set correctly. |
| 713 | YangModule yangNode = (YangModule) node; |
| 714 | assertThat(yangNode.getName(), is("Test")); |
| 715 | |
| 716 | // Check whether container is the sibling of grouping. |
| 717 | YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling(); |
| 718 | |
| 719 | // Check whether list is the child of container. |
| 720 | YangList yangList = (YangList) yangContainer.getChild(); |
| 721 | |
| 722 | // Check whether uses is the child of list. |
| 723 | assertThat((yangList.getChild() instanceof YangUses), is(true)); |
| 724 | YangUses yangUses1 = (YangUses) yangList.getChild(); |
| 725 | assertThat(yangUses1.getName(), is("FirstClass")); |
| 726 | |
| 727 | // Check whether uses is getting resolved. |
| 728 | assertThat(yangUses1.getResolvableStatus(), |
| 729 | is(ResolvableStatus.RESOLVED)); |
| 730 | |
| 731 | // Check whether grouping is the sibling of uses. |
| 732 | YangGrouping yangGrouping1 = (YangGrouping) yangUses1.getNextSibling(); |
| 733 | assertThat(yangGrouping1.getName(), is("FirstClass")); |
| 734 | |
| 735 | // Check whether uses is the child of grouping which has prefix from other module. |
| 736 | YangUses yangUses2 = (YangUses) yangGrouping1.getChild(); |
| 737 | assertThat(yangUses2.getName(), is("PassingClass")); |
| 738 | |
| 739 | // Check whether uses gets intra-file-resolved. |
| 740 | assertThat(yangUses2.getResolvableStatus(), |
| 741 | is(ResolvableStatus.INTRA_FILE_RESOLVED)); |
| 742 | |
| 743 | // Check whether grouping is the sibling of list. |
| 744 | YangGrouping yangGrouping2 = (YangGrouping) yangList.getNextSibling(); |
| 745 | assertThat(yangGrouping2.getName(), is("PassingClass")); |
| 746 | |
| 747 | // Check uses is the child of that grouping which has prefix of the same module. |
| 748 | YangUses yangUses3 = (YangUses) yangGrouping2.getChild(); |
| 749 | assertThat(yangUses3.getName(), is("Percentage")); |
| 750 | |
| 751 | // Check uses is getting resolved. |
| 752 | assertThat(yangUses3.getResolvableStatus(), |
| 753 | is(ResolvableStatus.RESOLVED)); |
| 754 | |
| 755 | // Check grouping is the child of module. |
| 756 | YangGrouping yangGrouping3 = (YangGrouping) node.getChild(); |
| 757 | ListIterator<YangLeaf> leafIterator; |
| 758 | YangLeaf leafInfo; |
| 759 | leafIterator = yangGrouping3.getListOfLeaf().listIterator(); |
| 760 | leafInfo = leafIterator.next(); |
| 761 | |
| 762 | // Check whether the information in the leaf is correct under grouping. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 763 | assertThat(leafInfo.getName(), is("hello")); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame^] | 764 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 765 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 766 | } |
| 767 | |
| 768 | } |