BUG-1210: refactored imports handling in parser.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserWithContextTest.java
index 28ce35975aedb3afb850400a275600495a7a9baa..16ebf9e4ce91559f1fa68b5c1d80e530f16b7e53 100644 (file)
@@ -12,29 +12,44 @@ 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.*;
+import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.MustDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+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.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 
-import com.google.common.collect.Lists;
-
 public class YangParserWithContextTest {
     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
     private final YangParserImpl parser = new YangParserImpl();
@@ -42,12 +57,12 @@ public class YangParserWithContextTest {
     @Test
     public void testTypeFromContext() throws Exception {
         String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
-        InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
+        InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
         SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
         stream.close();
 
         resource = "/context-test/test1.yang";
-        InputStream stream2 = new FileInputStream(getClass().getResource(resource).getPath());
+        InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
         Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
         stream2.close();
         assertNotNull(module);
@@ -72,20 +87,21 @@ public class YangParserWithContextTest {
         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;
-        try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/baz.yang").getPath());
-                InputStream stream2 = new FileInputStream(getClass().getResource("/model/bar.yang").getPath());
-                InputStream stream3 = new FileInputStream(getClass().getResource("/model/foo.yang").getPath())) {
+        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;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
+                .toURI()))) {
             testModule = TestUtils.loadModuleWithContext("test2", stream, context);
         }
         assertNotNull(testModule);
@@ -182,13 +198,14 @@ public class YangParserWithContextTest {
     @Test
     public void testUsesRefineFromContext() throws Exception {
         SchemaContext context;
-        try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/baz.yang").getPath());
-                InputStream stream2 = new FileInputStream(getClass().getResource("/model/bar.yang").getPath());
-                InputStream stream3 = new FileInputStream(getClass().getResource("/model/foo.yang").getPath())) {
+        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;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
+                .toURI()))) {
             module = TestUtils.loadModuleWithContext("test2", stream, context);
         }
         assertNotNull(module);
@@ -204,7 +221,7 @@ public class YangParserWithContextTest {
         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
@@ -258,13 +275,14 @@ public class YangParserWithContextTest {
     @Test
     public void testIdentity() throws Exception {
         SchemaContext context;
-        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
-        File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
-        YangModelParser parser = new YangParserImpl();
-        context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
+        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(getClass().getResource("/context-test/test3.yang").getPath())) {
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
+                .toURI()))) {
             module = TestUtils.loadModuleWithContext("test3", stream, context);
         }
         assertNotNull(module);
@@ -290,13 +308,14 @@ public class YangParserWithContextTest {
     @Test
     public void testUnknownNodes() throws Exception {
         SchemaContext context;
-        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
-        File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
-        YangModelParser parser = new YangParserImpl();
-        context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
+        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(getClass().getResource("/context-test/test3.yang").getPath())) {
+        try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
+                .toURI()))) {
             module = TestUtils.loadModuleWithContext("test3", stream, context);
         }
 
@@ -314,32 +333,22 @@ public class YangParserWithContextTest {
         assertNotNull(un.getExtensionDefinition());
     }
 
-    @Ignore
     @Test
     public void testAugment() throws Exception {
         // load first module
-        SchemaContext context;
         String resource = "/context-augment-test/test4.yang";
-
-        try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
+        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;
-        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 t3 = TestUtils.findModule(modules, "test4");
-        ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
+        Module t4 = TestUtils.findModule(modules, "test4");
+        ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
 
         // test augmentation process
@@ -366,14 +375,14 @@ public class YangParserWithContextTest {
         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;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/deviation-test.yang")
-                .getPath())) {
+        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);
         }
@@ -393,7 +402,7 @@ public class YangParserWithContextTest {
         List<QName> path = new ArrayList<>();
         path.add(new QName(expectedNS, expectedRev, "bar", "interfaces"));
         path.add(new QName(expectedNS, expectedRev, "bar", "ifEntry"));
-        SchemaPath expectedPath = new SchemaPath(path, true);
+        SchemaPath expectedPath = SchemaPath.create(path, true);
 
         assertEquals(expectedPath, dev.getTargetPath());
         assertEquals(Deviate.ADD, dev.getDeviate());