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 java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.text.ParseException;
17 import java.util.Collections;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
31 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
37 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
38 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
39 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.stmt.TestUtils;
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 = BaseTypes.int32Type();
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, YangSyntaxErrorException,
76 ParseException, 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, YangSyntaxErrorException,
190 ParseException, 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, YangSyntaxErrorException,
284 ParseException, 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, YangSyntaxErrorException, ParseException,
352 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
354 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
355 QName.parseRevision("2014-10-07"));
357 DataSchemaNode node = myModule.getDataChildByName("my-container");
359 Module foundModule = SchemaContextUtil.findParentModule(context, node);
361 assertEquals(myModule, foundModule);
364 @Test(expected = IllegalArgumentException.class)
365 public void findParentModuleIllegalArgumentTest() {
367 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
368 SchemaContextUtil.findParentModule(mockContext, null);
372 @Test(expected = IllegalArgumentException.class)
373 public void findParentModuleIllegalArgumentTest2() {
375 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
376 SchemaContextUtil.findParentModule(null, mockSchemaNode);
380 @Test(expected = IllegalStateException.class)
381 public void findParentModuleIllegalStateTest() {
383 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
384 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
385 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
386 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
390 @Test(expected = IllegalArgumentException.class)
391 public void findDataSchemaNodeIllegalArgumentTest() {
393 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
394 SchemaContextUtil.findDataSchemaNode(mockContext, null);
398 @Test(expected = IllegalArgumentException.class)
399 public void findDataSchemaNodeIllegalArgumentTest2() {
401 SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
402 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
407 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
410 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
412 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
413 QName.parseRevision("2014-10-07"));
414 Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
415 QName.parseRevision("2014-10-07"));
417 SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
418 .getDataChildByName("my-imported-leaf");
420 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
422 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
424 assertNotNull(foundNode);
425 assertNotNull(testNode);
426 assertEquals(testNode, foundNode);
431 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException, ReactorException {
432 // findDataSchemaNode(final SchemaContext context, final Module module,
433 // final RevisionAwareXPath nonCondXPath) {
435 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
437 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
438 QName.parseRevision("2014-10-07"));
440 GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
441 SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
443 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
445 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
447 assertNotNull(foundNode);
448 assertNotNull(testNode);
449 assertEquals(testNode, foundNode);
453 @Test(expected = IllegalArgumentException.class)
454 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
456 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
457 Module module = Mockito.mock(Module.class);
459 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
463 @Test(expected = IllegalArgumentException.class)
464 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
466 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
467 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
469 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
473 @Test(expected = IllegalArgumentException.class)
474 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
476 Module module = Mockito.mock(Module.class);
477 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
479 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
483 @Test(expected = IllegalArgumentException.class)
484 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
486 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
487 Module module = Mockito.mock(Module.class);
488 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
491 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
496 public void findDataSchemaNodeFromXPathNullTest() {
498 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
499 Module module = Mockito.mock(Module.class);
500 RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
502 Mockito.when(xpath.toString()).thenReturn(null);
503 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
508 public void findDataSchemaNodeFromXPathNullTest2() {
510 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
511 Module module = Mockito.mock(Module.class);
512 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
514 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
519 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
520 YangSyntaxErrorException, ParseException, ReactorException {
522 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
524 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
525 QName.parseRevision("2014-10-07"));
527 // find grouping in container
528 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
529 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
531 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
532 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
533 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
535 assertNotNull(testNode);
536 assertNotNull(foundNode);
537 assertEquals(testNode, foundNode);
539 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
540 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
542 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
544 assertNotNull(testNode);
545 assertNotNull(foundNode);
546 assertEquals(testNode, foundNode);
548 // find grouping in list
549 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
550 .getDataChildByName("my-list");
551 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
553 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
554 QName.create(myModule.getQNameModule(), "my-list"),
555 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
556 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
558 assertNotNull(testNode);
559 assertNotNull(foundNode);
560 assertEquals(testNode, foundNode);
562 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
563 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
565 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
567 assertNotNull(testNode);
568 assertNotNull(foundNode);
569 assertEquals(testNode, foundNode);
571 // find grouping in grouping
572 dataContainer = getGroupingByName(myModule, "my-grouping");
573 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
575 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
576 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
577 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
579 assertNotNull(testNode);
580 assertNotNull(foundNode);
581 assertEquals(testNode, foundNode);
583 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
584 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
586 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
588 assertNotNull(testNode);
589 assertNotNull(foundNode);
590 assertEquals(testNode, foundNode);
592 // find grouping in rpc
593 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
594 for (GroupingDefinition grouping : rpc.getGroupings()) {
595 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
600 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
601 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
602 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
604 assertNotNull(testNode);
605 assertNotNull(foundNode);
606 assertEquals(testNode, foundNode);
608 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
609 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
611 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
613 assertNotNull(testNode);
614 assertNotNull(foundNode);
615 assertEquals(testNode, foundNode);
617 // find grouping in output
618 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
619 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
621 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
622 QName.create(myModule.getQNameModule(), "output"),
623 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
624 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
626 assertNotNull(testNode);
627 assertNotNull(foundNode);
628 assertEquals(testNode, foundNode);
630 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
631 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
633 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
635 assertNotNull(testNode);
636 assertNotNull(foundNode);
637 assertEquals(testNode, foundNode);
639 // find grouping in input
640 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
641 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
643 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
644 QName.create(myModule.getQNameModule(), "input"),
645 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
646 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
648 assertNotNull(testNode);
649 assertNotNull(foundNode);
650 assertEquals(testNode, foundNode);
652 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
653 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
655 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
657 assertNotNull(testNode);
658 assertNotNull(foundNode);
659 assertEquals(testNode, foundNode);
661 // find grouping in notification
662 dataContainer = getNotificationByName(myModule, "my-notification");
663 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
665 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
666 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
667 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
669 assertNotNull(testNode);
670 assertNotNull(foundNode);
671 assertEquals(testNode, foundNode);
673 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
674 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
676 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
678 assertNotNull(testNode);
679 assertNotNull(foundNode);
680 assertEquals(testNode, foundNode);
682 // find grouping in case
683 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
684 "one").getDataChildByName("my-container-in-case");
685 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
687 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
688 QName.create(myModule.getQNameModule(), "one"),
689 QName.create(myModule.getQNameModule(), "my-container-in-case"),
690 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
691 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
693 assertNotNull(testNode);
694 assertNotNull(foundNode);
695 assertEquals(testNode, foundNode);
697 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
698 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
700 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
702 assertNotNull(testNode);
703 assertNotNull(foundNode);
704 assertEquals(testNode, foundNode);
709 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
710 YangSyntaxErrorException, ParseException, ReactorException {
712 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
714 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
715 QName.parseRevision("2014-10-07"));
717 // find grouping in container
718 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
719 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
721 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
722 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
723 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
725 assertNull(testNode);
726 assertNull(foundNode);
728 // find grouping in list
729 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
730 .getDataChildByName("my-list");
731 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
733 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
734 QName.create(myModule.getQNameModule(), "my-list"),
735 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
736 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
738 assertNull(testNode);
739 assertNull(foundNode);
741 // find grouping in grouping
742 dataContainer = getGroupingByName(myModule, "my-grouping");
743 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
745 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
746 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
747 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
749 assertNull(testNode);
750 assertNull(foundNode);
752 // find grouping in rpc
753 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
754 for (GroupingDefinition grouping : rpc.getGroupings()) {
755 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
760 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
761 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
762 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
764 assertNull(testNode);
765 assertNull(foundNode);
767 // find grouping in output
768 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
769 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
771 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
772 QName.create(myModule.getQNameModule(), "output"),
773 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
774 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
776 assertNull(testNode);
777 assertNull(foundNode);
779 // find grouping in input
780 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
781 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
783 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
784 QName.create(myModule.getQNameModule(), "input"),
785 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
786 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
788 assertNull(testNode);
789 assertNull(foundNode);
791 // find grouping in notification
792 dataContainer = getNotificationByName(myModule, "my-notification");
793 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
795 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
796 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
797 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
799 assertNull(testNode);
800 assertNull(foundNode);
802 // find grouping in case
803 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
804 "one").getDataChildByName("my-container-in-case");
805 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
807 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
808 QName.create(myModule.getQNameModule(), "one"),
809 QName.create(myModule.getQNameModule(), "my-container-in-case"),
810 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
811 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
813 assertNull(testNode);
814 assertNull(foundNode);
818 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
819 for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
820 if (grouping.getQName().getLocalName().equals(name)) {
827 private static RpcDefinition getRpcByName(final Module module, final String name) {
828 for (RpcDefinition rpc : module.getRpcs()) {
829 if (rpc.getQName().getLocalName().equals(name)) {
836 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
837 for (NotificationDefinition notification : module.getNotifications()) {
838 if (notification.getQName().getLocalName().equals(name)) {
846 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
847 YangSyntaxErrorException, ParseException, ReactorException {
849 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
851 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
852 QName.parseRevision("2014-10-07"));
854 ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
855 SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
857 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
858 QName.create(myModule.getQNameModule(), "input"),
859 QName.create(myModule.getQNameModule(), "my-choice"),
860 QName.create(myModule.getQNameModule(), "case-two"),
861 QName.create(myModule.getQNameModule(), "two"));
862 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
864 assertNotNull(testNode);
865 assertNotNull(foundNode);
866 assertEquals(testNode, foundNode);