BUG-4688: Make SourceIdentifier use Revision
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserWithContextTest.java
index 6fdd1ff4386f4cb732c4bb41d2a8638eb4986305..333b7589f34fb620401249af72ae7dbd362aec2f 100644 (file)
@@ -13,18 +13,16 @@ 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.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 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.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
@@ -52,7 +50,7 @@ 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 static Date rev;
+    private static final Revision REV = Revision.valueOf("2013-06-18");
 
     private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
     private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
@@ -68,18 +66,12 @@ public class YangParserWithContextTest {
             sourceForResource("/ietf/network-topology@2013-07-12.yang"),
             sourceForResource("/ietf/network-topology@2013-10-21.yang") };
 
-    @BeforeClass
-    public static void init() throws ParseException {
-        final DateFormat simpleDateFormat = SimpleDateFormatUtil.getRevisionFormat();
-        rev = simpleDateFormat.parse("2013-06-18");
-    }
-
     @Test
     public void testTypeFromContext() throws Exception {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
         final StatementStreamSource test1 = sourceForResource("/context-test/test1.yang");
 
         reactor.addSources(IETF);
@@ -87,10 +79,7 @@ public class YangParserWithContextTest {
 
         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", Revision.valueOf("2013-06-18")).get();
         final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
                 "id"));
 
@@ -98,13 +87,13 @@ public class YangParserWithContextTest {
         final UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
         QName qname = leafType.getQName();
         assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
-        assertEquals(SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"), qname.getRevision());
+        assertEquals(Optional.of(Revision.valueOf("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(SimpleDateFormatUtil.getRevisionFormat().parse("2010-09-24"), qname.getRevision());
+        assertEquals(Optional.of(Revision.valueOf("2010-09-24")), qname.getRevision());
         assertEquals("port-number", qname.getLocalName());
 
         final UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
@@ -124,12 +113,8 @@ public class YangParserWithContextTest {
         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", Revision.valueOf("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());
@@ -235,9 +220,7 @@ public class YangParserWithContextTest {
         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", Revision.valueOf("2013-06-18")).get();
         final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "peer"));
         final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
@@ -248,8 +231,7 @@ public class YangParserWithContextTest {
 
         // test grouping path
         final List<QName> path = new ArrayList<>();
-        final QName qname = QName.create(URI.create("urn:opendaylight.baz"),
-            SimpleDateFormatUtil.getRevisionFormat().parse("2013-02-27"), "target");
+        final QName qname = QName.create(URI.create("urn:opendaylight.baz"), Revision.valueOf("2013-02-27"), "target");
         path.add(qname);
         final SchemaPath expectedPath = SchemaPath.create(path, true);
         assertEquals(expectedPath, usesNode.getGroupingPath());
@@ -307,30 +289,27 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
         final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
 
         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", Revision.valueOf("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(SimpleDateFormatUtil.getRevisionFormat().parse("2013-06-18"), idQName.getRevision());
+        assertEquals(Optional.of(Revision.valueOf("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(SimpleDateFormatUtil.getRevisionFormat().parse("2012-04-16"), idBaseQName.getRevision());
+        assertEquals(Optional.of(Revision.valueOf("2012-04-16")), idBaseQName.getRevision());
         assertEquals("service-type", idBaseQName.getLocalName());
     }
 
@@ -339,7 +318,7 @@ public class YangParserWithContextTest {
 
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.yang");
+        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-04-04.yang");
         final StatementStreamSource test3 = sourceForResource("/context-test/test3.yang");
 
         reactor.addSources(IETF);
@@ -347,10 +326,7 @@ public class YangParserWithContextTest {
 
         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", Revision.valueOf("2013-06-18")).get();
         final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "network"));
         final List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
@@ -359,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(SimpleDateFormatUtil.getRevisionFormat().parse("2012-04-16"), unType.getRevision());
+        assertEquals(Optional.of(Revision.valueOf("2012-04-16")), unType.getRevision());
         assertEquals("mountpoint", unType.getLocalName());
         assertEquals("point", un.getNodeParameter());
         assertNotNull(un.getExtensionDefinition());
@@ -373,10 +349,7 @@ public class YangParserWithContextTest {
         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(),
@@ -384,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(
@@ -415,10 +388,7 @@ public class YangParserWithContextTest {
         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", Revision.valueOf("2013-02-27")).get();
         final Set<Deviation> deviations = testModule.getDeviations();
         assertEquals(1, deviations.size());
         final Deviation dev = deviations.iterator().next();
@@ -426,7 +396,7 @@ public class YangParserWithContextTest {
         assertEquals("system/user ref", dev.getReference());
 
         final URI expectedNS = URI.create("urn:opendaylight.bar");
-        final Date expectedRev = SimpleDateFormatUtil.getRevisionFormat().parse("2013-07-03");
+        final Revision expectedRev = Revision.valueOf("2013-07-03");
         final List<QName> path = new ArrayList<>();
         path.add(QName.create(expectedNS, expectedRev, "interfaces"));
         path.add(QName.create(expectedNS, expectedRev, "ifEntry"));