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