Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserWithContextTest.java
index 6f659082636e93c67fcbff87147b27af2f4fbe0f..9fe0a6e986e64d212cb6adad9b1d38cfcbb0b5af 100644 (file)
@@ -13,25 +13,25 @@ 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 com.google.common.collect.Range;
 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.Collection;
 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;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
+import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
@@ -43,18 +43,17 @@ import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
-import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
+import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 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;
 
 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 Revision REV = Revision.of("2013-06-18");
 
     private static final StatementStreamSource BAR = sourceForResource("/model/bar.yang");
     private static final StatementStreamSource BAZ = sourceForResource("/model/baz.yang");
@@ -70,68 +69,48 @@ 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 = new SimpleDateFormat("yyyy-MM-dd");
-        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 test1 = sourceForResource("/context-test/test1.yang");
-
-        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);
-
+    public void testTypeFromContext() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSources(IETF)
+                .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
+                .addSource(sourceForResource("/context-test/test1.yang"))
+                .buildEffective();
+
+        final Module module = context.findModule("test1", Revision.of("2013-06-18")).get();
         final LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName(QName.create(module.getQNameModule(),
                 "id"));
 
-        assertTrue(leaf.getType() instanceof UnsignedIntegerTypeDefinition);
-        final UnsignedIntegerTypeDefinition leafType = (UnsignedIntegerTypeDefinition) leaf.getType();
+        assertTrue(leaf.getType() instanceof Uint16TypeDefinition);
+        final Uint16TypeDefinition leafType = (Uint16TypeDefinition) leaf.getType();
         QName qname = leafType.getQName();
         assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
-        assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
+        assertEquals(Revision.ofNullable("2013-06-18"), qname.getRevision());
         assertEquals("port-number", qname.getLocalName());
 
-        final UnsignedIntegerTypeDefinition leafBaseType = leafType.getBaseType();
+        final Uint16TypeDefinition 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(Revision.ofNullable("2010-09-24"), qname.getRevision());
         assertEquals("port-number", qname.getLocalName());
 
-        final UnsignedIntegerTypeDefinition dscpExt = (UnsignedIntegerTypeDefinition) TestUtils.findTypedef(
-                module.getTypeDefinitions(), "dscp-ext");
-        final List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
+        final Uint8TypeDefinition dscpExt = (Uint8TypeDefinition) TestUtils.findTypedef(module.getTypeDefinitions(),
+            "dscp-ext");
+        final Set<? extends Range<?>> ranges = dscpExt.getRangeConstraint().get().getAllowedRanges().asRanges();
         assertEquals(1, ranges.size());
-        final RangeConstraint range = ranges.get(0);
-        assertEquals(0, range.getMin().intValue());
-        assertEquals(63, range.getMax().intValue());
+        final Range<?> range = ranges.iterator().next();
+        assertEquals((short)0, range.lowerEndpoint());
+        assertEquals((short)63, range.upperEndpoint());
     }
 
     @Test
-    public void testUsesFromContext() throws Exception {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-
-        final StatementStreamSource test2 = sourceForResource("/context-test/test2.yang");
-        reactor.addSources(BAZ, FOO, BAR, SUBFOO, test2);
-        final SchemaContext context = reactor.buildEffective();
+    public void testUsesFromContext() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
+                .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.of("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());
@@ -229,17 +208,12 @@ public class YangParserWithContextTest {
     }
 
     @Test
-    public void testUsesRefineFromContext() throws Exception {
-
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+    public void testUsesRefineFromContext() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSources(BAZ, FOO, BAR, SUBFOO, sourceForResource("/context-test/test2.yang"))
+                .buildEffective();
 
-        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", Revision.of("2013-06-18")).get();
         final ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "peer"));
         final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
@@ -250,8 +224,7 @@ 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"), Revision.of("2013-02-27"), "target");
         path.add(qname);
         final SchemaPath expectedPath = SchemaPath.create(path, true);
         assertEquals(expectedPath, usesNode.getGroupingPath());
@@ -277,82 +250,68 @@ public class YangParserWithContextTest {
         // leaf address
         assertNotNull(refineLeaf);
         assertEquals("address", refineLeaf.getQName().getLocalName());
-        assertEquals("description of address defined by refine", refineLeaf.getDescription());
-        assertEquals("address reference added by refine", refineLeaf.getReference());
+        assertEquals(Optional.of("description of address defined by refine"), refineLeaf.getDescription());
+        assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
         assertFalse(refineLeaf.isConfiguration());
-        assertTrue(refineLeaf.getConstraints().isMandatory());
-        final Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
+        assertTrue(refineLeaf.isMandatory());
+        final Collection<MustDefinition> leafMustConstraints = refineLeaf.getMustConstraints();
         assertEquals(1, leafMustConstraints.size());
         final MustDefinition leafMust = leafMustConstraints.iterator().next();
         assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.toString());
 
         // container port
         assertNotNull(refineContainer);
-        final Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
+        final Collection<MustDefinition> mustConstraints = refineContainer.getMustConstraints();
         assertTrue(mustConstraints.isEmpty());
-        assertEquals("description of port defined by refine", refineContainer.getDescription());
-        assertEquals("port reference added by refine", refineContainer.getReference());
+        assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
+        assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
         assertFalse(refineContainer.isConfiguration());
         assertTrue(refineContainer.isPresenceContainer());
 
         // list addresses
         assertNotNull(refineList);
-        assertEquals("description of addresses defined by refine", refineList.getDescription());
-        assertEquals("addresses reference added by refine", refineList.getReference());
+        assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
+        assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
         assertFalse(refineList.isConfiguration());
-        assertEquals(2, (int) refineList.getConstraints().getMinElements());
-        assertEquals(12, (int) refineList.getConstraints().getMaxElements());
+        final ElementCountConstraint constraint = refineList.getElementCountConstraint().get();
+        assertEquals(2, constraint.getMinElements().intValue());
+        assertEquals(12, constraint.getMaxElements().intValue());
     }
 
     @Test
-    public void testIdentity() throws Exception {
-
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-
-        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.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);
-
+    public void testIdentity() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSources(IETF)
+                .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
+                .addSource(sourceForResource("/context-test/test3.yang"))
+                .buildEffective();
+
+        final Module module = context.findModule("test3", Revision.of("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(Revision.ofNullable("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(Revision.ofNullable("2012-04-16"), idBaseQName.getRevision());
         assertEquals("service-type", idBaseQName.getLocalName());
     }
 
     @Test
-    public void testUnknownNodes() throws Exception {
-
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-
-        final StatementStreamSource types = sourceForResource("/types/custom-types-test@2012-4-4.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);
-
+    public void testUnknownNodes() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSources(IETF)
+                .addSource(sourceForResource("/types/custom-types-test@2012-04-04.yang"))
+                .addSource(sourceForResource("/context-test/test3.yang"))
+                .buildEffective();
+
+        final Module module = context.findModule("test3", Revision.of("2013-06-18")).get();
         final ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName(QName.create(
                 module.getQNameModule(), "network"));
         final List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
@@ -361,24 +320,21 @@ 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(Revision.ofNullable("2012-04-16"), unType.getRevision());
         assertEquals("mountpoint", unType.getLocalName());
         assertEquals("point", un.getNodeParameter());
         assertNotNull(un.getExtensionDefinition());
     }
 
     @Test
-    public void testAugment() throws Exception {
+    public void testAugment() throws ReactorException {
         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(),
@@ -386,19 +342,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(
@@ -407,29 +363,21 @@ public class YangParserWithContextTest {
     }
 
     @Test
-    public void testDeviation() throws Exception {
-
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
-
-        final StatementStreamSource bar = sourceForResource("/model/bar.yang");
-        final StatementStreamSource deviationTest = sourceForResource("/context-test/deviation-test.yang");
-
-        reactor.addSources(bar, deviationTest);
-        final SchemaContext context = reactor.buildEffective();
-
-        final Module testModule = context.findModuleByName("deviation-test", SimpleDateFormatUtil.getRevisionFormat()
-                .parse("2013-02-27"));
-        assertNotNull(testModule);
+    public void testDeviation() throws ReactorException {
+        final SchemaContext context = RFC7950Reactors.defaultReactor().newBuild()
+                .addSource(sourceForResource("/model/bar.yang"))
+                .addSource(sourceForResource("/context-test/deviation-test.yang"))
+                .buildEffective();
 
+        final Module testModule = context.findModule("deviation-test", Revision.of("2013-02-27")).get();
         final Set<Deviation> deviations = testModule.getDeviations();
         assertEquals(1, deviations.size());
         final Deviation dev = deviations.iterator().next();
 
-        assertEquals("system/user ref", dev.getReference());
+        assertEquals(Optional.of("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 Revision expectedRev = Revision.of("2013-07-03");
         final List<QName> path = new ArrayList<>();
         path.add(QName.create(expectedNS, expectedRev, "interfaces"));
         path.add(QName.create(expectedNS, expectedRev, "ifEntry"));