1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
6 import java.util.regex.Pattern;
10 public class YangAndXmlToJsonConversionRegExTest {
14 * Test for simple yang types (leaf, list, leaf-list, container and various combination of them)
17 public void simpleYangTypesTest() {
18 String jsonOutput = null;
20 // jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml",
21 // "/yang-to-json-conversion/simple-yang-types");
24 TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",true);
26 verifyJsonOutputForSimpleYangTypes(jsonOutput);
29 private void verifyJsonOutputForSimpleYangTypes(String jsonOutput) {
31 assertTrue("First and last character has to be '{' and '}'", Pattern.compile("\\A\\{.*\\}\\z", Pattern.DOTALL)
32 .matcher(jsonOutput).matches());
34 String prefix = "\"(smptp:|)";
37 String cont1 = prefix + "cont1\":\\{";
38 testLeaf(cont1, "lf11", new String("lf"), jsonOutput, prefix);
39 testLeafList(cont1, "lflst11", jsonOutput, prefix, new Integer(55), new Integer(56), new Integer(57));
40 testLeafList(cont1, "lflst12", jsonOutput, prefix, new String("lflst12 str1"), new String("lflst12 str2"),
41 new String("lflst12 str1"));
44 // first object of lst111
45 String lst11 = cont1 + ".*" + prefix + "lst11\":\\[\\{";
46 testLeaf(lst11, "lf111", new Integer(140), jsonOutput, prefix);
47 testLeaf(lst11, "lf112", new String("lf112 str"), jsonOutput, prefix);
49 // subnodes of cont111
50 String cont111 = lst11 + ".*" + prefix + "cont111\":\\{";
51 testLeaf(cont111, "lf1111", new String("lf1111 str"), jsonOutput, prefix);
52 testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2048), new Integer(1024), new Integer(4096));
54 // subnodes of lst1111
55 String lst1111 = cont111 + ".*" + prefix + "lst1111\":\\[\\{";
56 testLeaf(lst1111, "lf1111A", new String("lf1111A str11"), jsonOutput, prefix);
57 testLeaf(lst1111, "lf1111B", new Integer(4), jsonOutput, prefix);
58 testLeaf(lst1111, "lf1111A", new String("lf1111A str12"), jsonOutput, prefix);
59 testLeaf(lst1111, "lf1111B", new Integer(7), jsonOutput, prefix);
60 // :subnodes of lst1111
61 // :subnodes of cont111
62 // :first object of lst111
64 // second object of lst111
65 testLeaf(lst11, "lf111", new Integer(141), jsonOutput, prefix);
66 testLeaf(lst11, "lf112", new String("lf112 str2"), jsonOutput, prefix);
68 // subnodes of cont111
69 testLeaf(cont111, "lf1111", new String("lf1111 str2"), jsonOutput, prefix);
70 testLeafList(cont1, "lflst1111", jsonOutput, prefix, new Integer(2049), new Integer(1025), new Integer(4097));
72 // subnodes of lst1111
73 testLeaf(lst1111, "lf1111A", new String("lf1111A str21"), jsonOutput, prefix);
74 testLeaf(lst1111, "lf1111B", new Integer(5), jsonOutput, prefix);
75 testLeaf(lst1111, "lf1111A", new String("lf1111A str22"), jsonOutput, prefix);
76 testLeaf(lst1111, "lf1111B", new Integer(8), jsonOutput, prefix);
77 // :subnodes of lst111
78 // :subnodes of cont111
79 // :second object of lst111
80 // :second object of lst111
84 private void testLeaf(String prevRegEx, String lstName, Object value, String jsonFile, String elementPrefix) {
85 String newValue = null;
86 if (value instanceof Integer) {
87 newValue = value.toString();
88 } else if (value instanceof String) {
89 newValue = "\"" + value.toString() + "\"";
91 String lf = elementPrefix + lstName + "\":" + newValue;
92 assertTrue(">>\"" + lstName + "\":" + newValue + "<< is missing",
93 Pattern.compile(".*" + prevRegEx + ".*" + lf + ".*", Pattern.DOTALL).matcher(jsonFile).matches());
97 private void testLeafList(String prevRegEx, String lflstName, String jsonFile, String elementPrefix,
98 Object... dataInList) {
99 // order of element in the list isn't defined :(
100 String lflstBegin = elementPrefix + lflstName + "\":\\[";
101 String lflstEnd = ".*\\],";
103 ">>\"" + lflstName + "\": [],<< is missing",
104 Pattern.compile(".*" + prevRegEx + ".*" + lflstBegin + lflstEnd + ".*", Pattern.DOTALL)
105 .matcher(jsonFile).matches());
107 for (Object obj : dataInList) {
108 testValueOfLeafList(prevRegEx, lflstBegin, obj, jsonFile);
112 private void testValueOfLeafList(String prevRegEx, String lflstPrevRegEx, Object value, String jsonFile) {
113 String lflstData = null;
114 lflstData = regExForDataValueInList(value);
115 assertNotNull(lflstPrevRegEx + ": value doesn't have correct type.", lflstData);
117 prevRegEx + ": data value >" + value + "< is missing.",
118 Pattern.compile(".*" + prevRegEx + ".*" + lflstPrevRegEx + lflstData + ".*", Pattern.DOTALL)
119 .matcher(jsonFile).matches());
124 * Data value can be first, inner, last or only one member of list
129 private String regExForDataValueInList(Object dataValue) {
131 if (dataValue instanceof Integer) {
132 newDataValue = dataValue.toString();
133 return "(" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue + "(,[0-9]+)+|([0-9]+,)+" + newDataValue
134 + "|" + newDataValue + ")\\]";
135 } else if (dataValue instanceof String) {
136 newDataValue = "\"" + dataValue.toString() + "\"";
137 return "(" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue + "(,\".+\")+|(\".+\",)+" + newDataValue
138 + "|" + newDataValue + ")\\]";
146 // private String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) {
147 // String jsonResult = null;
148 // Set<Module> modules = null;
151 // modules = TestUtils.loadModules(YangAndXmlToJsonConversionJsonReaderTest.class.getResource(yangPath).getPath());
152 // } catch (FileNotFoundException e) {
153 // e.printStackTrace();
155 // assertNotNull("modules can't be null.", modules);
157 // InputStream xmlStream = YangAndXmlToJsonConversionJsonReaderTest.class.getResourceAsStream(xmlDataPath);
158 // CompositeNode compositeNode = null;
160 // compositeNode = TestUtils.loadCompositeNode(xmlStream);
161 // } catch (FileNotFoundException e) {
162 // e.printStackTrace();
164 // assertNotNull("Composite node can't be null", compositeNode);
166 // StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
167 // for (Module module : modules) {
168 // for (DataSchemaNode dataSchemaNode : module.getChildNodes()) {
169 // StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode);
170 // ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
172 // structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
173 // } catch (WebApplicationException | IOException e) {
174 // e.printStackTrace();
177 // "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
178 // byteArrayOS.toString().isEmpty());
179 // jsonResult = byteArrayOS.toString();
181 // outputToFile(byteArrayOS);
182 // } catch (IOException e) {
183 // System.out.println("Output file wasn't cloased sucessfuly.");
187 // return jsonResult;
190 // private void outputToFile(ByteArrayOutputStream outputStream) throws IOException {
191 // FileOutputStream fileOS = null;
193 // String path = YangAndXmlToJsonConversionJsonReaderTest.class.getResource("/yang-to-json-conversion/xml").getPath();
194 // File outFile = new File(path + "/data.json");
195 // fileOS = new FileOutputStream(outFile);
197 // fileOS.write(outputStream.toByteArray());
198 // } catch (IOException e) {
199 // e.printStackTrace();
202 // } catch (FileNotFoundException e1) {
203 // e1.printStackTrace();