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