X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FToJsonBasicYangTypesTest.java;fp=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FYangAndXmlToJsonBasicYangTypesTest.java;h=e7a185a01db90c35df5168a09a79535b4d28ad83;hp=3872964ab8b98bac645ea1ab40ca67b0389e2300;hb=b8465eb2a33d343c224b5dcf86662197ecd9343b;hpb=d8b2ea8f945aa225138d13f8681bd8ae879caf01 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java similarity index 50% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java index 3872964ab8..e7a185a01d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java @@ -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 lsts = dataFromJson.getLsts(); + assertEquals(1, lsts.size()); + Lst lst11 = lsts.get("lst11"); + assertNotNull(lst11); + Set 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; + } + }