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.RevisionAwareXPathImpl;
39 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
40 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
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 final QName qName = QName.create("TestQName");
55 final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
56 assertEquals("Should be null. Module TestQName not found", null,
57 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
59 final 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 final SchemaNode schemaNode = BaseTypes.int32Type();
64 final 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 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
81 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
82 QName.parseRevision("2014-10-07"));
84 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
85 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
86 "my-leaf-in-container"));
88 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
89 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
90 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
92 assertNotNull(testNode);
93 assertNotNull(foundNode);
94 assertEquals(testNode, foundNode);
96 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
97 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
99 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
100 QName.create(myModule.getQNameModule(), "input"),
101 QName.create(myModule.getQNameModule(), "my-input-leaf"));
103 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
105 assertNotNull(testNode);
106 assertNotNull(foundNode);
107 assertEquals(testNode, foundNode);
109 rpc = getRpcByName(myModule, "my-rpc");
110 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
112 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
113 QName.create(myModule.getQNameModule(), "output"),
114 QName.create(myModule.getQNameModule(), "my-output-leaf"));
116 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
118 assertNotNull(testNode);
119 assertNotNull(foundNode);
120 assertEquals(testNode, foundNode);
122 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
123 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
125 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
126 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
128 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
130 assertNotNull(testNode);
131 assertNotNull(foundNode);
132 assertEquals(testNode, foundNode);
134 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
135 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
136 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
137 "my-leaf-in-grouping"));
139 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
140 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
141 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
143 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
145 assertNotNull(testNode);
146 assertNotNull(foundNode);
147 assertEquals(testNode, foundNode);
149 testNode = ((ChoiceSchemaNode) myModule
150 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
151 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
153 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
154 QName.create(myModule.getQNameModule(), "one"),
155 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
156 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
158 assertNotNull(testNode);
159 assertNotNull(foundNode);
160 assertEquals(testNode, foundNode);
162 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
163 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
166 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
168 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
169 QName.create(myModule.getQNameModule(), "my-list"),
170 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
171 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
173 assertNotNull(testNode);
174 assertNotNull(foundNode);
175 assertEquals(testNode, foundNode);
177 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
178 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
181 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
183 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
184 QName.create(myModule.getQNameModule(), "my-list"),
185 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
186 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
188 assertNotNull(testNode);
189 assertNotNull(foundNode);
190 assertEquals(testNode, foundNode);
195 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
196 ParseException, ReactorException {
198 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
200 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
201 QName.parseRevision("2014-10-07"));
203 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
204 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
205 "my-leaf-not-in-container"));
207 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
208 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
209 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
211 assertNull(testNode);
212 assertNull(foundNode);
214 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
215 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
217 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
218 QName.create(myModule.getQNameModule(), "input"),
219 QName.create(myModule.getQNameModule(), "no-input-leaf"));
221 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
223 assertNull(testNode);
224 assertNull(foundNode);
226 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
227 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
229 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
230 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
232 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
234 assertNull(testNode);
235 assertNull(foundNode);
237 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
238 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
239 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
240 "no-leaf-in-grouping"));
242 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
243 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
244 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
246 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
248 assertNull(testNode);
249 assertNull(foundNode);
251 testNode = ((ChoiceSchemaNode) myModule
252 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
253 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
255 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
256 QName.create(myModule.getQNameModule(), "one"),
257 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
258 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
260 assertNull(testNode);
261 assertNull(foundNode);
263 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
264 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
267 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
269 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
270 QName.create(myModule.getQNameModule(), "my-list"),
271 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
272 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
274 assertNull(testNode);
275 assertNull(foundNode);
277 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
278 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
281 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
283 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
284 QName.create(myModule.getQNameModule(), "my-list"),
285 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
286 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
288 assertNull(testNode);
289 assertNull(foundNode);
294 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
295 ParseException, ReactorException {
297 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
299 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
300 QName.parseRevision("2014-10-07"));
302 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
304 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
305 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
307 assertNotNull(testNode);
308 assertNotNull(foundNode);
309 assertEquals(testNode, foundNode);
311 testNode = getRpcByName(myModule, "my-rpc");
313 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
314 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
316 assertNotNull(testNode);
317 assertNotNull(foundNode);
318 assertEquals(testNode, foundNode);
320 testNode = myModule.getNotifications().iterator().next();
322 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
323 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
325 assertNotNull(testNode);
326 assertNotNull(foundNode);
327 assertEquals(testNode, foundNode);
329 testNode = getGroupingByName(myModule, "my-grouping");
331 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
332 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
334 assertNotNull(testNode);
335 assertNotNull(foundNode);
336 assertEquals(testNode, foundNode);
338 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
340 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
341 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
343 assertNotNull(testNode);
344 assertNotNull(foundNode);
345 assertEquals(testNode, foundNode);
347 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
348 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
350 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
351 QName.create(myModule.getQNameModule(), "my-list"));
352 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
354 assertNotNull(testNode);
355 assertNotNull(foundNode);
356 assertEquals(testNode, foundNode);
361 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
362 ParseException, ReactorException {
364 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
366 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
367 QName.parseRevision("2014-10-07"));
369 final DataSchemaNode node = myModule
370 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
372 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
374 assertEquals(myModule, foundModule);
377 @Test(expected = IllegalArgumentException.class)
378 public void findParentModuleIllegalArgumentTest() {
380 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
381 SchemaContextUtil.findParentModule(mockContext, null);
385 @Test(expected = IllegalArgumentException.class)
386 public void findParentModuleIllegalArgumentTest2() {
388 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
389 SchemaContextUtil.findParentModule(null, mockSchemaNode);
393 @Test(expected = IllegalStateException.class)
394 public void findParentModuleIllegalStateTest() {
396 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
397 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
398 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
399 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
403 @Test(expected = IllegalArgumentException.class)
404 public void findDataSchemaNodeIllegalArgumentTest() {
406 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
407 SchemaContextUtil.findDataSchemaNode(mockContext, null);
411 @Test(expected = IllegalArgumentException.class)
412 public void findDataSchemaNodeIllegalArgumentTest2() {
414 final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
415 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
420 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
423 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
425 final Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
426 QName.parseRevision("2014-10-07"));
427 final Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"),
428 QName.parseRevision("2014-10-07"));
430 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
431 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
432 importedModule.getQNameModule(), "my-imported-leaf"));
434 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
437 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
439 assertNotNull(foundNode);
440 assertNotNull(testNode);
441 assertEquals(testNode, foundNode);
445 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
447 // findDataSchemaNode(final SchemaContext context, final Module module,
448 // final RevisionAwareXPath nonCondXPath) {
450 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
452 final Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
453 QName.parseRevision("2014-10-07"));
455 final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
456 final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
457 "my-leaf-in-gouping2"));
459 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
461 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
463 assertNotNull(foundNode);
464 assertNotNull(testNode);
465 assertEquals(testNode, foundNode);
469 @Test(expected = IllegalArgumentException.class)
470 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
472 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
473 final Module module = Mockito.mock(Module.class);
475 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
479 @Test(expected = IllegalArgumentException.class)
480 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
482 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
483 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
485 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
489 @Test(expected = IllegalArgumentException.class)
490 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
492 final Module module = Mockito.mock(Module.class);
493 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
495 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
499 @Test(expected = IllegalArgumentException.class)
500 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
502 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
503 final Module module = Mockito.mock(Module.class);
504 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl(
505 "my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
507 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
512 public void findDataSchemaNodeFromXPathNullTest() {
514 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
515 final Module module = Mockito.mock(Module.class);
516 final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
518 Mockito.when(xpath.toString()).thenReturn(null);
519 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
524 public void findDataSchemaNodeFromXPathNullTest2() {
526 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
527 final Module module = Mockito.mock(Module.class);
528 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
530 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
535 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
536 YangSyntaxErrorException, ParseException, ReactorException {
538 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
540 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
541 QName.parseRevision("2014-10-07"));
543 // find grouping in container
544 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
545 myModule.getQNameModule(), "my-container"));
546 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
548 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
549 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
550 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
552 assertNotNull(testNode);
553 assertNotNull(foundNode);
554 assertEquals(testNode, foundNode);
556 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
557 "my-leaf-in-grouping-in-container"));
558 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
560 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
562 assertNotNull(testNode);
563 assertNotNull(foundNode);
564 assertEquals(testNode, foundNode);
566 // find grouping in list
567 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
568 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
570 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
572 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
573 QName.create(myModule.getQNameModule(), "my-list"),
574 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
575 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
577 assertNotNull(testNode);
578 assertNotNull(foundNode);
579 assertEquals(testNode, foundNode);
581 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
582 "my-leaf-in-grouping-in-list"));
583 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
585 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
587 assertNotNull(testNode);
588 assertNotNull(foundNode);
589 assertEquals(testNode, foundNode);
591 // find grouping in grouping
592 dataContainer = getGroupingByName(myModule, "my-grouping");
593 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
595 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
596 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
597 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
599 assertNotNull(testNode);
600 assertNotNull(foundNode);
601 assertEquals(testNode, foundNode);
603 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
604 "my-leaf-in-grouping-in-grouping"));
605 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
607 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
609 assertNotNull(testNode);
610 assertNotNull(foundNode);
611 assertEquals(testNode, foundNode);
613 // find grouping in rpc
614 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
615 for (final GroupingDefinition grouping : rpc.getGroupings()) {
616 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
621 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
622 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
623 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
625 assertNotNull(testNode);
626 assertNotNull(foundNode);
627 assertEquals(testNode, foundNode);
629 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
630 "my-leaf-in-grouping-in-rpc"));
631 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
633 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
635 assertNotNull(testNode);
636 assertNotNull(foundNode);
637 assertEquals(testNode, foundNode);
639 // find grouping in output
640 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
641 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
643 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
644 QName.create(myModule.getQNameModule(), "output"),
645 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
646 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
648 assertNotNull(testNode);
649 assertNotNull(foundNode);
650 assertEquals(testNode, foundNode);
652 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
653 "my-leaf-in-grouping-in-output"));
654 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
656 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
658 assertNotNull(testNode);
659 assertNotNull(foundNode);
660 assertEquals(testNode, foundNode);
662 // find grouping in input
663 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
664 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
666 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
667 QName.create(myModule.getQNameModule(), "input"),
668 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
669 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
671 assertNotNull(testNode);
672 assertNotNull(foundNode);
673 assertEquals(testNode, foundNode);
675 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
676 "my-leaf-in-grouping-in-input"));
677 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
679 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
681 assertNotNull(testNode);
682 assertNotNull(foundNode);
683 assertEquals(testNode, foundNode);
685 // find grouping in notification
686 dataContainer = getNotificationByName(myModule, "my-notification");
687 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
689 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
690 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
691 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
693 assertNotNull(testNode);
694 assertNotNull(foundNode);
695 assertEquals(testNode, foundNode);
697 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
698 "my-leaf-in-grouping-in-notification"));
699 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
701 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
703 assertNotNull(testNode);
704 assertNotNull(foundNode);
705 assertEquals(testNode, foundNode);
707 // find grouping in case
708 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(QName.create(
709 myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one").getDataChildByName(
710 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, ParseException, ReactorException {
739 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
741 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
742 QName.parseRevision("2014-10-07"));
744 // find grouping in container
745 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
746 myModule.getQNameModule(), "my-container"));
747 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
749 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
750 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
751 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
753 assertNull(testNode);
754 assertNull(foundNode);
756 // find grouping in list
757 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
758 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
760 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
762 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
763 QName.create(myModule.getQNameModule(), "my-list"),
764 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
765 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
767 assertNull(testNode);
768 assertNull(foundNode);
770 // find grouping in grouping
771 dataContainer = getGroupingByName(myModule, "my-grouping");
772 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
774 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
775 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
776 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
778 assertNull(testNode);
779 assertNull(foundNode);
781 // find grouping in rpc
782 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
783 for (final GroupingDefinition grouping : rpc.getGroupings()) {
784 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
789 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
790 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
791 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
793 assertNull(testNode);
794 assertNull(foundNode);
796 // find grouping in output
797 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
798 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
800 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
801 QName.create(myModule.getQNameModule(), "output"),
802 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
803 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
805 assertNull(testNode);
806 assertNull(foundNode);
808 // find grouping in input
809 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
810 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
812 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
813 QName.create(myModule.getQNameModule(), "input"),
814 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
815 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
817 assertNull(testNode);
818 assertNull(foundNode);
820 // find grouping in notification
821 dataContainer = getNotificationByName(myModule, "my-notification");
822 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
824 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
825 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
826 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
828 assertNull(testNode);
829 assertNull(foundNode);
831 // find grouping in case
832 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(QName.create(
833 myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one").getDataChildByName(
834 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, ParseException, ReactorException {
879 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
881 final Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
882 QName.parseRevision("2014-10-07"));
884 final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
885 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
886 final SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName(
887 QName.create(myModule.getQNameModule(), "two"));
889 final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
890 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
891 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
892 final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
894 assertNotNull(testNode);
895 assertNotNull(foundNode);
896 assertEquals(testNode, foundNode);