Merge "Immutable implementation of new yang-data-api"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / GroupingTest.java
index c6c559481eb9b118d727022a79e00399a8e1a78b..0e75a2a144c50d17f0b97702f9b5efafca81286d 100644 (file)
@@ -7,11 +7,18 @@
  */
 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.assertNull;
+import static org.junit.Assert.assertTrue;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.net.URI;
-import java.text.ParseException;
+import java.net.URISyntaxException;
+import java.text.SimpleDateFormat;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -21,6 +28,7 @@ import org.junit.Before;
 import org.junit.Test;
 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;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -33,45 +41,48 @@ 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.parser.api.YangModelParser;
 
 public class GroupingTest {
     private Set<Module> modules;
 
     @Before
-    public void init() throws FileNotFoundException {
-        modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
+    public void init() throws FileNotFoundException, URISyntaxException {
+        modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
         assertEquals(3, modules.size());
     }
 
     @Test
     public void testRefine() {
-        Module testModule = TestUtils.findModule(modules, "nodes");
-
+        Module testModule = TestUtils.findModule(modules, "foo");
         ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
         ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
+
         Set<UsesNode> usesNodes = destination.getUses();
         assertEquals(1, usesNodes.size());
         UsesNode usesNode = usesNodes.iterator().next();
         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
-        assertEquals(3, refines.size());
+        assertEquals(4, refines.size());
 
         LeafSchemaNode refineLeaf = null;
         ContainerSchemaNode refineContainer = null;
         ListSchemaNode refineList = null;
+        LeafSchemaNode refineInnerLeaf = null;
         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
             SchemaNode value = entry.getValue();
-            if (value instanceof LeafSchemaNode) {
+            if ("address".equals(value.getQName().getLocalName())) {
                 refineLeaf = (LeafSchemaNode) value;
-            } else if (value instanceof ContainerSchemaNode) {
+            } else if ("port".equals(value.getQName().getLocalName())) {
                 refineContainer = (ContainerSchemaNode) value;
-            } else if (value instanceof ListSchemaNode) {
+            } else if ("addresses".equals(value.getQName().getLocalName())) {
                 refineList = (ListSchemaNode) value;
+            } else if ("id".equals(value.getQName().getLocalName())) {
+                refineInnerLeaf = (LeafSchemaNode) value;
             }
         }
 
         // leaf address
         assertNotNull(refineLeaf);
-        assertEquals("address", refineLeaf.getQName().getLocalName());
         assertEquals("IP address of target node", refineLeaf.getDescription());
         assertEquals("address reference added by refine", refineLeaf.getReference());
         assertFalse(refineLeaf.isConfiguration());
@@ -80,6 +91,7 @@ public class GroupingTest {
         assertEquals(1, leafMustConstraints.size());
         MustDefinition leafMust = leafMustConstraints.iterator().next();
         assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
+        assertEquals(1, refineLeaf.getUnknownSchemaNodes().size());
 
         // container port
         assertNotNull(refineContainer);
@@ -97,11 +109,15 @@ public class GroupingTest {
         assertFalse(refineList.isConfiguration());
         assertEquals(2, (int) refineList.getConstraints().getMinElements());
         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
+
+        // leaf id
+        assertNotNull(refineInnerLeaf);
+        assertEquals("id of address", refineInnerLeaf.getDescription());
     }
 
     @Test
     public void testGrouping() {
-        Module testModule = TestUtils.findModule(modules, "custom");
+        Module testModule = TestUtils.findModule(modules, "baz");
         Set<GroupingDefinition> groupings = testModule.getGroupings();
         assertEquals(1, groupings.size());
         GroupingDefinition grouping = groupings.iterator().next();
@@ -114,14 +130,14 @@ public class GroupingTest {
         // suffix _u = added by uses
         // suffix _g = defined in grouping
 
-        Module testModule = TestUtils.findModule(modules, "custom");
+        Module testModule = TestUtils.findModule(modules, "baz");
 
         // get grouping
         Set<GroupingDefinition> groupings = testModule.getGroupings();
         assertEquals(1, groupings.size());
         GroupingDefinition grouping = groupings.iterator().next();
 
-        testModule = TestUtils.findModule(modules, "nodes");
+        testModule = TestUtils.findModule(modules, "foo");
 
         // get node containing uses
         ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
@@ -143,11 +159,13 @@ public class GroupingTest {
 
         ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
         assertNotNull(how_u);
-        assertTrue(how_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(how_u, true);
+        assertEquals(2, how_u.getCases().size());
 
         ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
         assertNotNull(how_g);
-        assertFalse(how_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(how_g, false);
+        assertEquals(2, how_g.getCases().size());
         assertFalse(how_u.equals(how_g));
 
         LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
@@ -171,33 +189,33 @@ public class GroupingTest {
 
         ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
         assertNotNull(port_u);
-        assertTrue(port_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(port_u, true);
 
         ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
         assertNotNull(port_g);
-        assertFalse(port_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(port_g, false);
         assertFalse(port_u.equals(port_g));
 
         ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
         assertNotNull(addresses_u);
-        assertTrue(addresses_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(addresses_u, true);
 
         ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
         assertNotNull(addresses_g);
-        assertFalse(addresses_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(addresses_g, false);
         assertFalse(addresses_u.equals(addresses_g));
 
         // grouping defined by 'uses'
         Set<GroupingDefinition> groupings_u = destination.getGroupings();
         assertEquals(1, groupings_u.size());
         GroupingDefinition grouping_u = groupings_u.iterator().next();
-        assertTrue(grouping_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(grouping_u, true);
 
         // grouping defined in 'grouping' node
         Set<GroupingDefinition> groupings_g = grouping.getGroupings();
         assertEquals(1, groupings_g.size());
         GroupingDefinition grouping_g = groupings_g.iterator().next();
-        assertFalse(grouping_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(grouping_g, false);
         assertFalse(grouping_u.equals(grouping_g));
 
         List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
@@ -217,7 +235,7 @@ public class GroupingTest {
         // suffix _u = added by uses
         // suffix _g = defined in grouping
 
-        Module testModule = TestUtils.findModule(modules, "custom");
+        Module testModule = TestUtils.findModule(modules, "baz");
 
         // get grouping
         Set<GroupingDefinition> groupings = testModule.getGroupings();
@@ -225,14 +243,14 @@ public class GroupingTest {
         GroupingDefinition grouping = groupings.iterator().next();
 
         // get node containing uses
-        Module destination = TestUtils.findModule(modules, "nodes");
+        Module foo = TestUtils.findModule(modules, "foo");
 
         // check uses
-        Set<UsesNode> uses = destination.getUses();
+        Set<UsesNode> uses = foo.getUses();
         assertEquals(1, uses.size());
 
         // check uses process
-        AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
+        AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) foo.getDataChildByName("data");
         assertNotNull(data_u);
         assertTrue(data_u.isAddedByUses());
 
@@ -241,16 +259,25 @@ public class GroupingTest {
         assertFalse(data_g.isAddedByUses());
         assertFalse(data_u.equals(data_g));
 
-        ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
+        ChoiceNode how_u = (ChoiceNode) foo.getDataChildByName("how");
         assertNotNull(how_u);
-        assertTrue(how_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(how_u, true);
+        assertFalse(how_u.isAugmenting());
+        Set<ChoiceCaseNode> cases_u = how_u.getCases();
+        assertEquals(2, cases_u.size());
+        ChoiceCaseNode interval = how_u.getCaseNodeByName("interval");
+        assertFalse(interval.isAugmenting());
+        LeafSchemaNode name = (LeafSchemaNode) interval.getDataChildByName("name");
+        assertTrue(name.isAugmenting());
+        LeafSchemaNode intervalLeaf = (LeafSchemaNode) interval.getDataChildByName("interval");
+        assertFalse(intervalLeaf.isAugmenting());
 
         ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
         assertNotNull(how_g);
-        assertFalse(how_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(how_g, false);
         assertFalse(how_u.equals(how_g));
 
-        LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
+        LeafSchemaNode address_u = (LeafSchemaNode) foo.getDataChildByName("address");
         assertNotNull(address_u);
         assertNull(address_u.getDefault());
         assertEquals("Target IP address", address_u.getDescription());
@@ -267,38 +294,38 @@ public class GroupingTest {
         assertTrue(address_g.isConfiguration());
         assertFalse(address_u.equals(address_g));
 
-        ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
+        ContainerSchemaNode port_u = (ContainerSchemaNode) foo.getDataChildByName("port");
         assertNotNull(port_u);
-        assertTrue(port_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(port_u, true);
 
         ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
         assertNotNull(port_g);
-        assertFalse(port_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(port_g, false);
         assertFalse(port_u.equals(port_g));
 
-        ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
+        ListSchemaNode addresses_u = (ListSchemaNode) foo.getDataChildByName("addresses");
         assertNotNull(addresses_u);
-        assertTrue(addresses_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(addresses_u, true);
 
         ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
         assertNotNull(addresses_g);
-        assertFalse(addresses_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(addresses_g, false);
         assertFalse(addresses_u.equals(addresses_g));
 
         // grouping defined by 'uses'
-        Set<GroupingDefinition> groupings_u = destination.getGroupings();
+        Set<GroupingDefinition> groupings_u = foo.getGroupings();
         assertEquals(1, groupings_u.size());
         GroupingDefinition grouping_u = groupings_u.iterator().next();
-        assertTrue(grouping_u.isAddedByUses());
+        TestUtils.checkIsAddedByUses(grouping_u, true);
 
         // grouping defined in 'grouping' node
         Set<GroupingDefinition> groupings_g = grouping.getGroupings();
         assertEquals(1, groupings_g.size());
         GroupingDefinition grouping_g = groupings_g.iterator().next();
-        assertFalse(grouping_g.isAddedByUses());
+        TestUtils.checkIsAddedByUses(grouping_g, false);
         assertFalse(grouping_u.equals(grouping_g));
 
-        List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
+        List<UnknownSchemaNode> nodes_u = foo.getUnknownSchemaNodes();
         assertEquals(1, nodes_u.size());
         UnknownSchemaNode node_u = nodes_u.get(0);
         assertTrue(node_u.isAddedByUses());
@@ -322,8 +349,12 @@ public class GroupingTest {
     }
 
     @Test
-    public void testCascadeUses() throws FileNotFoundException, ParseException {
-        modules = TestUtils.loadModules(getClass().getResource("/grouping-test").getPath());
+    public void testCascadeUses() throws Exception {
+        File yangFile = new File(getClass().getResource("/grouping-test/cascade-uses.yang").toURI());
+        YangModelParser parser = new YangParserImpl();
+        modules = parser.parseYangModels(Collections.singletonList(yangFile));
+        assertEquals(1, modules.size());
+
         Module testModule = TestUtils.findModule(modules, "cascade-uses");
         Set<GroupingDefinition> groupings = testModule.getGroupings();
 
@@ -334,18 +365,27 @@ public class GroupingTest {
         GroupingDefinition gz = null;
         GroupingDefinition gzz = null;
         for (GroupingDefinition gd : groupings) {
-            if ("grouping-U".equals(gd.getQName().getLocalName()))
+            String name = gd.getQName().getLocalName();
+            switch (name) {
+            case "grouping-U":
                 gu = gd;
-            if ("grouping-V".equals(gd.getQName().getLocalName()))
+                break;
+            case "grouping-V":
                 gv = gd;
-            if ("grouping-X".equals(gd.getQName().getLocalName()))
+                break;
+            case "grouping-X":
                 gx = gd;
-            if ("grouping-Y".equals(gd.getQName().getLocalName()))
+                break;
+            case "grouping-Y":
                 gy = gd;
-            if ("grouping-Z".equals(gd.getQName().getLocalName()))
+                break;
+            case "grouping-Z":
                 gz = gd;
-            if ("grouping-ZZ".equals(gd.getQName().getLocalName()))
+                break;
+            case "grouping-ZZ":
                 gzz = gd;
+                break;
+            }
         }
         assertNotNull(gu);
         assertNotNull(gv);
@@ -354,44 +394,122 @@ public class GroupingTest {
         assertNotNull(gz);
         assertNotNull(gzz);
 
-        assertEquals(7, gu.getChildNodes().size());
-        assertEquals(4, gv.getChildNodes().size());
-        assertEquals(2, gx.getChildNodes().size());
-        assertEquals(1, gy.getChildNodes().size());
-        assertEquals(1, gz.getChildNodes().size());
-        assertEquals(1, gzz.getChildNodes().size());
-
         URI expectedNS = URI.create("urn:grouping:cascade-uses");
-        Date expectedRev = TestUtils.simpleDateFormat.parse("2013-07-18");
+        Date expectedRev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-18");
         String expectedPref = "cu";
         SchemaPath expectedPath;
 
+        // grouping-U
+        Set<DataSchemaNode> childNodes = gu.getChildNodes();
+        assertEquals(7, childNodes.size());
+        LeafSchemaNode leafGroupingU = null;
+        for (DataSchemaNode childNode : childNodes) {
+            if ("leaf-grouping-U".equals(childNode.getQName().getLocalName())) {
+                leafGroupingU = (LeafSchemaNode) childNode;
+            } else {
+                TestUtils.checkIsAddedByUses(childNode, true);
+            }
+        }
+        assertNotNull(leafGroupingU);
+        assertFalse(leafGroupingU.isAddedByUses());
+
+        // grouping-V
+        childNodes = gv.getChildNodes();
+        assertEquals(4, childNodes.size());
+        LeafSchemaNode leafGroupingV = null;
+        ContainerSchemaNode containerGroupingV = null;
+        for (DataSchemaNode childNode : childNodes) {
+            if ("leaf-grouping-V".equals(childNode.getQName().getLocalName())) {
+                leafGroupingV = (LeafSchemaNode) childNode;
+            } else if ("container-grouping-V".equals(childNode.getQName().getLocalName())) {
+                containerGroupingV = (ContainerSchemaNode) childNode;
+            } else {
+                TestUtils.checkIsAddedByUses(childNode, true);
+            }
+        }
+        assertNotNull(leafGroupingV);
+        assertFalse(leafGroupingV.isAddedByUses());
+
         // grouping-V/container-grouping-V
-        ContainerSchemaNode containerV = (ContainerSchemaNode)gv.getDataChildByName("container-grouping-V");
-        assertNotNull(containerV);
-        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V", "container-grouping-V");
-        assertEquals(2, containerV.getChildNodes().size());
+        assertNotNull(containerGroupingV);
+        assertFalse(containerGroupingV.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V",
+                "container-grouping-V");
+        assertEquals(expectedPath, containerGroupingV.getPath());
+        childNodes = containerGroupingV.getChildNodes();
+        assertEquals(2, childNodes.size());
+        for (DataSchemaNode childNode : childNodes) {
+            TestUtils.checkIsAddedByUses(childNode, true);
+        }
+
         // grouping-V/container-grouping-V/leaf-grouping-X
-        LeafSchemaNode leafXinContainerV = (LeafSchemaNode)containerV.getDataChildByName("leaf-grouping-X");
+        LeafSchemaNode leafXinContainerV = (LeafSchemaNode) containerGroupingV.getDataChildByName("leaf-grouping-X");
         assertNotNull(leafXinContainerV);
-        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V", "container-grouping-V", "leaf-grouping-X");
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V",
+                "container-grouping-V", "leaf-grouping-X");
         assertEquals(expectedPath, leafXinContainerV.getPath());
         // grouping-V/container-grouping-V/leaf-grouping-Y
-        LeafSchemaNode leafYinContainerV = (LeafSchemaNode)containerV.getDataChildByName("leaf-grouping-Y");
+        LeafSchemaNode leafYinContainerV = (LeafSchemaNode) containerGroupingV.getDataChildByName("leaf-grouping-Y");
         assertNotNull(leafYinContainerV);
-        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V", "container-grouping-V", "leaf-grouping-Y");
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-V",
+                "container-grouping-V", "leaf-grouping-Y");
         assertEquals(expectedPath, leafYinContainerV.getPath());
 
+        // grouping-X
+        childNodes = gx.getChildNodes();
+        assertEquals(2, childNodes.size());
+
         // grouping-X/leaf-grouping-X
-        LeafSchemaNode leafXinGX = (LeafSchemaNode)gx.getDataChildByName("leaf-grouping-X");
+        LeafSchemaNode leafXinGX = (LeafSchemaNode) gx.getDataChildByName("leaf-grouping-X");
         assertNotNull(leafXinGX);
-        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-X", "leaf-grouping-X");
+        assertFalse(leafXinGX.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-X",
+                "leaf-grouping-X");
         assertEquals(expectedPath, leafXinGX.getPath());
+
         // grouping-X/leaf-grouping-Y
-        LeafSchemaNode leafYinGY = (LeafSchemaNode)gx.getDataChildByName("leaf-grouping-Y");
+        LeafSchemaNode leafYinGX = (LeafSchemaNode) gx.getDataChildByName("leaf-grouping-Y");
+        assertNotNull(leafYinGX);
+        assertTrue(leafYinGX.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-X",
+                "leaf-grouping-Y");
+        assertEquals(expectedPath, leafYinGX.getPath());
+
+        // grouping-Y
+        childNodes = gy.getChildNodes();
+        assertEquals(1, childNodes.size());
+
+        // grouping-Y/leaf-grouping-Y
+        LeafSchemaNode leafYinGY = (LeafSchemaNode) gy.getDataChildByName("leaf-grouping-Y");
         assertNotNull(leafYinGY);
-        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-X", "leaf-grouping-Y");
+        assertFalse(leafYinGY.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-Y",
+                "leaf-grouping-Y");
         assertEquals(expectedPath, leafYinGY.getPath());
+
+        // grouping-Z
+        childNodes = gz.getChildNodes();
+        assertEquals(1, childNodes.size());
+
+        // grouping-Z/leaf-grouping-Z
+        LeafSchemaNode leafZinGZ = (LeafSchemaNode) gz.getDataChildByName("leaf-grouping-Z");
+        assertNotNull(leafZinGZ);
+        assertFalse(leafZinGZ.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-Z",
+                "leaf-grouping-Z");
+        assertEquals(expectedPath, leafZinGZ.getPath());
+
+        // grouping-ZZ
+        childNodes = gzz.getChildNodes();
+        assertEquals(1, childNodes.size());
+
+        // grouping-ZZ/leaf-grouping-ZZ
+        LeafSchemaNode leafZZinGZZ = (LeafSchemaNode) gzz.getDataChildByName("leaf-grouping-ZZ");
+        assertNotNull(leafZZinGZZ);
+        assertFalse(leafZZinGZZ.isAddedByUses());
+        expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-ZZ",
+                "leaf-grouping-ZZ");
+        assertEquals(expectedPath, leafZZinGZZ.getPath());
     }
 
 }