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