From bcb326afe5eb70c5b4e285f0aa2b3d3e2bcc1768 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Wed, 20 Nov 2013 13:26:53 +0100 Subject: [PATCH] tests for empty leaf, namespace changing, multi simple node 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 --- .../controller/sal/rest/impl/JsonReader.java | 31 ++--- ....java => FromJsonToCompositeNodeTest.java} | 106 ++++++++++++++++-- ...e.java => FromXmlToCompositeNodeTest.java} | 6 +- .../array-with-null.json | 5 + .../multiple-leaflist-items.json | 5 + .../simple-container.yang | 4 +- .../{simple-list.yang => simple-list1.yang} | 6 +- .../simple-list-yang/simple-list2.yang | 20 ++++ 8 files changed, 153 insertions(+), 30 deletions(-) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/{FromJsonToCompositeNode.java => FromJsonToCompositeNodeTest.java} (71%) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/{FromXmlToCompositeNode.java => FromXmlToCompositeNodeTest.java} (97%) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json rename opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/{simple-list.yang => simple-list1.yang} (68%) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java index a0acaf156f..a2ae1c9f7f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonReader.java @@ -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> 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)); } } 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/FromJsonToCompositeNodeTest.java similarity index 71% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNode.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java index dbbb4a6996..ede225c709 100644 --- 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/FromJsonToCompositeNodeTest.java @@ -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 stay unchanged 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); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNode.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java similarity index 97% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNode.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java index 093cac57d0..6249d2a5b0 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNode.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java @@ -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 index 0000000000..a19d9485f6 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json @@ -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 index 0000000000..b61a8a8f2e --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json @@ -0,0 +1,5 @@ +{ + "cont": { + "lflst1":[45,55,66] + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang index ddd67f7f80..493101ced1 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang @@ -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-list.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang similarity index 68% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang index af8edfaf7f..0ce8ea428c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang @@ -1,7 +1,7 @@ -module simple-data-types { - namespace "simple:data:types"; +module simple-list-yang1 { + namespace "simple:list:yang1"; - prefix "smpdtp"; + prefix "smplstyg"; 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 index 0000000000..0872a4754d --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang @@ -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 -- 2.36.6