BUG-1382: eliminate use of QName.getPrefix from yang parser
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserWithContextTest.java
index 59e3c8d5a26db4a48f37a26e4dd61c2c8e1e9871..7b0a59a367f06cf244edecbb7d8c122add62df18 100644 (file)
@@ -7,20 +7,26 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
-import static org.junit.Assert.*;
+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 com.google.common.collect.Lists;
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.math.BigInteger;
 import java.net.URI;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
-import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
@@ -41,27 +47,23 @@ 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.parser.api.YangContextParser;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 
-import com.google.common.collect.Lists;
-
-@Ignore
 public class YangParserWithContextTest {
     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
     private final YangParserImpl parser = new YangParserImpl();
 
     @Test
     public void testTypeFromContext() throws Exception {
-        SchemaContext context = null;
-        String resource = "/types/ietf-inet-types@2010-09-24.yang";
-        InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
-        context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
+        String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
+        InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
+        SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
         stream.close();
 
-        Module module = null;
         resource = "/context-test/test1.yang";
-        InputStream stream2 = new FileInputStream(getClass().getResource(resource).getPath());
-        module = TestUtils.loadModuleWithContext(stream2, context);
+        InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
+        Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
         stream2.close();
         assertNotNull(module);
 
@@ -71,35 +73,34 @@ public class YangParserWithContextTest {
         QName qname = leafType.getQName();
         assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
         assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
-        assertEquals("t1", qname.getPrefix());
         assertEquals("port-number", qname.getLocalName());
 
         ExtendedType leafBaseType = (ExtendedType) 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("inet", qname.getPrefix());
         assertEquals("port-number", qname.getLocalName());
 
         ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
         List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
         assertEquals(1, ranges.size());
         RangeConstraint range = ranges.get(0);
-        assertEquals(0L, range.getMin());
-        assertEquals(63L, range.getMax());
+        assertEquals(BigInteger.ZERO, range.getMin());
+        assertEquals(BigInteger.valueOf(63), range.getMax());
     }
 
     @Test
     public void testUsesFromContext() throws Exception {
-        SchemaContext context = null;
-        try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
-                InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
-                InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
+        SchemaContext context;
+        try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
+                InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
+                InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
         }
-        Module testModule = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
-            testModule = TestUtils.loadModuleWithContext(stream, context);
+        Module testModule;
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
+                .toURI()))) {
+            testModule = TestUtils.loadModuleWithContext("test2", stream, context);
         }
         assertNotNull(testModule);
 
@@ -107,7 +108,7 @@ public class YangParserWithContextTest {
         // suffix _g = defined in grouping from context
 
         // get grouping
-        Module contextModule = context.findModuleByNamespace(URI.create("urn:custom.nodes.test")).iterator().next();
+        Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
         assertNotNull(contextModule);
         Set<GroupingDefinition> groupings = contextModule.getGroupings();
         assertEquals(1, groupings.size());
@@ -194,15 +195,16 @@ public class YangParserWithContextTest {
 
     @Test
     public void testUsesRefineFromContext() throws Exception {
-        SchemaContext context = null;
-        try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath());
-                InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath());
-                InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) {
+        SchemaContext context;
+        try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
+                InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
+                InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()))) {
             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3)));
         }
-        Module module = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
-            module = TestUtils.loadModuleWithContext(stream, context);
+        Module module;
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
+                .toURI()))) {
+            module = TestUtils.loadModuleWithContext("test2", stream, context);
         }
         assertNotNull(module);
 
@@ -213,11 +215,11 @@ public class YangParserWithContextTest {
         UsesNode usesNode = usesNodes.iterator().next();
 
         // test grouping path
-        List<QName> path = new ArrayList<QName>();
-        QName qname = new QName(URI.create("urn:custom.nodes.test"), simpleDateFormat.parse("2013-02-27"), "c",
+        List<QName> path = new ArrayList<>();
+        QName qname = new QName(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "baz",
                 "target");
         path.add(qname);
-        SchemaPath expectedPath = new SchemaPath(path, true);
+        SchemaPath expectedPath = SchemaPath.create(path, true);
         assertEquals(expectedPath, usesNode.getGroupingPath());
 
         // test refine
@@ -270,14 +272,16 @@ public class YangParserWithContextTest {
 
     @Test
     public void testIdentity() throws Exception {
-        SchemaContext context = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
-                .getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
-        Module module = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
-            module = TestUtils.loadModuleWithContext(stream, context);
+        SchemaContext context;
+        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
+        File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
+        YangContextParser parser = new YangParserImpl();
+        context = parser.parseFile(yangFile, dependenciesDir);
+
+        Module module;
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
+                .toURI()))) {
+            module = TestUtils.loadModuleWithContext("test3", stream, context);
         }
         assertNotNull(module);
 
@@ -288,28 +292,27 @@ public class YangParserWithContextTest {
         QName idQName = identity.getQName();
         assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
         assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
-        assertEquals("t3", idQName.getPrefix());
         assertEquals("pt", idQName.getLocalName());
 
         IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
         QName idBaseQName = baseIdentity.getQName();
         assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
         assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
-        assertEquals("iit", idBaseQName.getPrefix());
         assertEquals("service-type", idBaseQName.getLocalName());
     }
 
     @Test
     public void testUnknownNodes() throws Exception {
-        SchemaContext context = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
-                .getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
-
-        Module module = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
-            module = TestUtils.loadModuleWithContext(stream, context);
+        SchemaContext context;
+        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
+        File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
+        YangContextParser parser = new YangParserImpl();
+        context = parser.parseFile(yangFile, dependenciesDir);
+
+        Module module;
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
+                .toURI()))) {
+            module = TestUtils.loadModuleWithContext("test3", stream, context);
         }
 
         ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
@@ -320,7 +323,6 @@ public class YangParserWithContextTest {
         QName unType = un.getNodeType();
         assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
         assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
-        assertEquals("custom", unType.getPrefix());
         assertEquals("mountpoint", unType.getLocalName());
         assertEquals("point", un.getNodeParameter());
         assertNotNull(un.getExtensionDefinition());
@@ -329,31 +331,21 @@ public class YangParserWithContextTest {
     @Test
     public void testAugment() throws Exception {
         // load first module
-        SchemaContext context = null;
         String resource = "/context-augment-test/test4.yang";
-
-        try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
-
-        Set<Module> contextModules = context.getModules();
-        Module t3 = TestUtils.findModule(contextModules, "test4");
-        ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
-        ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
+        SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
+                .toURI())));
 
         // load another modules and parse them against already existing context
-        Set<Module> modules = null;
-        try (InputStream stream1 = new FileInputStream(getClass().getResource("/context-augment-test/test1.yang")
-                .getPath());
-                InputStream stream2 = new FileInputStream(getClass().getResource("/context-augment-test/test2.yang")
-                        .getPath());
-                InputStream stream3 = new FileInputStream(getClass().getResource("/context-augment-test/test3.yang")
-                        .getPath())) {
-            List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
-            modules = TestUtils.loadModulesWithContext(input, context);
-        }
+        File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
+        File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
+        File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
+        Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
         assertNotNull(modules);
 
+        Module t4 = TestUtils.findModule(modules, "test4");
+        ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
+        ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
+
         // test augmentation process
         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
         assertNotNull(augmentHolder);
@@ -375,17 +367,17 @@ public class YangParserWithContextTest {
     @Test
     public void testDeviation() throws Exception {
         // load first module
-        SchemaContext context = null;
-        String resource = "/model/types.yang";
+        SchemaContext context;
+        String resource = "/model/bar.yang";
 
-        try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
             context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
         }
 
         // load another modules and parse them against already existing context
-        Set<Module> modules = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/deviation-test.yang")
-                .getPath())) {
+        Set<Module> modules;
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource(
+                "/context-test/deviation-test.yang").toURI()))) {
             List<InputStream> input = Lists.newArrayList(stream);
             modules = TestUtils.loadModulesWithContext(input, context);
         }
@@ -399,13 +391,13 @@ public class YangParserWithContextTest {
 
         assertEquals("system/user ref", dev.getReference());
 
-        URI expectedNS = URI.create("urn:simple.types.test");
+        URI expectedNS = URI.create("urn:opendaylight.bar");
         DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
         Date expectedRev = simpleDateFormat.parse("2013-07-03");
-        List<QName> path = new ArrayList<QName>();
-        path.add(new QName(expectedNS, expectedRev, "t", "interfaces"));
-        path.add(new QName(expectedNS, expectedRev, "t", "ifEntry"));
-        SchemaPath expectedPath = new SchemaPath(path, true);
+        List<QName> path = new ArrayList<>();
+        path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
+        path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
+        SchemaPath expectedPath = SchemaPath.create(path, true);
 
         assertEquals(expectedPath, dev.getTargetPath());
         assertEquals(Deviate.ADD, dev.getDeviate());