Json to composite node test 13/2813/3
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 18 Nov 2013 09:12:32 +0000 (10:12 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Tue, 19 Nov 2013 14:25:37 +0000 (15:25 +0100)
contains tests for:
- elements leaf, leaf list, list, container under list or container
- json top level elements check
- check of read empty simple data like leaf, leaf list

Change-Id: I6826cb32415fc135eb7f966f7325c5d15e21ac1a
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
13 files changed:
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonToCompositeNodeProvider.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNode.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicDataTypesTest.java
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml

index 27ebeba..dea4a73 100644 (file)
@@ -36,7 +36,7 @@ public enum JsonToCompositeNodeProvider implements MessageBodyReader<CompositeNo
         try {
             return jsonReader.read(entityStream);
         } catch (UnsupportedFormatException e) {
-            throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
+            throw new WebApplicationException(e,Response.status(Response.Status.BAD_REQUEST)
                     .entity(e.getMessage()).build());
         }
     }
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNode.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNode.java
new file mode 100644 (file)
index 0000000..df2c03f
--- /dev/null
@@ -0,0 +1,234 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.*;
+import java.net.*;
+import java.util.List;
+
+import javax.ws.rs.WebApplicationException;
+
+import org.junit.Test;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.*;
+import org.opendaylight.yangtools.yang.data.api.*;
+import org.slf4j.*;
+
+import com.google.gson.JsonSyntaxException;
+
+public class FromJsonToCompositeNode {
+    Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
+
+    @Test
+    public void simpleListTest() {
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json");
+        assertNotNull(compositeNode);
+
+        assertEquals("lst", compositeNode.getNodeType().getLocalName());
+        verifyCompositeNode(compositeNode);
+    }
+
+    @Test
+    public void simpleContainerTest() {
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-container.json");
+        assertNotNull(compositeNode);
+
+        assertEquals("cont", compositeNode.getNodeType().getLocalName());
+
+        verifyCompositeNode(compositeNode);
+    }
+
+    /**
+     * List contains 4 items and in every item are other elements. It is
+     * supposed that there should be: lf11, lflst11, cont11, lst11
+     */
+    @Test
+    public void multipleItemsInListTest() {
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json");
+        assertNotNull(compositeNode);
+
+        assertEquals("lst", compositeNode.getNodeType().getLocalName());
+
+        verityMultipleItemsInList(compositeNode);
+    }
+
+    @Test
+    public void incorrectTopLevelElementsTest() {
+        Throwable cause1 = null;
+        try {
+            compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json");
+        } catch (WebApplicationException e) {
+            cause1 = e;
+        }
+
+        assertNotNull(cause1);
+        assertTrue(cause1
+                .getCause()
+                .getMessage()
+                .contains(
+                        "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
+
+        Throwable cause2 = null;
+        try {
+            compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json");
+        } catch (WebApplicationException e) {
+            cause2 = e;
+        }
+        assertNotNull(cause2);
+        assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element"));
+
+        Throwable cause3 = null;
+        try {
+            compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json");
+        } catch (WebApplicationException e) {
+            cause3 = e;
+        }
+        assertNotNull(cause3);
+        assertTrue(cause3
+                .getCause()
+                .getMessage()
+                .contains(
+                        "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet."));
+
+    }
+
+    /**
+     * if leaf list with no data is in json then no corresponding data is
+     * created in composite node. if leaf with no data then exception is raised
+     */
+    @Test
+    public void emptyDataReadTest() {
+        CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json");
+
+        assertNotNull(compositeNode);
+
+        assertEquals("cont", compositeNode.getNodeType().getLocalName());
+        assertTrue(compositeNode instanceof CompositeNode);
+        List<Node<?>> children = ((CompositeNode) compositeNode).getChildren();
+        assertEquals(1, children.size());
+        assertEquals("lflst2", children.get(0).getNodeType().getLocalName());
+        assertEquals("45", children.get(0).getValue());
+
+        String reason = null;
+        try {
+            compositeContainerFromJson("/json-to-composite-node/empty-data1.json");
+        } catch (JsonSyntaxException e) {
+            reason = e.getMessage();
+        }
+
+        assertTrue(reason.contains("Expected value at line"));
+
+    }
+
+    private void verityMultipleItemsInList(CompositeNode compositeNode) {
+        List<Node<?>> childrenNodes = compositeNode.getChildren();
+        assertEquals(4, childrenNodes.size());
+        boolean lf11Found = false;
+        boolean cont11Found = false;
+        boolean lst11Found = false;
+        for (Node<?> lst1Item : childrenNodes) {
+            assertEquals("lst1", lst1Item.getNodeType().getLocalName());
+            assertTrue(lst1Item instanceof CompositeNode);
+
+            List<Node<?>> childrenLst1 = ((CompositeNode) lst1Item).getChildren();
+            assertEquals(1, childrenLst1.size());
+            String localName = childrenLst1.get(0).getNodeType().getLocalName();
+            if (localName.equals("lf11")) {
+                assertTrue(childrenLst1.get(0) instanceof SimpleNode);
+                lf11Found = true;
+            } else if (localName.equals("lflst11")) {
+                assertTrue(childrenLst1.get(0) instanceof SimpleNode);
+                assertEquals("45", ((SimpleNode<?>) childrenLst1.get(0)).getValue());
+                lf11Found = true;
+            } else if (localName.equals("cont11")) {
+                assertTrue(childrenLst1.get(0) instanceof CompositeNode);
+                cont11Found = true;
+            } else if (localName.equals("lst11")) {
+                lst11Found = true;
+                assertTrue(childrenLst1.get(0) instanceof CompositeNode);
+                assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size());
+            }
+
+        }
+
+        assertTrue(lf11Found);
+        assertTrue(cont11Found);
+        assertTrue(lst11Found);
+
+    }
+
+    private void verifyCompositeNode(CompositeNode compositeNode) {
+        boolean cont1Found = false;
+        boolean lst1Found = false;
+        boolean lflst1_1Found = false;
+        boolean lflst1_2Found = false;
+        boolean lf1Found = false;
+
+        for (Node<?> node : compositeNode.getChildren()) {
+            if (node.getNodeType().getLocalName().equals("cont1")) {
+                if (node instanceof CompositeNode) {
+                    cont1Found = true;
+                    assertEquals(0, ((CompositeNode) node).getChildren().size());
+                }
+            } else if (node.getNodeType().getLocalName().equals("lst1")) {
+                if (node instanceof CompositeNode) {
+                    lst1Found = true;
+                    assertEquals(0, ((CompositeNode) node).getChildren().size());
+                }
+            } else if (node.getNodeType().getLocalName().equals("lflst1")) {
+                if (node instanceof SimpleNode) {
+                    if (((SimpleNode<?>) node).getValue().equals("lflst1_1")) {
+                        lflst1_1Found = true;
+                    } else if (((SimpleNode<?>) node).getValue().equals("lflst1_2")) {
+                        lflst1_2Found = true;
+                    }
+                }
+
+            } else if (node.getNodeType().getLocalName().equals("lf1")) {
+                if (node instanceof SimpleNode) {
+                    if (((SimpleNode<?>) node).getValue().equals("lf1")) {
+                        lf1Found = true;
+                    }
+                }
+            }
+        }
+        assertTrue(cont1Found);
+        assertTrue(lst1Found);
+        assertTrue(lflst1_1Found);
+        assertTrue(lflst1_2Found);
+        assertTrue(lf1Found);
+    }
+
+    private CompositeNode compositeContainerFromJson(String jsonPath) throws WebApplicationException {
+
+        JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE;
+        InputStream jsonStream = FromJsonToCompositeNode.class.getResourceAsStream(jsonPath);
+        try {
+            CompositeNode compositeNode = jsonToCompositeNodeProvider
+                    .readFrom(null, null, null, null, null, jsonStream);
+            assertTrue(compositeNode instanceof CompositeNodeWrapper);
+            try {
+                addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
+                return ((CompositeNodeWrapper) compositeNode).unwrap(null);
+            } catch (URISyntaxException e) {
+                LOG.error(e.getMessage());
+                assertTrue(e.getMessage(), false);
+            }
+        } catch (IOException e) {
+            LOG.error(e.getMessage());
+            assertTrue(e.getMessage(), false);
+        }
+        return null;
+    }
+
+    private void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
+        wrappedNode.setNamespace(new URI(""));
+        if (wrappedNode instanceof CompositeNodeWrapper) {
+            for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
+                addDummyNamespaceToAllNodes(childNodeWrapper);
+            }
+        }
+    }
+}
index 8cdbf78..f88a335 100644 (file)
@@ -72,7 +72,8 @@ public class ToJsonBasicDataTypesTest {
         boolean lfbinaryChecked = false;
         // boolean lfref1Checked = false;
         boolean lfemptyChecked = false;
-
+        boolean lfstr1Checked = false;
+        
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
             JsonToken peek = null;
@@ -98,6 +99,10 @@ public class ToJsonBasicDataTypesTest {
                 assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
                 assertEquals("lfstr", jReader.nextString());
                 lfstrChecked = true;
+            } else if (keyName.equals("lfstr1")) {
+                assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
+                assertEquals("", jReader.nextString());
+                lfstr1Checked = true;
             } else if (keyName.equals("lfbool1")) {
                 assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
                 assertEquals(true, jReader.nextBoolean());
@@ -167,6 +172,7 @@ public class ToJsonBasicDataTypesTest {
         assertTrue("lfbool1 wasn't checked", lfbool1Checked);
         assertTrue("lfbool2 wasn't checked", lfbool2Checked);
         assertTrue("lfstr wasn't checked", lfstrChecked);
+        assertTrue("lfstr1 wasn't checked", lfstr1Checked);
         assertTrue("lfbinary wasn't checked", lfbinaryChecked);
         assertTrue("lfempty wasn't checked", lfemptyChecked);
         // assertTrue("lfref1 wasn't checked", lfref1Checked);
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json
new file mode 100644 (file)
index 0000000..a6ad7f6
--- /dev/null
@@ -0,0 +1,6 @@
+{
+       "cont": {       
+               "lflst1":[],
+               "lflst2":[45]           
+       }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json
new file mode 100644 (file)
index 0000000..10d964d
--- /dev/null
@@ -0,0 +1,5 @@
+{
+       "cont": {       
+               "lf":                           
+       }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json
new file mode 100644 (file)
index 0000000..8e88266
--- /dev/null
@@ -0,0 +1,26 @@
+{
+       "lst":[
+               {
+                       "lst1": [
+                               {
+                                       "lf11":"lf11_1"
+                               },
+                               {
+                                       "lflst11":[
+                                               45
+                                       ]
+                               },
+                               {
+                                       "cont11":{
+                                       }
+                               },
+                               {
+                                       "lst11":[
+                                       {
+                                               }
+                                       ]
+                               }
+                       ]
+               }
+       ]
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json
new file mode 100644 (file)
index 0000000..1be4149
--- /dev/null
@@ -0,0 +1,15 @@
+{
+       "cont":{
+               "cont1":{
+               },
+               "lst1": [
+                       {
+                       }
+               ],
+               "lflst1":[
+                       "lflst1_1",
+                       "lflst1_2"
+               ],
+               "lf1":"lf1"
+       }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json
new file mode 100644 (file)
index 0000000..fee6baa
--- /dev/null
@@ -0,0 +1,17 @@
+{
+       "lst":[
+               {
+                       "cont1":{
+                       },
+                       "lst1": [
+                               {
+                               }
+                       ],
+                       "lflst1":[
+                               "lflst1_1",
+                               "lflst1_2"
+                       ],
+                       "lf1":"lf1"
+               }
+       ]
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json
new file mode 100644 (file)
index 0000000..3ae3a72
--- /dev/null
@@ -0,0 +1,9 @@
+{
+
+       "lst":[
+               {
+               },
+               {
+               }
+       ]
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json
new file mode 100644 (file)
index 0000000..f0f5540
--- /dev/null
@@ -0,0 +1,9 @@
+{
+
+       "cont": {
+       },
+       "lst":[
+               {
+               }
+       ]
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json
new file mode 100644 (file)
index 0000000..7288969
--- /dev/null
@@ -0,0 +1,3 @@
+{
+       "lf":"hello"
+}
\ No newline at end of file
index 4ad0983..010d3b1 100644 (file)
@@ -46,6 +46,9 @@ module simple-data-types {
          leaf lfstr {
                type string;
          }       
+         leaf lfstr1 {
+               type string;
+         }       
          leaf lfbool1 {
                type boolean;
          }       
index 1e83cb4..df00ca9 100644 (file)
@@ -11,6 +11,7 @@
        <lfnuint16Max>65535</lfnuint16Max>
        <lfnuint32Max>4294967295</lfnuint32Max>
        <lfstr>lfstr</lfstr>
+       <lfstr1></lfstr1>
        <lfbool1>true</lfbool1>
        <lfbool2>false</lfbool2>
        <lfbool3>bla</lfbool3>