1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.*;
7 import java.util.regex.*;
9 import javax.ws.rs.WebApplicationException;
12 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
13 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
14 import org.opendaylight.yangtools.yang.model.api.*;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 public class YangAndXmlToJsonConversion {
22 * Test for simple yang types (leaf, list, leaf-list, container and various combination of them)
25 public void simpleYangTypesTest() {
26 String jsonOutput = null;
28 jsonOutput = convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml",
29 "/yang-to-json-conversion/simple-yang-types");
32 // readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/output.json");
34 verifyJsonOutputForSimpleYangTypes(jsonOutput);
38 private void verifyJsonOutputForSimpleYangTypes(String jsonOutput) {
40 assertTrue("First and last character has to be '{' and '}'", Pattern.compile("\\A\\{.*\\}\\z", Pattern.DOTALL)
41 .matcher(jsonOutput).matches());
43 String prefix = "\"(smptp:|)";
46 String cont1 = prefix + "cont1\":\\{";
47 testLeaf(cont1, "lf11", new String("lf"), jsonOutput, prefix);
48 testLeafList(cont1, "lflst11", jsonOutput, prefix, new Integer(55), new Integer(56), new Integer(57));
49 testLeafList(cont1, "lflst12", jsonOutput, prefix, new String("lflst12 str1"), new String("lflst12 str2"),
50 new String("lflst12 str1"));
53 // first object of lst111
54 String lst11 = cont1 + ".*" + prefix + "lst11\":\\[\\{";
55 testLeaf(lst11, "lf111", new Integer(140), jsonOutput, prefix);
56 testLeaf(lst11, "lf112", new String("lf112 str"), jsonOutput, prefix);
58 // subnodes of cont111
59 String cont111 = lst11 + ".*" + prefix + "cont111\":\\{";
60 testLeaf(cont111, "lf1111", new String("lf1111 str"), jsonOutput, prefix);
61 testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2048), new Integer(1024), new Integer(4096));
63 // subnodes of lst1111
64 String lst1111 = cont111 + ".*" + prefix + "lst1111\":\\[\\{";
65 testLeaf(lst1111, "lf1111A", new String("lf1111A str11"), jsonOutput, prefix);
66 testLeaf(lst1111, "lf1111B", new Integer(4), jsonOutput, prefix);
67 testLeaf(lst1111, "lf1111A", new String("lf1111A str12"), jsonOutput, prefix);
68 testLeaf(lst1111, "lf1111B", new Integer(7), jsonOutput, prefix);
69 // :subnodes of lst1111
70 // :subnodes of cont111
71 // :first object of lst111
73 // second object of lst111
74 testLeaf(lst11, "lf111", new Integer(141), jsonOutput, prefix);
75 testLeaf(lst11, "lf112", new String("lf112 str2"), jsonOutput, prefix);
77 // subnodes of cont111
78 testLeaf(cont111, "lf1111", new String("lf1111 str2"), jsonOutput, prefix);
79 testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2049), new Integer(1025), new Integer(4097));
81 // subnodes of lst1111
82 testLeaf(lst1111, "lf1111A", new String("lf1111A str21"), jsonOutput, prefix);
83 testLeaf(lst1111, "lf1111B", new Integer(5), jsonOutput, prefix);
84 testLeaf(lst1111, "lf1111A", new String("lf1111A str22"), jsonOutput, prefix);
85 testLeaf(lst1111, "lf1111B", new Integer(8), jsonOutput, prefix);
86 // :subnodes of lst111
87 // :subnodes of cont111
88 // :second object of lst111
89 // :second object of lst111
93 private void testLeaf(String prevRegEx, String lstName, Object value, String jsonFile, String elementPrefix) {
94 String newValue = null;
95 if (value instanceof Integer) {
96 newValue = value.toString();
97 } else if (value instanceof String) {
98 newValue = "\"" + value.toString() + "\"";
100 String lf = elementPrefix + lstName + "\":" + newValue;
101 assertTrue(">>\"" + lstName + "\":" + newValue + "<< is missing",
102 Pattern.compile(".*" + prevRegEx + ".*" + lf + ".*", Pattern.DOTALL).matcher(jsonFile).matches());
106 private void testLeafList(String prevRegEx, String lflstName, String jsonFile, String elementPrefix,
107 Object... dataInList) {
108 // order of element in the list isn't defined :(
109 String lflstBegin = elementPrefix + lflstName + "\":\\[";
110 String lflstEnd = ".*\\],";
112 ">>\"" + lflstName + "\": [],<< is missing",
113 Pattern.compile(".*" + prevRegEx + ".*" + lflstBegin + lflstEnd + ".*", Pattern.DOTALL)
114 .matcher(jsonFile).matches());
116 for (Object obj : dataInList) {
117 testValueOfLeafList(prevRegEx, lflstBegin, obj, jsonFile);
121 private void testValueOfLeafList(String prevRegEx, String lflstPrevRegEx, Object value, String jsonFile) {
122 String lflstData = null;
123 lflstData = regExForDataValueInList(value);
124 assertNotNull(lflstPrevRegEx + ": value doesn't have correct type.", lflstData);
126 prevRegEx + ": data value >" + value + "< is missing.",
127 Pattern.compile(".*" + prevRegEx + ".*" + lflstPrevRegEx + lflstData + ".*", Pattern.DOTALL)
128 .matcher(jsonFile).matches());
133 * Data value can be first, inner, last or only one member of list
138 private String regExForDataValueInList(Object dataValue) {
140 if (dataValue instanceof Integer) {
141 newDataValue = dataValue.toString();
142 return "(" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue
143 + "|" + newDataValue + ")\\]";
144 } else if (dataValue instanceof String) {
145 newDataValue = "\"" + dataValue.toString() + "\"";
146 return "(" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue
147 + "|" + newDataValue + ")\\]";
152 private String readJsonFromFile(String path) {
153 String fullPath = YangAndXmlToJsonConversion.class.getResource(path).getPath();
154 assertNotNull("Path to file can't be null.", fullPath);
155 File file = new File(fullPath);
156 assertNotNull("File can't be null", file);
157 FileReader fileReader = null;
159 fileReader = new FileReader(file);
160 } catch (FileNotFoundException e) {
163 assertNotNull("File reader can't be null.", fileReader);
165 StringBuilder strBuilder = new StringBuilder();
166 char[] buffer = new char[1000];
171 loadedCharNum = fileReader.read(buffer);
172 } catch (IOException e) {
175 if (loadedCharNum == -1) {
178 strBuilder.append(buffer, 0, loadedCharNum);
182 } catch (IOException e) {
183 System.out.println("The file wasn't closed");
186 String rawStr = strBuilder.toString();
187 rawStr = rawStr.replace("\n", "");
188 rawStr = rawStr.replace("\r", "");
189 rawStr = rawStr.replace("\t", "");
190 rawStr = removeSpaces(rawStr);
195 private String removeSpaces(String rawStr) {
196 StringBuilder strBuilder = new StringBuilder();
199 while (i < rawStr.length()) {
200 if (rawStr.substring(i, i + 1).equals("\"")) {
204 if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) {
205 strBuilder.append(rawStr.charAt(i));
210 return strBuilder.toString();
213 private String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) {
214 String jsonResult = null;
215 Set<Module> modules = null;
218 modules = TestUtils.loadModules(YangAndXmlToJsonConversion.class.getResource(yangPath).getPath());
219 } catch (FileNotFoundException e) {
222 assertNotNull("modules can't be null.", modules);
224 InputStream xmlStream = YangAndXmlToJsonConversion.class.getResourceAsStream(xmlDataPath);
225 CompositeNode compositeNode = null;
227 compositeNode = TestUtils.loadCompositeNode(xmlStream);
228 } catch (FileNotFoundException e) {
231 assertNotNull("Composite node can't be null", compositeNode);
233 StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
234 for (Module module : modules) {
235 for (DataSchemaNode dataSchemaNode : module.getChildNodes()) {
236 StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode);
237 ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
239 structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
240 } catch (WebApplicationException | IOException e) {
244 "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
245 byteArrayOS.toString().isEmpty());
246 jsonResult = byteArrayOS.toString();
248 outputToFile(byteArrayOS);
249 } catch (IOException e) {
250 System.out.println("Output file wasn't cloased sucessfuly.");
257 private void outputToFile(ByteArrayOutputStream outputStream) throws IOException {
258 FileOutputStream fileOS = null;
260 String path = YangAndXmlToJsonConversion.class.getResource("/yang-to-json-conversion/xml").getPath();
261 File outFile = new File(path + "/data.json");
262 fileOS = new FileOutputStream(outFile);
264 fileOS.write(outputStream.toByteArray());
265 } catch (IOException e) {
269 } catch (FileNotFoundException e1) {
270 e1.printStackTrace();