Merge "Bug 1125: Added regression test"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / xml / to / cnsn / test / XmlLeafrefToCnSnTest.java
index 894b4c2bab514cfb562ece1efe8b43d017403a83..6c11bc1861995879818ce31b3d6d75696c2decf7 100644 (file)
@@ -1,21 +1,26 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
-import java.io.*;
-import java.net.URISyntaxException;
 import java.util.List;
 import java.util.Set;
-
-import javax.ws.rs.WebApplicationException;
-
+import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.*;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -24,20 +29,14 @@ public class XmlLeafrefToCnSnTest {
     private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class);
 
     /**
-     * top level element represents container. second level element is list with
-     * two elements.
+     * top level element represents container. second level element is list with two elements.
      */
     @Test
     public void testXmlDataContainer() {
-        CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false);
+        CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-container.xml", false,
+                XmlToCompositeNodeProvider.INSTANCE);
         assertNotNull(compNode);
-        Set<Module> modules = null;
-        try {
-            modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-container-yang").getPath());
-        } catch (FileNotFoundException e) {
-            LOG.error(e.getMessage());
-            assertTrue(false);
-        }
+        Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-container-yang");
 
         assertNotNull(modules);
         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
@@ -49,13 +48,13 @@ public class XmlLeafrefToCnSnTest {
         verifyCommonPartAOfXml(compNode, "", nameSpace);
     }
 
-    private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) {
+    private void verifyNullAndEmptyStringSingleNode(final CompositeNode compNode, final String nameSpace) {
         assertEquals("cont", compNode.getNodeType().getLocalName());
 
         SimpleNode<?> lf2 = null;
         SimpleNode<?> lf3 = null;
         int found = 0;
-        for (Node<?> child : compNode.getChildren()) {
+        for (Node<?> child : compNode.getValue()) {
             if (found == 0x3)
                 break;
             if (child instanceof SimpleNode<?>) {
@@ -77,14 +76,11 @@ public class XmlLeafrefToCnSnTest {
 
     @Test
     public void testXmlDataList() {
-        CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false);
+        CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false,
+                XmlToCompositeNodeProvider.INSTANCE);
         assertNotNull(compNode);
 
-        Set<Module> modules = null;
-        try {
-            modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-list-yang").getPath());
-        } catch (FileNotFoundException e) {
-        }
+        Set<Module> modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang");
         assertNotNull(modules);
 
         TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont");
@@ -93,11 +89,11 @@ public class XmlLeafrefToCnSnTest {
         String nameSpaceCont = "data:container:yang";
         assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
         assertEquals("cont", compNode.getNodeType().getLocalName());
-        assertEquals(3, compNode.getChildren().size());
+        assertEquals(3, compNode.getValue().size());
         CompositeNode lst1_1 = null;
         CompositeNode lst1_2 = null;
         int loopCount = 0;
-        for (Node<?> node : compNode.getChildren()) {
+        for (Node<?> node : compNode.getValue()) {
             if (node.getNodeType().getLocalName().equals("lf1")) {
                 assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
                 assertTrue(node instanceof SimpleNode<?>);
@@ -122,7 +118,7 @@ public class XmlLeafrefToCnSnTest {
         // lst1_2
         SimpleNode<?> lflst11 = null;
         CompositeNode cont11 = null;
-        for (Node<?> node : lst1_2.getChildren()) {
+        for (Node<?> node : lst1_2.getValue()) {
             String nodeName = node.getNodeType().getLocalName();
             if (nodeName.equals("lflst11")) {
                 assertTrue(node instanceof SimpleNode<?>);
@@ -136,9 +132,9 @@ public class XmlLeafrefToCnSnTest {
         }
         assertEquals("221", lflst11.getValue());
 
-        assertEquals(1, cont11.getChildren().size());
-        assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
-        SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
+        assertEquals(1, cont11.getValue().size());
+        assertTrue(cont11.getValue().get(0) instanceof SimpleNode<?>);
+        SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getValue().get(0);
         assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
         assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
         assertEquals((short) 100, cont11_lf111.getValue());
@@ -148,14 +144,15 @@ public class XmlLeafrefToCnSnTest {
 
     @Test
     public void testXmlEmptyData() {
-        CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/empty-data.xml", true);
+        CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/empty-data.xml", true,
+                XmlToCompositeNodeProvider.INSTANCE);
         assertEquals("cont", compNode.getNodeType().getLocalName());
         SimpleNode<?> lf1 = null;
         SimpleNode<?> lflst1_1 = null;
         SimpleNode<?> lflst1_2 = null;
         CompositeNode lst1 = null;
         int lflst1Count = 0;
-        for (Node<?> node : compNode.getChildren()) {
+        for (Node<?> node : compNode.getValue()) {
             if (node.getNodeType().getLocalName().equals("lf1")) {
                 assertTrue(node instanceof SimpleNode<?>);
                 lf1 = (SimpleNode<?>) node;
@@ -184,11 +181,11 @@ public class XmlLeafrefToCnSnTest {
         assertEquals("", lf1.getValue());
         assertEquals("", lflst1_1.getValue());
         assertEquals("", lflst1_2.getValue());
-        assertEquals(1, lst1.getChildren().size());
-        assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName());
+        assertEquals(1, lst1.getValue().size());
+        assertEquals("lf11", lst1.getValue().get(0).getNodeType().getLocalName());
 
-        assertTrue(lst1.getChildren().get(0) instanceof SimpleNode<?>);
-        assertEquals("", lst1.getChildren().get(0).getValue());
+        assertTrue(lst1.getValue().get(0) instanceof SimpleNode<?>);
+        assertEquals("", lst1.getValue().get(0).getValue());
 
     }
 
@@ -202,9 +199,8 @@ public class XmlLeafrefToCnSnTest {
     }
 
     /**
-     * 
-     * Test case like <lf11 xmlns="namespace1"
-     * xmlns:x="namespace">identity</lf11>
+     *
+     * Test case like <lf11 xmlns="namespace1" xmlns:x="namespace">identity</lf11>
      */
 
     @Test
@@ -214,9 +210,8 @@ public class XmlLeafrefToCnSnTest {
     }
 
     /**
-     * 
-     * Test case like <cont1 xmlns="namespace1"> <lf11
-     * xmlns:x="namespace">identity</lf11> </cont1>
+     *
+     * Test case like <cont1 xmlns="namespace1"> <lf11 xmlns:x="namespace">identity</lf11> </cont1>
      */
     @Test
     public void testIdentityrefDefaultNmspcInParrentElement() {
@@ -225,21 +220,19 @@ public class XmlLeafrefToCnSnTest {
     }
 
     /**
-     * 
-     * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace">
-     * <lf11>x:identity</lf11> </cont1>
+     *
+     * Test case like <cont1 xmlns="namespace1" xmlns:x="namespace"> <lf11>x:identity</lf11> </cont1>
      */
+    @Ignore
     @Test
     public void testIdentityrefNmspcInParrentElement() {
         testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-parrent-element.xml",
                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "z:namespace");
-
     }
 
     /**
-     * 
-     * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11>
-     * </cont1>
+     *
+     * Test case like (without namespace in xml) <cont1> <lf11>x:identity</lf11> </cont1>
      */
     @Test
     public void testIdentityrefNoNmspcValueWithPrefix() {
@@ -248,9 +241,8 @@ public class XmlLeafrefToCnSnTest {
     }
 
     /**
-     * 
-     * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11>
-     * </cont1>
+     *
+     * Test case like (without namespace in xml) <cont1> <lf11>identity</lf11> </cont1>
      */
     @Test
     public void testIdentityrefNoNmspcValueWithoutPrefix() {
@@ -258,7 +250,7 @@ public class XmlLeafrefToCnSnTest {
                 "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module");
     }
 
-    private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
+    private void verifyCommonPartAOfXml(final CompositeNode compNode, final String suf, final String nameSpace) {
         SimpleNode<?> lf1suf = null;
         SimpleNode<?> lflst1suf_1 = null;
         SimpleNode<?> lflst1suf_2 = null;
@@ -268,7 +260,7 @@ public class XmlLeafrefToCnSnTest {
 
         int lflstCount = 0;
 
-        for (Node<?> node : compNode.getChildren()) {
+        for (Node<?> node : compNode.getValue()) {
             String localName = node.getNodeType().getLocalName();
             if (localName.equals("lf1" + suf)) {
                 assertTrue(node instanceof SimpleNode<?>);
@@ -308,55 +300,28 @@ public class XmlLeafrefToCnSnTest {
         assertEquals("131", lflst1suf_2.getValue());
         assertEquals("str1", lflst1suf_3.getValue());
 
-        assertEquals(1, lst1suf.getChildren().size());
+        assertEquals(1, lst1suf.getValue().size());
 
-        assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
-        SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
+        assertTrue(lst1suf.getValue().get(0) instanceof SimpleNode<?>);
+        SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getValue().get(0);
         assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
         assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
         assertEquals("str2", lst11_lf11.getValue());
 
-        assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
-        SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
+        assertTrue(cont1suf.getValue().get(0) instanceof SimpleNode<?>);
+        SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getValue().get(0);
         assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
         assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
         assertEquals((short) 100, cont1_lf11.getValue());
     }
 
-    private CompositeNode compositeNodeFromXml(String xmlPath, boolean dummyNamespaces) {
-        XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
-        try {
-            InputStream xmlStream = XmlLeafrefToCnSnTest.class.getResourceAsStream(xmlPath);
-            CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
-            if (dummyNamespaces) {
-                try {
-                    TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
-                    return ((CompositeNodeWrapper) compositeNode).unwrap();
-                } catch (URISyntaxException e) {
-                    LOG.error(e.getMessage());
-                    assertTrue(e.getMessage(), false);
-                }
-            }
-            return compositeNode;
-
-        } catch (WebApplicationException | IOException e) {
-            LOG.error(e.getMessage());
-            assertTrue(false);
-        }
-        return null;
-    }
-
-    private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName,
-            int moduleCount, String resultLocalName, String resultNamespace) {
-        CompositeNode compositeNode = compositeNodeFromXml(xmlPath, false);
+    private void testIdentityrefToCnSn(final String xmlPath, final String yangPath, final String moduleName,
+            final String schemaName, final int moduleCount, final String resultLocalName, final String resultNamespace) {
+        CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlPath, false, XmlToCompositeNodeProvider.INSTANCE);
         assertNotNull(compositeNode);
 
-        Set<Module> modules = TestUtils.resolveModulesFrom(yangPath);
+        Set<Module> modules = TestUtils.loadModulesFrom(yangPath);
         assertEquals(moduleCount, modules.size());
-        Module module = TestUtils.resolveModule(moduleName, modules);
-        assertNotNull(module);
-        DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module);
-        assertNotNull(dataSchemaNode);
 
         TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName);
 
@@ -365,19 +330,18 @@ public class XmlLeafrefToCnSnTest {
         QName qName = (QName) lf11.getValue();
         assertEquals(resultLocalName, qName.getLocalName());
         assertEquals(resultNamespace, qName.getNamespace().toString());
-
     }
 
-    private SimpleNode<?> getLf11(CompositeNode compositeNode) {
+    private SimpleNode<?> getLf11(final CompositeNode compositeNode) {
         assertEquals("cont", compositeNode.getNodeType().getLocalName());
 
-        List<Node<?>> childs = compositeNode.getChildren();
+        List<Node<?>> childs = compositeNode.getValue();
         assertEquals(1, childs.size());
         Node<?> nd = childs.iterator().next();
         assertTrue(nd instanceof CompositeNode);
         assertEquals("cont1", nd.getNodeType().getLocalName());
 
-        childs = ((CompositeNode) nd).getChildren();
+        childs = ((CompositeNode) nd).getValue();
         SimpleNode<?> lf11 = null;
         for (Node<?> child : childs) {
             assertTrue(child instanceof SimpleNode);