Fixed bugs in naming conventions of packages.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / YangModelParserListenerTest.java
index a9bcb918b7ca6ee1ff8396082567899291f453ea..4bd54f3364762af736f51521ed55bddd3f8882e0 100644 (file)
@@ -12,6 +12,8 @@ import static org.junit.Assert.*;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -22,11 +24,9 @@ import org.antlr.v4.runtime.ANTLRInputStream;
 import org.antlr.v4.runtime.CommonTokenStream;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.antlr.v4.runtime.tree.ParseTreeWalker;
-import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
 import org.opendaylight.controller.antlrv4.code.gen.YangParser;
-import org.opendaylight.controller.model.util.UnknownType;
 import org.opendaylight.controller.yang.common.QName;
 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
@@ -40,36 +40,75 @@ import org.opendaylight.controller.yang.model.api.SchemaPath;
 import org.opendaylight.controller.yang.model.api.Status;
 import org.opendaylight.controller.yang.model.api.TypeDefinition;
 import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.controller.yang.model.util.Leafref;
+import org.opendaylight.controller.yang.model.util.UnknownType;
 
 public class YangModelParserListenerTest {
 
-    private final String testFile = "/test-model.yang";
-    ModuleBuilder builder;
-    Module module;
+    @Test
+    public void testParseImport() throws Exception {
+        Module module = getModule("/abstract-topology.yang");
+
+        Set<ModuleImport> imports = module.getImports();
+        assertEquals(1, imports.size());
+        ModuleImport moduleImport = imports.iterator().next();
 
+        assertEquals("inet", moduleImport.getPrefix());
 
-    @Before
-    public void init() throws IOException {
-        builder = getBuilder(testFile);
-        module = builder.build();
+        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
+        Date expectedDate = simpleDateFormat.parse("2010-09-24");
+        assertEquals(expectedDate, moduleImport.getRevision());
     }
 
     @Test
-    public void testParseModule() throws IOException {
-        Set<ModuleImport> imports = module.getImports();
-        assertEquals(3, imports.size());
+    public void testParseHeaders() throws Exception {
+        Module module = getModule("/abstract-topology.yang");
 
         URI namespace = module.getNamespace();
-        URI expectedNS = URI.create("urn:cisco:params:xml:ns:yang:controller:network");
+        URI expectedNS = URI.create("");
         assertEquals(expectedNS, namespace);
 
-        Date revision = module.getRevision();
-        assertNull(revision);
+        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
+        Date expectedDate = simpleDateFormat.parse("2013-02-08");
+        assertEquals(expectedDate, module.getRevision());
 
         String prefix = module.getPrefix();
-        String expectedPrefix = "topos";
+        String expectedPrefix = "tp";
         assertEquals(expectedPrefix, prefix);
 
+        String expectedDescription = "This module contains the definitions of elements that creates network";
+        assertTrue(module.getDescription().contains(expectedDescription));
+
+        String expectedReference = "~~~ WILL BE DEFINED LATER";
+        assertEquals(expectedReference, module.getReference());
+
+        assertEquals("1", module.getYangVersion());
+    }
+
+    @Test
+    public void testParseLeafref() throws Exception {
+        Module module = getModule("/abstract-topology.yang");
+
+        Set<TypeDefinition<?>> typedefs = module.getTypeDefinitions();
+        assertEquals(2, typedefs.size());
+        for(TypeDefinition<?> td : typedefs) {
+            Leafref baseType = (Leafref)td.getBaseType();
+            if(td.getQName().getLocalName().equals("network-node-id-ref")) {
+                assertEquals("/tp:topology/tp:network-nodes/tp:network-node/tp:node-id", baseType.getPathStatement().toString());
+            } else {
+                assertEquals("/tp:topology/tp:network-links/tp:network-link/tp:link-id", baseType.getPathStatement().toString());
+            }
+        }
+    }
+
+    @Test
+    public void testParseModule() throws IOException {
+        Module module = getModule("/test-model.yang");
+
+        URI namespace = module.getNamespace();
+        Date revision = module.getRevision();
+        String prefix = module.getPrefix();
+
         String expectedDescription = "module description";
         assertEquals(expectedDescription, module.getDescription());
 
@@ -93,7 +132,9 @@ public class YangModelParserListenerTest {
     }
 
     @Test
-    public void testParseContainer() {
+    public void testParseContainer() throws IOException {
+        Module module = getModule("/test-model.yang");
+
         URI namespace = module.getNamespace();
         Date revision = module.getRevision();
         String prefix = module.getPrefix();
@@ -120,7 +161,9 @@ public class YangModelParserListenerTest {
     }
 
     @Test
-    public void testParseList() {
+    public void testParseList() throws IOException {
+        Module module = getModule("/test-model.yang");
+
         URI namespace = module.getNamespace();
         Date revision = module.getRevision();
         String prefix = module.getPrefix();
@@ -156,7 +199,9 @@ public class YangModelParserListenerTest {
     }
 
     @Test
-    public void testParseLeaf() {
+    public void testParseLeaf() throws IOException {
+        Module module = getModule("/test-model.yang");
+
         URI namespace = module.getNamespace();
         Date revision = module.getRevision();
         String prefix = module.getPrefix();
@@ -194,6 +239,11 @@ public class YangModelParserListenerTest {
         assertEquals(expectedStatus, tested.getStatus());
     }
 
+    private Module getModule(String testFile) throws IOException {
+        ModuleBuilder builder = getBuilder(testFile);
+        return builder.build();
+    }
+
     private ModuleBuilder getBuilder(String fileName) throws IOException {
         final InputStream inStream = getClass().getResourceAsStream(fileName);
         ANTLRInputStream input = new ANTLRInputStream(inStream);