Bug 6022: Deviation statement is not fully available in the YANG parser output
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / SchemaContextUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import java.io.IOException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.Collections;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
36 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
37 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40
41 public class SchemaContextUtilTest {
42     @Mock
43     private SchemaContext mockSchemaContext;
44     @Mock
45     private Module mockModule;
46
47     @Test
48     public void testFindDummyData() {
49         MockitoAnnotations.initMocks(this);
50
51         QName qName = QName.create("TestQName");
52         SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
53         assertEquals("Should be null. Module TestQName not found", null,
54                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
55
56         RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
57         assertEquals("Should be null. Module bookstore not found", null,
58                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
59
60         SchemaNode schemaNode = BaseTypes.int32Type();
61         RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
62         assertEquals("Should be null, Module prefix not found", null,
63                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
64                         xPathRelative));
65
66         assertEquals("Should be null. Module TestQName not found", null,
67                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
68
69         assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
70     }
71
72     @Test
73     public void findNodeInSchemaContextTest() throws URISyntaxException, IOException,
74              SourceException, ReactorException {
75
76         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
77
78         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
79                 QName.parseRevision("2014-10-07"));
80
81         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
82                 .getDataChildByName("my-leaf-in-container");
83
84         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
85                 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
86         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
87
88         assertNotNull(testNode);
89         assertNotNull(foundNode);
90         assertEquals(testNode, foundNode);
91
92         RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
93         testNode = rpc.getInput().getDataChildByName("my-input-leaf");
94
95         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
96                 QName.create(myModule.getQNameModule(), "input"),
97                 QName.create(myModule.getQNameModule(), "my-input-leaf"));
98
99         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
100
101         assertNotNull(testNode);
102         assertNotNull(foundNode);
103         assertEquals(testNode, foundNode);
104
105         rpc = getRpcByName(myModule,"my-rpc");
106         testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
107
108         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
109                 QName.create(myModule.getQNameModule(), "output"),
110                 QName.create(myModule.getQNameModule(), "my-output-leaf"));
111
112         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
113
114         assertNotNull(testNode);
115         assertNotNull(foundNode);
116         assertEquals(testNode, foundNode);
117
118         NotificationDefinition notification = myModule.getNotifications().iterator().next();
119         testNode = notification.getDataChildByName("my-notification-leaf");
120
121         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
122                 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
123
124         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
125
126         assertNotNull(testNode);
127         assertNotNull(foundNode);
128         assertEquals(testNode, foundNode);
129
130         GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
131         testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
132                 .getDataChildByName("my-leaf-in-grouping");
133
134         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
135                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
136                 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
137
138         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
139
140         assertNotNull(testNode);
141         assertNotNull(foundNode);
142         assertEquals(testNode, foundNode);
143
144         testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
145                 "my-choice-leaf-one");
146
147         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
148                 QName.create(myModule.getQNameModule(), "one"),
149                 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
150         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
151
152         assertNotNull(testNode);
153         assertNotNull(foundNode);
154         assertEquals(testNode, foundNode);
155
156         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
157                 .getDataChildByName("my-list");
158
159         testNode = listNode.getDataChildByName("my-leaf-in-list");
160
161         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
162                 QName.create(myModule.getQNameModule(), "my-list"),
163                 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
164         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
165
166         assertNotNull(testNode);
167         assertNotNull(foundNode);
168         assertEquals(testNode, foundNode);
169
170         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
171                 .getDataChildByName("my-list");
172
173         testNode = listNode.getDataChildByName("my-leaf-list-in-list");
174
175         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
176                 QName.create(myModule.getQNameModule(), "my-list"),
177                 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
178         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
179
180         assertNotNull(testNode);
181         assertNotNull(foundNode);
182         assertEquals(testNode, foundNode);
183
184     }
185
186     @Test
187     public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException,
188              SourceException, ReactorException {
189
190         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
191
192         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
193                 QName.parseRevision("2014-10-07"));
194
195         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
196                 .getDataChildByName("my-leaf-not-in-container");
197
198         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
199                 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
200         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
201
202         assertNull(testNode);
203         assertNull(foundNode);
204
205         RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
206         testNode = rpc.getInput().getDataChildByName("no-input-leaf");
207
208         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
209                 QName.create(myModule.getQNameModule(), "input"),
210                 QName.create(myModule.getQNameModule(), "no-input-leaf"));
211
212         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
213
214         assertNull(testNode);
215         assertNull(foundNode);
216
217         NotificationDefinition notification = myModule.getNotifications().iterator().next();
218         testNode = notification.getDataChildByName("no-notification-leaf");
219
220         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
221                 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
222
223         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
224
225         assertNull(testNode);
226         assertNull(foundNode);
227
228         GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
229         testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
230                 .getDataChildByName("no-leaf-in-grouping");
231
232         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
233                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
234                 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
235
236         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
237
238         assertNull(testNode);
239         assertNull(foundNode);
240
241         testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
242                 "no-choice-leaf");
243
244         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
245                 QName.create(myModule.getQNameModule(), "one"),
246                 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
247         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
248
249         assertNull(testNode);
250         assertNull(foundNode);
251
252         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
253                 .getDataChildByName("my-list");
254
255         testNode = listNode.getDataChildByName("no-leaf-in-list");
256
257         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
258                 QName.create(myModule.getQNameModule(), "my-list"),
259                 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
260         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
261
262         assertNull(testNode);
263         assertNull(foundNode);
264
265         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
266                 .getDataChildByName("my-list");
267
268         testNode = listNode.getDataChildByName("no-leaf-list-in-list");
269
270         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
271                 QName.create(myModule.getQNameModule(), "my-list"),
272                 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
273         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
274
275         assertNull(testNode);
276         assertNull(foundNode);
277
278     }
279
280     @Test
281     public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException,
282              SourceException, ReactorException {
283
284         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
285
286         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
287                 QName.parseRevision("2014-10-07"));
288
289         SchemaNode testNode = myModule.getDataChildByName("my-container");
290
291         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
292         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
293
294         assertNotNull(testNode);
295         assertNotNull(foundNode);
296         assertEquals(testNode, foundNode);
297
298         testNode = getRpcByName(myModule,"my-rpc");
299
300         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
301         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
302
303         assertNotNull(testNode);
304         assertNotNull(foundNode);
305         assertEquals(testNode, foundNode);
306
307         testNode = myModule.getNotifications().iterator().next();
308
309         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
310         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
311
312         assertNotNull(testNode);
313         assertNotNull(foundNode);
314         assertEquals(testNode, foundNode);
315
316         testNode = getGroupingByName(myModule,"my-grouping");
317
318         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
319         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
320
321         assertNotNull(testNode);
322         assertNotNull(foundNode);
323         assertEquals(testNode, foundNode);
324
325         testNode = myModule.getDataChildByName("my-choice");
326
327         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
328         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
329
330         assertNotNull(testNode);
331         assertNotNull(foundNode);
332         assertEquals(testNode, foundNode);
333
334         testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list");
335
336         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
337                 QName.create(myModule.getQNameModule(), "my-list"));
338         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
339
340         assertNotNull(testNode);
341         assertNotNull(foundNode);
342         assertEquals(testNode, foundNode);
343
344     }
345
346     @Test
347     public void findParentModuleTest() throws URISyntaxException, IOException,   SourceException, ReactorException {
348
349         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
350
351         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
352                 QName.parseRevision("2014-10-07"));
353
354         DataSchemaNode node = myModule.getDataChildByName("my-container");
355
356         Module foundModule = SchemaContextUtil.findParentModule(context, node);
357
358         assertEquals(myModule, foundModule);
359     }
360
361     @Test(expected = IllegalArgumentException.class)
362     public void findParentModuleIllegalArgumentTest() {
363
364         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
365         SchemaContextUtil.findParentModule(mockContext, null);
366
367     }
368
369     @Test(expected = IllegalArgumentException.class)
370     public void findParentModuleIllegalArgumentTest2() {
371
372         SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
373         SchemaContextUtil.findParentModule(null, mockSchemaNode);
374
375     }
376
377     @Test(expected = IllegalStateException.class)
378     public void findParentModuleIllegalStateTest() {
379
380         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
381         SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
382         Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
383         SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
384
385     }
386
387     @Test(expected = IllegalArgumentException.class)
388     public void findDataSchemaNodeIllegalArgumentTest() {
389
390         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
391         SchemaContextUtil.findDataSchemaNode(mockContext, null);
392
393     }
394
395     @Test(expected = IllegalArgumentException.class)
396     public void findDataSchemaNodeIllegalArgumentTest2() {
397
398         SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
399         SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
400
401     }
402
403     @Test
404     public void findDataSchemaNodeTest() throws URISyntaxException, IOException,  SourceException, ReactorException {
405
406         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
407
408         Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
409                 QName.parseRevision("2014-10-07"));
410         Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
411                 QName.parseRevision("2014-10-07"));
412
413         SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
414                 .getDataChildByName("my-imported-leaf");
415
416         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
417
418         SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
419
420         assertNotNull(foundNode);
421         assertNotNull(testNode);
422         assertEquals(testNode, foundNode);
423
424     }
425
426     @Test
427     public void findDataSchemaNodeTest2() throws URISyntaxException, IOException,  SourceException, ReactorException {
428
429         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
430
431         Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
432                 QName.parseRevision("2014-10-07"));
433
434         GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
435         SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
436
437         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
438
439         SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
440
441         assertNotNull(foundNode);
442         assertNotNull(testNode);
443         assertEquals(testNode, foundNode);
444
445     }
446
447     @Test(expected = IllegalArgumentException.class)
448     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
449
450         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
451         Module module = Mockito.mock(Module.class);
452
453         SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
454
455     }
456
457     @Test(expected = IllegalArgumentException.class)
458     public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
459
460         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
461         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
462
463         SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
464
465     }
466
467     @Test(expected = IllegalArgumentException.class)
468     public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
469
470         Module module = Mockito.mock(Module.class);
471         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
472
473         SchemaContextUtil.findDataSchemaNode(null, module, xpath);
474
475     }
476
477     @Test(expected = IllegalArgumentException.class)
478     public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
479
480         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
481         Module module = Mockito.mock(Module.class);
482         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
483                 true);
484
485         SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
486
487     }
488
489     @Test
490     public void findDataSchemaNodeFromXPathNullTest() {
491
492         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
493         Module module = Mockito.mock(Module.class);
494         RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
495
496         Mockito.when(xpath.toString()).thenReturn(null);
497         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
498
499     }
500
501     @Test
502     public void findDataSchemaNodeFromXPathNullTest2() {
503
504         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
505         Module module = Mockito.mock(Module.class);
506         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
507
508         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
509
510     }
511
512     @Test
513     public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
514               SourceException, ReactorException {
515
516         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
517
518         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
519                 QName.parseRevision("2014-10-07"));
520
521         // find grouping in container
522         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
523         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
524
525         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
526                 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
527         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
528
529         assertNotNull(testNode);
530         assertNotNull(foundNode);
531         assertEquals(testNode, foundNode);
532
533         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
534         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
535
536         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
537
538         assertNotNull(testNode);
539         assertNotNull(foundNode);
540         assertEquals(testNode, foundNode);
541
542         // find grouping in list
543         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
544                 .getDataChildByName("my-list");
545         testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
546
547         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
548                 QName.create(myModule.getQNameModule(), "my-list"),
549                 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
550         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
551
552         assertNotNull(testNode);
553         assertNotNull(foundNode);
554         assertEquals(testNode, foundNode);
555
556         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
557         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
558
559         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
560
561         assertNotNull(testNode);
562         assertNotNull(foundNode);
563         assertEquals(testNode, foundNode);
564
565         // find grouping in grouping
566         dataContainer = getGroupingByName(myModule, "my-grouping");
567         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
568
569         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
570                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
571         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
572
573         assertNotNull(testNode);
574         assertNotNull(foundNode);
575         assertEquals(testNode, foundNode);
576
577         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
578         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
579
580         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
581
582         assertNotNull(testNode);
583         assertNotNull(foundNode);
584         assertEquals(testNode, foundNode);
585
586         // find grouping in rpc
587         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
588         for (GroupingDefinition grouping : rpc.getGroupings()) {
589             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
590                 testNode = grouping;
591             }
592         }
593
594         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
595                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
596         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
597
598         assertNotNull(testNode);
599         assertNotNull(foundNode);
600         assertEquals(testNode, foundNode);
601
602         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
603         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
604
605         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
606
607         assertNotNull(testNode);
608         assertNotNull(foundNode);
609         assertEquals(testNode, foundNode);
610
611         // find grouping in output
612         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
613         testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
614
615         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
616                 QName.create(myModule.getQNameModule(), "output"),
617                 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
618         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
619
620         assertNotNull(testNode);
621         assertNotNull(foundNode);
622         assertEquals(testNode, foundNode);
623
624         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
625         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
626
627         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
628
629         assertNotNull(testNode);
630         assertNotNull(foundNode);
631         assertEquals(testNode, foundNode);
632
633         // find grouping in input
634         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
635         testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
636
637         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
638                 QName.create(myModule.getQNameModule(), "input"),
639                 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
640         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
641
642         assertNotNull(testNode);
643         assertNotNull(foundNode);
644         assertEquals(testNode, foundNode);
645
646         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
647         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
648
649         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
650
651         assertNotNull(testNode);
652         assertNotNull(foundNode);
653         assertEquals(testNode, foundNode);
654
655         // find grouping in notification
656         dataContainer = getNotificationByName(myModule, "my-notification");
657         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
658
659         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
660                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
661         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
662
663         assertNotNull(testNode);
664         assertNotNull(foundNode);
665         assertEquals(testNode, foundNode);
666
667         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
668         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
669
670         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
671
672         assertNotNull(testNode);
673         assertNotNull(foundNode);
674         assertEquals(testNode, foundNode);
675
676         // find grouping in case
677         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
678                 "one").getDataChildByName("my-container-in-case");
679         testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
680
681         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
682                 QName.create(myModule.getQNameModule(), "one"),
683                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
684                 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
685         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
686
687         assertNotNull(testNode);
688         assertNotNull(foundNode);
689         assertEquals(testNode, foundNode);
690
691         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
692         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
693
694         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
695
696         assertNotNull(testNode);
697         assertNotNull(foundNode);
698         assertEquals(testNode, foundNode);
699
700     }
701
702     @Test
703     public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
704               SourceException, ReactorException {
705
706         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
707
708         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
709                 QName.parseRevision("2014-10-07"));
710
711         // find grouping in container
712         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
713         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
714
715         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
716                 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
717         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
718
719         assertNull(testNode);
720         assertNull(foundNode);
721
722         // find grouping in list
723         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
724                 .getDataChildByName("my-list");
725         testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
726
727         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
728                 QName.create(myModule.getQNameModule(), "my-list"),
729                 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
730         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
731
732         assertNull(testNode);
733         assertNull(foundNode);
734
735         // find grouping in grouping
736         dataContainer = getGroupingByName(myModule, "my-grouping");
737         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
738
739         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
740                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
741         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
742
743         assertNull(testNode);
744         assertNull(foundNode);
745
746         // find grouping in rpc
747         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
748         for (GroupingDefinition grouping : rpc.getGroupings()) {
749             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
750                 testNode = grouping;
751             }
752         }
753
754         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
755                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
756         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
757
758         assertNull(testNode);
759         assertNull(foundNode);
760
761         // find grouping in output
762         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
763         testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
764
765         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
766                 QName.create(myModule.getQNameModule(), "output"),
767                 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
768         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
769
770         assertNull(testNode);
771         assertNull(foundNode);
772
773         // find grouping in input
774         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
775         testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
776
777         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
778                 QName.create(myModule.getQNameModule(), "input"),
779                 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
780         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
781
782         assertNull(testNode);
783         assertNull(foundNode);
784
785         // find grouping in notification
786         dataContainer = getNotificationByName(myModule, "my-notification");
787         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
788
789         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
790                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
791         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
792
793         assertNull(testNode);
794         assertNull(foundNode);
795
796         // find grouping in case
797         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
798                 "one").getDataChildByName("my-container-in-case");
799         testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
800
801         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
802                 QName.create(myModule.getQNameModule(), "one"),
803                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
804                 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
805         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
806
807         assertNull(testNode);
808         assertNull(foundNode);
809
810     }
811
812     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
813         for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
814             if (grouping.getQName().getLocalName().equals(name)) {
815                 return grouping;
816             }
817         }
818         return null;
819     }
820
821     private static RpcDefinition getRpcByName(final Module module, final String name) {
822         for (RpcDefinition rpc : module.getRpcs()) {
823             if (rpc.getQName().getLocalName().equals(name)) {
824                 return rpc;
825             }
826         }
827         return null;
828     }
829
830     private static NotificationDefinition getNotificationByName(final Module module, final String name) {
831         for (NotificationDefinition notification : module.getNotifications()) {
832             if (notification.getQName().getLocalName().equals(name)) {
833                 return notification;
834             }
835         }
836         return null;
837     }
838
839     @Test
840     public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
841               SourceException, ReactorException {
842
843         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
844
845         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
846                 QName.parseRevision("2014-10-07"));
847
848         ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
849         SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
850
851         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
852                 QName.create(myModule.getQNameModule(), "input"),
853                 QName.create(myModule.getQNameModule(), "my-choice"),
854                 QName.create(myModule.getQNameModule(), "case-two"),
855                 QName.create(myModule.getQNameModule(), "two"));
856         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
857
858         assertNotNull(testNode);
859         assertNotNull(foundNode);
860         assertEquals(testNode, foundNode);
861
862     }
863
864 }