BUG-865: remove unused model.util types
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / util / 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.parser.util;
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.text.ParseException;
17 import java.util.Collections;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
31 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
37 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
38 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
39 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.stmt.TestUtils;
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 = BaseTypes.int32Type();
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, YangSyntaxErrorException,
76             ParseException, 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, YangSyntaxErrorException,
190             ParseException, 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, YangSyntaxErrorException,
284             ParseException, 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, YangSyntaxErrorException, ParseException,
350             ReactorException {
351
352         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
353
354         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
355                 QName.parseRevision("2014-10-07"));
356
357         DataSchemaNode node = myModule.getDataChildByName("my-container");
358
359         Module foundModule = SchemaContextUtil.findParentModule(context, node);
360
361         assertEquals(myModule, foundModule);
362     }
363
364     @Test(expected = IllegalArgumentException.class)
365     public void findParentModuleIllegalArgumentTest() {
366
367         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
368         SchemaContextUtil.findParentModule(mockContext, null);
369
370     }
371
372     @Test(expected = IllegalArgumentException.class)
373     public void findParentModuleIllegalArgumentTest2() {
374
375         SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
376         SchemaContextUtil.findParentModule(null, mockSchemaNode);
377
378     }
379
380     @Test(expected = IllegalStateException.class)
381     public void findParentModuleIllegalStateTest() {
382
383         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
384         SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
385         Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
386         SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
387
388     }
389
390     @Test(expected = IllegalArgumentException.class)
391     public void findDataSchemaNodeIllegalArgumentTest() {
392
393         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
394         SchemaContextUtil.findDataSchemaNode(mockContext, null);
395
396     }
397
398     @Test(expected = IllegalArgumentException.class)
399     public void findDataSchemaNodeIllegalArgumentTest2() {
400
401         SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
402         SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
403
404     }
405
406     @Test
407     public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
408             ReactorException {
409
410         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
411
412         Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
413                 QName.parseRevision("2014-10-07"));
414         Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
415                 QName.parseRevision("2014-10-07"));
416
417         SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
418                 .getDataChildByName("my-imported-leaf");
419
420         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
421
422         SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
423
424         assertNotNull(foundNode);
425         assertNotNull(testNode);
426         assertEquals(testNode, foundNode);
427
428     }
429
430     @Test
431     public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException, ReactorException {
432         // findDataSchemaNode(final SchemaContext context, final Module module,
433         // final RevisionAwareXPath nonCondXPath) {
434
435         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
436
437         Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
438                 QName.parseRevision("2014-10-07"));
439
440         GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
441         SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
442
443         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
444
445         SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
446
447         assertNotNull(foundNode);
448         assertNotNull(testNode);
449         assertEquals(testNode, foundNode);
450
451     }
452
453     @Test(expected = IllegalArgumentException.class)
454     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
455
456         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
457         Module module = Mockito.mock(Module.class);
458
459         SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
460
461     }
462
463     @Test(expected = IllegalArgumentException.class)
464     public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
465
466         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
467         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
468
469         SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
470
471     }
472
473     @Test(expected = IllegalArgumentException.class)
474     public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
475
476         Module module = Mockito.mock(Module.class);
477         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
478
479         SchemaContextUtil.findDataSchemaNode(null, module, xpath);
480
481     }
482
483     @Test(expected = IllegalArgumentException.class)
484     public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
485
486         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
487         Module module = Mockito.mock(Module.class);
488         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
489                 true);
490
491         SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
492
493     }
494
495     @Test
496     public void findDataSchemaNodeFromXPathNullTest() {
497
498         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
499         Module module = Mockito.mock(Module.class);
500         RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
501
502         Mockito.when(xpath.toString()).thenReturn(null);
503         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
504
505     }
506
507     @Test
508     public void findDataSchemaNodeFromXPathNullTest2() {
509
510         SchemaContext mockContext = Mockito.mock(SchemaContext.class);
511         Module module = Mockito.mock(Module.class);
512         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
513
514         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
515
516     }
517
518     @Test
519     public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
520             YangSyntaxErrorException, ParseException, ReactorException {
521
522         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
523
524         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
525                 QName.parseRevision("2014-10-07"));
526
527         // find grouping in container
528         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
529         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
530
531         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
532                 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
533         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
534
535         assertNotNull(testNode);
536         assertNotNull(foundNode);
537         assertEquals(testNode, foundNode);
538
539         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
540         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
541
542         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
543
544         assertNotNull(testNode);
545         assertNotNull(foundNode);
546         assertEquals(testNode, foundNode);
547
548         // find grouping in list
549         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
550                 .getDataChildByName("my-list");
551         testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
552
553         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
554                 QName.create(myModule.getQNameModule(), "my-list"),
555                 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
556         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
557
558         assertNotNull(testNode);
559         assertNotNull(foundNode);
560         assertEquals(testNode, foundNode);
561
562         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
563         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
564
565         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
566
567         assertNotNull(testNode);
568         assertNotNull(foundNode);
569         assertEquals(testNode, foundNode);
570
571         // find grouping in grouping
572         dataContainer = getGroupingByName(myModule, "my-grouping");
573         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
574
575         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
576                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
577         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
578
579         assertNotNull(testNode);
580         assertNotNull(foundNode);
581         assertEquals(testNode, foundNode);
582
583         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
584         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
585
586         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
587
588         assertNotNull(testNode);
589         assertNotNull(foundNode);
590         assertEquals(testNode, foundNode);
591
592         // find grouping in rpc
593         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
594         for (GroupingDefinition grouping : rpc.getGroupings()) {
595             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
596                 testNode = grouping;
597             }
598         }
599
600         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
601                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
602         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
603
604         assertNotNull(testNode);
605         assertNotNull(foundNode);
606         assertEquals(testNode, foundNode);
607
608         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
609         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
610
611         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
612
613         assertNotNull(testNode);
614         assertNotNull(foundNode);
615         assertEquals(testNode, foundNode);
616
617         // find grouping in output
618         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
619         testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
620
621         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
622                 QName.create(myModule.getQNameModule(), "output"),
623                 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
624         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
625
626         assertNotNull(testNode);
627         assertNotNull(foundNode);
628         assertEquals(testNode, foundNode);
629
630         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
631         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
632
633         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
634
635         assertNotNull(testNode);
636         assertNotNull(foundNode);
637         assertEquals(testNode, foundNode);
638
639         // find grouping in input
640         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
641         testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
642
643         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
644                 QName.create(myModule.getQNameModule(), "input"),
645                 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
646         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
647
648         assertNotNull(testNode);
649         assertNotNull(foundNode);
650         assertEquals(testNode, foundNode);
651
652         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
653         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
654
655         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
656
657         assertNotNull(testNode);
658         assertNotNull(foundNode);
659         assertEquals(testNode, foundNode);
660
661         // find grouping in notification
662         dataContainer = getNotificationByName(myModule, "my-notification");
663         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
664
665         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
666                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
667         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
668
669         assertNotNull(testNode);
670         assertNotNull(foundNode);
671         assertEquals(testNode, foundNode);
672
673         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
674         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
675
676         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
677
678         assertNotNull(testNode);
679         assertNotNull(foundNode);
680         assertEquals(testNode, foundNode);
681
682         // find grouping in case
683         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
684                 "one").getDataChildByName("my-container-in-case");
685         testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
686
687         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
688                 QName.create(myModule.getQNameModule(), "one"),
689                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
690                 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
691         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
692
693         assertNotNull(testNode);
694         assertNotNull(foundNode);
695         assertEquals(testNode, foundNode);
696
697         testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
698         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
699
700         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
701
702         assertNotNull(testNode);
703         assertNotNull(foundNode);
704         assertEquals(testNode, foundNode);
705
706     }
707
708     @Test
709     public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
710             YangSyntaxErrorException, ParseException, ReactorException {
711
712         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
713
714         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
715                 QName.parseRevision("2014-10-07"));
716
717         // find grouping in container
718         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
719         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
720
721         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
722                 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
723         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
724
725         assertNull(testNode);
726         assertNull(foundNode);
727
728         // find grouping in list
729         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
730                 .getDataChildByName("my-list");
731         testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
732
733         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
734                 QName.create(myModule.getQNameModule(), "my-list"),
735                 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
736         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
737
738         assertNull(testNode);
739         assertNull(foundNode);
740
741         // find grouping in grouping
742         dataContainer = getGroupingByName(myModule, "my-grouping");
743         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
744
745         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
746                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
747         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
748
749         assertNull(testNode);
750         assertNull(foundNode);
751
752         // find grouping in rpc
753         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
754         for (GroupingDefinition grouping : rpc.getGroupings()) {
755             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
756                 testNode = grouping;
757             }
758         }
759
760         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
761                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
762         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
763
764         assertNull(testNode);
765         assertNull(foundNode);
766
767         // find grouping in output
768         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
769         testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
770
771         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
772                 QName.create(myModule.getQNameModule(), "output"),
773                 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
774         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
775
776         assertNull(testNode);
777         assertNull(foundNode);
778
779         // find grouping in input
780         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
781         testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
782
783         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
784                 QName.create(myModule.getQNameModule(), "input"),
785                 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
786         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
787
788         assertNull(testNode);
789         assertNull(foundNode);
790
791         // find grouping in notification
792         dataContainer = getNotificationByName(myModule, "my-notification");
793         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
794
795         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
796                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
797         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
798
799         assertNull(testNode);
800         assertNull(foundNode);
801
802         // find grouping in case
803         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
804                 "one").getDataChildByName("my-container-in-case");
805         testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
806
807         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
808                 QName.create(myModule.getQNameModule(), "one"),
809                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
810                 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
811         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
812
813         assertNull(testNode);
814         assertNull(foundNode);
815
816     }
817
818     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
819         for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
820             if (grouping.getQName().getLocalName().equals(name)) {
821                 return grouping;
822             }
823         }
824         return null;
825     }
826
827     private static RpcDefinition getRpcByName(final Module module, final String name) {
828         for (RpcDefinition rpc : module.getRpcs()) {
829             if (rpc.getQName().getLocalName().equals(name)) {
830                 return rpc;
831             }
832         }
833         return null;
834     }
835
836     private static NotificationDefinition getNotificationByName(final Module module, final String name) {
837         for (NotificationDefinition notification : module.getNotifications()) {
838             if (notification.getQName().getLocalName().equals(name)) {
839                 return notification;
840             }
841         }
842         return null;
843     }
844
845     @Test
846     public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
847             YangSyntaxErrorException, ParseException, ReactorException {
848
849         SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
850
851         Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
852                 QName.parseRevision("2014-10-07"));
853
854         ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
855         SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
856
857         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
858                 QName.create(myModule.getQNameModule(), "input"),
859                 QName.create(myModule.getQNameModule(), "my-choice"),
860                 QName.create(myModule.getQNameModule(), "case-two"),
861                 QName.create(myModule.getQNameModule(), "two"));
862         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
863
864         assertNotNull(testNode);
865         assertNotNull(foundNode);
866         assertEquals(testNode, foundNode);
867
868     }
869
870 }