2 * Copyright (c) 2016 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.stmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
15 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.util.Collections;
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.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.util.Int32;
40 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
41 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
43 public class SchemaContextUtilTest {
45 private SchemaContext mockSchemaContext;
47 private Module mockModule;
50 public void testFindDummyData() {
51 MockitoAnnotations.initMocks(this);
53 QName qName = QName.create("TestQName");
54 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
55 assertEquals("Should be null. Module TestQName not found", null,
56 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
58 RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
59 assertEquals("Should be null. Module bookstore not found", null,
60 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
62 SchemaNode schemaNode = Int32.getInstance();
63 RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
64 assertEquals("Should be null, Module prefix not found", null,
65 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
68 assertEquals("Should be null. Module TestQName not found", null,
69 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
71 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
75 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException,
76 SourceException, ReactorException {
78 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
80 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
81 QName.parseRevision("2014-10-07"));
83 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
84 .getDataChildByName("my-leaf-in-container");
86 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
87 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
88 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
90 assertNotNull(testNode);
91 assertNotNull(foundNode);
92 assertEquals(testNode, foundNode);
94 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
95 testNode = rpc.getInput().getDataChildByName("my-input-leaf");
97 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
98 QName.create(myModule.getQNameModule(), "input"),
99 QName.create(myModule.getQNameModule(), "my-input-leaf"));
101 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
103 assertNotNull(testNode);
104 assertNotNull(foundNode);
105 assertEquals(testNode, foundNode);
107 rpc = getRpcByName(myModule,"my-rpc");
108 testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
110 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
111 QName.create(myModule.getQNameModule(), "output"),
112 QName.create(myModule.getQNameModule(), "my-output-leaf"));
114 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
116 assertNotNull(testNode);
117 assertNotNull(foundNode);
118 assertEquals(testNode, foundNode);
120 NotificationDefinition notification = myModule.getNotifications().iterator().next();
121 testNode = notification.getDataChildByName("my-notification-leaf");
123 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
124 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
126 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
128 assertNotNull(testNode);
129 assertNotNull(foundNode);
130 assertEquals(testNode, foundNode);
132 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
133 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
134 .getDataChildByName("my-leaf-in-grouping");
136 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
137 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
138 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
140 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
142 assertNotNull(testNode);
143 assertNotNull(foundNode);
144 assertEquals(testNode, foundNode);
146 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
147 "my-choice-leaf-one");
149 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
150 QName.create(myModule.getQNameModule(), "one"),
151 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
152 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
154 assertNotNull(testNode);
155 assertNotNull(foundNode);
156 assertEquals(testNode, foundNode);
158 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
159 .getDataChildByName("my-list");
161 testNode = listNode.getDataChildByName("my-leaf-in-list");
163 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
164 QName.create(myModule.getQNameModule(), "my-list"),
165 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
166 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
168 assertNotNull(testNode);
169 assertNotNull(foundNode);
170 assertEquals(testNode, foundNode);
172 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
173 .getDataChildByName("my-list");
175 testNode = listNode.getDataChildByName("my-leaf-list-in-list");
177 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
178 QName.create(myModule.getQNameModule(), "my-list"),
179 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
180 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
182 assertNotNull(testNode);
183 assertNotNull(foundNode);
184 assertEquals(testNode, foundNode);
189 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException,
190 SourceException, ReactorException {
192 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
194 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
195 QName.parseRevision("2014-10-07"));
197 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
198 .getDataChildByName("my-leaf-not-in-container");
200 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
201 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
202 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
204 assertNull(testNode);
205 assertNull(foundNode);
207 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
208 testNode = rpc.getInput().getDataChildByName("no-input-leaf");
210 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
211 QName.create(myModule.getQNameModule(), "input"),
212 QName.create(myModule.getQNameModule(), "no-input-leaf"));
214 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
216 assertNull(testNode);
217 assertNull(foundNode);
219 NotificationDefinition notification = myModule.getNotifications().iterator().next();
220 testNode = notification.getDataChildByName("no-notification-leaf");
222 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
223 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
225 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
227 assertNull(testNode);
228 assertNull(foundNode);
230 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
231 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
232 .getDataChildByName("no-leaf-in-grouping");
234 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
235 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
236 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
238 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
240 assertNull(testNode);
241 assertNull(foundNode);
243 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
246 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
247 QName.create(myModule.getQNameModule(), "one"),
248 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
249 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
251 assertNull(testNode);
252 assertNull(foundNode);
254 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
255 .getDataChildByName("my-list");
257 testNode = listNode.getDataChildByName("no-leaf-in-list");
259 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
260 QName.create(myModule.getQNameModule(), "my-list"),
261 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
262 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
264 assertNull(testNode);
265 assertNull(foundNode);
267 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
268 .getDataChildByName("my-list");
270 testNode = listNode.getDataChildByName("no-leaf-list-in-list");
272 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
273 QName.create(myModule.getQNameModule(), "my-list"),
274 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
275 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
277 assertNull(testNode);
278 assertNull(foundNode);
283 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException,
284 SourceException, ReactorException {
286 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
288 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
289 QName.parseRevision("2014-10-07"));
291 SchemaNode testNode = myModule.getDataChildByName("my-container");
293 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
294 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
296 assertNotNull(testNode);
297 assertNotNull(foundNode);
298 assertEquals(testNode, foundNode);
300 testNode = getRpcByName(myModule,"my-rpc");
302 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
303 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
305 assertNotNull(testNode);
306 assertNotNull(foundNode);
307 assertEquals(testNode, foundNode);
309 testNode = myModule.getNotifications().iterator().next();
311 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
312 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
314 assertNotNull(testNode);
315 assertNotNull(foundNode);
316 assertEquals(testNode, foundNode);
318 testNode = getGroupingByName(myModule,"my-grouping");
320 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
321 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
323 assertNotNull(testNode);
324 assertNotNull(foundNode);
325 assertEquals(testNode, foundNode);
327 testNode = myModule.getDataChildByName("my-choice");
329 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
330 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
332 assertNotNull(testNode);
333 assertNotNull(foundNode);
334 assertEquals(testNode, foundNode);
336 testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list");
338 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
339 QName.create(myModule.getQNameModule(), "my-list"));
340 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
342 assertNotNull(testNode);
343 assertNotNull(foundNode);
344 assertEquals(testNode, foundNode);
349 public void findParentModuleTest() throws URISyntaxException, IOException, SourceException, ReactorException {
351 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
353 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
354 QName.parseRevision("2014-10-07"));
356 DataSchemaNode node = myModule.getDataChildByName("my-container");
358 Module foundModule = SchemaContextUtil.findParentModule(context, node);
360 assertEquals(myModule, foundModule);
363 @Test(expected = IllegalArgumentException.class)
364 public void findParentModuleIllegalArgumentTest() {
366 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
367 SchemaContextUtil.findParentModule(mockContext, null);
371 @Test(expected = IllegalArgumentException.class)
372 public void findParentModuleIllegalArgumentTest2() {
374 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
375 SchemaContextUtil.findParentModule(null, mockSchemaNode);
379 @Test(expected = IllegalStateException.class)
380 public void findParentModuleIllegalStateTest() {
382 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
383 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
384 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
385 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
389 @Test(expected = IllegalArgumentException.class)
390 public void findDataSchemaNodeIllegalArgumentTest() {
392 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
393 SchemaContextUtil.findDataSchemaNode(mockContext, null);
397 @Test(expected = IllegalArgumentException.class)
398 public void findDataSchemaNodeIllegalArgumentTest2() {
400 SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
401 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
406 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, SourceException, ReactorException {
408 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
410 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
411 QName.parseRevision("2014-10-07"));
412 Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
413 QName.parseRevision("2014-10-07"));
415 SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
416 .getDataChildByName("my-imported-leaf");
418 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
420 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
422 assertNotNull(foundNode);
423 assertNotNull(testNode);
424 assertEquals(testNode, foundNode);
429 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, SourceException, ReactorException {
431 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
433 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
434 QName.parseRevision("2014-10-07"));
436 GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
437 SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
439 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
441 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
443 assertNotNull(foundNode);
444 assertNotNull(testNode);
445 assertEquals(testNode, foundNode);
449 @Test(expected = IllegalArgumentException.class)
450 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
452 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
453 Module module = Mockito.mock(Module.class);
455 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
459 @Test(expected = IllegalArgumentException.class)
460 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
462 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
463 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
465 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
469 @Test(expected = IllegalArgumentException.class)
470 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
472 Module module = Mockito.mock(Module.class);
473 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
475 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
479 @Test(expected = IllegalArgumentException.class)
480 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
482 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
483 Module module = Mockito.mock(Module.class);
484 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
487 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
492 public void findDataSchemaNodeFromXPathNullTest() {
494 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
495 Module module = Mockito.mock(Module.class);
496 RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
498 Mockito.when(xpath.toString()).thenReturn(null);
499 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
504 public void findDataSchemaNodeFromXPathNullTest2() {
506 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
507 Module module = Mockito.mock(Module.class);
508 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
510 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
515 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
516 SourceException, ReactorException {
518 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
520 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
521 QName.parseRevision("2014-10-07"));
523 // find grouping in container
524 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
525 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
527 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
528 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
529 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
531 assertNotNull(testNode);
532 assertNotNull(foundNode);
533 assertEquals(testNode, foundNode);
535 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
536 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
538 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
540 assertNotNull(testNode);
541 assertNotNull(foundNode);
542 assertEquals(testNode, foundNode);
544 // find grouping in list
545 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
546 .getDataChildByName("my-list");
547 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
549 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
550 QName.create(myModule.getQNameModule(), "my-list"),
551 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
552 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
554 assertNotNull(testNode);
555 assertNotNull(foundNode);
556 assertEquals(testNode, foundNode);
558 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
559 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
561 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
563 assertNotNull(testNode);
564 assertNotNull(foundNode);
565 assertEquals(testNode, foundNode);
567 // find grouping in grouping
568 dataContainer = getGroupingByName(myModule, "my-grouping");
569 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
571 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
572 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
573 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
575 assertNotNull(testNode);
576 assertNotNull(foundNode);
577 assertEquals(testNode, foundNode);
579 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
580 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
582 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
584 assertNotNull(testNode);
585 assertNotNull(foundNode);
586 assertEquals(testNode, foundNode);
588 // find grouping in rpc
589 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
590 for (GroupingDefinition grouping : rpc.getGroupings()) {
591 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
596 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
597 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
598 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
600 assertNotNull(testNode);
601 assertNotNull(foundNode);
602 assertEquals(testNode, foundNode);
604 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
605 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
607 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
609 assertNotNull(testNode);
610 assertNotNull(foundNode);
611 assertEquals(testNode, foundNode);
613 // find grouping in output
614 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
615 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
617 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
618 QName.create(myModule.getQNameModule(), "output"),
619 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
620 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
622 assertNotNull(testNode);
623 assertNotNull(foundNode);
624 assertEquals(testNode, foundNode);
626 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
627 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
629 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
631 assertNotNull(testNode);
632 assertNotNull(foundNode);
633 assertEquals(testNode, foundNode);
635 // find grouping in input
636 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
637 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
639 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
640 QName.create(myModule.getQNameModule(), "input"),
641 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
642 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
644 assertNotNull(testNode);
645 assertNotNull(foundNode);
646 assertEquals(testNode, foundNode);
648 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
649 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
651 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
653 assertNotNull(testNode);
654 assertNotNull(foundNode);
655 assertEquals(testNode, foundNode);
657 // find grouping in notification
658 dataContainer = getNotificationByName(myModule, "my-notification");
659 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
661 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
662 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
663 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
665 assertNotNull(testNode);
666 assertNotNull(foundNode);
667 assertEquals(testNode, foundNode);
669 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
670 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
672 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
674 assertNotNull(testNode);
675 assertNotNull(foundNode);
676 assertEquals(testNode, foundNode);
678 // find grouping in case
679 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
680 "one").getDataChildByName("my-container-in-case");
681 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
683 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
684 QName.create(myModule.getQNameModule(), "one"),
685 QName.create(myModule.getQNameModule(), "my-container-in-case"),
686 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
687 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
689 assertNotNull(testNode);
690 assertNotNull(foundNode);
691 assertEquals(testNode, foundNode);
693 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
694 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
696 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
698 assertNotNull(testNode);
699 assertNotNull(foundNode);
700 assertEquals(testNode, foundNode);
705 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
706 SourceException, ReactorException {
708 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
710 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
711 QName.parseRevision("2014-10-07"));
713 // find grouping in container
714 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
715 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
717 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
718 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
719 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
721 assertNull(testNode);
722 assertNull(foundNode);
724 // find grouping in list
725 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
726 .getDataChildByName("my-list");
727 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
729 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
730 QName.create(myModule.getQNameModule(), "my-list"),
731 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
732 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
734 assertNull(testNode);
735 assertNull(foundNode);
737 // find grouping in grouping
738 dataContainer = getGroupingByName(myModule, "my-grouping");
739 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
741 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
742 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
743 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
745 assertNull(testNode);
746 assertNull(foundNode);
748 // find grouping in rpc
749 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
750 for (GroupingDefinition grouping : rpc.getGroupings()) {
751 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
756 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
757 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
758 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
760 assertNull(testNode);
761 assertNull(foundNode);
763 // find grouping in output
764 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
765 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
767 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
768 QName.create(myModule.getQNameModule(), "output"),
769 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
770 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
772 assertNull(testNode);
773 assertNull(foundNode);
775 // find grouping in input
776 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
777 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
779 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
780 QName.create(myModule.getQNameModule(), "input"),
781 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
782 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
784 assertNull(testNode);
785 assertNull(foundNode);
787 // find grouping in notification
788 dataContainer = getNotificationByName(myModule, "my-notification");
789 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
791 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
792 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
793 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
795 assertNull(testNode);
796 assertNull(foundNode);
798 // find grouping in case
799 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
800 "one").getDataChildByName("my-container-in-case");
801 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
803 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
804 QName.create(myModule.getQNameModule(), "one"),
805 QName.create(myModule.getQNameModule(), "my-container-in-case"),
806 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
807 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
809 assertNull(testNode);
810 assertNull(foundNode);
814 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
815 for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
816 if (grouping.getQName().getLocalName().equals(name)) {
823 private static RpcDefinition getRpcByName(final Module module, final String name) {
824 for (RpcDefinition rpc : module.getRpcs()) {
825 if (rpc.getQName().getLocalName().equals(name)) {
832 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
833 for (NotificationDefinition notification : module.getNotifications()) {
834 if (notification.getQName().getLocalName().equals(name)) {
842 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
843 SourceException, ReactorException {
845 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
847 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
848 QName.parseRevision("2014-10-07"));
850 ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
851 SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
853 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
854 QName.create(myModule.getQNameModule(), "input"),
855 QName.create(myModule.getQNameModule(), "my-choice"),
856 QName.create(myModule.getQNameModule(), "case-two"),
857 QName.create(myModule.getQNameModule(), "two"));
858 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
860 assertNotNull(testNode);
861 assertNotNull(foundNode);
862 assertEquals(testNode, foundNode);