3872964ab8b98bac645ea1ab40ca67b0389e2300
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / YangAndXmlToJsonBasicYangTypesTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.assertFalse;
8
9 import java.io.*;
10 import java.util.*;
11
12 import javax.validation.constraints.AssertFalse;
13
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.restconf.impl.test.structures.*;
16
17 import com.google.gson.stream.JsonReader;
18
19 public class YangAndXmlToJsonBasicYangTypesTest {
20
21     @Test
22     public void simpleYangTypesWithJsonReaderTest() {
23         String jsonOutput;
24         // jsonOutput =
25         // TestUtils.readJsonFromFile("/yang-to-json-conversion/simple-yang-types/xml/awaited_output.json",
26         // false);
27
28         jsonOutput = TestUtils.convertXmlDataAndYangToJson("/yang-to-json-conversion/simple-yang-types/xml/data.xml",
29                 "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml");
30
31         verifyJsonOutput(jsonOutput);
32
33     }
34
35     private void verifyJsonOutput(String jsonOutput) {
36         StringReader strReader = new StringReader(jsonOutput);
37         JsonReader jReader = new JsonReader(strReader);
38
39         String exception = null;
40         Cont dataFromJson = null;
41         try {
42             dataFromJson = jsonReadCont1(jReader);
43         } catch (IOException e) {
44             exception = e.getMessage();
45         }
46
47         assertNotNull("Data structures from json are missing.", dataFromJson);
48         checkDataFromJson(dataFromJson);
49
50         assertNull("Error during reading Json output: " + exception, exception);
51     }
52
53     private Cont jsonReadCont1(JsonReader jReader) throws IOException {
54         jReader.beginObject();
55         assertNotNull("cont1 is missing.", jReader.hasNext());
56
57         Cont dataFromJson = new Cont(jReader.nextName());
58         dataFromJson = jsonReadCont1Elements(jReader, dataFromJson);
59
60         assertFalse("cont shouldn't have other element.", jReader.hasNext());
61         jReader.endObject();
62         return dataFromJson;
63
64     }
65
66     private Cont jsonReadCont1Elements(JsonReader jReader, Cont redData) throws IOException {
67         jReader.beginObject();
68         while (jReader.hasNext()) {
69             String keyName = jReader.nextName();
70             if (keyName.equals("lf11")) {
71                 redData.addLf(new Lf(keyName, jReader.nextString()));
72             } else if (keyName.equals("lflst11")) {
73                 LfLst lfLst = new LfLst(keyName);
74                 lfLst = jsonReadLflstValues(jReader, lfLst);
75                 redData.addLfLst(lfLst);
76             } else if (keyName.equals("lflst12")) {
77                 LfLst lfLst = new LfLst(keyName);
78                 jsonReadLflstValues(jReader, lfLst);
79                 redData.addLfLst(lfLst);
80             } else if (keyName.equals("lst11")) {
81                 Lst lst = new Lst(keyName);
82                 lst = jsonReadLst11(jReader, lst);
83                 redData.addLst(lst);
84             } else {
85                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
86             }
87         }
88         jReader.endObject();
89         return redData;
90
91     }
92
93     private Lst jsonReadLst11(JsonReader jReader, Lst lst) throws IOException {
94         jReader.beginArray();
95
96         while (jReader.hasNext()) {
97             LstItem lstItem = jsonReadLst11Elements(jReader);
98             lst.addLstItem(lstItem);
99         }
100         jReader.endArray();
101         return lst;
102     }
103
104     private LstItem jsonReadLst11Elements(JsonReader jReader) throws IOException {
105         LstItem lstItem = new LstItem();
106         jReader.beginObject();
107         while (jReader.hasNext()) {
108             String keyName = jReader.nextName();
109             if (keyName.equals("lf111")) {
110                 lstItem.addLf(new Lf(keyName, jReader.nextString()));
111             } else if (keyName.equals("lf112")) {
112                 lstItem.addLf(new Lf(keyName, jReader.nextString()));
113             } else if (keyName.equals("cont111")) {
114                 Cont cont = new Cont(keyName);
115                 cont = jsonReadCont111(jReader, cont);
116                 lstItem.addCont(cont);
117             } else if (keyName.equals("lst111")) {
118                 Lst lst = new Lst(keyName);
119                 lst = jsonReadLst111(jReader, lst);
120                 lstItem.addLst(lst);
121             } else if (keyName.equals("lst112")) {
122                 Lst lst = new Lst(keyName);
123                 lst = jsonReadLst112(jReader, lst);
124                 lstItem.addLst(lst);
125             } else {
126                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
127             }
128         }
129         jReader.endObject();
130         return lstItem;
131     }
132
133     private Lst jsonReadLst112(JsonReader jReader, Lst lst) throws IOException {
134         jReader.beginArray();
135         while (jReader.hasNext()) {
136             LstItem lstItem = jsonReadLst112Elements(jReader);
137             lst.addLstItem(lstItem);
138         }
139         jReader.endArray();
140         return lst;
141     }
142
143     private LstItem jsonReadLst112Elements(JsonReader jReader) throws IOException {
144         LstItem lstItem = new LstItem();
145         jReader.beginObject();
146         if (jReader.hasNext()) {
147             String keyName = jReader.nextName();
148             if (keyName.equals("lf1121")) {
149                 lstItem.addLf(new Lf(keyName, jReader.nextString()));
150             }
151         }
152         jReader.endObject();
153         return lstItem;
154
155     }
156
157     private Lst jsonReadLst111(JsonReader jReader, Lst lst) throws IOException {
158         jReader.beginArray();
159         while (jReader.hasNext()) {
160             LstItem lstItem = jsonReadLst111Elements(jReader);
161             lst.addLstItem(lstItem);
162         }
163         jReader.endArray();
164         return lst;
165     }
166
167     private LstItem jsonReadLst111Elements(JsonReader jReader) throws IOException {
168         LstItem lstItem = new LstItem();
169         jReader.beginObject();
170         if (jReader.hasNext()) {
171             String keyName = jReader.nextName();
172             if (keyName.equals("lf1111")) {
173                 lstItem.addLf(new Lf(keyName, jReader.nextString()));
174             }
175         }
176         jReader.endObject();
177         return lstItem;
178     }
179
180     private Cont jsonReadCont111(JsonReader jReader, Cont cont) throws IOException {
181         jReader.beginObject();
182         cont = jsonReadCont111Elements(jReader, cont);
183         jReader.endObject();
184         return cont;
185     }
186
187     private Cont jsonReadCont111Elements(JsonReader jReader, Cont cont) throws IOException {
188         while (jReader.hasNext()) {
189             String keyName = jReader.nextName();
190             if (keyName.equals("lf1111")) {
191                 cont.addLf(new Lf(keyName, jReader.nextString()));
192             } else if (keyName.equals("lflst1111")) {
193                 LfLst lfLst = new LfLst(keyName);
194                 lfLst = jsonReadLflstValues(jReader, lfLst);
195                 cont.addLfLst(lfLst);
196             } else if (keyName.equals("lst1111")) {
197                 Lst lst = new Lst(keyName);
198                 lst = jsonReadLst1111(jReader, lst);
199                 cont.addLst(lst);
200             } else {
201                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
202             }
203         }
204         return cont;
205
206     }
207
208     private Lst jsonReadLst1111(JsonReader jReader, Lst lst) throws IOException {
209         jReader.beginArray();
210         while (jReader.hasNext()) {
211             LstItem lstItem = jsonReadLst1111Elements(jReader);
212             lst.addLstItem(lstItem);
213         }
214         jReader.endArray();
215         return lst;
216     }
217
218     private LstItem jsonReadLst1111Elements(JsonReader jReader) throws IOException {
219         jReader.beginObject();
220         LstItem lstItem = new LstItem();
221         while (jReader.hasNext()) {
222             String keyName = jReader.nextName();
223             if (keyName.equals("lf1111A") || keyName.equals("lf1111B")) {
224                 lstItem.addLf(new Lf(keyName, jReader.nextString()));
225             }
226         }
227         jReader.endObject();
228         return lstItem;
229     }
230
231     private LfLst jsonReadLflstValues(JsonReader jReader, LfLst lfLst) throws IOException {
232         jReader.beginArray();
233         while (jReader.hasNext()) {
234             lfLst.addLf(new Lf(jReader.nextString()));
235         }
236         jReader.endArray();
237         return lfLst;
238     }
239
240     private void checkDataFromJson(Cont dataFromJson) {
241         assertNotNull(dataFromJson.getLfs().get("lf11"));
242         assertEquals(dataFromJson.getLfs().get("lf11"), new Lf("lf11", "lf"));
243
244         LfLst lflst11 = null;
245         LfLst lflst12 = null;
246
247         lflst11 = dataFromJson.getLfLsts().get("lflst11");
248         lflst12 = dataFromJson.getLfLsts().get("lflst12");
249
250         assertNotNull(lflst11);
251         assertNotNull(lflst12);
252
253         assertEquals(3, lflst11.getLfs().size());
254         assertTrue(lflst11.getLfs().contains(new Lf("55")));
255         assertTrue(lflst11.getLfs().contains(new Lf("56")));
256         assertTrue(lflst11.getLfs().contains(new Lf("57")));
257
258         assertEquals(3, lflst12.getLfs().size());
259         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str1")));
260         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str2")));
261         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str3")));
262
263         assertEquals(1, dataFromJson.getLsts().size());
264         Lst lst11 = dataFromJson.getLsts().get("lst11");
265         assertNotNull(lst11);
266         assertEquals(2, lst11.getLstItems().size());
267
268         LstItem lst11_1 = null;
269         LstItem lst11_2 = null;
270         for (LstItem lstItem : lst11.getLstItems()) {
271             Lf lf = lstItem.getLfs().get("lf111");
272             if (lf != null && lf.getValue().equals("140")) {
273                 lst11_1 = lstItem;
274             } else if (lf != null && lf.getValue().equals("141")) {
275                 lst11_2 = lstItem;
276             }
277         }
278
279         checkLst11_1(lst11_1);
280         checkLst11_2(lst11_2);
281     }
282
283     private void checkLst11_2(LstItem lst11_2) {
284         assertNotNull(lst11_2);
285         assertEquals(2, lst11_2.getLfs().size());
286         assertEquals(1, lst11_2.getConts().size());
287         assertEquals(2, lst11_2.getLsts().size());
288
289         assertEquals(lst11_2.getLfs().get("lf112"), new Lf("lf112", "lf112 str2"));
290
291         Cont lst11_2_cont = lst11_2.getConts().get("cont111");
292         assertEquals(0, lst11_2_cont.getConts().size());
293         assertEquals(1, lst11_2_cont.getLfLsts().size());
294         assertEquals(1, lst11_2_cont.getLfs().size());
295         assertEquals(1, lst11_2_cont.getLsts().size());
296
297         // cont111 check
298         assertEquals(new Lf("lf1111", "lf1111 str2"), lst11_2_cont.getLfs().get("lf1111"));
299         assertEquals(new LfLst("lflst1111").addLf(new Lf("2049")).addLf(new Lf("1025")).addLf(new Lf("4097")),
300                 lst11_2_cont.getLfLsts().get("lflst1111"));
301
302         assertNotNull(lst11_2_cont.getLsts().get("lst1111"));
303         checkLst1111(lst11_2_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str21"), new Lf(
304                 "lf1111B", "5"), new Lf("lf1111A", "lf1111A str22"), new Lf("lf1111B", "8"));
305
306         checkLst11x(lst11_2.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "55")),
307                 new LstItem().addLf(new Lf("lf1111", "56")));
308         checkLst11x(lst11_2.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str22")),
309                 new LstItem().addLf(new Lf("lf1121", "lf1121 str21")));
310     }
311
312     private void checkLst11_1(LstItem lst11_1) {
313         assertNotNull(lst11_1);
314
315         assertEquals(2, lst11_1.getLfs().size());
316         assertEquals(1, lst11_1.getConts().size());
317         assertEquals(2, lst11_1.getLsts().size());
318
319         assertEquals(lst11_1.getLfs().get("lf112"), new Lf("lf112", "lf112 str"));
320
321         Cont lst11_1_cont = lst11_1.getConts().get("cont111");
322         assertEquals(0, lst11_1_cont.getConts().size());
323         assertEquals(1, lst11_1_cont.getLfLsts().size());
324         assertEquals(1, lst11_1_cont.getLfs().size());
325         assertEquals(1, lst11_1_cont.getLsts().size());
326
327         // cont111 check
328         assertEquals(new Lf("lf1111", "lf1111 str"), lst11_1_cont.getLfs().get("lf1111"));
329         assertEquals(new LfLst("lflst1111").addLf(new Lf("2048")).addLf(new Lf("1024")).addLf(new Lf("4096")),
330                 lst11_1_cont.getLfLsts().get("lflst1111"));
331
332         assertNotNull(lst11_1_cont.getLsts().get("lst1111"));
333         checkLst1111(lst11_1_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str11"), new Lf(
334                 "lf1111B", "4"), new Lf("lf1111A", "lf1111A str12"), new Lf("lf1111B", "7"));
335
336         checkLst11x(lst11_1.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "65")));
337         checkLst11x(lst11_1.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str11")));
338     }
339
340     private void checkLst11x(Lst lst, LstItem... lstItems) {
341         assertNotNull(lst);
342
343         Lst requiredLst = new Lst(lst.getName());
344         for (LstItem lstItem : lstItems) {
345             requiredLst.addLstItem(lstItem);
346         }
347
348         assertEquals(requiredLst, lst);
349
350     }
351
352     private void checkLst1111(Set<LstItem> lstItems, Lf lf11, Lf lf12, Lf lf21, Lf lf22) {
353         LstItem lst11_1_cont_lst1111_1 = null;
354         LstItem lst11_1_cont_lst1111_2 = null;
355         for (LstItem lstItem : lstItems) {
356             if (new LstItem().addLf(lf11).addLf(lf12).equals(lstItem)) {
357                 lst11_1_cont_lst1111_1 = lstItem;
358             } else if (new LstItem().addLf(lf21).addLf(lf22).equals(lstItem)) {
359                 lst11_1_cont_lst1111_2 = lstItem;
360             }
361         }
362
363         assertNotNull(lst11_1_cont_lst1111_1);
364         assertNotNull(lst11_1_cont_lst1111_2);
365     }
366
367 }