BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserWithContextTest.java
index 1ef5203b205ad4697ab483deb639aca3ab96be7f..f26fd2a6d13b3443e8ebd7374ec7781e4cccff34 100644 (file)
@@ -11,20 +11,17 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
 
+import com.google.common.collect.Iterables;
 import java.net.URI;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
@@ -47,53 +44,41 @@ import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinit
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
 
 public class YangParserWithContextTest {
     private static final URI T1_NS = URI.create("urn:simple.demo.test1");
     private static final URI T2_NS = URI.create("urn:simple.demo.test2");
     private static final URI T3_NS = URI.create("urn:simple.demo.test3");
-    private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
-    private static Date rev;
-
-    private static final YangStatementSourceImpl BAR = new YangStatementSourceImpl("/model/bar.yang", false);
-    private static final YangStatementSourceImpl BAZ = new YangStatementSourceImpl("/model/baz.yang", false);
-    private static final YangStatementSourceImpl FOO = new YangStatementSourceImpl("/model/foo.yang", false);
-    private static final YangStatementSourceImpl SUBFOO = new YangStatementSourceImpl("/model/subfoo.yang", false);
-
-    private static final YangStatementSourceImpl[] IETF = new YangStatementSourceImpl[] {
-            new YangStatementSourceImpl("/ietf/iana-afn-safi@2012-06-04.yang", false),
-            new YangStatementSourceImpl("/ietf/iana-if-type@2012-06-05.yang", false),
-            new YangStatementSourceImpl("/ietf/iana-timezones@2012-07-09.yang", false),
-            new YangStatementSourceImpl("/ietf/ietf-inet-types@2010-09-24.yang", false),
-            new YangStatementSourceImpl("/ietf/ietf-yang-types@2010-09-24.yang", false),
-            new YangStatementSourceImpl("/ietf/network-topology@2013-07-12.yang", false),
-            new YangStatementSourceImpl("/ietf/network-topology@2013-10-21.yang", false) };
-
-    @BeforeClass
-    public static void init() throws ParseException {
-        final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
-        rev = simpleDateFormat.parse("2013-06-18");
-    }
+    private static final Date REV = QName.parseRevision("2013-06-18");
+
+    private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
+    private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
+    private static final StatementStreamSource FOO = sourceForResource("/model/foo.yang");
+    private static final StatementStreamSource SUBFOO = sourceForResource("/model/subfoo.yang");
+
+    private static final StatementStreamSource[] IETF = new StatementStreamSource[] {
+            sourceForResource("/ietf/iana-afn-safi@2012-06-04.yang"),
+            sourceForResource("/ietf/iana-if-type@2012-06-05.yang"),
+            sourceForResource("/ietf/iana-timezones@2012-07-09.yang"),
+            sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"),
+            sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang"),
+            sourceForResource("/ietf/network-topology@2013-07-12.yang"),
+            sourceForResource("/ietf/network-topology@2013-10-21.yang") };
 
     @Test
     public void testTypeFromContext() throws Exception {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl types = new YangStatementSourceImpl("/types/custom-types-test@2012-4-4.yang",
-                false);
-        final YangStatementSourceImpl test1 = new YangStatementSourceImpl("/context-test/test1.yang", false);
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource test1 = sourceForResource("/context-test/test1.yang");
 
-        StmtTestUtils.addSources(reactor, IETF);
-        StmtTestUtils.addSources(reactor, types, test1);
+        reactor.addSources(IETF);
+        reactor.addSources(types, test1);
 
         final SchemaContext context = reactor.buildEffective();
 
-        final Module module = context.findModuleByName("test1",
-                SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
-        assertNotNull(module);
-
+        final Module module = context.findModule("test1", QName.parseRevision("2013-06-18")).get();
         final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
                 "id"));
 
@@ -101,13 +86,13 @@ public class YangParserWithContextTest {
         final UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
         QName qname = leafType.getQName();
         assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
-        assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
+        assertEquals(QName.parseRevision("2013-06-18"), qname.getRevision());
         assertEquals("port-number", qname.getLocalName());
 
         final UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType();
         qname = leafBaseType.getQName();
         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
-        assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
+        assertEquals(QName.parseRevision("2010-09-24"), qname.getRevision());
         assertEquals("port-number", qname.getLocalName());
 
         final UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
@@ -123,16 +108,12 @@ public class YangParserWithContextTest {
     public void testUsesFromContext() throws Exception {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl test2 = new YangStatementSourceImpl("/context-test/test2.yang", false);
-        StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
+        final StatementStreamSource test2 = sourceForResource("/context-test/test2.yang");
+        reactor.addSources(BAZ, FOO, BAR, SUBFOO, test2);
         final SchemaContext context = reactor.buildEffective();
 
-        final Module testModule = context.findModuleByName("test2",
-                SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
-        assertNotNull(testModule);
-
-        final Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator()
-                .next();
+        final Module testModule = context.findModule("test2", QName.parseRevision("2013-06-18")).get();
+        final Module contextModule = context.findModules(URI.create("urn:opendaylight.baz")).iterator().next();
         assertNotNull(contextModule);
         final Set<GroupingDefinition> groupings = contextModule.getGroupings();
         assertEquals(1, groupings.size());
@@ -234,13 +215,11 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl test2 = new YangStatementSourceImpl("/context-test/test2.yang", false);
-        StmtTestUtils.addSources(reactor, BAZ, FOO, BAR, SUBFOO, test2);
+        final StatementStreamSource test2 = sourceForResource("/context-test/test2.yang");
+        reactor.addSources(BAZ, FOO, BAR, SUBFOO, test2);
         final SchemaContext context = reactor.buildEffective();
 
-        final Module module = context.findModuleByName("test2",
-                SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
-        assertNotNull(module);
+        final Module module = context.findModule("test2", QName.parseRevision("2013-06-18")).get();
         final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "peer"));
         final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
@@ -251,8 +230,8 @@ public class YangParserWithContextTest {
 
         // test grouping path
         final List<QName> path = new ArrayList<>();
-        final QName qname = QName.create(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"),
-                "target");
+        final QName qname = QName.create(URI.create("urn:opendaylight.baz"), QName.parseRevision("2013-02-27"),
+            "target");
         path.add(qname);
         final SchemaPath expectedPath = SchemaPath.create(path, true);
         assertEquals(expectedPath, usesNode.getGroupingPath());
@@ -310,31 +289,27 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl types = new YangStatementSourceImpl("/types/custom-types-test@2012-4-4.yang",
-                false);
-        final YangStatementSourceImpl test3 = new YangStatementSourceImpl("/context-test/test3.yang", false);
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
 
-        StmtTestUtils.addSources(reactor, IETF);
-        StmtTestUtils.addSources(reactor, types, test3);
+        reactor.addSources(IETF);
+        reactor.addSources(types, test3);
         final SchemaContext context = reactor.buildEffective();
 
-        final Module module = context.findModuleByName("test3",
-                SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
-        assertNotNull(module);
-
+        final Module module = context.findModule("test3", QName.parseRevision("2013-06-18")).get();
         final Set<IdentitySchemaNode> identities = module.getIdentities();
         assertEquals(1, identities.size());
 
         final IdentitySchemaNode identity = identities.iterator().next();
         final QName idQName = identity.getQName();
         assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
-        assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
+        assertEquals(QName.parseRevision("2013-06-18"), idQName.getRevision());
         assertEquals("pt", idQName.getLocalName());
 
-        final IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
+        final IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(identity.getBaseIdentities());
         final QName idBaseQName = baseIdentity.getQName();
         assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
-        assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
+        assertEquals(QName.parseRevision("2012-04-16"), idBaseQName.getRevision());
         assertEquals("service-type", idBaseQName.getLocalName());
     }
 
@@ -343,19 +318,15 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl types = new YangStatementSourceImpl("/types/custom-types-test@2012-4-4.yang",
-                false);
-        final YangStatementSourceImpl test3 = new YangStatementSourceImpl("/context-test/test3.yang", false);
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
 
-        StmtTestUtils.addSources(reactor, IETF);
-        StmtTestUtils.addSources(reactor, types, test3);
+        reactor.addSources(IETF);
+        reactor.addSources(types, test3);
 
         final SchemaContext context = reactor.buildEffective();
 
-        final Module module = context.findModuleByName("test3",
-                SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"));
-        assertNotNull(module);
-
+        final Module module = context.findModule("test3", QName.parseRevision("2013-06-18")).get();
         final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "network"));
         final List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
@@ -364,7 +335,7 @@ public class YangParserWithContextTest {
         final UnknownSchemaNode un = unknownNodes.get(0);
         final QName unType = un.getNodeType();
         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
-        assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
+        assertEquals(QName.parseRevision("2012-04-16"), unType.getRevision());
         assertEquals("mountpoint", unType.getLocalName());
         assertEquals("point", un.getNodeParameter());
         assertNotNull(un.getExtensionDefinition());
@@ -372,16 +343,13 @@ public class YangParserWithContextTest {
 
     @Test
     public void testAugment() throws Exception {
-        final StatementStreamSource resource = new YangStatementSourceImpl("/context-augment-test/test4.yang", false);
-        final StatementStreamSource test1 = new YangStatementSourceImpl("/context-augment-test/test1.yang", false);
-        final StatementStreamSource test2 = new YangStatementSourceImpl("/context-augment-test/test2.yang", false);
-        final StatementStreamSource test3 = new YangStatementSourceImpl("/context-augment-test/test3.yang", false);
+        final StatementStreamSource resource = sourceForResource("/context-augment-test/test4.yang");
+        final StatementStreamSource test1 = sourceForResource("/context-augment-test/test1.yang");
+        final StatementStreamSource test2 = sourceForResource("/context-augment-test/test2.yang");
+        final StatementStreamSource test3 = sourceForResource("/context-augment-test/test3.yang");
 
         final SchemaContext context = TestUtils.parseYangSources(resource, test1, test2, test3);
-        final Set<Module> modules = context.getModules();
-        assertNotNull(modules);
-
-        final Module t4 = TestUtils.findModule(modules, "test4");
+        final Module t4 = TestUtils.findModule(context, "test4").get();
         final ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName(QName.create(
                 t4.getQNameModule(), "interfaces"));
         final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(t4.getQNameModule(),
@@ -389,19 +357,19 @@ public class YangParserWithContextTest {
 
         // test augmentation process
         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(T3_NS,
-                rev, "augment-holder"));
+                REV, "augment-holder"));
         assertNotNull(augmentHolder);
-        final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, rev, "ds0ChannelNumber"));
+        final DataSchemaNode ds0 = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "ds0ChannelNumber"));
         assertNotNull(ds0);
-        final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, rev, "interface-id"));
+        final DataSchemaNode interfaceId = augmentHolder.getDataChildByName(QName.create(T2_NS, REV, "interface-id"));
         assertNotNull(interfaceId);
-        final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, rev,
+        final DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName(QName.create(T2_NS, REV,
                 "higher-layer-if"));
         assertNotNull(higherLayerIf);
         final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(T2_NS,
-                rev, "schemas"));
+                REV, "schemas"));
         assertNotNull(schemas);
-        assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, rev, "id")));
+        assertNotNull(schemas.getDataChildByName(QName.create(T1_NS, REV, "id")));
 
         // test augment target after augmentation: check if it is same instance
         final ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
@@ -414,17 +382,13 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final YangStatementSourceImpl bar = new YangStatementSourceImpl("/model/bar.yang", false);
-        final YangStatementSourceImpl deviationTest = new YangStatementSourceImpl("/context-test/deviation-test.yang",
-                false);
+        final StatementStreamSource bar = sourceForResource("/model/bar.yang");
+        final StatementStreamSource deviationTest = sourceForResource("/context-test/deviation-test.yang");
 
-        StmtTestUtils.addSources(reactor, bar, deviationTest);
+        reactor.addSources(bar, deviationTest);
         final SchemaContext context = reactor.buildEffective();
 
-        final Module testModule = context.findModuleByName("deviation-test", SimpleDateFormatUtil.getRevisionFormat()
-                .parse("2013-02-27"));
-        assertNotNull(testModule);
-
+        final Module testModule = context.findModule("deviation-test", QName.parseRevision("2013-02-27")).get();
         final Set<Deviation> deviations = testModule.getDeviations();
         assertEquals(1, deviations.size());
         final Deviation dev = deviations.iterator().next();
@@ -432,8 +396,7 @@ public class YangParserWithContextTest {
         assertEquals("system/user ref", dev.getReference());
 
         final URI expectedNS = URI.create("urn:opendaylight.bar");
-        final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
-        final Date expectedRev = simpleDateFormat.parse("2013-07-03");
+        final Date expectedRev = QName.parseRevision("2013-07-03");
         final List<QName> path = new ArrayList<>();
         path.add(QName.create(expectedNS, expectedRev, "interfaces"));
         path.add(QName.create(expectedNS, expectedRev, "ifEntry"));