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.rfc6020.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.Matchers.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(URI.create("dummy")).when(mockModule).getNamespace();
59 doReturn(Optional.empty()).when(mockModule).getRevision();
61 final QName qName = QName.create("dummy", "TestQName");
62 final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
63 assertEquals("Should be null. Module TestQName not found", null,
64 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
66 final RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
67 assertEquals("Should be null. Module bookstore not found", null,
68 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
70 final SchemaNode schemaNode = BaseTypes.int32Type();
71 final RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
72 assertEquals("Should be null, Module prefix not found", null,
73 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
76 assertEquals("Should be null. Module TestQName not found", null,
77 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
79 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
83 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
86 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
88 final Module myModule = context.findModule(new URI("uri:my-module"),Revision.of("2014-10-07")).get();
90 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
91 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
92 "my-leaf-in-container"));
94 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
95 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
96 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
98 assertNotNull(testNode);
99 assertNotNull(foundNode);
100 assertEquals(testNode, foundNode);
102 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
103 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
105 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
106 QName.create(myModule.getQNameModule(), "input"),
107 QName.create(myModule.getQNameModule(), "my-input-leaf"));
109 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
111 assertNotNull(testNode);
112 assertNotNull(foundNode);
113 assertEquals(testNode, foundNode);
115 rpc = getRpcByName(myModule, "my-rpc");
116 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
118 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
119 QName.create(myModule.getQNameModule(), "output"),
120 QName.create(myModule.getQNameModule(), "my-output-leaf"));
122 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
124 assertNotNull(testNode);
125 assertNotNull(foundNode);
126 assertEquals(testNode, foundNode);
128 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
129 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
131 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
132 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
134 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
136 assertNotNull(testNode);
137 assertNotNull(foundNode);
138 assertEquals(testNode, foundNode);
140 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
141 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
142 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
143 "my-leaf-in-grouping"));
145 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
146 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
147 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
149 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
151 assertNotNull(testNode);
152 assertNotNull(foundNode);
153 assertEquals(testNode, foundNode);
155 testNode = ((ChoiceSchemaNode) myModule
156 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
157 .findCaseNodes("one").iterator().next()
158 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
160 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
161 QName.create(myModule.getQNameModule(), "one"),
162 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
163 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
165 assertNotNull(testNode);
166 assertNotNull(foundNode);
167 assertEquals(testNode, foundNode);
169 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
170 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
173 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-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-in-list"));
178 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
180 assertNotNull(testNode);
181 assertNotNull(foundNode);
182 assertEquals(testNode, foundNode);
184 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
185 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
188 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
190 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
191 QName.create(myModule.getQNameModule(), "my-list"),
192 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
193 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
195 assertNotNull(testNode);
196 assertNotNull(foundNode);
197 assertEquals(testNode, foundNode);
202 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
205 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
207 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
209 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
210 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
211 "my-leaf-not-in-container"));
213 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
214 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
215 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
217 assertNull(testNode);
218 assertNull(foundNode);
220 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
221 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
223 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
224 QName.create(myModule.getQNameModule(), "input"),
225 QName.create(myModule.getQNameModule(), "no-input-leaf"));
227 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
229 assertNull(testNode);
230 assertNull(foundNode);
232 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
233 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
235 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
236 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
238 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
240 assertNull(testNode);
241 assertNull(foundNode);
243 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
244 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
245 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
246 "no-leaf-in-grouping"));
248 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
249 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
250 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
252 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
254 assertNull(testNode);
255 assertNull(foundNode);
257 testNode = ((ChoiceSchemaNode) myModule
258 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
259 .findCaseNodes("one").iterator().next()
260 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
262 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
263 QName.create(myModule.getQNameModule(), "one"),
264 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
265 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
267 assertNull(testNode);
268 assertNull(foundNode);
270 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
271 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
274 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
276 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
277 QName.create(myModule.getQNameModule(), "my-list"),
278 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
279 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
281 assertNull(testNode);
282 assertNull(foundNode);
284 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
285 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
288 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
290 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
291 QName.create(myModule.getQNameModule(), "my-list"),
292 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
293 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
295 assertNull(testNode);
296 assertNull(foundNode);
301 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
304 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
306 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
308 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
310 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
311 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
313 assertNotNull(testNode);
314 assertNotNull(foundNode);
315 assertEquals(testNode, foundNode);
317 testNode = getRpcByName(myModule, "my-rpc");
319 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
320 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
322 assertNotNull(testNode);
323 assertNotNull(foundNode);
324 assertEquals(testNode, foundNode);
326 testNode = myModule.getNotifications().iterator().next();
328 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
329 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
331 assertNotNull(testNode);
332 assertNotNull(foundNode);
333 assertEquals(testNode, foundNode);
335 testNode = getGroupingByName(myModule, "my-grouping");
337 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
338 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
340 assertNotNull(testNode);
341 assertNotNull(foundNode);
342 assertEquals(testNode, foundNode);
344 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
346 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
347 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
349 assertNotNull(testNode);
350 assertNotNull(foundNode);
351 assertEquals(testNode, foundNode);
353 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
354 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
356 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
357 QName.create(myModule.getQNameModule(), "my-list"));
358 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
360 assertNotNull(testNode);
361 assertNotNull(foundNode);
362 assertEquals(testNode, foundNode);
367 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
370 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
372 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
374 final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
377 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
379 assertEquals(myModule, foundModule);
382 @Test(expected = IllegalArgumentException.class)
383 public void findParentModuleIllegalArgumentTest() {
385 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
386 SchemaContextUtil.findParentModule(mockContext, null);
390 @Test(expected = IllegalArgumentException.class)
391 public void findParentModuleIllegalArgumentTest2() {
393 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
394 SchemaContextUtil.findParentModule(null, mockSchemaNode);
398 @Test(expected = IllegalStateException.class)
399 public void findParentModuleIllegalStateTest() {
401 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
402 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
403 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
404 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
408 @Test(expected = IllegalArgumentException.class)
409 public void findDataSchemaNodeIllegalArgumentTest() {
411 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
412 SchemaContextUtil.findDataSchemaNode(mockContext, null);
416 @Test(expected = IllegalArgumentException.class)
417 public void findDataSchemaNodeIllegalArgumentTest2() {
419 final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
420 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
425 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
427 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
428 final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
429 final Module importedModule = context.findModule(new URI("uri:imported-module"),
430 Revision.of("2014-10-07")).get();
432 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
433 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
434 importedModule.getQNameModule(), "my-imported-leaf"));
436 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
439 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
441 assertNotNull(foundNode);
442 assertNotNull(testNode);
443 assertEquals(testNode, foundNode);
447 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
449 // findDataSchemaNode(final SchemaContext context, final Module module,
450 // final RevisionAwareXPath nonCondXPath) {
452 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
453 final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
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, ReactorException {
538 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
539 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
541 // find grouping in container
542 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
543 myModule.getQNameModule(), "my-container"));
544 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
546 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
547 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
548 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
550 assertNotNull(testNode);
551 assertNotNull(foundNode);
552 assertEquals(testNode, foundNode);
554 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
555 "my-leaf-in-grouping-in-container"));
556 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
558 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
560 assertNotNull(testNode);
561 assertNotNull(foundNode);
562 assertEquals(testNode, foundNode);
564 // find grouping in list
565 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
566 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
568 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
570 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
571 QName.create(myModule.getQNameModule(), "my-list"),
572 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
573 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
575 assertNotNull(testNode);
576 assertNotNull(foundNode);
577 assertEquals(testNode, foundNode);
579 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
580 "my-leaf-in-grouping-in-list"));
581 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
583 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
585 assertNotNull(testNode);
586 assertNotNull(foundNode);
587 assertEquals(testNode, foundNode);
589 // find grouping in grouping
590 dataContainer = getGroupingByName(myModule, "my-grouping");
591 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
593 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
594 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
595 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
597 assertNotNull(testNode);
598 assertNotNull(foundNode);
599 assertEquals(testNode, foundNode);
601 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
602 "my-leaf-in-grouping-in-grouping"));
603 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
605 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
607 assertNotNull(testNode);
608 assertNotNull(foundNode);
609 assertEquals(testNode, foundNode);
611 // find grouping in rpc
612 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
613 for (final GroupingDefinition grouping : rpc.getGroupings()) {
614 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
619 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
620 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
621 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
623 assertNotNull(testNode);
624 assertNotNull(foundNode);
625 assertEquals(testNode, foundNode);
627 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
628 "my-leaf-in-grouping-in-rpc"));
629 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
631 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
633 assertNotNull(testNode);
634 assertNotNull(foundNode);
635 assertEquals(testNode, foundNode);
637 // find grouping in output
638 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
639 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
641 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
642 QName.create(myModule.getQNameModule(), "output"),
643 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
644 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
646 assertNotNull(testNode);
647 assertNotNull(foundNode);
648 assertEquals(testNode, foundNode);
650 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
651 "my-leaf-in-grouping-in-output"));
652 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
654 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
656 assertNotNull(testNode);
657 assertNotNull(foundNode);
658 assertEquals(testNode, foundNode);
660 // find grouping in input
661 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
662 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
664 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
665 QName.create(myModule.getQNameModule(), "input"),
666 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
667 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
669 assertNotNull(testNode);
670 assertNotNull(foundNode);
671 assertEquals(testNode, foundNode);
673 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
674 "my-leaf-in-grouping-in-input"));
675 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
677 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
679 assertNotNull(testNode);
680 assertNotNull(foundNode);
681 assertEquals(testNode, foundNode);
683 // find grouping in notification
684 dataContainer = getNotificationByName(myModule, "my-notification");
685 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
687 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
688 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
689 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
691 assertNotNull(testNode);
692 assertNotNull(foundNode);
693 assertEquals(testNode, foundNode);
695 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
696 "my-leaf-in-grouping-in-notification"));
697 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
699 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
701 assertNotNull(testNode);
702 assertNotNull(foundNode);
703 assertEquals(testNode, foundNode);
705 // find grouping in case
706 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
707 QName.create(myModule.getQNameModule(), "my-choice")))
708 .findCaseNodes("one").iterator().next()
709 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
710 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
712 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
713 QName.create(myModule.getQNameModule(), "one"),
714 QName.create(myModule.getQNameModule(), "my-container-in-case"),
715 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
716 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
718 assertNotNull(testNode);
719 assertNotNull(foundNode);
720 assertEquals(testNode, foundNode);
722 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
723 "my-leaf-in-grouping-in-case"));
724 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
726 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
728 assertNotNull(testNode);
729 assertNotNull(foundNode);
730 assertEquals(testNode, foundNode);
735 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
736 YangSyntaxErrorException, ReactorException {
738 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
740 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
742 // find grouping in container
743 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
744 myModule.getQNameModule(), "my-container"));
745 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
747 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
748 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
749 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
751 assertNull(testNode);
752 assertNull(foundNode);
754 // find grouping in list
755 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
756 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
758 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
760 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
761 QName.create(myModule.getQNameModule(), "my-list"),
762 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
763 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
765 assertNull(testNode);
766 assertNull(foundNode);
768 // find grouping in grouping
769 dataContainer = getGroupingByName(myModule, "my-grouping");
770 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
772 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
773 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
774 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
776 assertNull(testNode);
777 assertNull(foundNode);
779 // find grouping in rpc
780 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
781 for (final GroupingDefinition grouping : rpc.getGroupings()) {
782 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
787 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
788 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
789 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
791 assertNull(testNode);
792 assertNull(foundNode);
794 // find grouping in output
795 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
796 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
798 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
799 QName.create(myModule.getQNameModule(), "output"),
800 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
801 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
803 assertNull(testNode);
804 assertNull(foundNode);
806 // find grouping in input
807 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
808 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
810 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
811 QName.create(myModule.getQNameModule(), "input"),
812 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
813 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
815 assertNull(testNode);
816 assertNull(foundNode);
818 // find grouping in notification
819 dataContainer = getNotificationByName(myModule, "my-notification");
820 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
822 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
823 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
824 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
826 assertNull(testNode);
827 assertNull(foundNode);
829 // find grouping in case
830 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
831 QName.create(myModule.getQNameModule(), "my-choice")))
832 .findCaseNodes("one").iterator().next()
833 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
834 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
836 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
837 QName.create(myModule.getQNameModule(), "one"),
838 QName.create(myModule.getQNameModule(), "my-container-in-case"),
839 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
840 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
842 assertNull(testNode);
843 assertNull(foundNode);
847 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
848 for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
849 if (grouping.getQName().getLocalName().equals(name)) {
856 private static RpcDefinition getRpcByName(final Module module, final String name) {
857 for (final RpcDefinition rpc : module.getRpcs()) {
858 if (rpc.getQName().getLocalName().equals(name)) {
865 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
866 for (final NotificationDefinition notification : module.getNotifications()) {
867 if (notification.getQName().getLocalName().equals(name)) {
875 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
876 YangSyntaxErrorException, ReactorException {
878 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
880 final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
881 final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
882 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
883 final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
884 .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
886 final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
887 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
888 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
889 final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
891 assertNotNull(testNode);
892 assertNotNull(foundNode);
893 assertEquals(testNode, foundNode);