Address FIXME for QueuedNotificationManager
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / GroupingTest.java
index 163ee55851e027611ac38d04929c243044217348..fc6ed21b7949bf1140d7153934050887ebb820a7 100644 (file)
@@ -7,26 +7,28 @@
  */
 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.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 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.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.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
@@ -36,15 +38,17 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 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());
     }
@@ -105,7 +109,7 @@ public class GroupingTest {
         assertEquals("addresses reference added by refine", refineList.getReference());
         assertFalse(refineList.isConfiguration());
         assertEquals(2, (int) refineList.getConstraints().getMinElements());
-        assertEquals(12, (int) refineList.getConstraints().getMaxElements());
+        assertEquals(Integer.MAX_VALUE, (int) refineList.getConstraints().getMaxElements());
 
         // leaf id
         assertNotNull(refineInnerLeaf);
@@ -118,7 +122,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());
     }
 
@@ -153,17 +157,19 @@ 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");
+        ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName("how");
         assertNotNull(how_u);
         TestUtils.checkIsAddedByUses(how_u, true);
         assertEquals(2, how_u.getCases().size());
 
-        ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
+        ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how");
         assertNotNull(how_g);
         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);
@@ -183,6 +189,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);
@@ -192,6 +199,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);
@@ -201,6 +209,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();
@@ -255,8 +264,9 @@ 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");
+        ChoiceSchemaNode how_u = (ChoiceSchemaNode) foo.getDataChildByName("how");
         assertNotNull(how_u);
         TestUtils.checkIsAddedByUses(how_u, true);
         assertFalse(how_u.isAugmenting());
@@ -269,10 +279,11 @@ public class GroupingTest {
         LeafSchemaNode intervalLeaf = (LeafSchemaNode) interval.getDataChildByName("interval");
         assertFalse(intervalLeaf.isAugmenting());
 
-        ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how");
+        ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how");
         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);
@@ -290,6 +301,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);
@@ -299,6 +311,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);
@@ -308,6 +321,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();
@@ -338,7 +352,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);
@@ -348,8 +362,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");
@@ -397,18 +411,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();
@@ -507,6 +522,64 @@ 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")));
+    }
+
+    @Test
+    public void testAddedByUsesLeafTypeQName() throws IOException,
+            URISyntaxException {
+
+        Set<Module> loadModules = TestUtils.loadModules(getClass().getResource(
+                "/added-by-uses-leaf-test").toURI());
+
+        assertEquals(2, loadModules.size());
+
+        Module foo = null;
+        Module imp = null;
+        for (Module module : loadModules) {
+            if (module.getName().equals("foo")) {
+                foo = module;
+            }
+            if (module.getName().equals("import-module")) {
+                imp = module;
+            }
+        }
+
+        LeafSchemaNode leaf = (LeafSchemaNode) ((ContainerSchemaNode) foo
+                .getDataChildByName("my-container"))
+                .getDataChildByName("my-leaf");
+
+        TypeDefinition<?> impType = null;
+        Set<TypeDefinition<?>> typeDefinitions = imp.getTypeDefinitions();
+        for (TypeDefinition<?> typeDefinition : typeDefinitions) {
+            if (typeDefinition.getQName().getLocalName().equals("imp-type")) {
+                impType = typeDefinition;
+                break;
+            }
+        }
+
+        assertEquals(leaf.getType().getQName(), impType.getQName());
+
     }
 
 }