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.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.stmt.TestUtils;
44 public class SchemaContextUtilTest {
46 private SchemaContext mockSchemaContext;
48 private Module mockModule;
51 public void testFindDummyData() {
52 MockitoAnnotations.initMocks(this);
54 QName qName = QName.create("TestQName");
55 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
56 assertEquals("Should be null. Module TestQName not found", null,
57 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
59 RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
60 assertEquals("Should be null. Module bookstore not found", null,
61 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
63 SchemaNode schemaNode = Int32.getInstance();
64 RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
65 assertEquals("Should be null, Module prefix not found", null,
66 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
69 assertEquals("Should be null. Module TestQName not found", null,
70 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
72 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
76 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
77 ParseException, ReactorException {
79 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
81 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
82 QName.parseRevision("2014-10-07"));
84 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
85 .getDataChildByName("my-leaf-in-container");
87 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
88 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
89 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
91 assertNotNull(testNode);
92 assertNotNull(foundNode);
93 assertEquals(testNode, foundNode);
95 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
96 testNode = rpc.getInput().getDataChildByName("my-input-leaf");
98 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
99 QName.create(myModule.getQNameModule(), "input"),
100 QName.create(myModule.getQNameModule(), "my-input-leaf"));
102 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
104 assertNotNull(testNode);
105 assertNotNull(foundNode);
106 assertEquals(testNode, foundNode);
108 rpc = getRpcByName(myModule,"my-rpc");
109 testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
111 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
112 QName.create(myModule.getQNameModule(), "output"),
113 QName.create(myModule.getQNameModule(), "my-output-leaf"));
115 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
117 assertNotNull(testNode);
118 assertNotNull(foundNode);
119 assertEquals(testNode, foundNode);
121 NotificationDefinition notification = myModule.getNotifications().iterator().next();
122 testNode = notification.getDataChildByName("my-notification-leaf");
124 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
125 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
127 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
129 assertNotNull(testNode);
130 assertNotNull(foundNode);
131 assertEquals(testNode, foundNode);
133 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
134 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
135 .getDataChildByName("my-leaf-in-grouping");
137 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
138 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
139 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
141 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
143 assertNotNull(testNode);
144 assertNotNull(foundNode);
145 assertEquals(testNode, foundNode);
147 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
148 "my-choice-leaf-one");
150 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
151 QName.create(myModule.getQNameModule(), "one"),
152 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
153 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
155 assertNotNull(testNode);
156 assertNotNull(foundNode);
157 assertEquals(testNode, foundNode);
159 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
160 .getDataChildByName("my-list");
162 testNode = listNode.getDataChildByName("my-leaf-in-list");
164 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
165 QName.create(myModule.getQNameModule(), "my-list"),
166 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
167 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
169 assertNotNull(testNode);
170 assertNotNull(foundNode);
171 assertEquals(testNode, foundNode);
173 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
174 .getDataChildByName("my-list");
176 testNode = listNode.getDataChildByName("my-leaf-list-in-list");
178 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
179 QName.create(myModule.getQNameModule(), "my-list"),
180 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
181 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
183 assertNotNull(testNode);
184 assertNotNull(foundNode);
185 assertEquals(testNode, foundNode);
190 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
191 ParseException, ReactorException {
193 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test") ;
195 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
196 QName.parseRevision("2014-10-07"));
198 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
199 .getDataChildByName("my-leaf-not-in-container");
201 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
202 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
203 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
205 assertNull(testNode);
206 assertNull(foundNode);
208 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
209 testNode = rpc.getInput().getDataChildByName("no-input-leaf");
211 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
212 QName.create(myModule.getQNameModule(), "input"),
213 QName.create(myModule.getQNameModule(), "no-input-leaf"));
215 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
217 assertNull(testNode);
218 assertNull(foundNode);
220 NotificationDefinition notification = myModule.getNotifications().iterator().next();
221 testNode = notification.getDataChildByName("no-notification-leaf");
223 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
224 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
226 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
228 assertNull(testNode);
229 assertNull(foundNode);
231 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
232 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
233 .getDataChildByName("no-leaf-in-grouping");
235 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
236 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
237 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
239 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
241 assertNull(testNode);
242 assertNull(foundNode);
244 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
247 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
248 QName.create(myModule.getQNameModule(), "one"),
249 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
250 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
252 assertNull(testNode);
253 assertNull(foundNode);
255 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
256 .getDataChildByName("my-list");
258 testNode = listNode.getDataChildByName("no-leaf-in-list");
260 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
261 QName.create(myModule.getQNameModule(), "my-list"),
262 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
263 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
265 assertNull(testNode);
266 assertNull(foundNode);
268 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
269 .getDataChildByName("my-list");
271 testNode = listNode.getDataChildByName("no-leaf-list-in-list");
273 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
274 QName.create(myModule.getQNameModule(), "my-list"),
275 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
276 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
278 assertNull(testNode);
279 assertNull(foundNode);
284 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
285 ParseException, ReactorException {
287 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
289 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
290 QName.parseRevision("2014-10-07"));
292 SchemaNode testNode = myModule.getDataChildByName("my-container");
294 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
295 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
297 assertNotNull(testNode);
298 assertNotNull(foundNode);
299 assertEquals(testNode, foundNode);
301 testNode = getRpcByName(myModule,"my-rpc");
303 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
304 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
306 assertNotNull(testNode);
307 assertNotNull(foundNode);
308 assertEquals(testNode, foundNode);
310 testNode = myModule.getNotifications().iterator().next();
312 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
313 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
315 assertNotNull(testNode);
316 assertNotNull(foundNode);
317 assertEquals(testNode, foundNode);
319 testNode = getGroupingByName(myModule,"my-grouping");
321 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
322 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
324 assertNotNull(testNode);
325 assertNotNull(foundNode);
326 assertEquals(testNode, foundNode);
328 testNode = myModule.getDataChildByName("my-choice");
330 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
331 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
333 assertNotNull(testNode);
334 assertNotNull(foundNode);
335 assertEquals(testNode, foundNode);
337 testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list");
339 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
340 QName.create(myModule.getQNameModule(), "my-list"));
341 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
343 assertNotNull(testNode);
344 assertNotNull(foundNode);
345 assertEquals(testNode, foundNode);
350 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException, ParseException,
353 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
355 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
356 QName.parseRevision("2014-10-07"));
358 DataSchemaNode node = myModule.getDataChildByName("my-container");
360 Module foundModule = SchemaContextUtil.findParentModule(context, node);
362 assertEquals(myModule, foundModule);
365 @Test(expected = IllegalArgumentException.class)
366 public void findParentModuleIllegalArgumentTest() {
368 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
369 SchemaContextUtil.findParentModule(mockContext, null);
373 @Test(expected = IllegalArgumentException.class)
374 public void findParentModuleIllegalArgumentTest2() {
376 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
377 SchemaContextUtil.findParentModule(null, mockSchemaNode);
381 @Test(expected = IllegalStateException.class)
382 public void findParentModuleIllegalStateTest() {
384 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
385 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
386 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
387 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
391 @Test(expected = IllegalArgumentException.class)
392 public void findDataSchemaNodeIllegalArgumentTest() {
394 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
395 SchemaContextUtil.findDataSchemaNode(mockContext, null);
399 @Test(expected = IllegalArgumentException.class)
400 public void findDataSchemaNodeIllegalArgumentTest2() {
402 SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
403 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
408 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
411 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
413 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
414 QName.parseRevision("2014-10-07"));
415 Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
416 QName.parseRevision("2014-10-07"));
418 SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
419 .getDataChildByName("my-imported-leaf");
421 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
423 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
425 assertNotNull(foundNode);
426 assertNotNull(testNode);
427 assertEquals(testNode, foundNode);
432 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException, ReactorException {
433 // findDataSchemaNode(final SchemaContext context, final Module module,
434 // final RevisionAwareXPath nonCondXPath) {
436 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
438 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
439 QName.parseRevision("2014-10-07"));
441 GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
442 SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
444 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
446 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
448 assertNotNull(foundNode);
449 assertNotNull(testNode);
450 assertEquals(testNode, foundNode);
454 @Test(expected = IllegalArgumentException.class)
455 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
457 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
458 Module module = Mockito.mock(Module.class);
460 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
464 @Test(expected = IllegalArgumentException.class)
465 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
467 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
468 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
470 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
474 @Test(expected = IllegalArgumentException.class)
475 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
477 Module module = Mockito.mock(Module.class);
478 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
480 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
484 @Test(expected = IllegalArgumentException.class)
485 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
487 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
488 Module module = Mockito.mock(Module.class);
489 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
492 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
497 public void findDataSchemaNodeFromXPathNullTest() {
499 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
500 Module module = Mockito.mock(Module.class);
501 RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
503 Mockito.when(xpath.toString()).thenReturn(null);
504 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
509 public void findDataSchemaNodeFromXPathNullTest2() {
511 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
512 Module module = Mockito.mock(Module.class);
513 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
515 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
520 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
521 YangSyntaxErrorException, ParseException, ReactorException {
523 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
525 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
526 QName.parseRevision("2014-10-07"));
528 // find grouping in container
529 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
530 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
532 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
533 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
534 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
536 assertNotNull(testNode);
537 assertNotNull(foundNode);
538 assertEquals(testNode, foundNode);
540 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
541 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
543 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
545 assertNotNull(testNode);
546 assertNotNull(foundNode);
547 assertEquals(testNode, foundNode);
549 // find grouping in list
550 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
551 .getDataChildByName("my-list");
552 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
554 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
555 QName.create(myModule.getQNameModule(), "my-list"),
556 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
557 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
559 assertNotNull(testNode);
560 assertNotNull(foundNode);
561 assertEquals(testNode, foundNode);
563 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
564 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
566 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
568 assertNotNull(testNode);
569 assertNotNull(foundNode);
570 assertEquals(testNode, foundNode);
572 // find grouping in grouping
573 dataContainer = getGroupingByName(myModule, "my-grouping");
574 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
576 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
577 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
578 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
580 assertNotNull(testNode);
581 assertNotNull(foundNode);
582 assertEquals(testNode, foundNode);
584 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
585 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
587 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
589 assertNotNull(testNode);
590 assertNotNull(foundNode);
591 assertEquals(testNode, foundNode);
593 // find grouping in rpc
594 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
595 for (GroupingDefinition grouping : rpc.getGroupings()) {
596 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
601 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
602 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
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-rpc");
610 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
612 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
614 assertNotNull(testNode);
615 assertNotNull(foundNode);
616 assertEquals(testNode, foundNode);
618 // find grouping in output
619 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
620 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
622 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
623 QName.create(myModule.getQNameModule(), "output"),
624 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
625 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
627 assertNotNull(testNode);
628 assertNotNull(foundNode);
629 assertEquals(testNode, foundNode);
631 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
632 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
634 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
636 assertNotNull(testNode);
637 assertNotNull(foundNode);
638 assertEquals(testNode, foundNode);
640 // find grouping in input
641 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
642 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
644 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
645 QName.create(myModule.getQNameModule(), "input"),
646 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
647 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
649 assertNotNull(testNode);
650 assertNotNull(foundNode);
651 assertEquals(testNode, foundNode);
653 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
654 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
656 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
658 assertNotNull(testNode);
659 assertNotNull(foundNode);
660 assertEquals(testNode, foundNode);
662 // find grouping in notification
663 dataContainer = getNotificationByName(myModule, "my-notification");
664 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
666 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
667 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
668 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
670 assertNotNull(testNode);
671 assertNotNull(foundNode);
672 assertEquals(testNode, foundNode);
674 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
675 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
677 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
679 assertNotNull(testNode);
680 assertNotNull(foundNode);
681 assertEquals(testNode, foundNode);
683 // find grouping in case
684 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
685 "one").getDataChildByName("my-container-in-case");
686 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
688 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
689 QName.create(myModule.getQNameModule(), "one"),
690 QName.create(myModule.getQNameModule(), "my-container-in-case"),
691 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
692 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
694 assertNotNull(testNode);
695 assertNotNull(foundNode);
696 assertEquals(testNode, foundNode);
698 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
699 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
701 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
703 assertNotNull(testNode);
704 assertNotNull(foundNode);
705 assertEquals(testNode, foundNode);
710 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
711 YangSyntaxErrorException, ParseException, ReactorException {
713 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
715 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
716 QName.parseRevision("2014-10-07"));
718 // find grouping in container
719 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
720 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
722 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
723 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
724 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
726 assertNull(testNode);
727 assertNull(foundNode);
729 // find grouping in list
730 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
731 .getDataChildByName("my-list");
732 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
734 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
735 QName.create(myModule.getQNameModule(), "my-list"),
736 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
737 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
739 assertNull(testNode);
740 assertNull(foundNode);
742 // find grouping in grouping
743 dataContainer = getGroupingByName(myModule, "my-grouping");
744 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
746 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
747 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
748 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
750 assertNull(testNode);
751 assertNull(foundNode);
753 // find grouping in rpc
754 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
755 for (GroupingDefinition grouping : rpc.getGroupings()) {
756 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
761 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
762 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
763 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
765 assertNull(testNode);
766 assertNull(foundNode);
768 // find grouping in output
769 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
770 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
772 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
773 QName.create(myModule.getQNameModule(), "output"),
774 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
775 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
777 assertNull(testNode);
778 assertNull(foundNode);
780 // find grouping in input
781 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
782 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
784 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
785 QName.create(myModule.getQNameModule(), "input"),
786 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
787 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
789 assertNull(testNode);
790 assertNull(foundNode);
792 // find grouping in notification
793 dataContainer = getNotificationByName(myModule, "my-notification");
794 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
796 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
797 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
798 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
800 assertNull(testNode);
801 assertNull(foundNode);
803 // find grouping in case
804 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
805 "one").getDataChildByName("my-container-in-case");
806 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
808 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
809 QName.create(myModule.getQNameModule(), "one"),
810 QName.create(myModule.getQNameModule(), "my-container-in-case"),
811 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
812 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
814 assertNull(testNode);
815 assertNull(foundNode);
819 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
820 for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
821 if (grouping.getQName().getLocalName().equals(name)) {
828 private static RpcDefinition getRpcByName(final Module module, final String name) {
829 for (RpcDefinition rpc : module.getRpcs()) {
830 if (rpc.getQName().getLocalName().equals(name)) {
837 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
838 for (NotificationDefinition notification : module.getNotifications()) {
839 if (notification.getQName().getLocalName().equals(name)) {
847 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
848 YangSyntaxErrorException, ParseException, ReactorException {
850 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
852 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
853 QName.parseRevision("2014-10-07"));
855 ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
856 SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
858 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
859 QName.create(myModule.getQNameModule(), "input"),
860 QName.create(myModule.getQNameModule(), "my-choice"),
861 QName.create(myModule.getQNameModule(), "case-two"),
862 QName.create(myModule.getQNameModule(), "two"));
863 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
865 assertNotNull(testNode);
866 assertNotNull(foundNode);
867 assertEquals(testNode, foundNode);