X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Futil%2FSchemaContextUtilTest.java;h=dcb9f2bdd30d9cd39e0c16dee13af6e810daa834;hb=1cb3b70ea959108d66dd19cbe30c86d78c77e5c9;hp=db9942f568f520a4b6eaec3bcfe4ce68a6e2f6d5;hpb=b401ea6d55a428bc5c90bad611ae675a0c92f765;p=yangtools.git diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java index db9942f568..dcb9f2bdd3 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java @@ -7,38 +7,43 @@ */ package org.opendaylight.yangtools.yang.parser.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collections; +import java.util.Optional; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.Revision; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; +import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; import org.opendaylight.yangtools.yang.model.api.SchemaNode; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.model.util.Int32; import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.text.ParseException; -import java.util.Collections; -import java.util.Set; - -import static org.junit.Assert.*; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.stmt.TestUtils; public class SchemaContextUtilTest { @Mock @@ -49,18 +54,21 @@ public class SchemaContextUtilTest { @Test public void testFindDummyData() { MockitoAnnotations.initMocks(this); + doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class)); + doReturn(URI.create("dummy")).when(mockModule).getNamespace(); + doReturn(Optional.empty()).when(mockModule).getRevision(); - QName qName = QName.create("TestQName"); - SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true); + final QName qName = QName.create("dummy", "TestQName"); + final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true); assertEquals("Should be null. Module TestQName not found", null, SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath)); - RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true); + final RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true); assertEquals("Should be null. Module bookstore not found", null, SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath)); - SchemaNode schemaNode = Int32.getInstance(); - RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false); + final SchemaNode schemaNode = BaseTypes.int32Type(); + final RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false); assertEquals("Should be null, Module prefix not found", null, SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode, xPathRelative)); @@ -73,19 +81,15 @@ public class SchemaContextUtilTest { @Test public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException, - ParseException { - - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); + ReactorException { - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); - Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); + final Module myModule = context.findModule(new URI("uri:my-module"),Revision.of("2014-10-07")).get(); - SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-leaf-in-container"); + SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-container")); SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-leaf-in-container")); @@ -95,9 +99,8 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - Set rpcs = myModule.getRpcs(); - RpcDefinition rpc = rpcs.iterator().next(); - testNode = rpc.getInput().getDataChildByName("my-input-leaf"); + RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), QName.create(myModule.getQNameModule(), "input"), @@ -109,8 +112,8 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - rpc = myModule.getRpcs().iterator().next(); - testNode = rpc.getOutput().getDataChildByName("my-output-leaf"); + rpc = getRpcByName(myModule, "my-rpc"); + testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), QName.create(myModule.getQNameModule(), "output"), @@ -122,8 +125,8 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - NotificationDefinition notification = myModule.getNotifications().iterator().next(); - testNode = notification.getDataChildByName("my-notification-leaf"); + final NotificationDefinition notification = myModule.getNotifications().iterator().next(); + testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), QName.create(myModule.getQNameModule(), "my-notification-leaf")); @@ -134,9 +137,10 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - GroupingDefinition grouping = myModule.getGroupings().iterator().next(); - testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) - .getDataChildByName("my-leaf-in-grouping"); + final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping"); + testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(), + "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), QName.create(myModule.getQNameModule(), "my-container-in-grouping"), @@ -148,8 +152,10 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = ((ChoiceNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName( - "my-choice-leaf-one"); + testNode = ((ChoiceSchemaNode) myModule + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))) + .findCaseNodes("one").iterator().next() + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), QName.create(myModule.getQNameModule(), "one"), @@ -160,10 +166,11 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-list"); + ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); - testNode = listNode.getDataChildByName("my-leaf-in-list"); + testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-list"), @@ -174,10 +181,11 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-list"); + listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); - testNode = listNode.getDataChildByName("my-leaf-list-in-list"); + testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-list"), @@ -192,19 +200,15 @@ public class SchemaContextUtilTest { @Test public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException, - ParseException { + ReactorException { - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); + final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); - Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); - - SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-leaf-not-in-container"); + SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-not-in-container")); SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-leaf-not-in-container")); @@ -213,9 +217,8 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - Set rpcs = myModule.getRpcs(); - RpcDefinition rpc = rpcs.iterator().next(); - testNode = rpc.getInput().getDataChildByName("no-input-leaf"); + final RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), QName.create(myModule.getQNameModule(), "input"), @@ -226,8 +229,8 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - NotificationDefinition notification = myModule.getNotifications().iterator().next(); - testNode = notification.getDataChildByName("no-notification-leaf"); + final NotificationDefinition notification = myModule.getNotifications().iterator().next(); + testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), QName.create(myModule.getQNameModule(), "no-notification-leaf")); @@ -237,9 +240,10 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - GroupingDefinition grouping = myModule.getGroupings().iterator().next(); - testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) - .getDataChildByName("no-leaf-in-grouping"); + final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping"); + testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(), + "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "no-leaf-in-grouping")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), QName.create(myModule.getQNameModule(), "my-container-in-grouping"), @@ -250,8 +254,10 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - testNode = ((ChoiceNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName( - "no-choice-leaf"); + testNode = ((ChoiceSchemaNode) myModule + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"))) + .findCaseNodes("one").iterator().next() + .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), QName.create(myModule.getQNameModule(), "one"), @@ -261,10 +267,11 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-list"); + ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); - testNode = listNode.getDataChildByName("no-leaf-in-list"); + testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-list"), @@ -274,10 +281,11 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) - .getDataChildByName("my-list"); + listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); - testNode = listNode.getDataChildByName("no-leaf-list-in-list"); + testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-list"), @@ -291,18 +299,13 @@ public class SchemaContextUtilTest { @Test public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException, - ParseException { - - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); + ReactorException { - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); - Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); + final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); - SchemaNode testNode = myModule.getDataChildByName("my-container"); + SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container")); SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container")); SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); @@ -311,7 +314,7 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = myModule.getRpcs().iterator().next(); + testNode = getRpcByName(myModule, "my-rpc"); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc")); foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); @@ -329,7 +332,7 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = myModule.getGroupings().iterator().next(); + testNode = getGroupingByName(myModule, "my-grouping"); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping")); foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); @@ -338,7 +341,7 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = myModule.getDataChildByName("my-choice"); + testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice")); foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); @@ -347,7 +350,8 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list"); + testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(), + "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list")); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), QName.create(myModule.getQNameModule(), "my-list")); @@ -360,20 +364,17 @@ public class SchemaContextUtilTest { } @Test - public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException, ParseException { + public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException, + ReactorException { - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); + final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); - Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); + final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), + "my-container")); - DataSchemaNode node = myModule.getDataChildByName("my-container"); - - Module foundModule = SchemaContextUtil.findParentModule(context, node); + final Module foundModule = SchemaContextUtil.findParentModule(context, node); assertEquals(myModule, foundModule); } @@ -381,7 +382,7 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findParentModuleIllegalArgumentTest() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); SchemaContextUtil.findParentModule(mockContext, null); } @@ -389,7 +390,7 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findParentModuleIllegalArgumentTest2() { - SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); + final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); SchemaContextUtil.findParentModule(null, mockSchemaNode); } @@ -397,8 +398,8 @@ public class SchemaContextUtilTest { @Test(expected = IllegalStateException.class) public void findParentModuleIllegalStateTest() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); Mockito.when(mockSchemaNode.getPath()).thenReturn(null); SchemaContextUtil.findParentModule(mockContext, mockSchemaNode); @@ -407,7 +408,7 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeIllegalArgumentTest() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); SchemaContextUtil.findDataSchemaNode(mockContext, null); } @@ -415,58 +416,49 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeIllegalArgumentTest2() { - SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class); + final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class); SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath); } @Test - public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException { - - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); - - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); - - Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); - Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"), - QName.parseRevision("2014-10-07")); - - SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container")) - .getDataChildByName("my-imported-leaf"); - - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true); + public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException, + ReactorException { + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); + final Module importedModule = context.findModule(new URI("uri:imported-module"), + Revision.of("2014-10-07")).get(); + + final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create( + importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create( + importedModule.getQNameModule(), "my-imported-leaf")); + + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", + true); - SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); + final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); assertNotNull(foundNode); assertNotNull(testNode); assertEquals(testNode, foundNode); - } @Test - public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException { + public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException, + ReactorException { // findDataSchemaNode(final SchemaContext context, final Module module, // final RevisionAwareXPath nonCondXPath) { - File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI()); - File resourceDir = resourceFile.getParentFile(); + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); - YangParserImpl parser = YangParserImpl.getInstance(); - SchemaContext context = parser.parseFile(resourceFile, resourceDir); + final GroupingDefinition grouping = getGroupingByName(module, "my-grouping"); + final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(), + "my-leaf-in-gouping2")); - Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), - QName.parseRevision("2014-10-07")); + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); - GroupingDefinition grouping = module.getGroupings().iterator().next(); - SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2"); - - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); - - SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); + final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); assertNotNull(foundNode); assertNotNull(testNode); @@ -477,8 +469,8 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeFromXPathIllegalArgumentTest() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - Module module = Mockito.mock(Module.class); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final Module module = Mockito.mock(Module.class); SchemaContextUtil.findDataSchemaNode(mockContext, module, null); @@ -487,8 +479,8 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeFromXPathIllegalArgumentTest2() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath); @@ -497,8 +489,8 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeFromXPathIllegalArgumentTest3() { - Module module = Mockito.mock(Module.class); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); + final Module module = Mockito.mock(Module.class); + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); SchemaContextUtil.findDataSchemaNode(null, module, xpath); @@ -507,10 +499,10 @@ public class SchemaContextUtilTest { @Test(expected = IllegalArgumentException.class) public void findDataSchemaNodeFromXPathIllegalArgumentTest4() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - Module module = Mockito.mock(Module.class); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", - true); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final Module module = Mockito.mock(Module.class); + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl( + "my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true); SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath); @@ -519,9 +511,9 @@ public class SchemaContextUtilTest { @Test public void findDataSchemaNodeFromXPathNullTest() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - Module module = Mockito.mock(Module.class); - RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final Module module = Mockito.mock(Module.class); + final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class); Mockito.when(xpath.toString()).thenReturn(null); assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath)); @@ -531,12 +523,373 @@ public class SchemaContextUtilTest { @Test public void findDataSchemaNodeFromXPathNullTest2() { - SchemaContext mockContext = Mockito.mock(SchemaContext.class); - Module module = Mockito.mock(Module.class); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false); + final SchemaContext mockContext = Mockito.mock(SchemaContext.class); + final Module module = Mockito.mock(Module.class); + final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false); assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath)); } + @Test + public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException, + YangSyntaxErrorException, ReactorException { + + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get(); + + // find grouping in container + DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container")); + SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-grouping-in-container")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-container")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in list + dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); + testNode = getGroupingByName(dataContainer, "my-grouping-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-grouping-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-list")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in grouping + dataContainer = getGroupingByName(myModule, "my-grouping"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-grouping-in-grouping")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-grouping")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in rpc + final RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + for (final GroupingDefinition grouping : rpc.getGroupings()) { + if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) { + testNode = grouping; + } + } + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "my-grouping-in-rpc")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-rpc")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in output + dataContainer = getRpcByName(myModule, "my-rpc").getOutput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-output"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "output"), + QName.create(myModule.getQNameModule(), "my-grouping-in-output")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-output")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in input + dataContainer = getRpcByName(myModule, "my-rpc").getInput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-input"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-grouping-in-input")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-input")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in notification + dataContainer = getNotificationByName(myModule, "my-notification"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-notification"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "my-grouping-in-notification")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-notification")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in case + dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName( + QName.create(myModule.getQNameModule(), "my-choice"))) + .findCaseNodes("one").iterator().next() + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case")); + testNode = getGroupingByName(dataContainer, "my-grouping-in-case"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "my-container-in-case"), + QName.create(myModule.getQNameModule(), "my-grouping-in-case")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-leaf-in-grouping-in-case")); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + } + + @Test + public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException, + YangSyntaxErrorException, ReactorException { + + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); + + // find grouping in container + DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container")); + SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-grouping-in-container2")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in list + dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create( + myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), + "my-list")); + testNode = getGroupingByName(dataContainer, "my-grouping-in-list2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-grouping-in-list2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in grouping + dataContainer = getGroupingByName(myModule, "my-grouping"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in rpc + final RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + for (final GroupingDefinition grouping : rpc.getGroupings()) { + if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) { + testNode = grouping; + } + } + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in output + dataContainer = getRpcByName(myModule, "my-rpc").getOutput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-output2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "output"), + QName.create(myModule.getQNameModule(), "my-grouping-in-output2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in input + dataContainer = getRpcByName(myModule, "my-rpc").getInput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-input2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-grouping-in-input2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in notification + dataContainer = getNotificationByName(myModule, "my-notification"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "my-grouping-in-notification2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in case + dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName( + QName.create(myModule.getQNameModule(), "my-choice"))) + .findCaseNodes("one").iterator().next() + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case")); + testNode = getGroupingByName(dataContainer, "my-grouping-in-case2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "my-container-in-case"), + QName.create(myModule.getQNameModule(), "my-grouping-in-case2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + } + + private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) { + for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) { + if (grouping.getQName().getLocalName().equals(name)) { + return grouping; + } + } + return null; + } + + private static RpcDefinition getRpcByName(final Module module, final String name) { + for (final RpcDefinition rpc : module.getRpcs()) { + if (rpc.getQName().getLocalName().equals(name)) { + return rpc; + } + } + return null; + } + + private static NotificationDefinition getNotificationByName(final Module module, final String name) { + for (final NotificationDefinition notification : module.getNotifications()) { + if (notification.getQName().getLocalName().equals(name)) { + return notification; + } + } + return null; + } + + @Test + public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException, + YangSyntaxErrorException, ReactorException { + + final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get(); + final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput() + .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")); + final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next() + .getDataChildByName(QName.create(myModule.getQNameModule(), "two")); + + final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"), + QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two")); + final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + } } \ No newline at end of file