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;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.util.Collections;
22 import java.util.Optional;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.Revision;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.PathExpression;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
44 import org.opendaylight.yangtools.yang.model.util.PathExpressionImpl;
45 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
46 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
48 import org.opendaylight.yangtools.yang.stmt.TestUtils;
50 @RunWith(MockitoJUnitRunner.class)
51 public class SchemaContextUtilTest {
53 private SchemaContext mockSchemaContext;
55 private Module mockModule;
58 public void testFindDummyData() {
59 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
60 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
61 doReturn(URI.create("dummy")).when(mockModule).getNamespace();
62 doReturn(Optional.empty()).when(mockModule).getRevision();
64 final QName qName = QName.create("dummy", "TestQName");
65 final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
66 assertEquals("Should be null. Module TestQName not found", null,
67 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
69 final PathExpression xPath = new PathExpressionImpl("/bookstore/book/title", true);
70 assertEquals("Should be null. Module bookstore not found", null,
71 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
73 final SchemaNode schemaNode = BaseTypes.int32Type();
74 final PathExpression xPathRelative = new PathExpressionImpl("../prefix", false);
75 assertEquals("Should be null, Module prefix not found", null,
76 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
79 assertEquals("Should be null. Module TestQName not found", null,
80 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
82 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
86 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
89 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
91 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
93 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
94 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
95 "my-leaf-in-container"));
97 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
98 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
99 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
101 assertNotNull(testNode);
102 assertNotNull(foundNode);
103 assertEquals(testNode, foundNode);
105 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
106 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
108 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
109 QName.create(myModule.getQNameModule(), "input"),
110 QName.create(myModule.getQNameModule(), "my-input-leaf"));
112 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
114 assertNotNull(testNode);
115 assertNotNull(foundNode);
116 assertEquals(testNode, foundNode);
118 rpc = getRpcByName(myModule, "my-rpc");
119 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
121 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
122 QName.create(myModule.getQNameModule(), "output"),
123 QName.create(myModule.getQNameModule(), "my-output-leaf"));
125 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
127 assertNotNull(testNode);
128 assertNotNull(foundNode);
129 assertEquals(testNode, foundNode);
131 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
132 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
134 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
135 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
137 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
139 assertNotNull(testNode);
140 assertNotNull(foundNode);
141 assertEquals(testNode, foundNode);
143 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
144 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
145 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
146 "my-leaf-in-grouping"));
148 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
149 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
150 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
152 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
154 assertNotNull(testNode);
155 assertNotNull(foundNode);
156 assertEquals(testNode, foundNode);
158 testNode = ((ChoiceSchemaNode) myModule
159 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
160 .findCaseNodes("one").iterator().next()
161 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
163 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
164 QName.create(myModule.getQNameModule(), "one"),
165 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
166 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
168 assertNotNull(testNode);
169 assertNotNull(foundNode);
170 assertEquals(testNode, foundNode);
172 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
173 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
176 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
178 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
179 QName.create(myModule.getQNameModule(), "my-list"),
180 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
181 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
183 assertNotNull(testNode);
184 assertNotNull(foundNode);
185 assertEquals(testNode, foundNode);
187 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
188 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
191 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
193 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
194 QName.create(myModule.getQNameModule(), "my-list"),
195 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
196 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
198 assertNotNull(testNode);
199 assertNotNull(foundNode);
200 assertEquals(testNode, foundNode);
205 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
208 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
210 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
212 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
213 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
214 "my-leaf-not-in-container"));
216 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
217 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
218 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
220 assertNull(testNode);
221 assertNull(foundNode);
223 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
224 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
226 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
227 QName.create(myModule.getQNameModule(), "input"),
228 QName.create(myModule.getQNameModule(), "no-input-leaf"));
230 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
232 assertNull(testNode);
233 assertNull(foundNode);
235 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
236 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
238 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
239 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
241 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
243 assertNull(testNode);
244 assertNull(foundNode);
246 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
247 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
248 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
249 "no-leaf-in-grouping"));
251 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
252 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
253 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
255 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
257 assertNull(testNode);
258 assertNull(foundNode);
260 testNode = ((ChoiceSchemaNode) myModule
261 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
262 .findCaseNodes("one").iterator().next()
263 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
265 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
266 QName.create(myModule.getQNameModule(), "one"),
267 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
268 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
270 assertNull(testNode);
271 assertNull(foundNode);
273 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
274 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
277 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
279 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
280 QName.create(myModule.getQNameModule(), "my-list"),
281 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
282 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
284 assertNull(testNode);
285 assertNull(foundNode);
287 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
288 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
291 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
293 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
294 QName.create(myModule.getQNameModule(), "my-list"),
295 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
296 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
298 assertNull(testNode);
299 assertNull(foundNode);
304 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
307 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
309 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
311 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
313 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
314 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
316 assertNotNull(testNode);
317 assertNotNull(foundNode);
318 assertEquals(testNode, foundNode);
320 testNode = getRpcByName(myModule, "my-rpc");
322 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
323 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
325 assertNotNull(testNode);
326 assertNotNull(foundNode);
327 assertEquals(testNode, foundNode);
329 testNode = myModule.getNotifications().iterator().next();
331 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
332 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
334 assertNotNull(testNode);
335 assertNotNull(foundNode);
336 assertEquals(testNode, foundNode);
338 testNode = getGroupingByName(myModule, "my-grouping");
340 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
341 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
343 assertNotNull(testNode);
344 assertNotNull(foundNode);
345 assertEquals(testNode, foundNode);
347 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
349 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
350 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
352 assertNotNull(testNode);
353 assertNotNull(foundNode);
354 assertEquals(testNode, foundNode);
356 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
357 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
359 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
360 QName.create(myModule.getQNameModule(), "my-list"));
361 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
363 assertNotNull(testNode);
364 assertNotNull(foundNode);
365 assertEquals(testNode, foundNode);
370 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
373 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
375 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
377 final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
380 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
382 assertEquals(myModule, foundModule);
385 @Test(expected = NullPointerException.class)
386 public void findParentModuleIllegalArgumentTest() {
387 SchemaContextUtil.findParentModule(mock(SchemaContext.class), null);
390 @Test(expected = NullPointerException.class)
391 public void findParentModuleIllegalArgumentTest2() {
392 SchemaContextUtil.findParentModule(null, mock(SchemaNode.class));
395 @Test(expected = NullPointerException.class)
396 public void findDataSchemaNodeIllegalArgumentTest() {
397 SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), null);
400 @Test(expected = NullPointerException.class)
401 public void findDataSchemaNodeIllegalArgumentTest2() {
402 SchemaContextUtil.findDataSchemaNode(null, SchemaPath.create(true,
403 QName.create(URI.create("uri:my-module"), Revision.of("2014-10-07"), "foo")));
407 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
409 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
410 final Module module = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
411 final Module importedModule = context.findModule(URI.create("uri:imported-module"),
412 Revision.of("2014-10-07")).get();
414 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
415 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
416 importedModule.getQNameModule(), "my-imported-leaf"));
418 final PathExpression xpath = new PathExpressionImpl("imp:my-imported-container/imp:my-imported-leaf", true);
420 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
422 assertNotNull(foundNode);
423 assertNotNull(testNode);
424 assertEquals(testNode, foundNode);
428 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
430 // findDataSchemaNode(final SchemaContext context, final Module module,
431 // final RevisionAwareXPath nonCondXPath) {
433 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
434 final Module module = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
436 final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
437 final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
438 "my-leaf-in-gouping2"));
440 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
442 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
444 assertNotNull(foundNode);
445 assertNotNull(testNode);
446 assertEquals(testNode, foundNode);
450 @Test(expected = NullPointerException.class)
451 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
452 SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null);
455 @Test(expected = NullPointerException.class)
456 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
457 final SchemaContext mockContext = mock(SchemaContext.class);
458 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
460 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
463 @Test(expected = NullPointerException.class)
464 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
465 final Module module = mock(Module.class);
466 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
468 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
471 @Test(expected = IllegalArgumentException.class)
472 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
473 final SchemaContext mockContext = mock(SchemaContext.class);
474 final Module module = mock(Module.class);
475 final PathExpression xpath = new PathExpressionImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
477 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
481 public void findDataSchemaNodeFromXPathNullTest() {
482 final SchemaContext mockContext = mock(SchemaContext.class);
483 final Module module = mock(Module.class);
484 final PathExpression xpath = mock(PathExpression.class);
486 when(xpath.getOriginalString()).thenReturn("");
487 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
491 public void findDataSchemaNodeFromXPathNullTest2() {
492 final SchemaContext mockContext = mock(SchemaContext.class);
493 final Module module = mock(Module.class);
494 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
496 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
500 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
501 YangSyntaxErrorException, ReactorException {
502 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
503 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
505 // find grouping in container
506 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
507 myModule.getQNameModule(), "my-container"));
508 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
510 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
511 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
512 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
514 assertNotNull(testNode);
515 assertNotNull(foundNode);
516 assertEquals(testNode, foundNode);
518 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
519 "my-leaf-in-grouping-in-container"));
520 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
522 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
524 assertNotNull(testNode);
525 assertNotNull(foundNode);
526 assertEquals(testNode, foundNode);
528 // find grouping in list
529 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
530 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
532 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
534 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
535 QName.create(myModule.getQNameModule(), "my-list"),
536 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
537 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
539 assertNotNull(testNode);
540 assertNotNull(foundNode);
541 assertEquals(testNode, foundNode);
543 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
544 "my-leaf-in-grouping-in-list"));
545 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
547 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
549 assertNotNull(testNode);
550 assertNotNull(foundNode);
551 assertEquals(testNode, foundNode);
553 // find grouping in grouping
554 dataContainer = getGroupingByName(myModule, "my-grouping");
555 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
557 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
558 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
559 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
561 assertNotNull(testNode);
562 assertNotNull(foundNode);
563 assertEquals(testNode, foundNode);
565 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
566 "my-leaf-in-grouping-in-grouping"));
567 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
569 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
571 assertNotNull(testNode);
572 assertNotNull(foundNode);
573 assertEquals(testNode, foundNode);
575 // find grouping in rpc
576 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
577 for (final GroupingDefinition grouping : rpc.getGroupings()) {
578 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
583 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
584 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
585 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
587 assertNotNull(testNode);
588 assertNotNull(foundNode);
589 assertEquals(testNode, foundNode);
591 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
592 "my-leaf-in-grouping-in-rpc"));
593 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
595 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
597 assertNotNull(testNode);
598 assertNotNull(foundNode);
599 assertEquals(testNode, foundNode);
601 // find grouping in output
602 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
603 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
605 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
606 QName.create(myModule.getQNameModule(), "output"),
607 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
608 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
610 assertNotNull(testNode);
611 assertNotNull(foundNode);
612 assertEquals(testNode, foundNode);
614 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
615 "my-leaf-in-grouping-in-output"));
616 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
618 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
620 assertNotNull(testNode);
621 assertNotNull(foundNode);
622 assertEquals(testNode, foundNode);
624 // find grouping in input
625 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
626 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
628 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
629 QName.create(myModule.getQNameModule(), "input"),
630 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
631 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
633 assertNotNull(testNode);
634 assertNotNull(foundNode);
635 assertEquals(testNode, foundNode);
637 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
638 "my-leaf-in-grouping-in-input"));
639 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
641 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
643 assertNotNull(testNode);
644 assertNotNull(foundNode);
645 assertEquals(testNode, foundNode);
647 // find grouping in notification
648 dataContainer = getNotificationByName(myModule, "my-notification");
649 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
651 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
652 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
653 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
655 assertNotNull(testNode);
656 assertNotNull(foundNode);
657 assertEquals(testNode, foundNode);
659 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
660 "my-leaf-in-grouping-in-notification"));
661 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
663 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
665 assertNotNull(testNode);
666 assertNotNull(foundNode);
667 assertEquals(testNode, foundNode);
669 // find grouping in case
670 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
671 QName.create(myModule.getQNameModule(), "my-choice")))
672 .findCaseNodes("one").iterator().next()
673 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
674 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
676 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
677 QName.create(myModule.getQNameModule(), "one"),
678 QName.create(myModule.getQNameModule(), "my-container-in-case"),
679 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
680 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
682 assertNotNull(testNode);
683 assertNotNull(foundNode);
684 assertEquals(testNode, foundNode);
686 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
687 "my-leaf-in-grouping-in-case"));
688 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
690 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
692 assertNotNull(testNode);
693 assertNotNull(foundNode);
694 assertEquals(testNode, foundNode);
698 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
699 YangSyntaxErrorException, ReactorException {
701 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
702 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
704 // find grouping in container
705 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
706 myModule.getQNameModule(), "my-container"));
707 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
709 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
710 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
711 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
713 assertNull(testNode);
714 assertNull(foundNode);
716 // find grouping in list
717 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
718 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
720 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
722 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
723 QName.create(myModule.getQNameModule(), "my-list"),
724 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
725 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
727 assertNull(testNode);
728 assertNull(foundNode);
730 // find grouping in grouping
731 dataContainer = getGroupingByName(myModule, "my-grouping");
732 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
734 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
735 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
736 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
738 assertNull(testNode);
739 assertNull(foundNode);
741 // find grouping in rpc
742 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
743 for (final GroupingDefinition grouping : rpc.getGroupings()) {
744 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
749 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
750 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
751 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
753 assertNull(testNode);
754 assertNull(foundNode);
756 // find grouping in output
757 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
758 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
760 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
761 QName.create(myModule.getQNameModule(), "output"),
762 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
763 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
765 assertNull(testNode);
766 assertNull(foundNode);
768 // find grouping in input
769 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
770 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
772 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
773 QName.create(myModule.getQNameModule(), "input"),
774 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
775 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
777 assertNull(testNode);
778 assertNull(foundNode);
780 // find grouping in notification
781 dataContainer = getNotificationByName(myModule, "my-notification");
782 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
784 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
785 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
786 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
788 assertNull(testNode);
789 assertNull(foundNode);
791 // find grouping in case
792 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
793 QName.create(myModule.getQNameModule(), "my-choice")))
794 .findCaseNodes("one").iterator().next()
795 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
796 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
798 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
799 QName.create(myModule.getQNameModule(), "one"),
800 QName.create(myModule.getQNameModule(), "my-container-in-case"),
801 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
802 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
804 assertNull(testNode);
805 assertNull(foundNode);
808 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
809 for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
810 if (grouping.getQName().getLocalName().equals(name)) {
817 private static RpcDefinition getRpcByName(final Module module, final String name) {
818 for (final RpcDefinition rpc : module.getRpcs()) {
819 if (rpc.getQName().getLocalName().equals(name)) {
826 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
827 for (final NotificationDefinition notification : module.getNotifications()) {
828 if (notification.getQName().getLocalName().equals(name)) {
836 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
837 YangSyntaxErrorException, ReactorException {
839 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
841 final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
842 final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
843 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
844 final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
845 .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
847 final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
848 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
849 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
850 final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
852 assertNotNull(testNode);
853 assertNotNull(foundNode);
854 assertEquals(testNode, foundNode);