From befccf53b65b379b476290f55590f54c7f250ebb Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Mon, 11 Nov 2013 11:55:07 +0100 Subject: [PATCH] Yang and XML conversion to Json test with equals Remove of tests for Yang + XML to Json conversion. - Test with uses equals method do compare and which read json data to specific structures (test of list, leaves, leaflists, container) - Test for testing conversion of yang data types (boolean, uint8...) to json Change-Id: I672964ec8042d1ed5d7b0dfade76f5c7224d045c Signed-off-by: Jozef Gloncak --- .../sal/restconf/impl/test/TestUtils.java | 47 ++- .../YangAndXmlToJsonBasicDataTypesTest.java | 175 +++++++++ .../YangAndXmlToJsonBasicYangTypesTest.java | 367 ++++++++++++++++++ ...gAndXmlToJsonConversionJsonReaderTest.java | 243 ------------ .../YangAndXmlToJsonConversionRegExTest.java | 208 ---------- .../impl/test/structures/CompareLf.java | 21 + .../restconf/impl/test/structures/Cont.java | 46 +++ .../sal/restconf/impl/test/structures/Lf.java | 51 +++ .../restconf/impl/test/structures/LfLst.java | 54 +++ .../restconf/impl/test/structures/Lst.java | 48 +++ .../impl/test/structures/LstItem.java | 100 +++++ .../impl/test/structures/YangElement.java | 42 ++ .../simple-data-types/simple-data-types.yang | 134 +++++++ .../simple-data-types/xml/data.xml | 30 ++ .../simple-yang-types/simple-yang-types.yang | 10 + .../simple-yang-types/xml/awaited_output.json | 6 +- .../simple-yang-types/xml/data.xml | 60 ++- 17 files changed, 1142 insertions(+), 500 deletions(-) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicDataTypesTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java delete mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionJsonReaderTest.java delete mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/CompareLf.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Cont.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lst.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/YangElement.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index c6296eb7ea..e9552ec890 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -17,7 +17,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; +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; @@ -114,19 +114,19 @@ final class TestUtils { } } - - static String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) { + + static String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath, String outputPath) { String jsonResult = null; Set modules = null; try { - modules = TestUtils.loadModules(YangAndXmlToJsonConversionJsonReaderTest.class.getResource(yangPath).getPath()); + modules = TestUtils.loadModules(YangAndXmlToJsonBasicDataTypesTest.class.getResource(yangPath).getPath()); } catch (FileNotFoundException e) { e.printStackTrace(); } assertNotNull("modules can't be null.", modules); - InputStream xmlStream = YangAndXmlToJsonConversionJsonReaderTest.class.getResourceAsStream(xmlDataPath); + InputStream xmlStream = YangAndXmlToJsonBasicDataTypesTest.class.getResourceAsStream(xmlDataPath); CompositeNode compositeNode = null; try { compositeNode = TestUtils.loadCompositeNode(xmlStream); @@ -137,9 +137,9 @@ final class TestUtils { StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; for (Module module : modules) { + ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); for (DataSchemaNode dataSchemaNode : module.getChildNodes()) { StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode); - ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); try { structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS); } catch (WebApplicationException | IOException e) { @@ -148,21 +148,22 @@ final class TestUtils { assertFalse( "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(), byteArrayOS.toString().isEmpty()); - jsonResult = byteArrayOS.toString(); - try { - outputToFile(byteArrayOS); - } catch (IOException e) { - System.out.println("Output file wasn't cloased sucessfuly."); - } } + jsonResult = byteArrayOS.toString(); + try { + outputToFile(byteArrayOS, outputPath); + } catch (IOException e) { + System.out.println("Output file wasn't cloased sucessfuly."); + } + } return jsonResult; } - - static void outputToFile(ByteArrayOutputStream outputStream) throws IOException { + + static void outputToFile(ByteArrayOutputStream outputStream, String outputDir) throws IOException { FileOutputStream fileOS = null; try { - String path = YangAndXmlToJsonConversionJsonReaderTest.class.getResource("/yang-to-json-conversion/xml").getPath(); + String path = YangAndXmlToJsonBasicDataTypesTest.class.getResource(outputDir).getPath(); File outFile = new File(path + "/data.json"); fileOS = new FileOutputStream(outFile); try { @@ -174,9 +175,9 @@ final class TestUtils { } catch (FileNotFoundException e1) { e1.printStackTrace(); } - } - - static String readJsonFromFile(String path,boolean removeWhiteChars) { + } + + static String readJsonFromFile(String path, boolean removeWhiteChars) { FileReader fileReader = getFileReader(path); StringBuilder strBuilder = new StringBuilder(); @@ -200,7 +201,7 @@ final class TestUtils { System.out.println("The file wasn't closed"); } String rawStr = strBuilder.toString(); - if (removeWhiteChars) { + if (removeWhiteChars) { rawStr = rawStr.replace("\n", ""); rawStr = rawStr.replace("\r", ""); rawStr = rawStr.replace("\t", ""); @@ -209,9 +210,9 @@ final class TestUtils { return rawStr; } - + private static FileReader getFileReader(String path) { - String fullPath = YangAndXmlToJsonConversionJsonReaderTest.class.getResource(path).getPath(); + String fullPath = YangAndXmlToJsonBasicDataTypesTest.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); @@ -241,8 +242,6 @@ final class TestUtils { } return strBuilder.toString(); - } - - + } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicDataTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicDataTypesTest.java new file mode 100644 index 0000000000..5bc0644ee6 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicDataTypesTest.java @@ -0,0 +1,175 @@ +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 java.io.*; +import java.util.*; + +import org.junit.Test; +import com.google.gson.stream.*; + +public class YangAndXmlToJsonBasicDataTypesTest { + + @Test + public void simpleYangDataTest() { + 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-data-types/xml/data.xml", + "/yang-to-json-conversion/simple-data-types", "/yang-to-json-conversion/simple-data-types/xml"); + verifyJsonOutput(jsonOutput); + } + + private void verifyJsonOutput(String jsonOutput) { + StringReader strReader = new StringReader(jsonOutput); + JsonReader jReader = new JsonReader(strReader); + + String exception = null; + try { + jsonReadCont(jReader); + } catch (IOException e) { + exception = e.getMessage(); + } + + assertNull("Error during reading Json output: " + exception, exception); + } + + private void jsonReadCont(JsonReader jReader) throws IOException { + jReader.beginObject(); + assertNotNull("cont1 is missing.", jReader.hasNext()); + + // Cont dataFromJson = new Cont(jReader.nextName()); + jReader.nextName(); + jsonReadContElements(jReader); + + assertFalse("cont shouldn't have other element.", jReader.hasNext()); + jReader.endObject(); + // return dataFromJson; + } + + private void jsonReadContElements(JsonReader jReader) throws IOException { + jReader.beginObject(); + List loadedLfs = new ArrayList<>(); + boolean exceptForDecimal5Raised = false; + boolean enumChecked = false; + boolean bitsChecked = false; + boolean lfdecimal6Checked = false; + boolean lfdecimal4Checked = false; + boolean lfdecimal3Checked = false; + boolean lfdecimal2Checked = false; + boolean lfdecimal1Checked = false; + boolean lfbool1Checked = false; + boolean lfbool2Checked = false; + boolean lfstrChecked = false; + boolean lfbinaryChecked = false; + // boolean lfref1Checked = false; + boolean lfemptyChecked = false; + + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + JsonToken peek = null; + try { + peek = jReader.peek(); + } catch (IOException e) { + if (keyName.equals("lfdecimal5")) { + exceptForDecimal5Raised = true; + } else { + assertTrue("Key " + keyName + " has incorrect value for specifed type", false); + } + } + + if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + try { + jReader.nextLong(); + } catch (NumberFormatException e) { + assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false); + } + loadedLfs.add(keyName.substring(3)); + } else if (keyName.equals("lfstr")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("lfstr", jReader.nextString()); + lfstrChecked = true; + } else if (keyName.equals("lfbool1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(true, jReader.nextBoolean()); + lfbool1Checked = true; + } else if (keyName.equals("lfbool2")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(false, jReader.nextBoolean()); + lfbool2Checked = true; + } else if (keyName.equals("lfbool3")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(false, jReader.nextBoolean()); + } else if (keyName.equals("lfdecimal1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43.32), (Double) jReader.nextDouble()); + lfdecimal1Checked = true; + } else if (keyName.equals("lfdecimal2")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(-0.43), (Double) jReader.nextDouble()); + lfdecimal2Checked = true; + } else if (keyName.equals("lfdecimal3")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43), (Double) jReader.nextDouble()); + lfdecimal3Checked = true; + } else if (keyName.equals("lfdecimal4")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43E3), (Double) jReader.nextDouble()); + lfdecimal4Checked = true; + } else if (keyName.equals("lfdecimal6")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(33.12345), (Double) jReader.nextDouble()); + lfdecimal6Checked = true; + } else if (keyName.equals("lfenum")) { + assertEquals("enum3", jReader.nextString()); + enumChecked = true; + } else if (keyName.equals("lfbits")) { + assertEquals("bit3", jReader.nextString()); + bitsChecked = true; + } else if (keyName.equals("lfbinary")) { + assertEquals("AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%%-#^", jReader.nextString()); + lfbinaryChecked = true; + } else if (keyName.equals("lfempty")) { + jReader.beginArray(); + jReader.nextNull(); + jReader.endArray(); + lfemptyChecked = true; + // TODO: test will be implemented when functionality will be + // implemented + } else if (keyName.equals("lflstunion")) { + jReader.skipValue(); + } else { + assertTrue("Key " + keyName + " doesn't exists in yang file.", false); + } + + } + Collections.sort(loadedLfs); + String expectedLfsStr = "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]"; + String actualLfsStr = loadedLfs.toString(); + assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr); + // assertTrue("For lfdecimal5 wasn't catch error",exceptForDecimal5Raised); + assertTrue("Enum wasn't checked", enumChecked); + assertTrue("Bits wasn't checked", bitsChecked); + assertTrue("Decimal1 wasn't checked", lfdecimal1Checked); + assertTrue("Decimal2 wasn't checked", lfdecimal2Checked); + assertTrue("Decimal3 wasn't checked", lfdecimal3Checked); + assertTrue("Decimal4 wasn't checked", lfdecimal4Checked); + assertTrue("Decimal5 wasn't checked", lfdecimal6Checked); + assertTrue("lfbool1 wasn't checked", lfbool1Checked); + assertTrue("lfbool2 wasn't checked", lfbool2Checked); + assertTrue("lfstr wasn't checked", lfstrChecked); + assertTrue("lfbinary wasn't checked", lfbinaryChecked); + assertTrue("lfempty wasn't checked", lfemptyChecked); + // assertTrue("lfref1 wasn't checked", lfref1Checked); + + jReader.endObject(); + + } +} 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/YangAndXmlToJsonBasicYangTypesTest.java new file mode 100644 index 0000000000..3872964ab8 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonBasicYangTypesTest.java @@ -0,0 +1,367 @@ +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.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 com.google.gson.stream.JsonReader; + +public class YangAndXmlToJsonBasicYangTypesTest { + + @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", + "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml"); + + verifyJsonOutput(jsonOutput); + + } + + private void verifyJsonOutput(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); + checkDataFromJson(dataFromJson); + + assertNull("Error during reading Json output: " + exception, exception); + } + + private Cont jsonReadCont1(JsonReader jReader) throws IOException { + jReader.beginObject(); + assertNotNull("cont1 is missing.", jReader.hasNext()); + + Cont dataFromJson = new Cont(jReader.nextName()); + dataFromJson = jsonReadCont1Elements(jReader, dataFromJson); + + assertFalse("cont shouldn't have other element.", jReader.hasNext()); + jReader.endObject(); + return dataFromJson; + + } + + private Cont jsonReadCont1Elements(JsonReader jReader, Cont redData) throws IOException { + jReader.beginObject(); + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf11")) { + redData.addLf(new Lf(keyName, jReader.nextString())); + } else if (keyName.equals("lflst11")) { + LfLst lfLst = new LfLst(keyName); + lfLst = jsonReadLflstValues(jReader, lfLst); + redData.addLfLst(lfLst); + } else if (keyName.equals("lflst12")) { + LfLst lfLst = new LfLst(keyName); + jsonReadLflstValues(jReader, lfLst); + redData.addLfLst(lfLst); + } else if (keyName.equals("lst11")) { + Lst lst = new Lst(keyName); + lst = jsonReadLst11(jReader, lst); + redData.addLst(lst); + } else { + assertTrue("Key " + keyName + " doesn't exists in yang file.", false); + } + } + jReader.endObject(); + return redData; + + } + + private Lst jsonReadLst11(JsonReader jReader, Lst lst) throws IOException { + jReader.beginArray(); + + while (jReader.hasNext()) { + LstItem lstItem = jsonReadLst11Elements(jReader); + lst.addLstItem(lstItem); + } + jReader.endArray(); + return lst; + } + + private LstItem jsonReadLst11Elements(JsonReader jReader) throws IOException { + LstItem lstItem = new LstItem(); + jReader.beginObject(); + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf111")) { + lstItem.addLf(new Lf(keyName, jReader.nextString())); + } else if (keyName.equals("lf112")) { + lstItem.addLf(new Lf(keyName, jReader.nextString())); + } else if (keyName.equals("cont111")) { + Cont cont = new Cont(keyName); + cont = jsonReadCont111(jReader, cont); + lstItem.addCont(cont); + } else if (keyName.equals("lst111")) { + Lst lst = new Lst(keyName); + lst = jsonReadLst111(jReader, lst); + lstItem.addLst(lst); + } else if (keyName.equals("lst112")) { + Lst lst = new Lst(keyName); + lst = jsonReadLst112(jReader, lst); + lstItem.addLst(lst); + } else { + assertTrue("Key " + keyName + " doesn't exists in yang file.", false); + } + } + jReader.endObject(); + return lstItem; + } + + private Lst jsonReadLst112(JsonReader jReader, Lst lst) throws IOException { + jReader.beginArray(); + while (jReader.hasNext()) { + LstItem lstItem = jsonReadLst112Elements(jReader); + lst.addLstItem(lstItem); + } + jReader.endArray(); + return lst; + } + + private LstItem jsonReadLst112Elements(JsonReader jReader) throws IOException { + LstItem lstItem = new LstItem(); + jReader.beginObject(); + if (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf1121")) { + lstItem.addLf(new Lf(keyName, jReader.nextString())); + } + } + jReader.endObject(); + return lstItem; + + } + + private Lst jsonReadLst111(JsonReader jReader, Lst lst) throws IOException { + jReader.beginArray(); + while (jReader.hasNext()) { + LstItem lstItem = jsonReadLst111Elements(jReader); + lst.addLstItem(lstItem); + } + jReader.endArray(); + return lst; + } + + private LstItem jsonReadLst111Elements(JsonReader jReader) throws IOException { + LstItem lstItem = new LstItem(); + jReader.beginObject(); + if (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf1111")) { + lstItem.addLf(new Lf(keyName, jReader.nextString())); + } + } + jReader.endObject(); + return lstItem; + } + + private Cont jsonReadCont111(JsonReader jReader, Cont cont) throws IOException { + jReader.beginObject(); + cont = jsonReadCont111Elements(jReader, cont); + jReader.endObject(); + return cont; + } + + private Cont jsonReadCont111Elements(JsonReader jReader, Cont cont) throws IOException { + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf1111")) { + cont.addLf(new Lf(keyName, jReader.nextString())); + } else if (keyName.equals("lflst1111")) { + LfLst lfLst = new LfLst(keyName); + lfLst = jsonReadLflstValues(jReader, lfLst); + cont.addLfLst(lfLst); + } else if (keyName.equals("lst1111")) { + Lst lst = new Lst(keyName); + lst = jsonReadLst1111(jReader, lst); + cont.addLst(lst); + } else { + assertTrue("Key " + keyName + " doesn't exists in yang file.", false); + } + } + return cont; + + } + + private Lst jsonReadLst1111(JsonReader jReader, Lst lst) throws IOException { + jReader.beginArray(); + while (jReader.hasNext()) { + LstItem lstItem = jsonReadLst1111Elements(jReader); + lst.addLstItem(lstItem); + } + jReader.endArray(); + return lst; + } + + private LstItem jsonReadLst1111Elements(JsonReader jReader) throws IOException { + jReader.beginObject(); + LstItem lstItem = new LstItem(); + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + if (keyName.equals("lf1111A") || keyName.equals("lf1111B")) { + lstItem.addLf(new Lf(keyName, jReader.nextString())); + } + } + jReader.endObject(); + return lstItem; + } + + private LfLst jsonReadLflstValues(JsonReader jReader, LfLst lfLst) throws IOException { + jReader.beginArray(); + while (jReader.hasNext()) { + lfLst.addLf(new Lf(jReader.nextString())); + } + jReader.endArray(); + return lfLst; + } + + private void checkDataFromJson(Cont dataFromJson) { + assertNotNull(dataFromJson.getLfs().get("lf11")); + assertEquals(dataFromJson.getLfs().get("lf11"), new Lf("lf11", "lf")); + + LfLst lflst11 = null; + LfLst lflst12 = null; + + lflst11 = dataFromJson.getLfLsts().get("lflst11"); + lflst12 = dataFromJson.getLfLsts().get("lflst12"); + + assertNotNull(lflst11); + assertNotNull(lflst12); + + assertEquals(3, lflst11.getLfs().size()); + assertTrue(lflst11.getLfs().contains(new Lf("55"))); + assertTrue(lflst11.getLfs().contains(new Lf("56"))); + assertTrue(lflst11.getLfs().contains(new Lf("57"))); + + assertEquals(3, lflst12.getLfs().size()); + assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str1"))); + assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str2"))); + assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str3"))); + + assertEquals(1, dataFromJson.getLsts().size()); + Lst lst11 = dataFromJson.getLsts().get("lst11"); + assertNotNull(lst11); + assertEquals(2, lst11.getLstItems().size()); + + LstItem lst11_1 = null; + LstItem lst11_2 = null; + for (LstItem lstItem : lst11.getLstItems()) { + Lf lf = lstItem.getLfs().get("lf111"); + if (lf != null && lf.getValue().equals("140")) { + lst11_1 = lstItem; + } else if (lf != null && lf.getValue().equals("141")) { + lst11_2 = lstItem; + } + } + + checkLst11_1(lst11_1); + checkLst11_2(lst11_2); + } + + private void checkLst11_2(LstItem lst11_2) { + assertNotNull(lst11_2); + assertEquals(2, lst11_2.getLfs().size()); + assertEquals(1, lst11_2.getConts().size()); + assertEquals(2, lst11_2.getLsts().size()); + + assertEquals(lst11_2.getLfs().get("lf112"), new Lf("lf112", "lf112 str2")); + + Cont lst11_2_cont = lst11_2.getConts().get("cont111"); + assertEquals(0, lst11_2_cont.getConts().size()); + assertEquals(1, lst11_2_cont.getLfLsts().size()); + assertEquals(1, lst11_2_cont.getLfs().size()); + assertEquals(1, lst11_2_cont.getLsts().size()); + + // cont111 check + assertEquals(new Lf("lf1111", "lf1111 str2"), lst11_2_cont.getLfs().get("lf1111")); + assertEquals(new LfLst("lflst1111").addLf(new Lf("2049")).addLf(new Lf("1025")).addLf(new Lf("4097")), + lst11_2_cont.getLfLsts().get("lflst1111")); + + assertNotNull(lst11_2_cont.getLsts().get("lst1111")); + checkLst1111(lst11_2_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str21"), new Lf( + "lf1111B", "5"), new Lf("lf1111A", "lf1111A str22"), new Lf("lf1111B", "8")); + + checkLst11x(lst11_2.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "55")), + new LstItem().addLf(new Lf("lf1111", "56"))); + checkLst11x(lst11_2.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str22")), + new LstItem().addLf(new Lf("lf1121", "lf1121 str21"))); + } + + private void checkLst11_1(LstItem lst11_1) { + assertNotNull(lst11_1); + + assertEquals(2, lst11_1.getLfs().size()); + assertEquals(1, lst11_1.getConts().size()); + assertEquals(2, lst11_1.getLsts().size()); + + assertEquals(lst11_1.getLfs().get("lf112"), new Lf("lf112", "lf112 str")); + + Cont lst11_1_cont = lst11_1.getConts().get("cont111"); + assertEquals(0, lst11_1_cont.getConts().size()); + assertEquals(1, lst11_1_cont.getLfLsts().size()); + assertEquals(1, lst11_1_cont.getLfs().size()); + assertEquals(1, lst11_1_cont.getLsts().size()); + + // cont111 check + assertEquals(new Lf("lf1111", "lf1111 str"), lst11_1_cont.getLfs().get("lf1111")); + assertEquals(new LfLst("lflst1111").addLf(new Lf("2048")).addLf(new Lf("1024")).addLf(new Lf("4096")), + lst11_1_cont.getLfLsts().get("lflst1111")); + + assertNotNull(lst11_1_cont.getLsts().get("lst1111")); + checkLst1111(lst11_1_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str11"), new Lf( + "lf1111B", "4"), new Lf("lf1111A", "lf1111A str12"), new Lf("lf1111B", "7")); + + checkLst11x(lst11_1.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "65"))); + checkLst11x(lst11_1.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str11"))); + } + + private void checkLst11x(Lst lst, LstItem... lstItems) { + assertNotNull(lst); + + Lst requiredLst = new Lst(lst.getName()); + for (LstItem lstItem : lstItems) { + requiredLst.addLstItem(lstItem); + } + + assertEquals(requiredLst, lst); + + } + + private void checkLst1111(Set lstItems, Lf lf11, Lf lf12, Lf lf21, Lf lf22) { + LstItem lst11_1_cont_lst1111_1 = null; + LstItem lst11_1_cont_lst1111_2 = null; + for (LstItem lstItem : lstItems) { + if (new LstItem().addLf(lf11).addLf(lf12).equals(lstItem)) { + lst11_1_cont_lst1111_1 = lstItem; + } else if (new LstItem().addLf(lf21).addLf(lf22).equals(lstItem)) { + lst11_1_cont_lst1111_2 = lstItem; + } + } + + assertNotNull(lst11_1_cont_lst1111_1); + assertNotNull(lst11_1_cont_lst1111_2); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionJsonReaderTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionJsonReaderTest.java deleted file mode 100644 index 0f39088055..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionJsonReaderTest.java +++ /dev/null @@ -1,243 +0,0 @@ -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.util.*; - -import org.junit.Test; - -import com.google.gson.stream.JsonReader; - -public class YangAndXmlToJsonConversionJsonReaderTest { - - @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", -// "/yang-to-json-conversion/simple-yang-types"); - - StringReader strReader = new StringReader(jsonOutput); - JsonReader jReader = new JsonReader(strReader); - try { - checkCont1(jReader); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void checkCont1(JsonReader jReader) throws IOException { - jReader.beginObject(); - assertNotNull("cont1 is missing.", jReader.hasNext()); - jReader.nextName(); - checkCont1Elements(jReader, prepareInputData(jReader), "cont1/"); - jReader.endObject(); - - } - - private Map prepareInputData(JsonReader jReader) { - Map dataMap = new HashMap<>(); - dataMap.put("cont1/lf11", "lf"); - dataMap.put("cont1/lflst11.1", "55"); - dataMap.put("cont1/lflst11.2", "56"); - dataMap.put("cont1/lflst11.3", "57"); - dataMap.put("cont1/lflst12.1", "lflst12 str1"); - dataMap.put("cont1/lflst12.2", "lflst12 str2"); - dataMap.put("cont1/lflst12.3", "lflst12 str3"); - - dataMap.put("cont1/lst11.1/lf111", "140"); - dataMap.put("cont1/lst11.1/lf112", "lf112 str"); - dataMap.put("cont1/lst11.1/cont111/lf1111", "lf1111 str"); - dataMap.put("cont1/lst11.1/cont111/lflst1111.1", "2048"); - dataMap.put("cont1/lst11.1/cont111/lflst1111.2", "1024"); - dataMap.put("cont1/lst11.1/cont111/lflst1111.3", "4096"); - dataMap.put("cont1/lst11.1/cont111/lst1111.1/lf1111A", "lf1111A str11"); - dataMap.put("cont1/lst11.1/cont111/lst1111.1/lf1111B", "4"); - dataMap.put("cont1/lst11.1/cont111/lst1111.2/lf1111A", "lf1111A str12"); - dataMap.put("cont1/lst11.1/cont111/lst1111.2/lf1111B", "7"); - dataMap.put("cont1/lst11.1/lst111.1/lf1111", "65"); - dataMap.put("cont1/lst11.1/lst112.1/lf1121", "lf1121 str11"); - - dataMap.put("cont1/lst11.2/lf111", "141"); - dataMap.put("cont1/lst11.2/lf112", "lf112 str2"); - dataMap.put("cont1/lst11.2/cont111/lf1111", "lf1111 str2"); - dataMap.put("cont1/lst11.2/cont111/lflst1111.1", "2049"); - dataMap.put("cont1/lst11.2/cont111/lflst1111.2", "1025"); - dataMap.put("cont1/lst11.2/cont111/lflst1111.3", "4097"); - dataMap.put("cont1/lst11.2/cont111/lst1111.1/lf1111A", "lf1111A str21"); - dataMap.put("cont1/lst11.2/cont111/lst1111.1/lf1111B", "5"); - dataMap.put("cont1/lst11.2/cont111/lst1111.2/lf1111A", "lf1111A str22"); - dataMap.put("cont1/lst11.2/cont111/lst1111.2/lf1111B", "8"); - dataMap.put("cont1/lst11.2/lst111.1/lf1111", "55"); - dataMap.put("cont1/lst11.2/lst111.2/lf1111", "56"); - dataMap.put("cont1/lst11.2/lst112.1/lf1121", "lf1121 str21"); - dataMap.put("cont1/lst11.2/lst112.2/lf1121", "lf1121 str22"); - - return dataMap; - - } - - private void checkCont1Elements(JsonReader jReader, Map dataMap, String pthPref) throws IOException { - Set keys = new HashSet<>(); - jReader.beginObject(); - while (jReader.hasNext()) { - String keyName = jReader.nextName(); - if (keyName.equals("lf11")) { - assertEquals("Key " + keyName + " has incorrect value.", dataMap.get(pthPref + keyName), - jReader.nextString()); - keys.add(keyName); - } else if (keyName.equals("lflst11")) { - checkLflstValues(jReader, pthPref + keyName, dataMap); - keys.add(keyName); - } else if (keyName.equals("lflst12")) { - checkLflstValues(jReader, pthPref + keyName, dataMap); - keys.add(keyName); - } else if (keyName.equals("lst11")) { - checkLst11(jReader, pthPref + keyName, dataMap); - keys.add(keyName); - } else { - assertTrue("Key " + keyName + " doesn't exists in yang file.", false); - } - } - jReader.endObject(); - assertEquals("Incorrect number of keys in cont1", 4, keys.size()); - - } - - private void checkLst11(JsonReader jReader, String pthPref, Map dataMap) throws IOException { - jReader.beginArray(); - - int arrayLength = 0; - while (jReader.hasNext()) { - checkLst11Elements(jReader, pthPref + "." + ++arrayLength + "/", dataMap); - } - jReader.endArray(); - assertEquals("Incorrect number of items in lst11 array.", 2, arrayLength); - } - - private void checkLst11Elements(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginObject(); - while (jReader.hasNext()) { - String keyName = jReader.nextName(); - if (keyName.equals("lf111")) { - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } else if (keyName.equals("lf112")) { - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } else if (keyName.equals("cont111")) { - checkCont111(jReader, pthPref + keyName, data); - } else if (keyName.equals("lst111")) { - checkLst111(jReader, pthPref + keyName, data); - } else if (keyName.equals("lst112")) { - checkLst112(jReader, pthPref + keyName, data); - } else { - assertTrue("Key " + keyName + " doesn't exists in yang file.", false); - } - } - jReader.endObject(); - } - - private void checkLst112(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginArray(); - int arrayIndex = 0; - while (jReader.hasNext()) { - checkLst112Elements(jReader, pthPref + "." + ++arrayIndex + "/", data); - } - jReader.endArray(); - } - - private void checkLst112Elements(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginObject(); - if (jReader.hasNext()) { - String keyName = jReader.nextName(); - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } - jReader.endObject(); - - } - - private void checkLst111(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginArray(); - int arrayIndex = 0; - while (jReader.hasNext()) { - checkLst111Elements(jReader, pthPref + "." + ++arrayIndex + "/", data); - } - jReader.endArray(); - } - - private void checkLst111Elements(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginObject(); - if (jReader.hasNext()) { - String keyName = jReader.nextName(); - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } - jReader.endObject(); - } - - private void checkCont111(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginObject(); - checkCont111Elements(jReader, pthPref + "/", data); - jReader.endObject(); - } - - private void checkCont111Elements(JsonReader jReader, String pthPref, Map data) throws IOException { - while (jReader.hasNext()) { - String keyName = jReader.nextName(); - if (keyName.equals("lf1111")) { - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } else if (keyName.equals("lflst1111")) { - checkLflstValues(jReader, pthPref + keyName, data); - } else if (keyName.equals("lst1111")) { - checkLst1111(jReader, pthPref + keyName, data); - } - } - - } - - private void checkLst1111(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginArray(); - int arrayIndex = 0; - while (jReader.hasNext()) { - checkLst1111Elements(jReader, pthPref + "." + ++arrayIndex + "/", data); - } - jReader.endArray(); - } - - private void checkLst1111Elements(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginObject(); - while (jReader.hasNext()) { - String keyName = jReader.nextName(); - if (keyName.equals("lf1111A")) { - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - - } else if (keyName.equals("lf1111B")) { - assertEquals("Incorrect value for key " + keyName, data.get(pthPref + keyName), jReader.nextString()); - } - } - jReader.endObject(); - } - - private void checkLflstValues(JsonReader jReader, String pthPref, Map data) throws IOException { - jReader.beginArray(); - int arrayIndex = 1; - String keyValue = null; - List searchedValues = new ArrayList<>(); - while ((keyValue = data.get(pthPref + "." + arrayIndex++)) != null) { - searchedValues.add(keyValue); - } - - while (jReader.hasNext()) { - String value = jReader.nextString(); - assertTrue("Value " + value + " of lflst " + pthPref + " wasn't found", searchedValues.contains(value)); - } - - jReader.endArray(); - } - - - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java deleted file mode 100644 index 5a8cd2c8ed..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java +++ /dev/null @@ -1,208 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.regex.Pattern; - -import org.junit.Test; - -public class YangAndXmlToJsonConversionRegExTest { - - @Test - /** - * Test for simple yang types (leaf, list, leaf-list, container and various combination of them) - * - */ - public void simpleYangTypesTest() { - String jsonOutput = null; - -// jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml", -// "/yang-to-json-conversion/simple-yang-types"); - - jsonOutput = - TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",true); - - verifyJsonOutputForSimpleYangTypes(jsonOutput); - } - - private void verifyJsonOutputForSimpleYangTypes(String jsonOutput) { - - assertTrue("First and last character has to be '{' and '}'", Pattern.compile("\\A\\{.*\\}\\z", Pattern.DOTALL) - .matcher(jsonOutput).matches()); - - String prefix = "\"(smptp:|)"; - - // subnodes of cont1 - String cont1 = prefix + "cont1\":\\{"; - testLeaf(cont1, "lf11", new String("lf"), jsonOutput, prefix); - testLeafList(cont1, "lflst11", jsonOutput, prefix, new Integer(55), new Integer(56), new Integer(57)); - testLeafList(cont1, "lflst12", jsonOutput, prefix, new String("lflst12 str1"), new String("lflst12 str2"), - new String("lflst12 str1")); - - // subnodes of lst111 - // first object of lst111 - String lst11 = cont1 + ".*" + prefix + "lst11\":\\[\\{"; - testLeaf(lst11, "lf111", new Integer(140), jsonOutput, prefix); - testLeaf(lst11, "lf112", new String("lf112 str"), jsonOutput, prefix); - - // subnodes of cont111 - String cont111 = lst11 + ".*" + prefix + "cont111\":\\{"; - testLeaf(cont111, "lf1111", new String("lf1111 str"), jsonOutput, prefix); - testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2048), new Integer(1024), new Integer(4096)); - - // subnodes of lst1111 - String lst1111 = cont111 + ".*" + prefix + "lst1111\":\\[\\{"; - testLeaf(lst1111, "lf1111A", new String("lf1111A str11"), jsonOutput, prefix); - testLeaf(lst1111, "lf1111B", new Integer(4), jsonOutput, prefix); - testLeaf(lst1111, "lf1111A", new String("lf1111A str12"), jsonOutput, prefix); - testLeaf(lst1111, "lf1111B", new Integer(7), jsonOutput, prefix); - // :subnodes of lst1111 - // :subnodes of cont111 - // :first object of lst111 - - // second object of lst111 - testLeaf(lst11, "lf111", new Integer(141), jsonOutput, prefix); - testLeaf(lst11, "lf112", new String("lf112 str2"), jsonOutput, prefix); - - // subnodes of cont111 - testLeaf(cont111, "lf1111", new String("lf1111 str2"), jsonOutput, prefix); - testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2049), new Integer(1025), new Integer(4097)); - - // subnodes of lst1111 - testLeaf(lst1111, "lf1111A", new String("lf1111A str21"), jsonOutput, prefix); - testLeaf(lst1111, "lf1111B", new Integer(5), jsonOutput, prefix); - testLeaf(lst1111, "lf1111A", new String("lf1111A str22"), jsonOutput, prefix); - testLeaf(lst1111, "lf1111B", new Integer(8), jsonOutput, prefix); - // :subnodes of lst111 - // :subnodes of cont111 - // :second object of lst111 - // :second object of lst111 - // :subnodes of cont1 - } - - private void testLeaf(String prevRegEx, String lstName, Object value, String jsonFile, String elementPrefix) { - String newValue = null; - if (value instanceof Integer) { - newValue = value.toString(); - } else if (value instanceof String) { - newValue = "\"" + value.toString() + "\""; - } - String lf = elementPrefix + lstName + "\":" + newValue; - assertTrue(">>\"" + lstName + "\":" + newValue + "<< is missing", - Pattern.compile(".*" + prevRegEx + ".*" + lf + ".*", Pattern.DOTALL).matcher(jsonFile).matches()); - - } - - private void testLeafList(String prevRegEx, String lflstName, String jsonFile, String elementPrefix, - Object... dataInList) { - // order of element in the list isn't defined :( - String lflstBegin = elementPrefix + lflstName + "\":\\["; - String lflstEnd = ".*\\],"; - assertTrue( - ">>\"" + lflstName + "\": [],<< is missing", - Pattern.compile(".*" + prevRegEx + ".*" + lflstBegin + lflstEnd + ".*", Pattern.DOTALL) - .matcher(jsonFile).matches()); - - for (Object obj : dataInList) { - testValueOfLeafList(prevRegEx, lflstBegin, obj, jsonFile); - } - } - - private void testValueOfLeafList(String prevRegEx, String lflstPrevRegEx, Object value, String jsonFile) { - String lflstData = null; - lflstData = regExForDataValueInList(value); - assertNotNull(lflstPrevRegEx + ": value doesn't have correct type.", lflstData); - assertTrue( - prevRegEx + ": data value >" + value + "< is missing.", - Pattern.compile(".*" + prevRegEx + ".*" + lflstPrevRegEx + lflstData + ".*", Pattern.DOTALL) - .matcher(jsonFile).matches()); - - } - - /** - * Data value can be first, inner, last or only one member of list - * - * @param dataValue - * @return - */ - private String regExForDataValueInList(Object dataValue) { - String newDataValue; - if (dataValue instanceof Integer) { - newDataValue = dataValue.toString(); - return "(" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue - + "|" + newDataValue + ")\\]"; - } else if (dataValue instanceof String) { - newDataValue = "\"" + dataValue.toString() + "\""; - return "(" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue - + "|" + newDataValue + ")\\]"; - } - return null; - } - - - - -// private String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) { -// String jsonResult = null; -// Set modules = null; -// -// try { -// modules = TestUtils.loadModules(YangAndXmlToJsonConversionJsonReaderTest.class.getResource(yangPath).getPath()); -// } catch (FileNotFoundException e) { -// e.printStackTrace(); -// } -// assertNotNull("modules can't be null.", modules); -// -// InputStream xmlStream = YangAndXmlToJsonConversionJsonReaderTest.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; -// for (Module module : modules) { -// for (DataSchemaNode dataSchemaNode : module.getChildNodes()) { -// StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode); -// ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); -// try { -// structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS); -// } catch (WebApplicationException | IOException e) { -// e.printStackTrace(); -// } -// assertFalse( -// "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(), -// byteArrayOS.toString().isEmpty()); -// jsonResult = byteArrayOS.toString(); -// try { -// outputToFile(byteArrayOS); -// } catch (IOException e) { -// System.out.println("Output file wasn't cloased sucessfuly."); -// } -// } -// } -// return jsonResult; -// } -// -// private void outputToFile(ByteArrayOutputStream outputStream) throws IOException { -// FileOutputStream fileOS = null; -// try { -// String path = YangAndXmlToJsonConversionJsonReaderTest.class.getResource("/yang-to-json-conversion/xml").getPath(); -// File outFile = new File(path + "/data.json"); -// fileOS = new FileOutputStream(outFile); -// try { -// fileOS.write(outputStream.toByteArray()); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// fileOS.close(); -// } catch (FileNotFoundException e1) { -// e1.printStackTrace(); -// } -// } - - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/CompareLf.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/CompareLf.java new file mode 100644 index 0000000000..96b9b35edb --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/CompareLf.java @@ -0,0 +1,21 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class CompareLf { + + @Test + public void test() { + Lf lf1 = new Lf("name", "value"); + Lf lf2 = new Lf("name", "value"); + Lf lf3 = new Lf("name1", "value"); + Lf lf4 = new Lf("name", "value1"); + + assertTrue(lf1.equals(lf2)); + assertFalse(lf1.equals(lf3)); + assertFalse(lf1.equals(lf4)); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Cont.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Cont.java new file mode 100644 index 0000000000..a1028ca032 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Cont.java @@ -0,0 +1,46 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +public class Cont extends LstItem { + String name = null; + + public Cont(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + if (!super.equals(obj)) { + return false; + } + Cont cont = (Cont) obj; + if (!this.name.equals(cont.name)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((this.name == null) ? 0 : this.name.hashCode()); + return result; + } + + @Override + public String toString() { + return name; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java new file mode 100644 index 0000000000..66cf1cc610 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java @@ -0,0 +1,51 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +public class Lf extends YangElement { + private String value; + + public Lf(String name, String value) { + super(name); + this.value = value; + } + + public Lf(String value) { + super(""); + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + if (!super.equals(obj)) { + return false; + } + Lf lf = (Lf) obj; + if (!this.value.equals(lf.value)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public String toString() { + return super.toString() + ":" + value; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java new file mode 100644 index 0000000000..7688f2ed22 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java @@ -0,0 +1,54 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +import java.util.*; + +public class LfLst extends YangElement { + Set lfs; + + public LfLst(String name) { + super(name); + lfs = new HashSet<>(); + } + + public LfLst addLf(Lf lf) { + lfs.add(lf); + return this; + } + + public Set getLfs() { + return lfs; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + if (!super.equals(obj)) { + return false; + } + LfLst lfLst = (LfLst) obj; + if (!this.lfs.equals(lfLst.lfs)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((lfs == null) ? 0 : lfs.hashCode()); + return result; + } + + @Override + public String toString() { + + return super.toString(); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lst.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lst.java new file mode 100644 index 0000000000..f5b3a5446e --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lst.java @@ -0,0 +1,48 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +import java.util.*; + +public class Lst extends YangElement { + private Set lstItems; + + public Lst(String name) { + super(name); + lstItems = new HashSet<>(); + } + + public void addLstItem(LstItem lstItem) { + lstItem.setLstName(name); + this.lstItems.add(lstItem); + } + + public Set getLstItems() { + return lstItems; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + if (!super.equals(obj)) { + return false; + } + Lst lst = (Lst) obj; + if (!this.lstItems.equals(lst.lstItems)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((lstItems == null) ? 0 : lstItems.hashCode()); + return result; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java new file mode 100644 index 0000000000..22b1a3adf6 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java @@ -0,0 +1,100 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +import java.util.*; + +public class LstItem { + String lstName; + Map lfs; + Map lfLsts; + Map lsts; + Map conts; + + public LstItem() { + lfs = new HashMap<>(); + conts = new HashMap<>(); + lfLsts = new HashMap<>(); + lsts = new HashMap<>(); + } + + public Map getLsts() { + return lsts; + } + + public Map getConts() { + return conts; + } + + public Map getLfLsts() { + return lfLsts; + } + + public Map getLfs() { + return lfs; + } + + public String getLstName() { + return lstName; + } + + public LstItem addLf(Lf lf) { + lfs.put(lf.getName(), lf); + return this; + } + + public void addLfLst(LfLst lfLst) { + lfLsts.put(lfLst.getName(), lfLst); + } + + public void addLst(Lst lst) { + lsts.put(lst.getName(), lst); + } + + public void addCont(Cont cont) { + conts.put(cont.getName(), cont); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + LstItem lstItem = (LstItem) obj; + if (!this.conts.equals(lstItem.conts)) { + return false; + } + if (!this.lfs.equals(lstItem.lfs)) { + return false; + } + if (!this.lfLsts.equals(lstItem.lfLsts)) { + return false; + } + if (!this.lsts.equals(lstItem.lsts)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((lfs == null) ? 0 : lfs.hashCode()); + result = prime * result + ((lfLsts == null) ? 0 : lfLsts.hashCode()); + result = prime * result + ((lsts == null) ? 0 : lsts.hashCode()); + result = prime * result + ((conts == null) ? 0 : conts.hashCode()); + return result; + } + + @Override + public String toString() { + return "lst item of " + lstName; + } + + public void setLstName(String name) { + this.lstName = name; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/YangElement.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/YangElement.java new file mode 100644 index 0000000000..d10eb342c2 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/YangElement.java @@ -0,0 +1,42 @@ +package org.opendaylight.controller.sal.restconf.impl.test.structures; + +public class YangElement { + protected String name; + + protected YangElement(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!this.getClass().equals(obj.getClass())) { + return false; + } + YangElement yangElement = (YangElement) obj; + if (!this.name.equals(yangElement.name)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public String toString() { + return name; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang new file mode 100644 index 0000000000..4ad0983e04 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang @@ -0,0 +1,134 @@ +module simple-data-types { + namespace "simple:data:types"; + + prefix "smpdtp"; + revision 2013-11-12 { + } + + container cont { + leaf lfnint8Min { + type int8; + } + leaf lfnint8Max { + type int8; + } + leaf lfnint16Min { + type int16; + } + leaf lfnint16Max { + type int16; + } + leaf lfnint32Min { + type int32; + } + leaf lfnint32Max { + type int32; + } + leaf lfnint64Min { + type int64; + } + leaf lfnint64Max { + type int64; + } + + leaf lfnuint8Max { + type uint8; + } + leaf lfnuint16Max { + type uint16; + } + leaf lfnuint32Max { + type uint32; + } + leaf lfuint64Max { + type uint64; + } + leaf lfstr { + type string; + } + leaf lfbool1 { + type boolean; + } + leaf lfbool2 { + type boolean; + } + leaf lfbool3 { + type boolean; + } + leaf lfdecimal1 { + type decimal64 { + fraction-digits 2; + } + } + leaf lfdecimal2 { + type decimal64 { + fraction-digits 2; + } + } + leaf lfdecimal3 { + type decimal64 { + fraction-digits 2; + } + } + + leaf lfdecimal4 { + type decimal64 { + fraction-digits 2; + } + } + + + leaf lfdecimal6 { + type decimal64 { + fraction-digits 2; + } + } + + leaf lfenum { + type enumeration { + enum enum1; + enum enum2; + enum enum3; + enum enum4; + } + } + + leaf lfbits { + type bits { + bit bit1; + bit bit2; + bit bit3; + bit bit4; + } + } + + leaf lfbinary { + type binary; + } + + leaf lfref1 { //reference to string type + type leafref { + path "../lfstr"; + } + } + + leaf lfref2 { //reference to number type + type leafref { + path "../lfnint8Max"; + } + } + + leaf lfempty { + type empty; + } + + leaf-list lflstunion { + type union { + type uint16; + type string; + } + } + + + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml new file mode 100644 index 0000000000..1e83cb4f69 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml @@ -0,0 +1,30 @@ + + -128 + 127 + -32768 + 32767 + -2147483648 + 2147483647 + -9223372036854775808 + 9223372036854775807 + 255 + 65535 + 4294967295 + lfstr + true + false + bla + 43.32 + -0.43 + 43 + 43E3 + 33.12345 + enum3 + bit3 + AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%%-#^ + + 324 + 33.3 + lfunion + true + \ 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/simple-yang-types.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/simple-yang-types.yang index 760dbd73ed..0216e97cfb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/simple-yang-types.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/simple-yang-types.yang @@ -40,6 +40,16 @@ module simple-yang-types { } } } + list lst111 { + leaf lf1111 { + type int32; + } + } + list lst112 { + leaf lf1121 { + type string; + } + } } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json index a0e5f2f0f2..8888e7f5fb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json @@ -1,5 +1,5 @@ { - "smptp:cont1": { + "cont1": { "lf11":"lf", "lflst11": [55,56,57], "lflst12": ["lflst12 str1", "lflst12 str2", "lflst12 str3"], @@ -58,8 +58,6 @@ "lf1111" : 56 } ], - } - ] "lst112" : [ { "lf1121" : "lf1121 str21" @@ -69,6 +67,6 @@ } ] } - ] + ] } } \ 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/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/data.xml index b47987ca23..17dc2657b0 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/data.xml +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/data.xml @@ -1,11 +1,40 @@ lf - 55 56 + 55 57 - lflst12 str1 - lflst12 str2 lflst12 str3 + + + lf1121 str22 + + 141 + lf112 str2 + + 55 + + + 4097 + 2049 + 1025 + + lf1111A str22 + 8 + + lf1111 str2 + + 5 + lf1111A str21 + + + + 56 + + + lf1121 str21 + + + lflst12 str1 140 lf112 str @@ -23,23 +52,12 @@ 7 + + 65 + + + lf1121 str11 + - - 141 - lf112 str2 - - lf1111 str2 - 2049 - 1025 - 4097 - - lf1111A str21 - 5 - - - lf1111A str22 - 8 - - - + lflst12 str2 -- 2.36.6