Added more tests. Fixed javadocs.
[yangtools.git] / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserSimpleTest.java
index 8a0863936dae4f2980ff347b0994bd75f90f8da6..484b18bc388f7c93f6695cb38db5009344ab47e6 100644 (file)
@@ -21,15 +21,25 @@ 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.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+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.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.UsesNode;
 
 public class YangParserSimpleTest {
 
     private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
     private Date snRev;
+    private final String snPref = "sn";
 
     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
     private Set<Module> modules;
@@ -41,7 +51,7 @@ public class YangParserSimpleTest {
     }
 
     @Test
-    public void testAnyXml() {
+    public void testParseAnyXml() {
         Module testModule = TestUtils.findModule(modules, "simple-nodes");
         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
         assertNotNull("'anyxml data not found'", data);
@@ -49,7 +59,7 @@ public class YangParserSimpleTest {
         // test SchemaNode args
         QName qname = data.getQName();
         assertEquals("data", qname.getLocalName());
-        assertEquals("sn", qname.getPrefix());
+        assertEquals(snPref, qname.getPrefix());
         assertEquals(snNS, qname.getNamespace());
         assertEquals(snRev, qname.getRevision());
         assertEquals("anyxml desc", data.getDescription());
@@ -71,9 +81,7 @@ public class YangParserSimpleTest {
 
         boolean found1 = false;
         boolean found2 = false;
-
         for (MustDefinition must : mustConstraints) {
-            System.out.println(must);
             if (must1.equals(must.toString())) {
                 found1 = true;
                 assertEquals(errMsg1, must.getErrorMessage());
@@ -93,4 +101,102 @@ public class YangParserSimpleTest {
         assertNull(constraints.getMaxElements());
     }
 
+    @Test
+    public void testParseContainer() {
+        Module test = TestUtils.findModule(modules, "simple-nodes");
+
+        ContainerSchemaNode nodes = (ContainerSchemaNode) test.getDataChildByName("nodes");
+        // test SchemaNode args
+        QName expectedQName = new QName(snNS, snRev, snPref, "nodes");
+        assertEquals(expectedQName, nodes.getQName());
+        SchemaPath expectedPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes");
+        assertEquals(expectedPath, nodes.getPath());
+        assertEquals("nodes collection", nodes.getDescription());
+        assertEquals("nodes ref", nodes.getReference());
+        assertEquals(Status.CURRENT, nodes.getStatus());
+        assertEquals(0, nodes.getUnknownSchemaNodes().size());
+        // test DataSchemaNode args
+        assertFalse(nodes.isAugmenting());
+        assertFalse(nodes.isConfiguration());
+
+        // constraints
+        ConstraintDefinition constraints = nodes.getConstraints();
+        assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
+        Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
+        assertEquals(2, constraints.getMustConstraints().size());
+
+        String must1 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
+        String errMsg1 = "An atm MTU must be  64 .. 17966";
+        String must2 = "ifId != 0";
+
+        boolean found1 = false;
+        boolean found2 = false;
+        for (MustDefinition must : mustConstraints) {
+            if (must1.equals(must.toString())) {
+                found1 = true;
+                assertEquals(errMsg1, must.getErrorMessage());
+            } else if (must2.equals(must.toString())) {
+                found2 = true;
+                assertNull(must.getErrorMessage());
+                assertNull(must.getErrorAppTag());
+                assertNull(must.getDescription());
+                assertNull(must.getReference());
+            }
+        }
+        assertTrue(found1);
+        assertTrue(found2);
+
+        assertFalse(constraints.isMandatory());
+        assertNull(constraints.getMinElements());
+        assertNull(constraints.getMaxElements());
+        assertTrue(nodes.isPresenceContainer());
+
+        // typedef
+        Set<TypeDefinition<?>> typedefs = nodes.getTypeDefinitions();
+        assertEquals(1, typedefs.size());
+        TypeDefinition<?> nodesType = typedefs.iterator().next();
+        QName typedefQName = new QName(snNS, snRev, snPref, "nodes-type");
+        assertEquals(typedefQName, nodesType.getQName());
+        SchemaPath nodesTypePath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "nodes-type");
+        assertEquals(nodesTypePath, nodesType.getPath());
+        assertNull(nodesType.getDescription());
+        assertNull(nodesType.getReference());
+        assertEquals(Status.CURRENT, nodesType.getStatus());
+        assertEquals(0, nodesType.getUnknownSchemaNodes().size());
+
+        // child nodes
+        // total size = 8: defined 6, inserted by uses 2
+        assertEquals(8, nodes.getChildNodes().size());
+        AnyXmlSchemaNode text = (AnyXmlSchemaNode)nodes.getDataChildByName("text");
+        assertNotNull(text);
+        ChoiceNode level = (ChoiceNode)nodes.getDataChildByName("level");
+        assertNotNull(level);
+        ContainerSchemaNode node = (ContainerSchemaNode)nodes.getDataChildByName("node");
+        assertNotNull(node);
+        LeafSchemaNode nodesId = (LeafSchemaNode)nodes.getDataChildByName("nodes-id");
+        assertNotNull(nodesId);
+        LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added");
+        assertNotNull(added);
+        ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links");
+        assertNotNull(links);
+        assertFalse(links.isUserOrdered());
+        LeafSchemaNode source = (LeafSchemaNode)nodes.getDataChildByName("source");
+        assertNotNull(source);
+        LeafSchemaNode target = (LeafSchemaNode)nodes.getDataChildByName("target");
+        assertNotNull(target);
+
+        Set<GroupingDefinition> groupings = nodes.getGroupings();
+        assertEquals(1, groupings.size());
+        GroupingDefinition nodeGroup = groupings.iterator().next();
+        QName groupQName = new QName(snNS, snRev, snPref, "node-group");
+        assertEquals(groupQName, nodeGroup.getQName());
+        SchemaPath nodeGroupPath = TestUtils.createPath(true, snNS, snRev, snPref, "nodes", "node-group");
+        assertEquals(nodeGroupPath, nodeGroup.getPath());
+
+        Set<UsesNode> uses = nodes.getUses();
+        assertEquals(1, uses.size());
+        UsesNode use = uses.iterator().next();
+        assertEquals(nodeGroupPath, use.getGroupingPath());
+    }
+
 }