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