Test composite node with empty conts and lists to Json 64/2764/1
authorJozef Gloncak <jgloncak@cisco.com>
Thu, 14 Nov 2013 07:24:19 +0000 (08:24 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Fri, 15 Nov 2013 08:19:43 +0000 (09:19 +0100)
Method for building composite node with empty elements (containers, lists)
was added.

Change-Id: I153fdba11b379d5cc7bd35907e530ff54c2d4a30
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
12 files changed:
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicDataTypesTest.java [moved from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicDataTypesTest.java with 97% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java [moved from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java with 50% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Cont.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lst.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/YangElement.java
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_data.json [moved from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json with 100% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_empty_data.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/empty_data.xml [new file with mode: 0644]

index e9552ec890895cf7c082161a323cb210ae77d301..532e29df66dbe5962ab246e56b674710fede7622 100644 (file)
@@ -4,6 +4,8 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.*;
+import java.net.*;
+import java.sql.Date;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -19,10 +21,9 @@ import javax.xml.transform.stream.StreamResult;
 
 import org.opendaylight.controller.sal.rest.impl.*;
 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
-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.data.impl.XmlTreeBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.*;
+import org.opendaylight.yangtools.yang.data.impl.*;
 import org.opendaylight.yangtools.yang.model.api.*;
 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
@@ -115,24 +116,17 @@ final class TestUtils {
 
     }
 
-    static String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath, String outputPath) {
+    static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath) {
         String jsonResult = null;
         Set<Module> modules = null;
 
         try {
-            modules = TestUtils.loadModules(YangAndXmlToJsonBasicDataTypesTest.class.getResource(yangPath).getPath());
+            modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangPath).getPath());
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }
         assertNotNull("modules can't be null.", modules);
 
-        InputStream xmlStream = YangAndXmlToJsonBasicDataTypesTest.class.getResourceAsStream(xmlDataPath);
-        CompositeNode compositeNode = null;
-        try {
-            compositeNode = TestUtils.loadCompositeNode(xmlStream);
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        }
         assertNotNull("Composite node can't be null", compositeNode);
 
         StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
@@ -160,10 +154,21 @@ final class TestUtils {
         return jsonResult;
     }
 
+    static CompositeNode loadCompositeNode(String xmlDataPath) {
+        InputStream xmlStream = ToJsonBasicDataTypesTest.class.getResourceAsStream(xmlDataPath);
+        CompositeNode compositeNode = null;
+        try {
+            compositeNode = TestUtils.loadCompositeNode(xmlStream);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
+        return compositeNode;
+    }
+
     static void outputToFile(ByteArrayOutputStream outputStream, String outputDir) throws IOException {
         FileOutputStream fileOS = null;
         try {
-            String path = YangAndXmlToJsonBasicDataTypesTest.class.getResource(outputDir).getPath();
+            String path = ToJsonBasicDataTypesTest.class.getResource(outputDir).getPath();
             File outFile = new File(path + "/data.json");
             fileOS = new FileOutputStream(outFile);
             try {
@@ -212,7 +217,7 @@ final class TestUtils {
     }
 
     private static FileReader getFileReader(String path) {
-        String fullPath = YangAndXmlToJsonBasicDataTypesTest.class.getResource(path).getPath();
+        String fullPath = ToJsonBasicDataTypesTest.class.getResource(path).getPath();
         assertNotNull("Path to file can't be null.", fullPath);
         File file = new File(fullPath);
         assertNotNull("File can't be null", file);
@@ -244,4 +249,20 @@ final class TestUtils {
         return strBuilder.toString();
     }
 
+    static QName buildQName(String name, String uri, String date) {
+        try {
+            URI u = new URI(uri);
+            Date dt = null;
+            if (date != null) {
+                dt = Date.valueOf(date);
+            }
+            return new QName(u, dt, name);
+        } catch (URISyntaxException e) {
+            return null;
+        }
+    }
+
+    static QName buildQName(String name) {
+        return buildQName(name, "", null);
+    }
 }
@@ -10,9 +10,10 @@ import java.io.*;
 import java.util.*;
 
 import org.junit.Test;
+
 import com.google.gson.stream.*;
 
-public class YangAndXmlToJsonBasicDataTypesTest {
+public class ToJsonBasicDataTypesTest {
 
     @Test
     public void simpleYangDataTest() {
@@ -21,7 +22,8 @@ public class YangAndXmlToJsonBasicDataTypesTest {
         // TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",
         // false);
 
-        jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-data-types/xml/data.xml",
+        jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(
+                TestUtils.loadCompositeNode("/yang-to-json-conversion/simple-data-types/xml/data.xml"),
                 "/yang-to-json-conversion/simple-data-types", "/yang-to-json-conversion/simple-data-types/xml");
         verifyJsonOutput(jsonOutput);
     }
@@ -1,35 +1,62 @@
 package org.opendaylight.controller.sal.restconf.impl.test;
 
 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 static org.junit.Assert.assertFalse;
 
 import java.io.*;
 import java.util.*;
 
-import javax.validation.constraints.AssertFalse;
-
 import org.junit.Test;
 import org.opendaylight.controller.sal.restconf.impl.test.structures.*;
+import org.opendaylight.yangtools.yang.data.api.*;
+import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
 
-import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.*;
 
-public class YangAndXmlToJsonBasicYangTypesTest {
+public class ToJsonBasicYangTypesTest {
 
+    /**
+     * Test of json output when as input are specified composite node with empty
+     * data + YANG file
+     */
     @Test
-    public void simpleYangTypesWithJsonReaderTest() {
-        String jsonOutput;
-        // jsonOutput =
-        // TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",
-        // false);
-
-        jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml",
+    public void compositeNodeAndYangWithJsonReaderEmptyDataTest() {
+        String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(prepareCompositeNodeWithEmpties(),
                 "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml");
+        verifyJsonOutputForEmpty(jsonOutput);
+    }
 
+    /**
+     * Test of json output when as input are specified xml file (no empty
+     * elements)and YANG file
+     */
+    @Test
+    public void xmlAndYangTypesWithJsonReaderTest() {
+        String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(
+                TestUtils.loadCompositeNode("/yang-to-json-conversion/simple-yang-types/xml/data.xml"),
+                "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml");
         verifyJsonOutput(jsonOutput);
+    }
+
+    private void verifyJsonOutputForEmpty(String jsonOutput) {
+        StringReader strReader = new StringReader(jsonOutput);
+        JsonReader jReader = new JsonReader(strReader);
 
+        String exception = null;
+        Cont dataFromJson = null;
+        try {
+            dataFromJson = jsonReadCont1(jReader);
+        } catch (IOException e) {
+            exception = e.getMessage();
+        }
+
+        assertNotNull("Data structures from json are missing.", dataFromJson);
+        checkDataFromJsonEmpty(dataFromJson);
+
+        assertNull("Error during reading Json output: " + exception, exception);
     }
 
     private void verifyJsonOutput(String jsonOutput) {
@@ -68,7 +95,7 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf11")) {
-                redData.addLf(new Lf(keyName, jReader.nextString()));
+                redData.addLf(new Lf(keyName, nextValue(jReader)));
             } else if (keyName.equals("lflst11")) {
                 LfLst lfLst = new LfLst(keyName);
                 lfLst = jsonReadLflstValues(jReader, lfLst);
@@ -107,9 +134,9 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf111")) {
-                lstItem.addLf(new Lf(keyName, jReader.nextString()));
+                lstItem.addLf(new Lf(keyName, nextValue(jReader)));
             } else if (keyName.equals("lf112")) {
-                lstItem.addLf(new Lf(keyName, jReader.nextString()));
+                lstItem.addLf(new Lf(keyName, nextValue(jReader)));
             } else if (keyName.equals("cont111")) {
                 Cont cont = new Cont(keyName);
                 cont = jsonReadCont111(jReader, cont);
@@ -146,7 +173,7 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         if (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf1121")) {
-                lstItem.addLf(new Lf(keyName, jReader.nextString()));
+                lstItem.addLf(new Lf(keyName, nextValue(jReader)));
             }
         }
         jReader.endObject();
@@ -170,13 +197,22 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         if (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf1111")) {
-                lstItem.addLf(new Lf(keyName, jReader.nextString()));
+                lstItem.addLf(new Lf(keyName, nextValue(jReader)));
             }
         }
         jReader.endObject();
         return lstItem;
     }
 
+    private String nextValue(JsonReader jReader) throws IOException {
+        if (jReader.peek().equals(JsonToken.NULL)) {
+            jReader.nextNull();
+            return null;
+        } else {
+            return jReader.nextString();
+        }
+    }
+
     private Cont jsonReadCont111(JsonReader jReader, Cont cont) throws IOException {
         jReader.beginObject();
         cont = jsonReadCont111Elements(jReader, cont);
@@ -188,7 +224,7 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf1111")) {
-                cont.addLf(new Lf(keyName, jReader.nextString()));
+                cont.addLf(new Lf(keyName, nextValue(jReader)));
             } else if (keyName.equals("lflst1111")) {
                 LfLst lfLst = new LfLst(keyName);
                 lfLst = jsonReadLflstValues(jReader, lfLst);
@@ -221,7 +257,7 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
             if (keyName.equals("lf1111A") || keyName.equals("lf1111B")) {
-                lstItem.addLf(new Lf(keyName, jReader.nextString()));
+                lstItem.addLf(new Lf(keyName, nextValue(jReader)));
             }
         }
         jReader.endObject();
@@ -231,12 +267,94 @@ public class YangAndXmlToJsonBasicYangTypesTest {
     private LfLst jsonReadLflstValues(JsonReader jReader, LfLst lfLst) throws IOException {
         jReader.beginArray();
         while (jReader.hasNext()) {
-            lfLst.addLf(new Lf(jReader.nextString()));
+            lfLst.addLf(new Lf(nextValue(jReader)));
         }
         jReader.endArray();
         return lfLst;
     }
 
+    private void checkDataFromJsonEmpty(Cont dataFromJson) {
+        assertTrue(dataFromJson.getLfs().isEmpty());
+        assertTrue(dataFromJson.getLfLsts().isEmpty());
+        assertTrue(dataFromJson.getConts().isEmpty());
+
+        Map<String, Lst> lsts = dataFromJson.getLsts();
+        assertEquals(1, lsts.size());
+        Lst lst11 = lsts.get("lst11");
+        assertNotNull(lst11);
+        Set<LstItem> lstItems = lst11.getLstItems();
+        assertNotNull(lstItems);
+
+        LstItem lst11_1 = null;
+        LstItem lst11_2 = null;
+        LstItem lst11_3 = null;
+        for (LstItem lstItem : lstItems) {
+            if (lstItem.getLfs().get("lf111").getValue().equals("1")) {
+                lst11_1 = lstItem;
+            } else if (lstItem.getLfs().get("lf111").getValue().equals("2")) {
+                lst11_2 = lstItem;
+            } else if (lstItem.getLfs().get("lf111").getValue().equals("3")) {
+                lst11_3 = lstItem;
+            }
+        }
+
+        assertNotNull(lst11_1);
+        assertNotNull(lst11_2);
+        assertNotNull(lst11_3);
+
+        // lst11_1
+        assertTrue(lst11_1.getLfLsts().isEmpty());
+        assertEquals(1, lst11_1.getLfs().size());
+        assertEquals(1, lst11_1.getConts().size());
+        assertEquals(1, lst11_1.getLsts().size());
+        assertEquals(lst11_1.getLsts().get("lst111"), new Lst("lst111").addLstItem(new LstItem().addLf("lf1111", "35"))
+                .addLstItem(new LstItem().addLf("lf1111", "34")).addLstItem(new LstItem()).addLstItem(new LstItem()));
+        assertEquals(lst11_1.getConts().get("cont111"), new Cont("cont111"));
+        // : lst11_1
+
+        // lst11_2
+        assertTrue(lst11_2.getLfLsts().isEmpty());
+        assertEquals(1, lst11_2.getLfs().size());
+        assertEquals(1, lst11_2.getConts().size());
+        assertEquals(1, lst11_2.getLsts().size());
+
+        Cont lst11_2_cont111 = lst11_2.getConts().get("cont111");
+
+        // -cont111
+        assertNotNull(lst11_2_cont111);
+        assertTrue(lst11_2_cont111.getLfs().isEmpty());
+        assertEquals(1, lst11_2_cont111.getLfLsts().size());
+        assertEquals(1, lst11_2_cont111.getLsts().size());
+        assertTrue(lst11_2_cont111.getConts().isEmpty());
+
+        assertEquals(new LfLst("lflst1111").addLf("1024").addLf("4096"), lst11_2_cont111.getLfLsts().get("lflst1111"));
+        assertEquals(
+                new Lst("lst1111").addLstItem(new LstItem().addLf("lf1111B", "4")).addLstItem(
+                        new LstItem().addLf("lf1111A", "lf1111A str12")), lst11_2_cont111.getLsts().get("lst1111"));
+        // :-cont111
+        assertEquals(lst11_2.getLsts().get("lst112"), new Lst("lst112").addLstItem(new LstItem()));
+        // : lst11_2
+
+        // lst11_3
+        assertEquals(1, lst11_3.getLfs().size());
+        assertTrue(lst11_3.getLfLsts().isEmpty());
+        assertTrue(lst11_3.getLsts().isEmpty());
+        assertTrue(lst11_3.getLsts().isEmpty());
+
+        // -cont111
+        Cont lst11_3_cont111 = lst11_3.getConts().get("cont111");
+        assertEquals(0, lst11_3_cont111.getLfs().size());
+        assertEquals(0, lst11_3_cont111.getLfLsts().size());
+        assertEquals(1, lst11_3_cont111.getLsts().size());
+        assertTrue(lst11_3_cont111.getConts().isEmpty());
+
+        assertEquals(new Lst("lst1111").addLstItem(new LstItem()).addLstItem(new LstItem()), lst11_3_cont111.getLsts()
+                .get("lst1111"));
+        // :-cont111
+        // : lst11_3
+
+    }
+
     private void checkDataFromJson(Cont dataFromJson) {
         assertNotNull(dataFromJson.getLfs().get("lf11"));
         assertEquals(dataFromJson.getLfs().get("lf11"), new Lf("lf11", "lf"));
@@ -364,4 +482,142 @@ public class YangAndXmlToJsonBasicYangTypesTest {
         assertNotNull(lst11_1_cont_lst1111_2);
     }
 
+    private CompositeNode prepareCompositeNodeWithEmpties() {
+        MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(
+                TestUtils.buildQName("cont1", "simple:yang:types", "2013-11-5"), null, null, ModifyAction.CREATE, null);
+
+        // lst11_1
+        MutableCompositeNode lst11_1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst11"), cont1,
+                null, ModifyAction.CREATE, null);
+        cont1.getChildren().add(lst11_1);
+
+        MutableSimpleNode<?> lf111_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_1, "1",
+                ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(lf111_1);
+
+        // lst111_1_1
+        MutableCompositeNode lst111_1_1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst111"),
+                lst11_1, null, ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(lst111_1_1);
+        MutableSimpleNode<?> lf1111_1_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111"),
+                lst111_1_1, "34", ModifyAction.CREATE, null);
+        lst111_1_1.getChildren().add(lf1111_1_1);
+        lst111_1_1.init();
+        // :lst111_1_1
+
+        // lst111_1_2
+        MutableCompositeNode lst111_1_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst111"),
+                lst11_1, null, ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(lst111_1_2);
+        MutableSimpleNode<?> lf1111_1_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111"),
+                lst111_1_2, "35", ModifyAction.CREATE, null);
+        lst111_1_2.getChildren().add(lf1111_1_2);
+        lst111_1_2.init();
+        // :lst111_1_2
+
+        // lst111_1_3
+        MutableCompositeNode lst111_1_3 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst111"),
+                lst11_1, null, ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(lst111_1_3);
+        lst111_1_2.init();
+        // :lst111_1_3
+
+        // lst111_1_4
+        MutableCompositeNode lst111_1_4 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst111"),
+                lst11_1, null, ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(lst111_1_4);
+        lst111_1_2.init();
+        // :lst111_1_4
+
+        MutableCompositeNode cont111_1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont111"),
+                lst11_1, null, ModifyAction.CREATE, null);
+        lst11_1.getChildren().add(cont111_1);
+
+        lst11_1.init();
+        // :lst11_1
+
+        // lst11_2
+        MutableCompositeNode lst11_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst11"), cont1,
+                null, ModifyAction.CREATE, null);
+        cont1.getChildren().add(lst11_2);
+
+        MutableSimpleNode<?> lf111_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_2, "2",
+                ModifyAction.CREATE, null);
+        lst11_2.getChildren().add(lf111_2);
+
+        // cont111_2
+        MutableCompositeNode cont111_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont111"),
+                lst11_2, null, ModifyAction.CREATE, null);
+        lst11_2.getChildren().add(cont111_2);
+
+        MutableSimpleNode<?> lflst1111_2_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lflst1111"),
+                cont111_2, "1024", ModifyAction.CREATE, null);
+        cont111_2.getChildren().add(lflst1111_2_2);
+        MutableSimpleNode<?> lflst1111_2_3 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lflst1111"),
+                cont111_2, "4096", ModifyAction.CREATE, null);
+        cont111_2.getChildren().add(lflst1111_2_3);
+
+        // lst1111_2
+        MutableCompositeNode lst1111_2_1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst1111"),
+                cont111_2, null, ModifyAction.CREATE, null);
+        cont111_2.getChildren().add(lst1111_2_1);
+        MutableSimpleNode<?> lf1111B_2_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111B"),
+                lst1111_2_1, "4", ModifyAction.CREATE, null);
+        lst1111_2_1.getChildren().add(lf1111B_2_1);
+        lst1111_2_1.init();
+
+        MutableCompositeNode lst1111_2_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst1111"),
+                cont111_2, null, ModifyAction.CREATE, null);
+        cont111_2.getChildren().add(lst1111_2_2);
+        MutableSimpleNode<?> lf1111B_2_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111A"),
+                lst1111_2_2, "lf1111A str12", ModifyAction.CREATE, null);
+        lst1111_2_2.getChildren().add(lf1111B_2_2);
+        lst1111_2_2.init();
+        // :lst1111_2
+
+        cont111_2.init();
+        // :cont111_2
+
+        MutableCompositeNode lst112_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst112"), lst11_2,
+                null, ModifyAction.CREATE, null);
+        lst11_2.getChildren().add(lst112_2);
+        lst112_2.init();
+        lst11_2.init();
+
+        // :lst11_2
+
+        // lst11_3
+        MutableCompositeNode lst11_3 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst11"), cont1,
+                null, ModifyAction.CREATE, null);
+        cont1.getChildren().add(lst11_3);
+
+        MutableSimpleNode<?> lf111_3 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_3, "3",
+                ModifyAction.CREATE, null);
+        lst11_3.getChildren().add(lf111_3);
+
+        // cont111_3
+        MutableCompositeNode cont111_3 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont111"),
+                lst11_3, null, ModifyAction.CREATE, null);
+        lst11_3.getChildren().add(cont111_3);
+
+        MutableCompositeNode lst1111_3_1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst1111"),
+                cont111_3, null, ModifyAction.CREATE, null);
+        cont111_3.getChildren().add(lst1111_3_1);
+        lst1111_3_1.init();
+
+        MutableCompositeNode lst1111_3_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst1111"),
+                cont111_3, null, ModifyAction.CREATE, null);
+        cont111_3.getChildren().add(lst1111_3_2);
+        lst1111_3_2.init();
+
+        cont111_3.init();
+        // :cont111_3
+
+        lst11_3.init();
+        // :lst11_3
+
+        cont1.init();
+        return cont1;
+    }
+
 }
index a1028ca0327ab58773f91c5c8de0c1b279f1ba60..00783537d8aff029b55ac1c0d9e6f25ec1f941b6 100644 (file)
@@ -24,7 +24,11 @@ public class Cont extends LstItem {
             return false;
         }
         Cont cont = (Cont) obj;
-        if (!this.name.equals(cont.name)) {
+        if (this.name == null) {
+            if (cont.name != null) {
+                return false;
+            }
+        } else if (!this.name.equals(cont.name)) {
             return false;
         }
         return true;
index 66cf1cc6101807b8a6f86aae7dacbb4541353dad..a1e06c3cabfa1d1cbeb207c676da1583cb4643d2 100644 (file)
@@ -2,6 +2,8 @@ package org.opendaylight.controller.sal.restconf.impl.test.structures;
 
 public class Lf extends YangElement {
     private String value;
+    private int numOfEqualItems = 0;
+
 
     public Lf(String name, String value) {
         super(name);
@@ -29,17 +31,30 @@ public class Lf extends YangElement {
             return false;
         }
         Lf lf = (Lf) obj;
-        if (!this.value.equals(lf.value)) {
+        if (this.value == null) {
+            if (lf.value != null) {
+                return false;
+            }
+        } else if (!this.value.equals(lf.value)) {
+            return false;
+        }
+        if (this.numOfEqualItems != lf.numOfEqualItems) {
             return false;
         }
         return true;
     }
+    
+    public void incNumOfEqualItems() {
+        this.numOfEqualItems++;
+    }
+    
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = super.hashCode();
         result = prime * result + ((value == null) ? 0 : value.hashCode());
+        result = prime * result + numOfEqualItems;
         return result;
     }
 
index 7688f2ed22bdf3817785cc368d6bd1cf0f0b7a44..87fed95f6ff15b4a2a3800a0adaa25c1aaf62ba0 100644 (file)
@@ -10,8 +10,16 @@ public class LfLst extends YangElement {
         lfs = new HashSet<>();
     }
 
+    public LfLst addLf(String value) {
+        return addLf(new Lf(value));
+    }
+
+    
     public LfLst addLf(Lf lf) {
-        lfs.add(lf);
+        while (this.lfs.contains(lf)) {
+            lf.incNumOfEqualItems();
+        }
+        this.lfs.add(lf);
         return this;
     }
 
@@ -31,7 +39,11 @@ public class LfLst extends YangElement {
             return false;
         }
         LfLst lfLst = (LfLst) obj;
-        if (!this.lfs.equals(lfLst.lfs)) {
+        if (this.lfs == null) {
+            if (lfLst.lfs != null) {
+                return false;
+            }
+        } else if (!this.lfs.equals(lfLst.lfs)) {
             return false;
         }
         return true;
index f5b3a5446e8f504c29f05989d9992cb2c7c448d1..56928e81e9d8ebdd15ec2442b576f0e1c0f7af3f 100644 (file)
@@ -10,9 +10,13 @@ public class Lst extends YangElement {
         lstItems = new HashSet<>();
     }
 
-    public void addLstItem(LstItem lstItem) {
+    public Lst addLstItem(LstItem lstItem) {
         lstItem.setLstName(name);
+        while (this.lstItems.contains(lstItem)) {
+            lstItem.incNumOfEqualItems();
+        }
         this.lstItems.add(lstItem);
+        return this;
     }
 
     public Set<LstItem> getLstItems() {
@@ -31,7 +35,11 @@ public class Lst extends YangElement {
             return false;
         }
         Lst lst = (Lst) obj;
-        if (!this.lstItems.equals(lst.lstItems)) {
+        if (this.lstItems == null) {
+            if (lst.lstItems != null) {
+                return false;
+            }
+        } else if (!this.lstItems.equals(lst.lstItems)) {
             return false;
         }
         return true;
index 22b1a3adf681a0efd2e38c212024f01255c1b230..9eb58b5344186690484658ffa489fc376df811cd 100644 (file)
@@ -8,6 +8,7 @@ public class LstItem {
     Map<String, LfLst> lfLsts;
     Map<String, Lst> lsts;
     Map<String, Cont> conts;
+    private int numOfEqualItems = 0;
 
     public LstItem() {
         lfs = new HashMap<>();
@@ -41,6 +42,11 @@ public class LstItem {
         return this;
     }
 
+    public LstItem addLf(String name, String value) {
+        lfs.put(name, new Lf(name, value));
+        return this;
+    }
+
     public void addLfLst(LfLst lfLst) {
         lfLsts.put(lfLst.getName(), lfLst);
     }
@@ -53,6 +59,10 @@ public class LstItem {
         conts.put(cont.getName(), cont);
     }
 
+    public void incNumOfEqualItems() {
+        this.numOfEqualItems++;
+    }
+
     @Override
     public boolean equals(Object obj) {
         if (this == obj) {
@@ -62,16 +72,35 @@ public class LstItem {
             return false;
         }
         LstItem lstItem = (LstItem) obj;
-        if (!this.conts.equals(lstItem.conts)) {
+        if (this.conts == null) {
+            if (lstItem.conts != null) {
+                return false;
+            }
+        } else if (!this.conts.equals(lstItem.conts)) {
+            return false;
+        }
+        if (this.lfs == null) {
+            if (lstItem.lfs != null) {
+                return false;
+            }
+        } else if (!this.lfs.equals(lstItem.lfs)) {
             return false;
         }
-        if (!this.lfs.equals(lstItem.lfs)) {
+        if (this.lfLsts == null) {
+            if (lstItem.lfLsts != null) {
+                return false;
+            }
+        } else if (!this.lfLsts.equals(lstItem.lfLsts)) {
             return false;
         }
-        if (!this.lfLsts.equals(lstItem.lfLsts)) {
+        if (this.lsts == null) {
+            if (lstItem.lsts != null) {
+                return false;
+            }
+        } else if (!this.lsts.equals(lstItem.lsts)) {
             return false;
         }
-        if (!this.lsts.equals(lstItem.lsts)) {
+        if (this.numOfEqualItems != lstItem.numOfEqualItems) {
             return false;
         }
         return true;
@@ -85,6 +114,7 @@ public class LstItem {
         result = prime * result + ((lfLsts == null) ? 0 : lfLsts.hashCode());
         result = prime * result + ((lsts == null) ? 0 : lsts.hashCode());
         result = prime * result + ((conts == null) ? 0 : conts.hashCode());
+        result = prime * result + numOfEqualItems;
         return result;
     }
 
index d10eb342c28e9be2998bb292fd024a592a6fce92..ed234582b8947b6400614b5d854323bc37049993 100644 (file)
@@ -6,7 +6,7 @@ public class YangElement {
     protected YangElement(String name) {
         this.name = name;
     }
-    
+
     public String getName() {
         return name;
     }
@@ -20,7 +20,11 @@ public class YangElement {
             return false;
         }
         YangElement yangElement = (YangElement) obj;
-        if (!this.name.equals(yangElement.name)) {
+        if (this.name == null) {
+            if (yangElement.name != null) {
+                return false;
+            }
+        } else if (!this.name.equals(yangElement.name)) {
             return false;
         }
         return true;
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_empty_data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_empty_data.json
new file mode 100644 (file)
index 0000000..4b19988
--- /dev/null
@@ -0,0 +1,49 @@
+{
+    "cont1": {
+        "lst11": [
+            {
+                "lf111": 1,
+                "lst111": [
+                    {
+                        "lf1111": 34
+                    },
+                    {
+                        "lf1111": 35
+                    },
+                    {},
+                    {}
+                ],
+                "cont111": {}
+            },
+            {
+                "lf111": 2,
+                "cont111": {
+                    "lflst1111": [
+                        1024,
+                        4096
+                    ],
+                    "lst1111": [
+                        {
+                            "lf1111B": 4
+                        },
+                        {
+                            "lf1111A": "lf1111A str12"
+                        }
+                    ]
+                },
+                "lst112": [
+                    {}
+                ]
+            },
+            {
+                "lf111": 3,
+                "cont111": {
+                    "lst1111": [
+                        {},
+                        {}
+                    ]
+                }
+            }
+        ]
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/empty_data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/empty_data.xml
new file mode 100644 (file)
index 0000000..68470ea
--- /dev/null
@@ -0,0 +1,40 @@
+<cont1>
+       <lst11>
+               <lf111>1</lf111>
+               <lst111></lst111>       
+               <lst111></lst111>       
+               <lst111>
+                       <lf1111></lf1111>
+               </lst111>       
+               <lst111>
+                       <lf1111>35</lf1111>
+               </lst111>       
+               <cont111></cont111>             
+       </lst11>
+       <lst11>
+               <lf111>2</lf111>
+               <cont111>
+                       <lf1111></lf1111>
+                       <lflst1111></lflst1111>
+                       <lflst1111>1024</lflst1111>
+                       <lflst1111>4096</lflst1111>
+                       <lst1111>
+                               <lf1111B>4</lf1111B>
+                       </lst1111>
+                       <lst1111>
+                               <lf1111A>lf1111A str12</lf1111A>
+                       </lst1111>
+               </cont111>
+               <lst112></lst112>
+       </lst11>
+       <lst11>
+               <lf111>3</lf111>
+               <cont111>
+                       <lf1111></lf1111>
+                       <lflst1111></lflst1111>
+                       <lflst1111></lflst1111>
+                       <lst1111></lst1111>
+                       <lst1111></lst1111>
+               </cont111>
+       </lst11>
+</cont1>