Degrade DataNodeContainer.getChildNodes() from Set to Collection
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / GroupingTest.java
index 0e75a2a144c50d17f0b97702f9b5efafca81286d..03dc0663de7ba5910a5f54bc294e1d09afe55cfe 100644 (file)
@@ -14,10 +14,11 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.text.SimpleDateFormat;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
@@ -41,13 +42,14 @@ 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;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
+import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
 
 public class GroupingTest {
     private Set<Module> modules;
 
     @Before
-    public void init() throws FileNotFoundException, URISyntaxException {
+    public void init() throws IOException, URISyntaxException {
         modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
         assertEquals(3, modules.size());
     }
@@ -121,7 +123,7 @@ public class GroupingTest {
         Set<GroupingDefinition> groupings = testModule.getGroupings();
         assertEquals(1, groupings.size());
         GroupingDefinition grouping = groupings.iterator().next();
-        Set<DataSchemaNode> children = grouping.getChildNodes();
+        Collection<DataSchemaNode> children = grouping.getChildNodes();
         assertEquals(5, children.size());
     }
 
@@ -156,6 +158,7 @@ public class GroupingTest {
         assertNotNull(data_g);
         assertFalse(data_g.isAddedByUses());
         assertFalse(data_u.equals(data_g));
+        assertEquals(data_g,  SchemaNodeUtils.getRootOriginalIfPossible(data_u));
 
         ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how");
         assertNotNull(how_u);
@@ -167,6 +170,7 @@ public class GroupingTest {
         TestUtils.checkIsAddedByUses(how_g, false);
         assertEquals(2, how_g.getCases().size());
         assertFalse(how_u.equals(how_g));
+        assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u));
 
         LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
         assertNotNull(address_u);
@@ -186,6 +190,7 @@ public class GroupingTest {
         assertTrue(address_g.isConfiguration());
         assertFalse(address_u.equals(address_g));
         assertTrue(address_g.getConstraints().isMandatory());
+        assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u));
 
         ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
         assertNotNull(port_u);
@@ -195,6 +200,7 @@ public class GroupingTest {
         assertNotNull(port_g);
         TestUtils.checkIsAddedByUses(port_g, false);
         assertFalse(port_u.equals(port_g));
+        assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u));
 
         ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
         assertNotNull(addresses_u);
@@ -204,6 +210,7 @@ public class GroupingTest {
         assertNotNull(addresses_g);
         TestUtils.checkIsAddedByUses(addresses_g, false);
         assertFalse(addresses_u.equals(addresses_g));
+        assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u));
 
         // grouping defined by 'uses'
         Set<GroupingDefinition> groupings_u = destination.getGroupings();
@@ -258,6 +265,7 @@ public class GroupingTest {
         assertNotNull(data_g);
         assertFalse(data_g.isAddedByUses());
         assertFalse(data_u.equals(data_g));
+        assertEquals(data_g, SchemaNodeUtils.getRootOriginalIfPossible(data_u));
 
         ChoiceNode how_u = (ChoiceNode) foo.getDataChildByName("how");
         assertNotNull(how_u);
@@ -276,6 +284,7 @@ public class GroupingTest {
         assertNotNull(how_g);
         TestUtils.checkIsAddedByUses(how_g, false);
         assertFalse(how_u.equals(how_g));
+        assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u));
 
         LeafSchemaNode address_u = (LeafSchemaNode) foo.getDataChildByName("address");
         assertNotNull(address_u);
@@ -293,6 +302,7 @@ public class GroupingTest {
         assertNull(address_g.getReference());
         assertTrue(address_g.isConfiguration());
         assertFalse(address_u.equals(address_g));
+        assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u));
 
         ContainerSchemaNode port_u = (ContainerSchemaNode) foo.getDataChildByName("port");
         assertNotNull(port_u);
@@ -302,6 +312,7 @@ public class GroupingTest {
         assertNotNull(port_g);
         TestUtils.checkIsAddedByUses(port_g, false);
         assertFalse(port_u.equals(port_g));
+        assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u));
 
         ListSchemaNode addresses_u = (ListSchemaNode) foo.getDataChildByName("addresses");
         assertNotNull(addresses_u);
@@ -311,6 +322,7 @@ public class GroupingTest {
         assertNotNull(addresses_g);
         TestUtils.checkIsAddedByUses(addresses_g, false);
         assertFalse(addresses_u.equals(addresses_g));
+        assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u));
 
         // grouping defined by 'uses'
         Set<GroupingDefinition> groupings_u = foo.getGroupings();
@@ -341,7 +353,7 @@ public class GroupingTest {
         assertEquals(1, usesAugments.size());
         AugmentationSchema augment = usesAugments.iterator().next();
         assertEquals("inner augment", augment.getDescription());
-        Set<DataSchemaNode> children = augment.getChildNodes();
+        Collection<DataSchemaNode> children = augment.getChildNodes();
         assertEquals(1, children.size());
         DataSchemaNode leaf = children.iterator().next();
         assertTrue(leaf instanceof LeafSchemaNode);
@@ -351,8 +363,8 @@ public class GroupingTest {
     @Test
     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));
+        YangContextParser parser = new YangParserImpl();
+        modules = parser.parseFiles(Collections.singleton(yangFile)).getModules();
         assertEquals(1, modules.size());
 
         Module testModule = TestUtils.findModule(modules, "cascade-uses");
@@ -400,18 +412,19 @@ public class GroupingTest {
         SchemaPath expectedPath;
 
         // grouping-U
-        Set<DataSchemaNode> childNodes = gu.getChildNodes();
+        Collection<DataSchemaNode> childNodes = gu.getChildNodes();
         assertEquals(7, childNodes.size());
-        LeafSchemaNode leafGroupingU = null;
+
+        LeafSchemaNode leafGroupingU = (LeafSchemaNode) gu.getDataChildByName("leaf-grouping-U");
+        assertNotNull(leafGroupingU);
+        assertFalse(leafGroupingU.isAddedByUses());
+        assertFalse(SchemaNodeUtils.getOriginalIfPossible(leafGroupingU).isPresent());
+
         for (DataSchemaNode childNode : childNodes) {
-            if ("leaf-grouping-U".equals(childNode.getQName().getLocalName())) {
-                leafGroupingU = (LeafSchemaNode) childNode;
-            } else {
+            if (!(childNode.getQName().equals(leafGroupingU.getQName()))) {
                 TestUtils.checkIsAddedByUses(childNode, true);
             }
         }
-        assertNotNull(leafGroupingU);
-        assertFalse(leafGroupingU.isAddedByUses());
 
         // grouping-V
         childNodes = gv.getChildNodes();
@@ -510,6 +523,27 @@ public class GroupingTest {
         expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-ZZ",
                 "leaf-grouping-ZZ");
         assertEquals(expectedPath, leafZZinGZZ.getPath());
+
+        // TEST getOriginal from grouping-U
+        assertEquals(gv.getDataChildByName("leaf-grouping-V"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-V")));
+        containerGroupingV = (ContainerSchemaNode) gu.getDataChildByName("container-grouping-V");
+        assertEquals(gv.getDataChildByName("container-grouping-V"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV));
+        assertEquals(gx.getDataChildByName("leaf-grouping-X"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName("leaf-grouping-X")
+                ));
+        assertEquals(gy.getDataChildByName("leaf-grouping-Y"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName("leaf-grouping-Y")
+                ));
+
+        assertEquals(gz.getDataChildByName("leaf-grouping-Z"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-Z")));
+        assertEquals(gzz.getDataChildByName("leaf-grouping-ZZ"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-ZZ")
+                ));
+
+        // TEST getOriginal from grouping-V
+        assertEquals(gz.getDataChildByName("leaf-grouping-Z"), SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName("leaf-grouping-Z")));
+        assertEquals(gzz.getDataChildByName("leaf-grouping-ZZ"), SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName("leaf-grouping-ZZ")
+                ));
+
+        // TEST getOriginal from grouping-X
+        assertEquals(gy.getDataChildByName("leaf-grouping-Y"),SchemaNodeUtils.getRootOriginalIfPossible( gx.getDataChildByName("leaf-grouping-Y")));
     }
 
 }