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