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.rfc7950.repo;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
16 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.Collections;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
36 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
42 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
43 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
44 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.opendaylight.yangtools.yang.stmt.TestUtils;
48 public class SchemaContextUtilTest {
50 private SchemaContext mockSchemaContext;
52 private Module mockModule;
55 public void testFindDummyData() {
56 MockitoAnnotations.initMocks(this);
57 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
58 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
59 doReturn(URI.create("dummy")).when(mockModule).getNamespace();
60 doReturn(Optional.empty()).when(mockModule).getRevision();
62 final QName qName = QName.create("dummy", "TestQName");
63 final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
64 assertEquals("Should be null. Module TestQName not found", null,
65 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
67 final RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
68 assertEquals("Should be null. Module bookstore not found", null,
69 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
71 final SchemaNode schemaNode = BaseTypes.int32Type();
72 final RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
73 assertEquals("Should be null, Module prefix not found", null,
74 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
77 assertEquals("Should be null. Module TestQName not found", null,
78 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
80 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
84 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
87 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
89 final Module myModule = context.findModule(new URI("uri:my-module"),Revision.of("2014-10-07")).get();
91 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
92 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
93 "my-leaf-in-container"));
95 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
96 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
97 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
99 assertNotNull(testNode);
100 assertNotNull(foundNode);
101 assertEquals(testNode, foundNode);
103 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
104 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
106 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
107 QName.create(myModule.getQNameModule(), "input"),
108 QName.create(myModule.getQNameModule(), "my-input-leaf"));
110 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
112 assertNotNull(testNode);
113 assertNotNull(foundNode);
114 assertEquals(testNode, foundNode);
116 rpc = getRpcByName(myModule, "my-rpc");
117 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
119 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
120 QName.create(myModule.getQNameModule(), "output"),
121 QName.create(myModule.getQNameModule(), "my-output-leaf"));
123 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
125 assertNotNull(testNode);
126 assertNotNull(foundNode);
127 assertEquals(testNode, foundNode);
129 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
130 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
132 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
133 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
135 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
137 assertNotNull(testNode);
138 assertNotNull(foundNode);
139 assertEquals(testNode, foundNode);
141 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
142 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
143 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
144 "my-leaf-in-grouping"));
146 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
147 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
148 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
150 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
152 assertNotNull(testNode);
153 assertNotNull(foundNode);
154 assertEquals(testNode, foundNode);
156 testNode = ((ChoiceSchemaNode) myModule
157 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
158 .findCaseNodes("one").iterator().next()
159 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
161 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
162 QName.create(myModule.getQNameModule(), "one"),
163 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
164 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
166 assertNotNull(testNode);
167 assertNotNull(foundNode);
168 assertEquals(testNode, foundNode);
170 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
171 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
174 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
176 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
177 QName.create(myModule.getQNameModule(), "my-list"),
178 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
179 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
181 assertNotNull(testNode);
182 assertNotNull(foundNode);
183 assertEquals(testNode, foundNode);
185 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
186 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
189 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
191 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
192 QName.create(myModule.getQNameModule(), "my-list"),
193 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
194 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
196 assertNotNull(testNode);
197 assertNotNull(foundNode);
198 assertEquals(testNode, foundNode);
203 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
206 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
208 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
210 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
211 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
212 "my-leaf-not-in-container"));
214 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
215 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
216 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
218 assertNull(testNode);
219 assertNull(foundNode);
221 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
222 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
224 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
225 QName.create(myModule.getQNameModule(), "input"),
226 QName.create(myModule.getQNameModule(), "no-input-leaf"));
228 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
230 assertNull(testNode);
231 assertNull(foundNode);
233 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
234 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
236 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
237 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
239 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
241 assertNull(testNode);
242 assertNull(foundNode);
244 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
245 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
246 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
247 "no-leaf-in-grouping"));
249 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
250 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
251 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
253 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
255 assertNull(testNode);
256 assertNull(foundNode);
258 testNode = ((ChoiceSchemaNode) myModule
259 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
260 .findCaseNodes("one").iterator().next()
261 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
263 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
264 QName.create(myModule.getQNameModule(), "one"),
265 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
266 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
268 assertNull(testNode);
269 assertNull(foundNode);
271 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
272 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
275 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
277 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
278 QName.create(myModule.getQNameModule(), "my-list"),
279 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
280 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
282 assertNull(testNode);
283 assertNull(foundNode);
285 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
286 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
289 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
291 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
292 QName.create(myModule.getQNameModule(), "my-list"),
293 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
294 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
296 assertNull(testNode);
297 assertNull(foundNode);
302 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
305 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
307 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
309 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
311 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
312 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
314 assertNotNull(testNode);
315 assertNotNull(foundNode);
316 assertEquals(testNode, foundNode);
318 testNode = getRpcByName(myModule, "my-rpc");
320 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
321 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
323 assertNotNull(testNode);
324 assertNotNull(foundNode);
325 assertEquals(testNode, foundNode);
327 testNode = myModule.getNotifications().iterator().next();
329 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
330 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
332 assertNotNull(testNode);
333 assertNotNull(foundNode);
334 assertEquals(testNode, foundNode);
336 testNode = getGroupingByName(myModule, "my-grouping");
338 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
339 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
341 assertNotNull(testNode);
342 assertNotNull(foundNode);
343 assertEquals(testNode, foundNode);
345 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
347 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
348 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
350 assertNotNull(testNode);
351 assertNotNull(foundNode);
352 assertEquals(testNode, foundNode);
354 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
355 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
357 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
358 QName.create(myModule.getQNameModule(), "my-list"));
359 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
361 assertNotNull(testNode);
362 assertNotNull(foundNode);
363 assertEquals(testNode, foundNode);
368 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
371 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
373 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
375 final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
378 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
380 assertEquals(myModule, foundModule);
383 @Test(expected = IllegalArgumentException.class)
384 public void findParentModuleIllegalArgumentTest() {
386 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
387 SchemaContextUtil.findParentModule(mockContext, null);
391 @Test(expected = IllegalArgumentException.class)
392 public void findParentModuleIllegalArgumentTest2() {
394 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
395 SchemaContextUtil.findParentModule(null, mockSchemaNode);
399 @Test(expected = IllegalStateException.class)
400 public void findParentModuleIllegalStateTest() {
402 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
403 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
404 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
405 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
409 @Test(expected = IllegalArgumentException.class)
410 public void findDataSchemaNodeIllegalArgumentTest() {
412 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
413 SchemaContextUtil.findDataSchemaNode(mockContext, null);
417 @Test(expected = IllegalArgumentException.class)
418 public void findDataSchemaNodeIllegalArgumentTest2() {
420 final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
421 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
426 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
428 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
429 final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
430 final Module importedModule = context.findModule(new URI("uri:imported-module"),
431 Revision.of("2014-10-07")).get();
433 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
434 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
435 importedModule.getQNameModule(), "my-imported-leaf"));
437 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
440 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
442 assertNotNull(foundNode);
443 assertNotNull(testNode);
444 assertEquals(testNode, foundNode);
448 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
450 // findDataSchemaNode(final SchemaContext context, final Module module,
451 // final RevisionAwareXPath nonCondXPath) {
453 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
454 final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
456 final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
457 final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
458 "my-leaf-in-gouping2"));
460 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
462 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
464 assertNotNull(foundNode);
465 assertNotNull(testNode);
466 assertEquals(testNode, foundNode);
470 @Test(expected = IllegalArgumentException.class)
471 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
473 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
474 final Module module = Mockito.mock(Module.class);
476 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
480 @Test(expected = IllegalArgumentException.class)
481 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
483 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
484 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
486 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
490 @Test(expected = IllegalArgumentException.class)
491 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
493 final Module module = Mockito.mock(Module.class);
494 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
496 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
500 @Test(expected = IllegalArgumentException.class)
501 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
503 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
504 final Module module = Mockito.mock(Module.class);
505 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl(
506 "my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
508 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
513 public void findDataSchemaNodeFromXPathNullTest() {
515 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
516 final Module module = Mockito.mock(Module.class);
517 final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
519 Mockito.when(xpath.toString()).thenReturn(null);
520 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
525 public void findDataSchemaNodeFromXPathNullTest2() {
527 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
528 final Module module = Mockito.mock(Module.class);
529 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
531 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
536 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
537 YangSyntaxErrorException, ReactorException {
539 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
540 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
542 // find grouping in container
543 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
544 myModule.getQNameModule(), "my-container"));
545 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
547 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
548 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
549 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
551 assertNotNull(testNode);
552 assertNotNull(foundNode);
553 assertEquals(testNode, foundNode);
555 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
556 "my-leaf-in-grouping-in-container"));
557 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
559 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
561 assertNotNull(testNode);
562 assertNotNull(foundNode);
563 assertEquals(testNode, foundNode);
565 // find grouping in list
566 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
567 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
569 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
571 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
572 QName.create(myModule.getQNameModule(), "my-list"),
573 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
574 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
576 assertNotNull(testNode);
577 assertNotNull(foundNode);
578 assertEquals(testNode, foundNode);
580 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
581 "my-leaf-in-grouping-in-list"));
582 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
584 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
586 assertNotNull(testNode);
587 assertNotNull(foundNode);
588 assertEquals(testNode, foundNode);
590 // find grouping in grouping
591 dataContainer = getGroupingByName(myModule, "my-grouping");
592 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
594 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
595 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
596 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
598 assertNotNull(testNode);
599 assertNotNull(foundNode);
600 assertEquals(testNode, foundNode);
602 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
603 "my-leaf-in-grouping-in-grouping"));
604 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
606 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
608 assertNotNull(testNode);
609 assertNotNull(foundNode);
610 assertEquals(testNode, foundNode);
612 // find grouping in rpc
613 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
614 for (final GroupingDefinition grouping : rpc.getGroupings()) {
615 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
620 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
621 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
622 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
624 assertNotNull(testNode);
625 assertNotNull(foundNode);
626 assertEquals(testNode, foundNode);
628 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
629 "my-leaf-in-grouping-in-rpc"));
630 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
632 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
634 assertNotNull(testNode);
635 assertNotNull(foundNode);
636 assertEquals(testNode, foundNode);
638 // find grouping in output
639 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
640 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
642 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
643 QName.create(myModule.getQNameModule(), "output"),
644 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
645 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
647 assertNotNull(testNode);
648 assertNotNull(foundNode);
649 assertEquals(testNode, foundNode);
651 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
652 "my-leaf-in-grouping-in-output"));
653 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
655 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
657 assertNotNull(testNode);
658 assertNotNull(foundNode);
659 assertEquals(testNode, foundNode);
661 // find grouping in input
662 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
663 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
665 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
666 QName.create(myModule.getQNameModule(), "input"),
667 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
668 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
670 assertNotNull(testNode);
671 assertNotNull(foundNode);
672 assertEquals(testNode, foundNode);
674 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
675 "my-leaf-in-grouping-in-input"));
676 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
678 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
680 assertNotNull(testNode);
681 assertNotNull(foundNode);
682 assertEquals(testNode, foundNode);
684 // find grouping in notification
685 dataContainer = getNotificationByName(myModule, "my-notification");
686 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
688 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
689 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
690 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
692 assertNotNull(testNode);
693 assertNotNull(foundNode);
694 assertEquals(testNode, foundNode);
696 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
697 "my-leaf-in-grouping-in-notification"));
698 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
700 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
702 assertNotNull(testNode);
703 assertNotNull(foundNode);
704 assertEquals(testNode, foundNode);
706 // find grouping in case
707 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
708 QName.create(myModule.getQNameModule(), "my-choice")))
709 .findCaseNodes("one").iterator().next()
710 .getDataChildByName(QName.create(myModule.getQNameModule(), "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(QName.create(myModule.getQNameModule(),
724 "my-leaf-in-grouping-in-case"));
725 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
727 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
729 assertNotNull(testNode);
730 assertNotNull(foundNode);
731 assertEquals(testNode, foundNode);
736 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
737 YangSyntaxErrorException, ReactorException {
739 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
741 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
743 // find grouping in container
744 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
745 myModule.getQNameModule(), "my-container"));
746 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
748 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
749 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
750 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
752 assertNull(testNode);
753 assertNull(foundNode);
755 // find grouping in list
756 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
757 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
759 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
761 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
762 QName.create(myModule.getQNameModule(), "my-list"),
763 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
764 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
766 assertNull(testNode);
767 assertNull(foundNode);
769 // find grouping in grouping
770 dataContainer = getGroupingByName(myModule, "my-grouping");
771 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
773 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
774 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
775 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
777 assertNull(testNode);
778 assertNull(foundNode);
780 // find grouping in rpc
781 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
782 for (final GroupingDefinition grouping : rpc.getGroupings()) {
783 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
788 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
789 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
790 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
792 assertNull(testNode);
793 assertNull(foundNode);
795 // find grouping in output
796 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
797 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
799 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
800 QName.create(myModule.getQNameModule(), "output"),
801 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
802 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
804 assertNull(testNode);
805 assertNull(foundNode);
807 // find grouping in input
808 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
809 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
811 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
812 QName.create(myModule.getQNameModule(), "input"),
813 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
814 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
816 assertNull(testNode);
817 assertNull(foundNode);
819 // find grouping in notification
820 dataContainer = getNotificationByName(myModule, "my-notification");
821 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
823 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
824 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
825 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
827 assertNull(testNode);
828 assertNull(foundNode);
830 // find grouping in case
831 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
832 QName.create(myModule.getQNameModule(), "my-choice")))
833 .findCaseNodes("one").iterator().next()
834 .getDataChildByName(QName.create(myModule.getQNameModule(), "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 (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
850 if (grouping.getQName().getLocalName().equals(name)) {
857 private static RpcDefinition getRpcByName(final Module module, final String name) {
858 for (final RpcDefinition rpc : module.getRpcs()) {
859 if (rpc.getQName().getLocalName().equals(name)) {
866 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
867 for (final NotificationDefinition notification : module.getNotifications()) {
868 if (notification.getQName().getLocalName().equals(name)) {
876 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
877 YangSyntaxErrorException, ReactorException {
879 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
881 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
882 final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
883 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
884 final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
885 .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
887 final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
888 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
889 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
890 final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
892 assertNotNull(testNode);
893 assertNotNull(foundNode);
894 assertEquals(testNode, foundNode);