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