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