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