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