From: Jozef Gloncak Date: Fri, 8 Nov 2013 09:21:29 +0000 (+0100) Subject: Test of conversing of yang + xml files to json 2. X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~458^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F42%2F2542%2F1;p=controller.git Test of conversing of yang + xml files to json 2. Test was broken to two test. First one uses regular expression. Second one uses JsonReader class. Change-Id: Iaeae471c9e93b873b37236926197dec439ea3611 Signed-off-by: Jozef Gloncak --- 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 b4cf6c985b..c6296eb7ea 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 @@ -1,15 +1,14 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStreamWriter; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Set; +import javax.ws.rs.WebApplicationException; import javax.xml.stream.XMLStreamException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; @@ -18,12 +17,13 @@ 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.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.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.*; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.Logger; @@ -114,5 +114,135 @@ final class TestUtils { } } + + static 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; + } + + static 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(); + } + } + + static String readJsonFromFile(String path,boolean removeWhiteChars) { + FileReader fileReader = getFileReader(path); + + StringBuilder strBuilder = new StringBuilder(); + char[] buffer = new char[1000]; + + while (true) { + int loadedCharNum; + try { + loadedCharNum = fileReader.read(buffer); + } catch (IOException e) { + break; + } + if (loadedCharNum == -1) { + break; + } + strBuilder.append(buffer, 0, loadedCharNum); + } + try { + fileReader.close(); + } catch (IOException e) { + System.out.println("The file wasn't closed"); + } + String rawStr = strBuilder.toString(); + if (removeWhiteChars) { + rawStr = rawStr.replace("\n", ""); + rawStr = rawStr.replace("\r", ""); + rawStr = rawStr.replace("\t", ""); + rawStr = removeSpaces(rawStr); + } + + return rawStr; + } + + private static FileReader getFileReader(String path) { + String fullPath = YangAndXmlToJsonConversionJsonReaderTest.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); + FileReader fileReader = null; + try { + fileReader = new FileReader(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + assertNotNull("File reader can't be null.", fileReader); + return fileReader; + } + + private static String removeSpaces(String rawStr) { + StringBuilder strBuilder = new StringBuilder(); + int i = 0; + int quoteCount = 0; + while (i < rawStr.length()) { + if (rawStr.substring(i, i + 1).equals("\"")) { + quoteCount++; + } + + if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) { + strBuilder.append(rawStr.charAt(i)); + } + i++; + } + + return strBuilder.toString(); + } + + } 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 new file mode 100644 index 0000000000..0f39088055 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionJsonReaderTest.java @@ -0,0 +1,243 @@ +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/YangAndXmlToJsonConversion.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java similarity index 51% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversion.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java index 841f8e593a..5a8cd2c8ed 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversion.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlToJsonConversionRegExTest.java @@ -1,22 +1,14 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import java.io.*; -import java.util.Set; -import java.util.regex.*; +import java.util.regex.Pattern; -import javax.ws.rs.WebApplicationException; +import org.junit.Test; -import org.junit.*; -import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; -import org.opendaylight.controller.sal.restconf.impl.StructuredData; -import org.opendaylight.yangtools.yang.model.api.*; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; +public class YangAndXmlToJsonConversionRegExTest { -public class YangAndXmlToJsonConversion { - - @Ignore @Test /** * Test for simple yang types (leaf, list, leaf-list, container and various combination of them) @@ -25,15 +17,14 @@ public class YangAndXmlToJsonConversion { public void simpleYangTypesTest() { String jsonOutput = null; - jsonOutput = convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml", - "/yang-to-json-conversion/simple-yang-types"); +// jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml", +// "/yang-to-json-conversion/simple-yang-types"); -// jsonOutput = -// readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/output.json"); + jsonOutput = + TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",true); verifyJsonOutputForSimpleYangTypes(jsonOutput); - - } + } private void verifyJsonOutputForSimpleYangTypes(String jsonOutput) { @@ -149,125 +140,69 @@ public class YangAndXmlToJsonConversion { return null; } - private String readJsonFromFile(String path) { - String fullPath = YangAndXmlToJsonConversion.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); - FileReader fileReader = null; - try { - fileReader = new FileReader(file); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - assertNotNull("File reader can't be null.", fileReader); - - StringBuilder strBuilder = new StringBuilder(); - char[] buffer = new char[1000]; - - while (true) { - int loadedCharNum; - try { - loadedCharNum = fileReader.read(buffer); - } catch (IOException e) { - break; - } - if (loadedCharNum == -1) { - break; - } - strBuilder.append(buffer, 0, loadedCharNum); - } - try { - fileReader.close(); - } catch (IOException e) { - System.out.println("The file wasn't closed"); - ; - } - String rawStr = strBuilder.toString(); - rawStr = rawStr.replace("\n", ""); - rawStr = rawStr.replace("\r", ""); - rawStr = rawStr.replace("\t", ""); - rawStr = removeSpaces(rawStr); - - return rawStr; - } - - private String removeSpaces(String rawStr) { - StringBuilder strBuilder = new StringBuilder(); - int i = 0; - int quoteCount = 0; - while (i < rawStr.length()) { - if (rawStr.substring(i, i + 1).equals("\"")) { - quoteCount++; - } - if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) { - strBuilder.append(rawStr.charAt(i)); - } - i++; - } - - return strBuilder.toString(); - } - private String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) { - String jsonResult = null; - Set modules = null; - try { - modules = TestUtils.loadModules(YangAndXmlToJsonConversion.class.getResource(yangPath).getPath()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - assertNotNull("modules can't be null.", modules); - - InputStream xmlStream = YangAndXmlToJsonConversion.class.getResourceAsStream(xmlDataPath); - CompositeNode compositeNode = null; - try { - compositeNode = TestUtils.loadCompositeNode(xmlStream); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - assertNotNull("Composite node can't be null", compositeNode); +// 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(); +// } +// } + - 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 = YangAndXmlToJsonConversion.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/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 e76e7bd9ef..a0e5f2f0f2 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 @@ -20,7 +20,18 @@ "lf1111B": 7 } ] - } + }, + "lst111" : [ + { + "lf1111" : 65 + } + ], + "lst112" : [ + { + "lf1121" : "lf1121 str11" + } + ] + }, { "lf111":141, @@ -38,8 +49,26 @@ "lf1111B": 8 } ] - } + }, + "lst111" : [ + { + "lf1111" : 55 + }, + { + "lf1111" : 56 + } + ], } ] + "lst112" : [ + { + "lf1121" : "lf1121 str21" + }, + { + "lf1121" : "lf1121 str22" + } + ] + } + ] } } \ No newline at end of file