tests for empty leaf, namespace changing, multi simple node 09/2909/2
authorJozef Gloncak <jgloncak@cisco.com>
Wed, 20 Nov 2013 12:26:53 +0000 (13:26 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 20 Nov 2013 13:09:09 +0000 (14:09 +0100)
following tests were added
- N elements in leaf list are red as N simple node instances
- Json [null] value is translated to simple node with null value
- for composite node for which were updated namespaces of its childs
  can't be namespaces changed again

Change-Id: Ibfb28b1b6fdaff49a7cf38531b058f8b0a79c524
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java [moved from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNode.java with 71% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java [moved from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNode.java with 97% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang [moved from opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list.yang with 68% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang [new file with mode: 0644]

index a0acaf156facb47a74f9e672f1514277f78fc4a4..a2ae1c9f7f248e81218f9862e4247376ae47dc75 100644 (file)
@@ -19,12 +19,12 @@ class JsonReader {
 
     public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
         JsonParser parser = new JsonParser();
-        
+
         JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
         if (!rootElement.isJsonObject()) {
             throw new UnsupportedFormatException("Root element of Json has to be Object");
         }
-        
+
         Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
         if (entrySetsOfRootJsonObject.size() != 1) {
             throw new UnsupportedFormatException("Json Object should contain one element");
@@ -41,13 +41,15 @@ class JsonReader {
                     if (firstElementInArray.isJsonObject()) {
                         return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
                     }
-                    throw new UnsupportedFormatException("Array as the first element in Json Object can have only Object element");
+                    throw new UnsupportedFormatException(
+                            "Array as the first element in Json Object can have only Object element");
                 }
             }
-            throw new UnsupportedFormatException("First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
+            throw new UnsupportedFormatException(
+                    "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
         }
     }
-    
+
     private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
         CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFrom(rootObjectName),
                 getLocalNameFrom(rootObjectName));
@@ -56,7 +58,7 @@ class JsonReader {
         }
         return firstNode;
     }
-    
+
     private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
         if (childType.isJsonObject()) {
             CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFrom(childName),
@@ -66,19 +68,18 @@ class JsonReader {
                 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
             }
         } else if (childType.isJsonArray()) {
-            for (JsonElement childOfChildType : childType.getAsJsonArray()) {
-                addChildToParent(childName, childOfChildType, parent);
+            if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
+                parent.addValue(new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), null));
+
+            } else {
+                for (JsonElement childOfChildType : childType.getAsJsonArray()) {
+                    addChildToParent(childName, childOfChildType, parent);
+                }
             }
         } else if (childType.isJsonPrimitive()) {
             JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
             String value = childPrimitive.getAsString();
-            SimpleNodeWrapper child = null;
-            if (value.equals("[null]")) {
-                child = new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), null);
-            } else {
-                child = new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), value);
-            }
-            parent.addValue(child);
+            parent.addValue(new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), value));
         }
     }
 
@@ -25,20 +25,53 @@ import org.opendaylight.yangtools.yang.model.api.*;
 
 import com.google.gson.JsonSyntaxException;
 
-public class FromJsonToCompositeNode {
+public class FromJsonToCompositeNodeTest {
 
-    private static Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
+    private static final Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNodeTest.class);
 
     @Test
     public void simpleListTest() {
         simpleTest("/json-to-composite-node/simple-list.json", "/json-to-composite-node/simple-list-yang", "lst",
-                "simple:data:types");
+                "simple:list:yang1", "simple-list-yang1");
     }
 
     @Test
     public void simpleContainerTest() {
         simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang",
-                "cont", "simple:data:types");
+                "cont", "simple:container:yang", "simple-container-yang");
+    }
+
+    /**
+     * test if for every leaf list item is simple node instance created
+     */
+    @Test
+    public void multipleItemsInLeafList() {
+        CompositeNode compositeNode = compositeContainerFromJson(
+                "/json-to-composite-node/multiple-leaflist-items.json", true);
+        assertNotNull(compositeNode);
+        assertEquals(3, compositeNode.getChildren().size());
+
+        boolean lflst1_1 = false;
+        boolean lflst1_2 = false;
+        boolean lflst1_3 = false;
+
+        for (Node<?> node : compositeNode.getChildren()) {
+            assertEquals("lflst1", node.getNodeType().getLocalName());
+            assertTrue(node instanceof SimpleNode<?>);
+            SimpleNode<?> simpleNode = (SimpleNode<?>) node;
+            if (simpleNode.getValue().equals("45")) {
+                lflst1_1 = true;
+            } else if (simpleNode.getValue().equals("55")) {
+                lflst1_2 = true;
+            } else if (simpleNode.getValue().equals("66")) {
+                lflst1_3 = true;
+            }
+        }
+
+        assertTrue(lflst1_1);
+        assertTrue(lflst1_2);
+        assertTrue(lflst1_3);
+
     }
 
     /**
@@ -56,6 +89,21 @@ public class FromJsonToCompositeNode {
         verityMultipleItemsInList(compositeNode);
     }
 
+    @Test
+    public void nullArrayToCompositeNodeWithNullValueTest() {
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/array-with-null.json", true);
+        assertNotNull(compositeNode);
+        assertEquals("cont", compositeNode.getNodeType().getLocalName());
+
+        assertNotNull(compositeNode.getChildren());
+        assertEquals(1, compositeNode.getChildren().size());
+        Node<?> lfNode = compositeNode.getChildren().iterator().next();
+
+        assertTrue(lfNode instanceof SimpleNode<?>);
+        assertEquals(null, ((SimpleNode<?>) lfNode).getValue());
+
+    }
+
     @Test
     public void incorrectTopLevelElementsTest() {
         Throwable cause1 = null;
@@ -124,13 +172,57 @@ public class FromJsonToCompositeNode {
 
     }
 
-    private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace) {
+    /**
+     * Tests whether namespace <b>stay unchanged</b> if concrete values are
+     * present in composite or simple node and if the method for update is
+     * called.
+     * 
+     */
+    @Test
+    public void notSupplyNamespaceIfAlreadySupplied() {
+
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
+        assertNotNull(compositeNode);
+
+        DataSchemaNode dataSchemaNode1 = null;
+        DataSchemaNode dataSchemaNode2 = null;
+        try {
+            dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
+                    "simple-list-yang1");
+            dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang",
+                    "simple-list-yang2");
+        } catch (FileNotFoundException e) {
+            LOG.error(e.getMessage());
+            assertTrue(false);
+        }
+        assertNotNull(dataSchemaNode1);
+        assertNotNull(dataSchemaNode2);
+
+        // supplement namespaces according to first data schema -
+        // "simple:data:types1"
+        TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
+
+        assertTrue(compositeNode instanceof CompositeNodeWrapper);
+        CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
+
+        assertEquals("lst", compNode.getNodeType().getLocalName());
+        verifyCompositeNode(compNode, "simple:list:yang1");
+
+        // dataSchemaNode2 should't be taken into account, because compNode
+        // isn't CompositeNodeWrapper
+        TestUtils.supplementNamespace(dataSchemaNode2, compNode);
+        verifyCompositeNode(compNode, "simple:list:yang1");
+
+    }
+
+    private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace,
+            String moduleName) {
         CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
         assertNotNull(compositeNode);
 
         DataSchemaNode dataSchemaNode = null;
         try {
-            dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath);
+            dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName);
         } catch (FileNotFoundException e) {
             LOG.error(e.getMessage());
             assertTrue(false);
@@ -234,7 +326,7 @@ public class FromJsonToCompositeNode {
             throws WebApplicationException {
 
         JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
-        InputStream jsonStream = FromJsonToCompositeNode.class.getResourceAsStream(jsonPath);
+        InputStream jsonStream = FromJsonToCompositeNodeTest.class.getResourceAsStream(jsonPath);
         try {
             CompositeNode compositeNode = jsonToCompositeNodeProvider
                     .readFrom(null, null, null, null, null, jsonStream);
@@ -16,8 +16,8 @@ import org.opendaylight.yangtools.yang.data.api.*;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.slf4j.*;
 
-public class FromXmlToCompositeNode {
-    private static Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNode.class);
+public class FromXmlToCompositeNodeTest {
+    private static final Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNodeTest.class);
 
     /**
      * top level element represents container. second level element is list with
@@ -230,7 +230,7 @@ public class FromXmlToCompositeNode {
     private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
         XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
         try {
-            InputStream xmlStream = FromXmlToCompositeNode.class.getResourceAsStream(xmlPath);
+            InputStream xmlStream = FromXmlToCompositeNodeTest.class.getResourceAsStream(xmlPath);
             CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
             if (dummyNamespaces) {
                 try {
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json
new file mode 100644 (file)
index 0000000..a19d948
--- /dev/null
@@ -0,0 +1,5 @@
+{
+       "cont": {
+               "lf":[null]
+       }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json
new file mode 100644 (file)
index 0000000..b61a8a8
--- /dev/null
@@ -0,0 +1,5 @@
+{
+       "cont": {
+               "lflst1":[45,55,66]
+       }
+}
\ No newline at end of file
index ddd67f7f8074dc03503d9426058a72ac2c9b7ca8..493101ced14f938845488f5ee70ecacb89e047e6 100644 (file)
@@ -1,5 +1,5 @@
-module simple-data-types {
-  namespace "simple:data:types";  
+module simple-container-yang {
+  namespace "simple:container:yang";  
 
   prefix "smpdtp";
   revision 2013-11-12 {    
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang
new file mode 100644 (file)
index 0000000..0872a47
--- /dev/null
@@ -0,0 +1,20 @@
+module simple-list-yang2 {
+  namespace "simple:list:yang2";  
+
+  prefix "smplstyg";
+  revision 2013-11-12 {    
+  }
+  
+  list lst {
+       container cont1 {
+       }
+       list lst1 {
+       }
+       leaf-list lflst1 {
+               type string;
+       }
+       leaf lf1 {
+               type string;
+       }
+  }
+}
\ No newline at end of file