Merge "creating a default subnet"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / YangAndXmlToJsonConversionRegExTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.util.regex.Pattern;
7
8 import org.junit.Test;
9
10 public class YangAndXmlToJsonConversionRegExTest {
11
12     @Test
13     /**
14      * Test for simple yang types (leaf, list, leaf-list, container and various combination of them)
15      * 
16      */
17     public void simpleYangTypesTest() {
18         String jsonOutput = null;
19
20 //        jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml",
21 //                "/yang-to-json-conversion/simple-yang-types");
22
23          jsonOutput =
24          TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",true);
25
26         verifyJsonOutputForSimpleYangTypes(jsonOutput);
27     }    
28
29     private void verifyJsonOutputForSimpleYangTypes(String jsonOutput) {
30
31         assertTrue("First and last character has to be '{' and '}'", Pattern.compile("\\A\\{.*\\}\\z", Pattern.DOTALL)
32                 .matcher(jsonOutput).matches());
33
34         String prefix = "\"(smptp:|)";
35
36         // subnodes of cont1
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"));
42
43         // subnodes of lst111
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);
48
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));
53
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
63
64         // second object of lst111
65         testLeaf(lst11, "lf111", new Integer(141), jsonOutput, prefix);
66         testLeaf(lst11, "lf112", new String("lf112 str2"), jsonOutput, prefix);
67
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));
71
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
81         // :subnodes of cont1
82     }
83
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() + "\"";
90         }
91         String lf = elementPrefix + lstName + "\":" + newValue;
92         assertTrue(">>\"" + lstName + "\":" + newValue + "<< is missing",
93                 Pattern.compile(".*" + prevRegEx + ".*" + lf + ".*", Pattern.DOTALL).matcher(jsonFile).matches());
94
95     }
96
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 = ".*\\],";
102         assertTrue(
103                 ">>\"" + lflstName + "\": [],<< is missing",
104                 Pattern.compile(".*" + prevRegEx + ".*" + lflstBegin + lflstEnd + ".*", Pattern.DOTALL)
105                         .matcher(jsonFile).matches());
106
107         for (Object obj : dataInList) {
108             testValueOfLeafList(prevRegEx, lflstBegin, obj, jsonFile);
109         }
110     }
111
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);
116         assertTrue(
117                 prevRegEx + ": data value >" + value + "< is missing.",
118                 Pattern.compile(".*" + prevRegEx + ".*" + lflstPrevRegEx + lflstData + ".*", Pattern.DOTALL)
119                         .matcher(jsonFile).matches());
120
121     }
122
123     /**
124      * Data value can be first, inner, last or only one member of list
125      * 
126      * @param dataValue
127      * @return
128      */
129     private String regExForDataValueInList(Object dataValue) {
130         String newDataValue;
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 + ")\\]";
139         }
140         return null;
141     }
142
143
144
145
146 //    private String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) {
147 //        String jsonResult = null;
148 //        Set<Module> modules = null;
149 //
150 //        try {
151 //            modules = TestUtils.loadModules(YangAndXmlToJsonConversionJsonReaderTest.class.getResource(yangPath).getPath());
152 //        } catch (FileNotFoundException e) {
153 //            e.printStackTrace();
154 //        }
155 //        assertNotNull("modules can't be null.", modules);
156 //
157 //        InputStream xmlStream = YangAndXmlToJsonConversionJsonReaderTest.class.getResourceAsStream(xmlDataPath);
158 //        CompositeNode compositeNode = null;
159 //        try {
160 //            compositeNode = TestUtils.loadCompositeNode(xmlStream);
161 //        } catch (FileNotFoundException e) {
162 //            e.printStackTrace();
163 //        }
164 //        assertNotNull("Composite node can't be null", compositeNode);
165 //
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();
171 //                try {
172 //                    structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
173 //                } catch (WebApplicationException | IOException e) {
174 //                    e.printStackTrace();
175 //                }
176 //                assertFalse(
177 //                        "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
178 //                        byteArrayOS.toString().isEmpty());
179 //                jsonResult = byteArrayOS.toString();
180 //                try {
181 //                    outputToFile(byteArrayOS);
182 //                } catch (IOException e) {
183 //                    System.out.println("Output file wasn't cloased sucessfuly.");
184 //                }
185 //            }
186 //        }
187 //        return jsonResult;
188 //    }
189 //
190 //    private void outputToFile(ByteArrayOutputStream outputStream) throws IOException {
191 //        FileOutputStream fileOS = null;
192 //        try {
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);
196 //            try {
197 //                fileOS.write(outputStream.toByteArray());
198 //            } catch (IOException e) {
199 //                e.printStackTrace();
200 //            }
201 //            fileOS.close();
202 //        } catch (FileNotFoundException e1) {
203 //            e1.printStackTrace();
204 //        }
205 //    }
206     
207
208 }