2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.util;
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;
16 import java.io.IOException;
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;
48 public class SchemaContextUtilTest {
50 private SchemaContext mockSchemaContext;
52 private Module mockModule;
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();
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));
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));
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,
76 assertEquals("Should be null. Module TestQName not found", null,
77 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
79 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
83 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
86 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
88 final Module myModule = context.findModule(new URI("uri:my-module"),Revision.valueOf("2014-10-07")).get();
90 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
91 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
92 "my-leaf-in-container"));
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());
98 assertNotNull(testNode);
99 assertNotNull(foundNode);
100 assertEquals(testNode, foundNode);
102 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
103 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
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"));
109 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
111 assertNotNull(testNode);
112 assertNotNull(foundNode);
113 assertEquals(testNode, foundNode);
115 rpc = getRpcByName(myModule, "my-rpc");
116 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
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"));
122 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
124 assertNotNull(testNode);
125 assertNotNull(foundNode);
126 assertEquals(testNode, foundNode);
128 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
129 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
131 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
132 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
134 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
136 assertNotNull(testNode);
137 assertNotNull(foundNode);
138 assertEquals(testNode, foundNode);
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"));
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"));
149 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
151 assertNotNull(testNode);
152 assertNotNull(foundNode);
153 assertEquals(testNode, foundNode);
155 testNode = ((ChoiceSchemaNode) myModule
156 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
157 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
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());
164 assertNotNull(testNode);
165 assertNotNull(foundNode);
166 assertEquals(testNode, foundNode);
168 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
169 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
172 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
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());
179 assertNotNull(testNode);
180 assertNotNull(foundNode);
181 assertEquals(testNode, foundNode);
183 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
184 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
187 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
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());
194 assertNotNull(testNode);
195 assertNotNull(foundNode);
196 assertEquals(testNode, foundNode);
201 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
204 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
206 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.valueOf("2014-10-07")).get();
208 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
209 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
210 "my-leaf-not-in-container"));
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());
216 assertNull(testNode);
217 assertNull(foundNode);
219 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
220 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
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"));
226 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
228 assertNull(testNode);
229 assertNull(foundNode);
231 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
232 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
234 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
235 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
237 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
239 assertNull(testNode);
240 assertNull(foundNode);
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"));
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"));
251 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
253 assertNull(testNode);
254 assertNull(foundNode);
256 testNode = ((ChoiceSchemaNode) myModule
257 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
258 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
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());
265 assertNull(testNode);
266 assertNull(foundNode);
268 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
269 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
272 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
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());
279 assertNull(testNode);
280 assertNull(foundNode);
282 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
283 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
286 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
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());
293 assertNull(testNode);
294 assertNull(foundNode);
299 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
302 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
304 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.valueOf("2014-10-07")).get();
306 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
308 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
309 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
311 assertNotNull(testNode);
312 assertNotNull(foundNode);
313 assertEquals(testNode, foundNode);
315 testNode = getRpcByName(myModule, "my-rpc");
317 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
318 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
320 assertNotNull(testNode);
321 assertNotNull(foundNode);
322 assertEquals(testNode, foundNode);
324 testNode = myModule.getNotifications().iterator().next();
326 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
327 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
329 assertNotNull(testNode);
330 assertNotNull(foundNode);
331 assertEquals(testNode, foundNode);
333 testNode = getGroupingByName(myModule, "my-grouping");
335 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
336 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
338 assertNotNull(testNode);
339 assertNotNull(foundNode);
340 assertEquals(testNode, foundNode);
342 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
344 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
345 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
347 assertNotNull(testNode);
348 assertNotNull(foundNode);
349 assertEquals(testNode, foundNode);
351 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
352 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
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());
358 assertNotNull(testNode);
359 assertNotNull(foundNode);
360 assertEquals(testNode, foundNode);
365 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
368 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
370 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.valueOf("2014-10-07")).get();
372 final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
375 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
377 assertEquals(myModule, foundModule);
380 @Test(expected = IllegalArgumentException.class)
381 public void findParentModuleIllegalArgumentTest() {
383 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
384 SchemaContextUtil.findParentModule(mockContext, null);
388 @Test(expected = IllegalArgumentException.class)
389 public void findParentModuleIllegalArgumentTest2() {
391 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
392 SchemaContextUtil.findParentModule(null, mockSchemaNode);
396 @Test(expected = IllegalStateException.class)
397 public void findParentModuleIllegalStateTest() {
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);
406 @Test(expected = IllegalArgumentException.class)
407 public void findDataSchemaNodeIllegalArgumentTest() {
409 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
410 SchemaContextUtil.findDataSchemaNode(mockContext, null);
414 @Test(expected = IllegalArgumentException.class)
415 public void findDataSchemaNodeIllegalArgumentTest2() {
417 final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
418 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
423 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
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();
430 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
431 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
432 importedModule.getQNameModule(), "my-imported-leaf"));
434 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
437 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
439 assertNotNull(foundNode);
440 assertNotNull(testNode);
441 assertEquals(testNode, foundNode);
445 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
447 // findDataSchemaNode(final SchemaContext context, final Module module,
448 // final RevisionAwareXPath nonCondXPath) {
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();
453 final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
454 final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
455 "my-leaf-in-gouping2"));
457 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
459 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
461 assertNotNull(foundNode);
462 assertNotNull(testNode);
463 assertEquals(testNode, foundNode);
467 @Test(expected = IllegalArgumentException.class)
468 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
470 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
471 final Module module = Mockito.mock(Module.class);
473 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
477 @Test(expected = IllegalArgumentException.class)
478 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
480 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
481 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
483 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
487 @Test(expected = IllegalArgumentException.class)
488 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
490 final Module module = Mockito.mock(Module.class);
491 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
493 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
497 @Test(expected = IllegalArgumentException.class)
498 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
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);
505 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
510 public void findDataSchemaNodeFromXPathNullTest() {
512 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
513 final Module module = Mockito.mock(Module.class);
514 final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
516 Mockito.when(xpath.toString()).thenReturn(null);
517 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
522 public void findDataSchemaNodeFromXPathNullTest2() {
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);
528 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
533 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
534 YangSyntaxErrorException, ReactorException {
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();
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");
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());
548 assertNotNull(testNode);
549 assertNotNull(foundNode);
550 assertEquals(testNode, foundNode);
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"));
556 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
558 assertNotNull(testNode);
559 assertNotNull(foundNode);
560 assertEquals(testNode, foundNode);
562 // find grouping in list
563 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
564 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
566 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
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());
573 assertNotNull(testNode);
574 assertNotNull(foundNode);
575 assertEquals(testNode, foundNode);
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"));
581 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
583 assertNotNull(testNode);
584 assertNotNull(foundNode);
585 assertEquals(testNode, foundNode);
587 // find grouping in grouping
588 dataContainer = getGroupingByName(myModule, "my-grouping");
589 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
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());
595 assertNotNull(testNode);
596 assertNotNull(foundNode);
597 assertEquals(testNode, foundNode);
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"));
603 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
605 assertNotNull(testNode);
606 assertNotNull(foundNode);
607 assertEquals(testNode, foundNode);
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")) {
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());
621 assertNotNull(testNode);
622 assertNotNull(foundNode);
623 assertEquals(testNode, foundNode);
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"));
629 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
631 assertNotNull(testNode);
632 assertNotNull(foundNode);
633 assertEquals(testNode, foundNode);
635 // find grouping in output
636 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
637 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
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());
644 assertNotNull(testNode);
645 assertNotNull(foundNode);
646 assertEquals(testNode, foundNode);
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"));
652 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
654 assertNotNull(testNode);
655 assertNotNull(foundNode);
656 assertEquals(testNode, foundNode);
658 // find grouping in input
659 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
660 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
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());
667 assertNotNull(testNode);
668 assertNotNull(foundNode);
669 assertEquals(testNode, foundNode);
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"));
675 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
677 assertNotNull(testNode);
678 assertNotNull(foundNode);
679 assertEquals(testNode, foundNode);
681 // find grouping in notification
682 dataContainer = getNotificationByName(myModule, "my-notification");
683 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
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());
689 assertNotNull(testNode);
690 assertNotNull(foundNode);
691 assertEquals(testNode, foundNode);
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"));
697 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
699 assertNotNull(testNode);
700 assertNotNull(foundNode);
701 assertEquals(testNode, foundNode);
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");
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());
715 assertNotNull(testNode);
716 assertNotNull(foundNode);
717 assertEquals(testNode, foundNode);
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"));
723 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
725 assertNotNull(testNode);
726 assertNotNull(foundNode);
727 assertEquals(testNode, foundNode);
732 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
733 YangSyntaxErrorException, ReactorException {
735 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
737 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.valueOf("2014-10-07")).get();
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");
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());
748 assertNull(testNode);
749 assertNull(foundNode);
751 // find grouping in list
752 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
753 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
755 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
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());
762 assertNull(testNode);
763 assertNull(foundNode);
765 // find grouping in grouping
766 dataContainer = getGroupingByName(myModule, "my-grouping");
767 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
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());
773 assertNull(testNode);
774 assertNull(foundNode);
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")) {
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());
788 assertNull(testNode);
789 assertNull(foundNode);
791 // find grouping in output
792 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
793 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
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());
800 assertNull(testNode);
801 assertNull(foundNode);
803 // find grouping in input
804 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
805 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
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());
812 assertNull(testNode);
813 assertNull(foundNode);
815 // find grouping in notification
816 dataContainer = getNotificationByName(myModule, "my-notification");
817 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
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());
823 assertNull(testNode);
824 assertNull(foundNode);
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");
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());
838 assertNull(testNode);
839 assertNull(foundNode);
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)) {
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)) {
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)) {
871 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
872 YangSyntaxErrorException, ReactorException {
874 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
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"));
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());
887 assertNotNull(testNode);
888 assertNotNull(foundNode);
889 assertEquals(testNode, foundNode);