BUG 1131: untangling package cyclic dependencies in yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserTest.java
index 0d0ddb47bbbedbbda9c4b8d10582db6f2d2a2496..ab26254433c5f249f51188c29da291e5671d1581 100644 (file)
@@ -7,21 +7,34 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.net.URI;
+import java.text.DateFormat;
 import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
@@ -36,9 +49,11 @@ import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 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.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
@@ -46,7 +61,7 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 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.Decimal64;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 import org.opendaylight.yangtools.yang.model.util.Int16;
@@ -54,8 +69,11 @@ import org.opendaylight.yangtools.yang.model.util.Int32;
 import org.opendaylight.yangtools.yang.model.util.StringType;
 import org.opendaylight.yangtools.yang.model.util.Uint32;
 import org.opendaylight.yangtools.yang.model.util.UnionType;
+import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
 
 public class YangParserTest {
+    public static final String FS = File.separator;
+
     private final URI fooNS = URI.create("urn:opendaylight.foo");
     private final URI barNS = URI.create("urn:opendaylight.bar");
     private final URI bazNS = URI.create("urn:opendaylight.baz");
@@ -66,12 +84,13 @@ public class YangParserTest {
     private Set<Module> modules;
 
     @Before
-    public void init() throws FileNotFoundException, ParseException {
-        fooRev = TestUtils.simpleDateFormat.parse("2013-02-27");
-        barRev = TestUtils.simpleDateFormat.parse("2013-07-03");
-        bazRev = TestUtils.simpleDateFormat.parse("2013-02-27");
+    public void init() throws Exception {
+        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        fooRev = simpleDateFormat.parse("2013-02-27");
+        barRev = simpleDateFormat.parse("2013-07-03");
+        bazRev = simpleDateFormat.parse("2013-02-27");
 
-        modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
+        modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
         assertEquals(3, modules.size());
     }
 
@@ -222,6 +241,7 @@ public class YangParserTest {
         assertEquals(0, ifEntry.getUses().size());
 
         LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
+        assertEquals(ifEntry.getKeyDefinition().get(0), ifIndex.getQName());
         assertTrue(ifIndex.getType() instanceof Uint32);
         LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
         assertTrue(ifMtu.getType() instanceof Int32);
@@ -673,7 +693,7 @@ public class YangParserTest {
         List<QName> path = new ArrayList<>();
         path.add(new QName(barNS, barRev, "br", "interfaces"));
         path.add(new QName(barNS, barRev, "br", "ifEntry"));
-        SchemaPath expectedPath = new SchemaPath(path, true);
+        SchemaPath expectedPath = SchemaPath.create(path, true);
 
         assertEquals(expectedPath, dev.getTargetPath());
         assertEquals(Deviate.ADD, dev.getDeviate());
@@ -733,8 +753,6 @@ public class YangParserTest {
 
         LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
         assertTrue(eventClass.getType() instanceof StringType);
-        AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
-        assertNotNull(reportingEntity);
         LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
         assertTrue(severity.getType() instanceof StringType);
     }
@@ -749,12 +767,6 @@ public class YangParserTest {
         RpcDefinition rpc = rpcs.iterator().next();
         assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
         assertEquals("RFC 6241, Section 7.1", rpc.getReference());
-
-        ContainerSchemaNode input = rpc.getInput();
-        assertNotNull(input.getDataChildByName("source"));
-        assertNotNull(input.getDataChildByName("filter"));
-        ContainerSchemaNode output = rpc.getOutput();
-        assertNotNull(output.getDataChildByName("data"));
     }
 
     @Test
@@ -772,9 +784,11 @@ public class YangParserTest {
         assertEquals("int32-ext1", int32TypedefQName.getLocalName());
 
         SchemaPath typeSchemaPath = int32ext1.getPath();
-        List<QName> typePath = typeSchemaPath.getPath();
-        assertEquals(1, typePath.size());
-        assertEquals(int32TypedefQName, typePath.get(0));
+        Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+        Iterator<QName> typePathIt = typePath.iterator();
+        assertEquals(int32TypedefQName, typePathIt.next());
+        assertFalse(typePathIt.hasNext());
+
 
         // int32-ext1/int32
         Int32 int32 = (Int32) int32ext1.getBaseType();
@@ -796,9 +810,10 @@ public class YangParserTest {
         assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
 
         SchemaPath typeSchemaPath = myDecType.getPath();
-        List<QName> typePath = typeSchemaPath.getPath();
-        assertEquals(1, typePath.size());
-        assertEquals(myDecTypeQName, typePath.get(0));
+        Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+        Iterator<QName> typePathIt = typePath.iterator();
+        assertEquals(myDecTypeQName, typePathIt.next());
+        assertFalse(typePathIt.hasNext());
 
         // my-base-int32-type/int32
         Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
@@ -810,28 +825,148 @@ public class YangParserTest {
         assertEquals("decimal64", dec64QName.getLocalName());
 
         SchemaPath dec64SchemaPath = dec64.getPath();
-        List<QName> dec64Path = dec64SchemaPath.getPath();
-        assertEquals(2, dec64Path.size());
-        assertEquals(myDecTypeQName, dec64Path.get(0));
-        assertEquals(dec64QName, dec64Path.get(1));
+        Iterable<QName> dec64Path = dec64SchemaPath.getPathFromRoot();
+        Iterator<QName> dec64PathIt = dec64Path.iterator();
+        assertEquals(myDecTypeQName, dec64PathIt.next());
+        assertEquals(dec64QName, dec64PathIt.next());
+        assertFalse(dec64PathIt.hasNext());
     }
 
     @Test
-    public void testParseMethod1() throws ParseException {
-        File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
-        File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
-        YangModelParser parser = new YangParserImpl();
-        modules = parser.parseYangModels(yangFile, dependenciesDir);
+    public void testParseMethod1() throws Exception {
+        File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+        File dependenciesDir = new File(getClass().getResource("/parse-methods").toURI());
+        YangContextParser parser = new YangParserImpl();
+        modules = parser.parseFile(yangFile, dependenciesDir).getModules();
         assertEquals(6, modules.size());
     }
 
     @Test
-    public void testParseMethod2() throws ParseException {
-        File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
-        File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
-        YangModelParser parser = new YangParserImpl();
-        modules = parser.parseYangModels(yangFile, dependenciesDir);
+    public void testParseMethod2() throws Exception {
+        File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+        File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").toURI());
+        YangContextParser parser = new YangParserImpl();
+        modules = parser.parseFile(yangFile, dependenciesDir).getModules();
         assertEquals(6, modules.size());
     }
 
+    @Test
+    public void testSorting() throws Exception {
+        // Correct order: m2, m4, m6, m8, m7, m6, m3, m1
+        File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").toURI());
+        File dependenciesDir = new File(getClass().getResource("/sorting-test").toURI());
+        YangContextParser parser = new YangParserImpl();
+        modules = parser.parseFile(yangFile, dependenciesDir).getModules();
+        SchemaContext ctx = new SchemaContextImpl(modules, Collections.<ModuleIdentifier, String>emptyMap());
+        checkOrder(modules);
+        assertSetEquals(modules, ctx.getModules());
+
+        // ##########
+        parser = new YangParserImpl();
+        final File testDir = dependenciesDir;
+        final String[] fileList = testDir.list();
+        final List<File> testFiles = new ArrayList<>();
+        if (fileList == null) {
+            throw new FileNotFoundException(dependenciesDir.getAbsolutePath());
+        }
+        for (String fileName : fileList) {
+            testFiles.add(new File(testDir, fileName));
+        }
+        Set<Module> newModules = parser.parseFiles(testFiles).getModules();
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+        // ##########
+        newModules = parser.parseFiles(testFiles, null).getModules();
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+        // ##########
+        List<InputStream> streams = new ArrayList<>();
+        for (File f : testFiles) {
+            streams.add(new FileInputStream(f));
+        }
+        newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules();
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+        // ##########
+        streams.clear();
+        for (File f : testFiles) {
+            streams.add(new FileInputStream(f));
+        }
+        newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules();
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+        // ##########
+        Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
+        newModules = new LinkedHashSet<>(mapped.values());
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+        // ##########
+        streams.clear();
+        for (File f : testFiles) {
+            streams.add(new FileInputStream(f));
+        }
+        Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
+        newModules = new LinkedHashSet<>(mappedStreams.values());
+        assertSetEquals(newModules, modules);
+        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+        assertSetEquals(newModules, ctx.getModules());
+    }
+
+    private void checkOrder(final Collection<Module> modules) {
+        Iterator<Module> it = modules.iterator();
+        Module m = it.next();
+        assertEquals("m2", m.getName());
+        m = it.next();
+        assertEquals("m4", m.getName());
+        m = it.next();
+        assertEquals("m6", m.getName());
+        m = it.next();
+        assertEquals("m8", m.getName());
+        m = it.next();
+        assertEquals("m7", m.getName());
+        m = it.next();
+        assertEquals("m5", m.getName());
+        m = it.next();
+        assertEquals("m3", m.getName());
+        m = it.next();
+        assertEquals("m1", m.getName());
+    }
+
+    private void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
+        assertEquals(s1, s2);
+        Iterator<Module> it = s1.iterator();
+        for (Module m : s2) {
+            assertEquals(m, it.next());
+        }
+    }
+
+    @Test
+    public void testSubmodules() throws Exception {
+        URI yangFilePath = getClass().getResource("/submodule-test/subfoo.yang").toURI();
+        URI directoryPath = getClass().getResource("/model").toURI();
+
+        File directory = new File(directoryPath);
+        File yangFile = new File(yangFilePath);
+
+        Set<Module> modules = new YangParserImpl().parseFile(yangFile, directory).getModules();
+        assertEquals(3, modules.size());
+
+        Module foo = TestUtils.findModule(modules, "foo");
+
+        DataSchemaNode id = foo.getDataChildByName("id");
+        assertNotNull(id);
+        DataSchemaNode subExt = foo.getDataChildByName("sub-ext");
+        assertNotNull(subExt);
+        DataSchemaNode subTransfer = foo.getDataChildByName("sub-transfer");
+        assertNotNull(subTransfer);
+
+        assertEquals(2, foo.getExtensionSchemaNodes().size());
+        assertEquals(2, foo.getAugmentations().size());
+    }
+
 }