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;
14 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import java.util.Collections;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
32 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
38 import org.opendaylight.yangtools.yang.model.util.Int32;
39 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
40 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
41 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
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, YangSyntaxErrorException,
78 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
79 File resourceDir = resourceFile.getParentFile();
81 YangParserImpl parser = YangParserImpl.getInstance();
82 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
84 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
85 QName.parseRevision("2014-10-07"));
87 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
88 .getDataChildByName("my-leaf-in-container");
90 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
91 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
92 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
94 assertNotNull(testNode);
95 assertNotNull(foundNode);
96 assertEquals(testNode, foundNode);
98 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
99 testNode = rpc.getInput().getDataChildByName("my-input-leaf");
101 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
102 QName.create(myModule.getQNameModule(), "input"),
103 QName.create(myModule.getQNameModule(), "my-input-leaf"));
105 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
107 assertNotNull(testNode);
108 assertNotNull(foundNode);
109 assertEquals(testNode, foundNode);
111 rpc = getRpcByName(myModule,"my-rpc");
112 testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
114 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
115 QName.create(myModule.getQNameModule(), "output"),
116 QName.create(myModule.getQNameModule(), "my-output-leaf"));
118 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
120 assertNotNull(testNode);
121 assertNotNull(foundNode);
122 assertEquals(testNode, foundNode);
124 NotificationDefinition notification = myModule.getNotifications().iterator().next();
125 testNode = notification.getDataChildByName("my-notification-leaf");
127 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
128 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
130 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
132 assertNotNull(testNode);
133 assertNotNull(foundNode);
134 assertEquals(testNode, foundNode);
136 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
137 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
138 .getDataChildByName("my-leaf-in-grouping");
140 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
141 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
142 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
144 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
146 assertNotNull(testNode);
147 assertNotNull(foundNode);
148 assertEquals(testNode, foundNode);
150 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
151 "my-choice-leaf-one");
153 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
154 QName.create(myModule.getQNameModule(), "one"),
155 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
156 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
158 assertNotNull(testNode);
159 assertNotNull(foundNode);
160 assertEquals(testNode, foundNode);
162 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
163 .getDataChildByName("my-list");
165 testNode = listNode.getDataChildByName("my-leaf-in-list");
167 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
168 QName.create(myModule.getQNameModule(), "my-list"),
169 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
170 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
172 assertNotNull(testNode);
173 assertNotNull(foundNode);
174 assertEquals(testNode, foundNode);
176 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
177 .getDataChildByName("my-list");
179 testNode = listNode.getDataChildByName("my-leaf-list-in-list");
181 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
182 QName.create(myModule.getQNameModule(), "my-list"),
183 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
184 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
186 assertNotNull(testNode);
187 assertNotNull(foundNode);
188 assertEquals(testNode, foundNode);
193 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
196 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
197 File resourceDir = resourceFile.getParentFile();
199 YangParserImpl parser = YangParserImpl.getInstance();
200 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
202 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
203 QName.parseRevision("2014-10-07"));
205 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
206 .getDataChildByName("my-leaf-not-in-container");
208 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
209 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
210 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
212 assertNull(testNode);
213 assertNull(foundNode);
215 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
216 testNode = rpc.getInput().getDataChildByName("no-input-leaf");
218 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
219 QName.create(myModule.getQNameModule(), "input"),
220 QName.create(myModule.getQNameModule(), "no-input-leaf"));
222 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
224 assertNull(testNode);
225 assertNull(foundNode);
227 NotificationDefinition notification = myModule.getNotifications().iterator().next();
228 testNode = notification.getDataChildByName("no-notification-leaf");
230 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
231 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
233 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
235 assertNull(testNode);
236 assertNull(foundNode);
238 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
239 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
240 .getDataChildByName("no-leaf-in-grouping");
242 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
243 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
244 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
246 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
248 assertNull(testNode);
249 assertNull(foundNode);
251 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
254 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
255 QName.create(myModule.getQNameModule(), "one"),
256 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
257 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
259 assertNull(testNode);
260 assertNull(foundNode);
262 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
263 .getDataChildByName("my-list");
265 testNode = listNode.getDataChildByName("no-leaf-in-list");
267 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
268 QName.create(myModule.getQNameModule(), "my-list"),
269 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
270 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
272 assertNull(testNode);
273 assertNull(foundNode);
275 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
276 .getDataChildByName("my-list");
278 testNode = listNode.getDataChildByName("no-leaf-list-in-list");
280 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
281 QName.create(myModule.getQNameModule(), "my-list"),
282 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
283 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
285 assertNull(testNode);
286 assertNull(foundNode);
291 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
294 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
295 File resourceDir = resourceFile.getParentFile();
297 YangParserImpl parser = YangParserImpl.getInstance();
298 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
300 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
301 QName.parseRevision("2014-10-07"));
303 SchemaNode testNode = myModule.getDataChildByName("my-container");
305 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
306 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
308 assertNotNull(testNode);
309 assertNotNull(foundNode);
310 assertEquals(testNode, foundNode);
312 testNode = getRpcByName(myModule,"my-rpc");
314 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
315 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
317 assertNotNull(testNode);
318 assertNotNull(foundNode);
319 assertEquals(testNode, foundNode);
321 testNode = myModule.getNotifications().iterator().next();
323 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
324 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
326 assertNotNull(testNode);
327 assertNotNull(foundNode);
328 assertEquals(testNode, foundNode);
330 testNode = getGroupingByName(myModule,"my-grouping");
332 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
333 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
335 assertNotNull(testNode);
336 assertNotNull(foundNode);
337 assertEquals(testNode, foundNode);
339 testNode = myModule.getDataChildByName("my-choice");
341 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
342 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
344 assertNotNull(testNode);
345 assertNotNull(foundNode);
346 assertEquals(testNode, foundNode);
348 testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list");
350 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
351 QName.create(myModule.getQNameModule(), "my-list"));
352 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
354 assertNotNull(testNode);
355 assertNotNull(foundNode);
356 assertEquals(testNode, foundNode);
361 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException, ParseException {
363 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
364 File resourceDir = resourceFile.getParentFile();
366 YangParserImpl parser = YangParserImpl.getInstance();
367 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
369 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
370 QName.parseRevision("2014-10-07"));
372 DataSchemaNode node = myModule.getDataChildByName("my-container");
374 Module foundModule = SchemaContextUtil.findParentModule(context, node);
376 assertEquals(myModule, foundModule);
379 @Test(expected = IllegalArgumentException.class)
380 public void findParentModuleIllegalArgumentTest() {
382 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
383 SchemaContextUtil.findParentModule(mockContext, null);
387 @Test(expected = IllegalArgumentException.class)
388 public void findParentModuleIllegalArgumentTest2() {
390 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
391 SchemaContextUtil.findParentModule(null, mockSchemaNode);
395 @Test(expected = IllegalStateException.class)
396 public void findParentModuleIllegalStateTest() {
398 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
399 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
400 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
401 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
405 @Test(expected = IllegalArgumentException.class)
406 public void findDataSchemaNodeIllegalArgumentTest() {
408 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
409 SchemaContextUtil.findDataSchemaNode(mockContext, null);
413 @Test(expected = IllegalArgumentException.class)
414 public void findDataSchemaNodeIllegalArgumentTest2() {
416 SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
417 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
422 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException {
424 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
425 File resourceDir = resourceFile.getParentFile();
427 YangParserImpl parser = YangParserImpl.getInstance();
428 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
430 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
431 QName.parseRevision("2014-10-07"));
432 Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
433 QName.parseRevision("2014-10-07"));
435 SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
436 .getDataChildByName("my-imported-leaf");
438 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
440 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
442 assertNotNull(foundNode);
443 assertNotNull(testNode);
444 assertEquals(testNode, foundNode);
449 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException {
450 // findDataSchemaNode(final SchemaContext context, final Module module,
451 // final RevisionAwareXPath nonCondXPath) {
453 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
454 File resourceDir = resourceFile.getParentFile();
456 YangParserImpl parser = YangParserImpl.getInstance();
457 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
459 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
460 QName.parseRevision("2014-10-07"));
462 GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
463 SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
465 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
467 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
469 assertNotNull(foundNode);
470 assertNotNull(testNode);
471 assertEquals(testNode, foundNode);
475 @Test(expected = IllegalArgumentException.class)
476 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
478 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
479 Module module = Mockito.mock(Module.class);
481 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
485 @Test(expected = IllegalArgumentException.class)
486 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
488 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
489 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
491 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
495 @Test(expected = IllegalArgumentException.class)
496 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
498 Module module = Mockito.mock(Module.class);
499 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
501 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
505 @Test(expected = IllegalArgumentException.class)
506 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
508 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
509 Module module = Mockito.mock(Module.class);
510 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
513 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
518 public void findDataSchemaNodeFromXPathNullTest() {
520 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
521 Module module = Mockito.mock(Module.class);
522 RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
524 Mockito.when(xpath.toString()).thenReturn(null);
525 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
530 public void findDataSchemaNodeFromXPathNullTest2() {
532 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
533 Module module = Mockito.mock(Module.class);
534 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
536 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
541 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
542 YangSyntaxErrorException, ParseException {
544 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
545 File resourceDir = resourceFile.getParentFile();
547 YangParserImpl parser = YangParserImpl.getInstance();
548 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
550 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
551 QName.parseRevision("2014-10-07"));
553 // find grouping in container
554 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
555 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
557 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
558 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
559 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
561 assertNotNull(testNode);
562 assertNotNull(foundNode);
563 assertEquals(testNode, foundNode);
565 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
566 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
568 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
570 assertNotNull(testNode);
571 assertNotNull(foundNode);
572 assertEquals(testNode, foundNode);
574 // find grouping in list
575 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
576 .getDataChildByName("my-list");
577 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
579 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
580 QName.create(myModule.getQNameModule(), "my-list"),
581 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
582 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
584 assertNotNull(testNode);
585 assertNotNull(foundNode);
586 assertEquals(testNode, foundNode);
588 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
589 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
591 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
593 assertNotNull(testNode);
594 assertNotNull(foundNode);
595 assertEquals(testNode, foundNode);
597 // find grouping in grouping
598 dataContainer = getGroupingByName(myModule, "my-grouping");
599 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
601 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
602 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
603 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
605 assertNotNull(testNode);
606 assertNotNull(foundNode);
607 assertEquals(testNode, foundNode);
609 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
610 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
612 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
614 assertNotNull(testNode);
615 assertNotNull(foundNode);
616 assertEquals(testNode, foundNode);
618 // find grouping in rpc
619 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
620 for (GroupingDefinition grouping : rpc.getGroupings()) {
621 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
626 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
627 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
628 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
630 assertNotNull(testNode);
631 assertNotNull(foundNode);
632 assertEquals(testNode, foundNode);
634 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
635 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
637 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
639 assertNotNull(testNode);
640 assertNotNull(foundNode);
641 assertEquals(testNode, foundNode);
643 // find grouping in output
644 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
645 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
647 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
648 QName.create(myModule.getQNameModule(), "output"),
649 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
650 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
652 assertNotNull(testNode);
653 assertNotNull(foundNode);
654 assertEquals(testNode, foundNode);
656 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
657 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
659 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
661 assertNotNull(testNode);
662 assertNotNull(foundNode);
663 assertEquals(testNode, foundNode);
665 // find grouping in input
666 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
667 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
669 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
670 QName.create(myModule.getQNameModule(), "input"),
671 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
672 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
674 assertNotNull(testNode);
675 assertNotNull(foundNode);
676 assertEquals(testNode, foundNode);
678 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
679 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
681 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
683 assertNotNull(testNode);
684 assertNotNull(foundNode);
685 assertEquals(testNode, foundNode);
687 // find grouping in notification
688 dataContainer = getNotificationByName(myModule, "my-notification");
689 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
691 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
692 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
693 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
695 assertNotNull(testNode);
696 assertNotNull(foundNode);
697 assertEquals(testNode, foundNode);
699 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
700 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
702 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
704 assertNotNull(testNode);
705 assertNotNull(foundNode);
706 assertEquals(testNode, foundNode);
708 // find grouping in case
709 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
710 "one").getDataChildByName("my-container-in-case");
711 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
713 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
714 QName.create(myModule.getQNameModule(), "one"),
715 QName.create(myModule.getQNameModule(), "my-container-in-case"),
716 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
717 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
719 assertNotNull(testNode);
720 assertNotNull(foundNode);
721 assertEquals(testNode, foundNode);
723 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
724 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
726 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
728 assertNotNull(testNode);
729 assertNotNull(foundNode);
730 assertEquals(testNode, foundNode);
735 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
736 YangSyntaxErrorException, ParseException {
738 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
739 File resourceDir = resourceFile.getParentFile();
741 YangParserImpl parser = YangParserImpl.getInstance();
742 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
744 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
745 QName.parseRevision("2014-10-07"));
747 // find grouping in container
748 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
749 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
751 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
752 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
753 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
755 assertNull(testNode);
756 assertNull(foundNode);
758 // find grouping in list
759 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
760 .getDataChildByName("my-list");
761 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
763 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
764 QName.create(myModule.getQNameModule(), "my-list"),
765 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
766 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
768 assertNull(testNode);
769 assertNull(foundNode);
771 // find grouping in grouping
772 dataContainer = getGroupingByName(myModule, "my-grouping");
773 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
775 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
776 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
777 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
779 assertNull(testNode);
780 assertNull(foundNode);
782 // find grouping in rpc
783 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
784 for (GroupingDefinition grouping : rpc.getGroupings()) {
785 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
790 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
791 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
792 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
794 assertNull(testNode);
795 assertNull(foundNode);
797 // find grouping in output
798 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
799 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
801 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
802 QName.create(myModule.getQNameModule(), "output"),
803 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
804 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
806 assertNull(testNode);
807 assertNull(foundNode);
809 // find grouping in input
810 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
811 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
813 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
814 QName.create(myModule.getQNameModule(), "input"),
815 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
816 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
818 assertNull(testNode);
819 assertNull(foundNode);
821 // find grouping in notification
822 dataContainer = getNotificationByName(myModule, "my-notification");
823 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
825 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
826 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
827 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
829 assertNull(testNode);
830 assertNull(foundNode);
832 // find grouping in case
833 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
834 "one").getDataChildByName("my-container-in-case");
835 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
837 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
838 QName.create(myModule.getQNameModule(), "one"),
839 QName.create(myModule.getQNameModule(), "my-container-in-case"),
840 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
841 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
843 assertNull(testNode);
844 assertNull(foundNode);
848 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
849 for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
850 if (grouping.getQName().getLocalName().equals(name)) {
857 private static RpcDefinition getRpcByName(final Module module, final String name) {
858 for (RpcDefinition rpc : module.getRpcs()) {
859 if (rpc.getQName().getLocalName().equals(name)) {
866 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
867 for (NotificationDefinition notification : module.getNotifications()) {
868 if (notification.getQName().getLocalName().equals(name)) {
876 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
877 YangSyntaxErrorException, ParseException {
879 File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
880 File resourceDir = resourceFile.getParentFile();
882 YangParserImpl parser = YangParserImpl.getInstance();
883 SchemaContext context = parser.parseFile(resourceFile, resourceDir);
885 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
886 QName.parseRevision("2014-10-07"));
888 ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
889 SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
891 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
892 QName.create(myModule.getQNameModule(), "input"),
893 QName.create(myModule.getQNameModule(), "my-choice"),
894 QName.create(myModule.getQNameModule(), "case-two"),
895 QName.create(myModule.getQNameModule(), "two"));
896 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
898 assertNotNull(testNode);
899 assertNotNull(foundNode);
900 assertEquals(testNode, foundNode);