2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.Collections;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
36 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
37 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
41 public class SchemaContextUtilTest {
43 private SchemaContext mockSchemaContext;
45 private Module mockModule;
48 public void testFindDummyData() {
49 MockitoAnnotations.initMocks(this);
51 QName qName = QName.create("TestQName");
52 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
53 assertEquals("Should be null. Module TestQName not found", null,
54 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
56 RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
57 assertEquals("Should be null. Module bookstore not found", null,
58 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
60 SchemaNode schemaNode = BaseTypes.int32Type();
61 RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
62 assertEquals("Should be null, Module prefix not found", null,
63 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
66 assertEquals("Should be null. Module TestQName not found", null,
67 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
69 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
73 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException,
74 SourceException, ReactorException {
76 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
78 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
79 QName.parseRevision("2014-10-07"));
81 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
82 .getDataChildByName("my-leaf-in-container");
84 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
85 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
86 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
88 assertNotNull(testNode);
89 assertNotNull(foundNode);
90 assertEquals(testNode, foundNode);
92 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
93 testNode = rpc.getInput().getDataChildByName("my-input-leaf");
95 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
96 QName.create(myModule.getQNameModule(), "input"),
97 QName.create(myModule.getQNameModule(), "my-input-leaf"));
99 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
101 assertNotNull(testNode);
102 assertNotNull(foundNode);
103 assertEquals(testNode, foundNode);
105 rpc = getRpcByName(myModule,"my-rpc");
106 testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
108 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
109 QName.create(myModule.getQNameModule(), "output"),
110 QName.create(myModule.getQNameModule(), "my-output-leaf"));
112 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
114 assertNotNull(testNode);
115 assertNotNull(foundNode);
116 assertEquals(testNode, foundNode);
118 NotificationDefinition notification = myModule.getNotifications().iterator().next();
119 testNode = notification.getDataChildByName("my-notification-leaf");
121 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
122 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
124 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
126 assertNotNull(testNode);
127 assertNotNull(foundNode);
128 assertEquals(testNode, foundNode);
130 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
131 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
132 .getDataChildByName("my-leaf-in-grouping");
134 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
135 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
136 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
138 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
140 assertNotNull(testNode);
141 assertNotNull(foundNode);
142 assertEquals(testNode, foundNode);
144 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
145 "my-choice-leaf-one");
147 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
148 QName.create(myModule.getQNameModule(), "one"),
149 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
150 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
152 assertNotNull(testNode);
153 assertNotNull(foundNode);
154 assertEquals(testNode, foundNode);
156 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
157 .getDataChildByName("my-list");
159 testNode = listNode.getDataChildByName("my-leaf-in-list");
161 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
162 QName.create(myModule.getQNameModule(), "my-list"),
163 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
164 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
166 assertNotNull(testNode);
167 assertNotNull(foundNode);
168 assertEquals(testNode, foundNode);
170 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
171 .getDataChildByName("my-list");
173 testNode = listNode.getDataChildByName("my-leaf-list-in-list");
175 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
176 QName.create(myModule.getQNameModule(), "my-list"),
177 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
178 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
180 assertNotNull(testNode);
181 assertNotNull(foundNode);
182 assertEquals(testNode, foundNode);
187 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException,
188 SourceException, ReactorException {
190 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
192 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
193 QName.parseRevision("2014-10-07"));
195 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
196 .getDataChildByName("my-leaf-not-in-container");
198 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
199 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
200 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
202 assertNull(testNode);
203 assertNull(foundNode);
205 RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
206 testNode = rpc.getInput().getDataChildByName("no-input-leaf");
208 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
209 QName.create(myModule.getQNameModule(), "input"),
210 QName.create(myModule.getQNameModule(), "no-input-leaf"));
212 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
214 assertNull(testNode);
215 assertNull(foundNode);
217 NotificationDefinition notification = myModule.getNotifications().iterator().next();
218 testNode = notification.getDataChildByName("no-notification-leaf");
220 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
221 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
223 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
225 assertNull(testNode);
226 assertNull(foundNode);
228 GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping");
229 testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping"))
230 .getDataChildByName("no-leaf-in-grouping");
232 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
233 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
234 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
236 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
238 assertNull(testNode);
239 assertNull(foundNode);
241 testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName(
244 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
245 QName.create(myModule.getQNameModule(), "one"),
246 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
247 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
249 assertNull(testNode);
250 assertNull(foundNode);
252 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
253 .getDataChildByName("my-list");
255 testNode = listNode.getDataChildByName("no-leaf-in-list");
257 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
258 QName.create(myModule.getQNameModule(), "my-list"),
259 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
260 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
262 assertNull(testNode);
263 assertNull(foundNode);
265 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container"))
266 .getDataChildByName("my-list");
268 testNode = listNode.getDataChildByName("no-leaf-list-in-list");
270 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
271 QName.create(myModule.getQNameModule(), "my-list"),
272 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
273 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
275 assertNull(testNode);
276 assertNull(foundNode);
281 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException,
282 SourceException, ReactorException {
284 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
286 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
287 QName.parseRevision("2014-10-07"));
289 SchemaNode testNode = myModule.getDataChildByName("my-container");
291 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
292 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
294 assertNotNull(testNode);
295 assertNotNull(foundNode);
296 assertEquals(testNode, foundNode);
298 testNode = getRpcByName(myModule,"my-rpc");
300 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
301 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
303 assertNotNull(testNode);
304 assertNotNull(foundNode);
305 assertEquals(testNode, foundNode);
307 testNode = myModule.getNotifications().iterator().next();
309 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
310 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
312 assertNotNull(testNode);
313 assertNotNull(foundNode);
314 assertEquals(testNode, foundNode);
316 testNode = getGroupingByName(myModule,"my-grouping");
318 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
319 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
321 assertNotNull(testNode);
322 assertNotNull(foundNode);
323 assertEquals(testNode, foundNode);
325 testNode = myModule.getDataChildByName("my-choice");
327 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
328 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
330 assertNotNull(testNode);
331 assertNotNull(foundNode);
332 assertEquals(testNode, foundNode);
334 testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list");
336 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
337 QName.create(myModule.getQNameModule(), "my-list"));
338 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
340 assertNotNull(testNode);
341 assertNotNull(foundNode);
342 assertEquals(testNode, foundNode);
347 public void findParentModuleTest() throws URISyntaxException, IOException, SourceException, ReactorException {
349 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
351 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
352 QName.parseRevision("2014-10-07"));
354 DataSchemaNode node = myModule.getDataChildByName("my-container");
356 Module foundModule = SchemaContextUtil.findParentModule(context, node);
358 assertEquals(myModule, foundModule);
361 @Test(expected = IllegalArgumentException.class)
362 public void findParentModuleIllegalArgumentTest() {
364 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
365 SchemaContextUtil.findParentModule(mockContext, null);
369 @Test(expected = IllegalArgumentException.class)
370 public void findParentModuleIllegalArgumentTest2() {
372 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
373 SchemaContextUtil.findParentModule(null, mockSchemaNode);
377 @Test(expected = IllegalStateException.class)
378 public void findParentModuleIllegalStateTest() {
380 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
381 SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
382 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
383 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
387 @Test(expected = IllegalArgumentException.class)
388 public void findDataSchemaNodeIllegalArgumentTest() {
390 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
391 SchemaContextUtil.findDataSchemaNode(mockContext, null);
395 @Test(expected = IllegalArgumentException.class)
396 public void findDataSchemaNodeIllegalArgumentTest2() {
398 SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
399 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
404 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, SourceException, ReactorException {
406 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
408 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
409 QName.parseRevision("2014-10-07"));
410 Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
411 QName.parseRevision("2014-10-07"));
413 SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container"))
414 .getDataChildByName("my-imported-leaf");
416 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true);
418 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
420 assertNotNull(foundNode);
421 assertNotNull(testNode);
422 assertEquals(testNode, foundNode);
427 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, SourceException, ReactorException {
429 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
431 Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
432 QName.parseRevision("2014-10-07"));
434 GroupingDefinition grouping = getGroupingByName(module,"my-grouping");
435 SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2");
437 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
439 SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
441 assertNotNull(foundNode);
442 assertNotNull(testNode);
443 assertEquals(testNode, foundNode);
447 @Test(expected = IllegalArgumentException.class)
448 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
450 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
451 Module module = Mockito.mock(Module.class);
453 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
457 @Test(expected = IllegalArgumentException.class)
458 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
460 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
461 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
463 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
467 @Test(expected = IllegalArgumentException.class)
468 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
470 Module module = Mockito.mock(Module.class);
471 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
473 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
477 @Test(expected = IllegalArgumentException.class)
478 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
480 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
481 Module module = Mockito.mock(Module.class);
482 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2",
485 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
490 public void findDataSchemaNodeFromXPathNullTest() {
492 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
493 Module module = Mockito.mock(Module.class);
494 RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
496 Mockito.when(xpath.toString()).thenReturn(null);
497 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
502 public void findDataSchemaNodeFromXPathNullTest2() {
504 SchemaContext mockContext = Mockito.mock(SchemaContext.class);
505 Module module = Mockito.mock(Module.class);
506 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
508 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
513 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
514 SourceException, ReactorException {
516 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
518 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
519 QName.parseRevision("2014-10-07"));
521 // find grouping in container
522 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
523 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
525 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
526 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
527 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
529 assertNotNull(testNode);
530 assertNotNull(foundNode);
531 assertEquals(testNode, foundNode);
533 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
534 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
536 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
538 assertNotNull(testNode);
539 assertNotNull(foundNode);
540 assertEquals(testNode, foundNode);
542 // find grouping in list
543 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
544 .getDataChildByName("my-list");
545 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
547 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
548 QName.create(myModule.getQNameModule(), "my-list"),
549 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
550 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
552 assertNotNull(testNode);
553 assertNotNull(foundNode);
554 assertEquals(testNode, foundNode);
556 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
557 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
559 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
561 assertNotNull(testNode);
562 assertNotNull(foundNode);
563 assertEquals(testNode, foundNode);
565 // find grouping in grouping
566 dataContainer = getGroupingByName(myModule, "my-grouping");
567 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
569 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
570 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
571 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
573 assertNotNull(testNode);
574 assertNotNull(foundNode);
575 assertEquals(testNode, foundNode);
577 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
578 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
580 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
582 assertNotNull(testNode);
583 assertNotNull(foundNode);
584 assertEquals(testNode, foundNode);
586 // find grouping in rpc
587 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
588 for (GroupingDefinition grouping : rpc.getGroupings()) {
589 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
594 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
595 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
596 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
598 assertNotNull(testNode);
599 assertNotNull(foundNode);
600 assertEquals(testNode, foundNode);
602 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
603 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
605 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
607 assertNotNull(testNode);
608 assertNotNull(foundNode);
609 assertEquals(testNode, foundNode);
611 // find grouping in output
612 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
613 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
615 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
616 QName.create(myModule.getQNameModule(), "output"),
617 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
618 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
620 assertNotNull(testNode);
621 assertNotNull(foundNode);
622 assertEquals(testNode, foundNode);
624 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
625 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
627 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
629 assertNotNull(testNode);
630 assertNotNull(foundNode);
631 assertEquals(testNode, foundNode);
633 // find grouping in input
634 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
635 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
637 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
638 QName.create(myModule.getQNameModule(), "input"),
639 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
640 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
642 assertNotNull(testNode);
643 assertNotNull(foundNode);
644 assertEquals(testNode, foundNode);
646 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
647 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
649 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
651 assertNotNull(testNode);
652 assertNotNull(foundNode);
653 assertEquals(testNode, foundNode);
655 // find grouping in notification
656 dataContainer = getNotificationByName(myModule, "my-notification");
657 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
659 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
660 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
661 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
663 assertNotNull(testNode);
664 assertNotNull(foundNode);
665 assertEquals(testNode, foundNode);
667 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
668 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
670 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
672 assertNotNull(testNode);
673 assertNotNull(foundNode);
674 assertEquals(testNode, foundNode);
676 // find grouping in case
677 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
678 "one").getDataChildByName("my-container-in-case");
679 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
681 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
682 QName.create(myModule.getQNameModule(), "one"),
683 QName.create(myModule.getQNameModule(), "my-container-in-case"),
684 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
685 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
687 assertNotNull(testNode);
688 assertNotNull(foundNode);
689 assertEquals(testNode, foundNode);
691 testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
692 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
694 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
696 assertNotNull(testNode);
697 assertNotNull(foundNode);
698 assertEquals(testNode, foundNode);
703 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
704 SourceException, ReactorException {
706 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
708 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
709 QName.parseRevision("2014-10-07"));
711 // find grouping in container
712 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
713 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
715 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
716 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
717 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
719 assertNull(testNode);
720 assertNull(foundNode);
722 // find grouping in list
723 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
724 .getDataChildByName("my-list");
725 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
727 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
728 QName.create(myModule.getQNameModule(), "my-list"),
729 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
730 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
732 assertNull(testNode);
733 assertNull(foundNode);
735 // find grouping in grouping
736 dataContainer = getGroupingByName(myModule, "my-grouping");
737 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
739 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
740 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
741 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
743 assertNull(testNode);
744 assertNull(foundNode);
746 // find grouping in rpc
747 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
748 for (GroupingDefinition grouping : rpc.getGroupings()) {
749 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
754 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
755 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
756 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
758 assertNull(testNode);
759 assertNull(foundNode);
761 // find grouping in output
762 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
763 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
765 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
766 QName.create(myModule.getQNameModule(), "output"),
767 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
768 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
770 assertNull(testNode);
771 assertNull(foundNode);
773 // find grouping in input
774 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
775 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
777 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
778 QName.create(myModule.getQNameModule(), "input"),
779 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
780 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
782 assertNull(testNode);
783 assertNull(foundNode);
785 // find grouping in notification
786 dataContainer = getNotificationByName(myModule, "my-notification");
787 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
789 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
790 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
791 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
793 assertNull(testNode);
794 assertNull(foundNode);
796 // find grouping in case
797 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
798 "one").getDataChildByName("my-container-in-case");
799 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
801 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
802 QName.create(myModule.getQNameModule(), "one"),
803 QName.create(myModule.getQNameModule(), "my-container-in-case"),
804 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
805 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
807 assertNull(testNode);
808 assertNull(foundNode);
812 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
813 for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
814 if (grouping.getQName().getLocalName().equals(name)) {
821 private static RpcDefinition getRpcByName(final Module module, final String name) {
822 for (RpcDefinition rpc : module.getRpcs()) {
823 if (rpc.getQName().getLocalName().equals(name)) {
830 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
831 for (NotificationDefinition notification : module.getNotifications()) {
832 if (notification.getQName().getLocalName().equals(name)) {
840 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
841 SourceException, ReactorException {
843 SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
845 Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
846 QName.parseRevision("2014-10-07"));
848 ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
849 SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
851 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
852 QName.create(myModule.getQNameModule(), "input"),
853 QName.create(myModule.getQNameModule(), "my-choice"),
854 QName.create(myModule.getQNameModule(), "case-two"),
855 QName.create(myModule.getQNameModule(), "two"));
856 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
858 assertNotNull(testNode);
859 assertNotNull(foundNode);
860 assertEquals(testNode, foundNode);