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;
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.text.ParseException;
20 import java.util.Collections;
21 import java.util.Optional;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
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));
59 final QName qName = QName.create("TestQName");
60 final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
61 assertEquals("Should be null. Module TestQName not found", null,
62 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
64 final RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
65 assertEquals("Should be null. Module bookstore not found", null,
66 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
68 final SchemaNode schemaNode = BaseTypes.int32Type();
69 final RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
70 assertEquals("Should be null, Module prefix not found", null,
71 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
74 assertEquals("Should be null. Module TestQName not found", null,
75 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
77 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
81 public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
82 ParseException, ReactorException {
84 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
86 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
88 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
89 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
90 "my-leaf-in-container"));
92 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
93 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
94 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
96 assertNotNull(testNode);
97 assertNotNull(foundNode);
98 assertEquals(testNode, foundNode);
100 RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
101 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
103 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
104 QName.create(myModule.getQNameModule(), "input"),
105 QName.create(myModule.getQNameModule(), "my-input-leaf"));
107 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
109 assertNotNull(testNode);
110 assertNotNull(foundNode);
111 assertEquals(testNode, foundNode);
113 rpc = getRpcByName(myModule, "my-rpc");
114 testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
116 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
117 QName.create(myModule.getQNameModule(), "output"),
118 QName.create(myModule.getQNameModule(), "my-output-leaf"));
120 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
122 assertNotNull(testNode);
123 assertNotNull(foundNode);
124 assertEquals(testNode, foundNode);
126 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
127 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
129 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
130 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
132 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
134 assertNotNull(testNode);
135 assertNotNull(foundNode);
136 assertEquals(testNode, foundNode);
138 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
139 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
140 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
141 "my-leaf-in-grouping"));
143 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
144 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
145 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
147 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
149 assertNotNull(testNode);
150 assertNotNull(foundNode);
151 assertEquals(testNode, foundNode);
153 testNode = ((ChoiceSchemaNode) myModule
154 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
155 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
157 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
158 QName.create(myModule.getQNameModule(), "one"),
159 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
160 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
162 assertNotNull(testNode);
163 assertNotNull(foundNode);
164 assertEquals(testNode, foundNode);
166 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
167 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
170 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
172 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
173 QName.create(myModule.getQNameModule(), "my-list"),
174 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
175 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
177 assertNotNull(testNode);
178 assertNotNull(foundNode);
179 assertEquals(testNode, foundNode);
181 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
182 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
185 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
187 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
188 QName.create(myModule.getQNameModule(), "my-list"),
189 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
190 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
192 assertNotNull(testNode);
193 assertNotNull(foundNode);
194 assertEquals(testNode, foundNode);
199 public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
200 ParseException, ReactorException {
202 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
204 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
206 SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
207 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
208 "my-leaf-not-in-container"));
210 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
211 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
212 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
214 assertNull(testNode);
215 assertNull(foundNode);
217 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
218 testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
220 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
221 QName.create(myModule.getQNameModule(), "input"),
222 QName.create(myModule.getQNameModule(), "no-input-leaf"));
224 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
226 assertNull(testNode);
227 assertNull(foundNode);
229 final NotificationDefinition notification = myModule.getNotifications().iterator().next();
230 testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
232 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
233 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
235 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
237 assertNull(testNode);
238 assertNull(foundNode);
240 final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
241 testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
242 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
243 "no-leaf-in-grouping"));
245 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
246 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
247 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
249 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
251 assertNull(testNode);
252 assertNull(foundNode);
254 testNode = ((ChoiceSchemaNode) myModule
255 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one")
256 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
258 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
259 QName.create(myModule.getQNameModule(), "one"),
260 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
261 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
263 assertNull(testNode);
264 assertNull(foundNode);
266 ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
267 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
270 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
272 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
273 QName.create(myModule.getQNameModule(), "my-list"),
274 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
275 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
277 assertNull(testNode);
278 assertNull(foundNode);
280 listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
281 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
284 testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
286 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
287 QName.create(myModule.getQNameModule(), "my-list"),
288 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
289 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
291 assertNull(testNode);
292 assertNull(foundNode);
297 public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
298 ParseException, ReactorException {
300 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
302 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
304 SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
306 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
307 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
309 assertNotNull(testNode);
310 assertNotNull(foundNode);
311 assertEquals(testNode, foundNode);
313 testNode = getRpcByName(myModule, "my-rpc");
315 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
316 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
318 assertNotNull(testNode);
319 assertNotNull(foundNode);
320 assertEquals(testNode, foundNode);
322 testNode = myModule.getNotifications().iterator().next();
324 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
325 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
327 assertNotNull(testNode);
328 assertNotNull(foundNode);
329 assertEquals(testNode, foundNode);
331 testNode = getGroupingByName(myModule, "my-grouping");
333 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
334 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
336 assertNotNull(testNode);
337 assertNotNull(foundNode);
338 assertEquals(testNode, foundNode);
340 testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
342 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
343 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
345 assertNotNull(testNode);
346 assertNotNull(foundNode);
347 assertEquals(testNode, foundNode);
349 testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
350 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
352 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
353 QName.create(myModule.getQNameModule(), "my-list"));
354 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
356 assertNotNull(testNode);
357 assertNotNull(foundNode);
358 assertEquals(testNode, foundNode);
363 public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
364 ParseException, ReactorException {
366 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
368 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
370 final DataSchemaNode node = myModule
371 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
373 final Module foundModule = SchemaContextUtil.findParentModule(context, node);
375 assertEquals(myModule, foundModule);
378 @Test(expected = IllegalArgumentException.class)
379 public void findParentModuleIllegalArgumentTest() {
381 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
382 SchemaContextUtil.findParentModule(mockContext, null);
386 @Test(expected = IllegalArgumentException.class)
387 public void findParentModuleIllegalArgumentTest2() {
389 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
390 SchemaContextUtil.findParentModule(null, mockSchemaNode);
394 @Test(expected = IllegalStateException.class)
395 public void findParentModuleIllegalStateTest() {
397 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
398 final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
399 Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
400 SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
404 @Test(expected = IllegalArgumentException.class)
405 public void findDataSchemaNodeIllegalArgumentTest() {
407 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
408 SchemaContextUtil.findDataSchemaNode(mockContext, null);
412 @Test(expected = IllegalArgumentException.class)
413 public void findDataSchemaNodeIllegalArgumentTest2() {
415 final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
416 SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
421 public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
423 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
424 final Module module = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
425 final Module importedModule = context.findModule(new URI("uri:imported-module"),
426 QName.parseRevision("2014-10-07")).get();
428 final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
429 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
430 importedModule.getQNameModule(), "my-imported-leaf"));
432 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
435 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
437 assertNotNull(foundNode);
438 assertNotNull(testNode);
439 assertEquals(testNode, foundNode);
443 public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
445 // findDataSchemaNode(final SchemaContext context, final Module module,
446 // final RevisionAwareXPath nonCondXPath) {
448 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
449 final Module module = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
451 final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
452 final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
453 "my-leaf-in-gouping2"));
455 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
457 final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
459 assertNotNull(foundNode);
460 assertNotNull(testNode);
461 assertEquals(testNode, foundNode);
465 @Test(expected = IllegalArgumentException.class)
466 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
468 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
469 final Module module = Mockito.mock(Module.class);
471 SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
475 @Test(expected = IllegalArgumentException.class)
476 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
478 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
479 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
481 SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
485 @Test(expected = IllegalArgumentException.class)
486 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
488 final Module module = Mockito.mock(Module.class);
489 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
491 SchemaContextUtil.findDataSchemaNode(null, module, xpath);
495 @Test(expected = IllegalArgumentException.class)
496 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
498 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
499 final Module module = Mockito.mock(Module.class);
500 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl(
501 "my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
503 SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
508 public void findDataSchemaNodeFromXPathNullTest() {
510 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
511 final Module module = Mockito.mock(Module.class);
512 final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
514 Mockito.when(xpath.toString()).thenReturn(null);
515 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
520 public void findDataSchemaNodeFromXPathNullTest2() {
522 final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
523 final Module module = Mockito.mock(Module.class);
524 final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
526 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
531 public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
532 YangSyntaxErrorException, ParseException, ReactorException {
534 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
535 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
537 // find grouping in container
538 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
539 myModule.getQNameModule(), "my-container"));
540 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
542 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
543 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
544 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
546 assertNotNull(testNode);
547 assertNotNull(foundNode);
548 assertEquals(testNode, foundNode);
550 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
551 "my-leaf-in-grouping-in-container"));
552 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
554 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
556 assertNotNull(testNode);
557 assertNotNull(foundNode);
558 assertEquals(testNode, foundNode);
560 // find grouping in list
561 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
562 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
564 testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
566 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
567 QName.create(myModule.getQNameModule(), "my-list"),
568 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
569 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
571 assertNotNull(testNode);
572 assertNotNull(foundNode);
573 assertEquals(testNode, foundNode);
575 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
576 "my-leaf-in-grouping-in-list"));
577 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
579 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
581 assertNotNull(testNode);
582 assertNotNull(foundNode);
583 assertEquals(testNode, foundNode);
585 // find grouping in grouping
586 dataContainer = getGroupingByName(myModule, "my-grouping");
587 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
589 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
590 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
591 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
593 assertNotNull(testNode);
594 assertNotNull(foundNode);
595 assertEquals(testNode, foundNode);
597 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
598 "my-leaf-in-grouping-in-grouping"));
599 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
601 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
603 assertNotNull(testNode);
604 assertNotNull(foundNode);
605 assertEquals(testNode, foundNode);
607 // find grouping in rpc
608 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
609 for (final GroupingDefinition grouping : rpc.getGroupings()) {
610 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
615 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
616 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
617 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
619 assertNotNull(testNode);
620 assertNotNull(foundNode);
621 assertEquals(testNode, foundNode);
623 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
624 "my-leaf-in-grouping-in-rpc"));
625 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
627 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
629 assertNotNull(testNode);
630 assertNotNull(foundNode);
631 assertEquals(testNode, foundNode);
633 // find grouping in output
634 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
635 testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
637 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
638 QName.create(myModule.getQNameModule(), "output"),
639 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
640 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
642 assertNotNull(testNode);
643 assertNotNull(foundNode);
644 assertEquals(testNode, foundNode);
646 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
647 "my-leaf-in-grouping-in-output"));
648 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
650 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
652 assertNotNull(testNode);
653 assertNotNull(foundNode);
654 assertEquals(testNode, foundNode);
656 // find grouping in input
657 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
658 testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
660 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
661 QName.create(myModule.getQNameModule(), "input"),
662 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
663 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
665 assertNotNull(testNode);
666 assertNotNull(foundNode);
667 assertEquals(testNode, foundNode);
669 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
670 "my-leaf-in-grouping-in-input"));
671 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
673 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
675 assertNotNull(testNode);
676 assertNotNull(foundNode);
677 assertEquals(testNode, foundNode);
679 // find grouping in notification
680 dataContainer = getNotificationByName(myModule, "my-notification");
681 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
683 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
684 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
685 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
687 assertNotNull(testNode);
688 assertNotNull(foundNode);
689 assertEquals(testNode, foundNode);
691 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
692 "my-leaf-in-grouping-in-notification"));
693 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
695 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
697 assertNotNull(testNode);
698 assertNotNull(foundNode);
699 assertEquals(testNode, foundNode);
701 // find grouping in case
702 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(QName.create(
703 myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one").getDataChildByName(
704 QName.create(myModule.getQNameModule(), "my-container-in-case"));
705 testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
707 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
708 QName.create(myModule.getQNameModule(), "one"),
709 QName.create(myModule.getQNameModule(), "my-container-in-case"),
710 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
711 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
713 assertNotNull(testNode);
714 assertNotNull(foundNode);
715 assertEquals(testNode, foundNode);
717 testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
718 "my-leaf-in-grouping-in-case"));
719 path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
721 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
723 assertNotNull(testNode);
724 assertNotNull(foundNode);
725 assertEquals(testNode, foundNode);
730 public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
731 YangSyntaxErrorException, ParseException, ReactorException {
733 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
735 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
737 // find grouping in container
738 DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
739 myModule.getQNameModule(), "my-container"));
740 SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
742 SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
743 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
744 SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
746 assertNull(testNode);
747 assertNull(foundNode);
749 // find grouping in list
750 dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
751 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
753 testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
755 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
756 QName.create(myModule.getQNameModule(), "my-list"),
757 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
758 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
760 assertNull(testNode);
761 assertNull(foundNode);
763 // find grouping in grouping
764 dataContainer = getGroupingByName(myModule, "my-grouping");
765 testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
767 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
768 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
769 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
771 assertNull(testNode);
772 assertNull(foundNode);
774 // find grouping in rpc
775 final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
776 for (final GroupingDefinition grouping : rpc.getGroupings()) {
777 if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
782 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
783 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
784 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
786 assertNull(testNode);
787 assertNull(foundNode);
789 // find grouping in output
790 dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
791 testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
793 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
794 QName.create(myModule.getQNameModule(), "output"),
795 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
796 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
798 assertNull(testNode);
799 assertNull(foundNode);
801 // find grouping in input
802 dataContainer = getRpcByName(myModule, "my-rpc").getInput();
803 testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
805 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
806 QName.create(myModule.getQNameModule(), "input"),
807 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
808 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
810 assertNull(testNode);
811 assertNull(foundNode);
813 // find grouping in notification
814 dataContainer = getNotificationByName(myModule, "my-notification");
815 testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
817 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
818 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
819 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
821 assertNull(testNode);
822 assertNull(foundNode);
824 // find grouping in case
825 dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(QName.create(
826 myModule.getQNameModule(), "my-choice"))).getCaseNodeByName("one").getDataChildByName(
827 QName.create(myModule.getQNameModule(), "my-container-in-case"));
828 testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
830 path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
831 QName.create(myModule.getQNameModule(), "one"),
832 QName.create(myModule.getQNameModule(), "my-container-in-case"),
833 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
834 foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
836 assertNull(testNode);
837 assertNull(foundNode);
841 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
842 for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
843 if (grouping.getQName().getLocalName().equals(name)) {
850 private static RpcDefinition getRpcByName(final Module module, final String name) {
851 for (final RpcDefinition rpc : module.getRpcs()) {
852 if (rpc.getQName().getLocalName().equals(name)) {
859 private static NotificationDefinition getNotificationByName(final Module module, final String name) {
860 for (final NotificationDefinition notification : module.getNotifications()) {
861 if (notification.getQName().getLocalName().equals(name)) {
869 public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
870 YangSyntaxErrorException, ParseException, ReactorException {
872 final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
874 final Module myModule = context.findModule(new URI("uri:my-module"), QName.parseRevision("2014-10-07")).get();
875 final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
876 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
877 final SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName(
878 QName.create(myModule.getQNameModule(), "two"));
880 final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
881 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
882 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
883 final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
885 assertNotNull(testNode);
886 assertNotNull(foundNode);
887 assertEquals(testNode, foundNode);