1be9294ad95da94206949b4af99fd0568610f909
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnJsonBasicYangTypesTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import com.google.gson.stream.JsonReader;
16 import com.google.gson.stream.JsonToken;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.util.Map;
20 import java.util.Set;
21 import org.junit.BeforeClass;
22 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
23 import org.opendaylight.controller.sal.restconf.impl.test.structures.Cont;
24 import org.opendaylight.controller.sal.restconf.impl.test.structures.Lf;
25 import org.opendaylight.controller.sal.restconf.impl.test.structures.LfLst;
26 import org.opendaylight.controller.sal.restconf.impl.test.structures.Lst;
27 import org.opendaylight.controller.sal.restconf.impl.test.structures.LstItem;
28
29 public class CnSnJsonBasicYangTypesTest extends YangAndXmlAndDataSchemaLoader {
30
31     @BeforeClass
32     public static void initialize() {
33         dataLoad("/cnsn-to-json/simple-yang-types", 1, "simple-yang-types", "cont1");
34     }
35
36     private void verifyJsonOutputForEmptyData(final String jsonOutput) {
37         assertNotNull(jsonOutput);
38         final StringReader strReader = new StringReader(jsonOutput);
39         final JsonReader jReader = new JsonReader(strReader);
40
41         String exception = null;
42         Cont dataFromJson = null;
43         try {
44             dataFromJson = jsonReadCont1(jReader);
45         } catch (final IOException e) {
46             exception = e.getMessage();
47         }
48
49         assertNotNull("Data structures from json are missing.", dataFromJson);
50         checkDataFromJsonEmpty(dataFromJson);
51
52         assertNull("Error during reading Json output: " + exception, exception);
53     }
54
55     private void verifyJsonOutput(final String jsonOutput) {
56         assertNotNull(jsonOutput);
57         final StringReader strReader = new StringReader(jsonOutput);
58         final JsonReader jReader = new JsonReader(strReader);
59
60         String exception = null;
61         Cont dataFromJson = null;
62         try {
63             dataFromJson = jsonReadCont1(jReader);
64         } catch (final IOException e) {
65             exception = e.getMessage();
66         }
67
68         assertNotNull("Data structures from json are missing.", dataFromJson);
69         checkDataFromJson(dataFromJson);
70
71         assertNull("Error during reading Json output: " + exception, exception);
72     }
73
74     private Cont jsonReadCont1(final JsonReader jReader) throws IOException {
75         jReader.beginObject();
76         assertNotNull("cont1 is missing.", jReader.hasNext());
77
78         Cont dataFromJson = new Cont(jReader.nextName());
79         dataFromJson = jsonReadCont1Elements(jReader, dataFromJson);
80
81         assertFalse("cont shouldn't have other element.", jReader.hasNext());
82         jReader.endObject();
83         return dataFromJson;
84
85     }
86
87     private Cont jsonReadCont1Elements(final JsonReader jReader, final Cont redData) throws IOException {
88         jReader.beginObject();
89         while (jReader.hasNext()) {
90             final String keyName = jReader.nextName();
91             if (keyName.equals("lf11")) {
92                 redData.addLf(new Lf(keyName, nextValue(jReader)));
93             } else if (keyName.equals("lflst11")) {
94                 LfLst lfLst = new LfLst(keyName);
95                 lfLst = jsonReadLflstValues(jReader, lfLst);
96                 redData.addLfLst(lfLst);
97             } else if (keyName.equals("lflst12")) {
98                 final LfLst lfLst = new LfLst(keyName);
99                 jsonReadLflstValues(jReader, lfLst);
100                 redData.addLfLst(lfLst);
101             } else if (keyName.equals("lst11")) {
102                 Lst lst = new Lst(keyName);
103                 lst = jsonReadLst11(jReader, lst);
104                 redData.addLst(lst);
105             } else {
106                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
107             }
108         }
109         jReader.endObject();
110         return redData;
111
112     }
113
114     private Lst jsonReadLst11(final JsonReader jReader, final Lst lst) throws IOException {
115         jReader.beginArray();
116
117         while (jReader.hasNext()) {
118             final LstItem lstItem = jsonReadLst11Elements(jReader);
119             lst.addLstItem(lstItem);
120         }
121         jReader.endArray();
122         return lst;
123     }
124
125     private LstItem jsonReadLst11Elements(final JsonReader jReader) throws IOException {
126         final LstItem lstItem = new LstItem();
127         jReader.beginObject();
128         while (jReader.hasNext()) {
129             final String keyName = jReader.nextName();
130             if (keyName.equals("lf111")) {
131                 lstItem.addLf(new Lf(keyName, nextValue(jReader)));
132             } else if (keyName.equals("lf112")) {
133                 lstItem.addLf(new Lf(keyName, nextValue(jReader)));
134             } else if (keyName.equals("cont111")) {
135                 Cont cont = new Cont(keyName);
136                 cont = jsonReadCont111(jReader, cont);
137                 lstItem.addCont(cont);
138             } else if (keyName.equals("lst111")) {
139                 Lst lst = new Lst(keyName);
140                 lst = jsonReadLst111(jReader, lst);
141                 lstItem.addLst(lst);
142             } else if (keyName.equals("lst112")) {
143                 Lst lst = new Lst(keyName);
144                 lst = jsonReadLst112(jReader, lst);
145                 lstItem.addLst(lst);
146             } else {
147                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
148             }
149         }
150         jReader.endObject();
151         return lstItem;
152     }
153
154     private Lst jsonReadLst112(final JsonReader jReader, final Lst lst) throws IOException {
155         jReader.beginArray();
156         while (jReader.hasNext()) {
157             final LstItem lstItem = jsonReadLst112Elements(jReader);
158             lst.addLstItem(lstItem);
159         }
160         jReader.endArray();
161         return lst;
162     }
163
164     private LstItem jsonReadLst112Elements(final JsonReader jReader) throws IOException {
165         final LstItem lstItem = new LstItem();
166         jReader.beginObject();
167         if (jReader.hasNext()) {
168             final String keyName = jReader.nextName();
169             if (keyName.equals("lf1121")) {
170                 lstItem.addLf(new Lf(keyName, nextValue(jReader)));
171             }
172         }
173         jReader.endObject();
174         return lstItem;
175
176     }
177
178     private Lst jsonReadLst111(final JsonReader jReader, final Lst lst) throws IOException {
179         jReader.beginArray();
180         while (jReader.hasNext()) {
181             final LstItem lstItem = jsonReadLst111Elements(jReader);
182             lst.addLstItem(lstItem);
183         }
184         jReader.endArray();
185         return lst;
186     }
187
188     private LstItem jsonReadLst111Elements(final JsonReader jReader) throws IOException {
189         final LstItem lstItem = new LstItem();
190         jReader.beginObject();
191         if (jReader.hasNext()) {
192             final String keyName = jReader.nextName();
193             if (keyName.equals("lf1111")) {
194                 lstItem.addLf(new Lf(keyName, nextValue(jReader)));
195             }
196         }
197         jReader.endObject();
198         return lstItem;
199     }
200
201     private Object nextValue(final JsonReader jReader) throws IOException {
202         if (jReader.peek().equals(JsonToken.NULL)) {
203             jReader.nextNull();
204             return null;
205         } else if (jReader.peek().equals(JsonToken.NUMBER)) {
206             return jReader.nextInt();
207         } else {
208             return jReader.nextString();
209         }
210     }
211
212     private Cont jsonReadCont111(final JsonReader jReader, Cont cont) throws IOException {
213         jReader.beginObject();
214         cont = jsonReadCont111Elements(jReader, cont);
215         jReader.endObject();
216         return cont;
217     }
218
219     private Cont jsonReadCont111Elements(final JsonReader jReader, final Cont cont) throws IOException {
220         while (jReader.hasNext()) {
221             final String keyName = jReader.nextName();
222             if (keyName.equals("lf1111")) {
223                 cont.addLf(new Lf(keyName, nextValue(jReader)));
224             } else if (keyName.equals("lflst1111")) {
225                 LfLst lfLst = new LfLst(keyName);
226                 lfLst = jsonReadLflstValues(jReader, lfLst);
227                 cont.addLfLst(lfLst);
228             } else if (keyName.equals("lst1111")) {
229                 Lst lst = new Lst(keyName);
230                 lst = jsonReadLst1111(jReader, lst);
231                 cont.addLst(lst);
232             } else {
233                 assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
234             }
235         }
236         return cont;
237
238     }
239
240     private Lst jsonReadLst1111(final JsonReader jReader, final Lst lst) throws IOException {
241         jReader.beginArray();
242         while (jReader.hasNext()) {
243             final LstItem lstItem = jsonReadLst1111Elements(jReader);
244             lst.addLstItem(lstItem);
245         }
246         jReader.endArray();
247         return lst;
248     }
249
250     private LstItem jsonReadLst1111Elements(final JsonReader jReader) throws IOException {
251         jReader.beginObject();
252         final LstItem lstItem = new LstItem();
253         while (jReader.hasNext()) {
254             final String keyName = jReader.nextName();
255             if (keyName.equals("lf1111A") || keyName.equals("lf1111B")) {
256                 lstItem.addLf(new Lf(keyName, nextValue(jReader)));
257             }
258         }
259         jReader.endObject();
260         return lstItem;
261     }
262
263     private LfLst jsonReadLflstValues(final JsonReader jReader, final LfLst lfLst) throws IOException {
264         jReader.beginArray();
265         while (jReader.hasNext()) {
266             lfLst.addLf(new Lf(nextValue(jReader)));
267         }
268         jReader.endArray();
269         return lfLst;
270     }
271
272     private void checkDataFromJsonEmpty(final Cont dataFromJson) {
273         assertTrue(dataFromJson.getLfs().isEmpty());
274         assertTrue(dataFromJson.getLfLsts().isEmpty());
275         assertTrue(dataFromJson.getConts().isEmpty());
276
277         final Map<String, Lst> lsts = dataFromJson.getLsts();
278         assertEquals(1, lsts.size());
279         final Lst lst11 = lsts.get("lst11");
280         assertNotNull(lst11);
281         final Set<LstItem> lstItems = lst11.getLstItems();
282         assertNotNull(lstItems);
283
284         LstItem lst11_1 = null;
285         LstItem lst11_2 = null;
286         LstItem lst11_3 = null;
287         for (final LstItem lstItem : lstItems) {
288             if (lstItem.getLfs().get("lf111").getValue().equals(1)) {
289                 lst11_1 = lstItem;
290             } else if (lstItem.getLfs().get("lf111").getValue().equals(2)) {
291                 lst11_2 = lstItem;
292             } else if (lstItem.getLfs().get("lf111").getValue().equals(3)) {
293                 lst11_3 = lstItem;
294             }
295         }
296
297         assertNotNull(lst11_1);
298         assertNotNull(lst11_2);
299         assertNotNull(lst11_3);
300
301         // lst11_1
302         assertTrue(lst11_1.getLfLsts().isEmpty());
303         assertEquals(1, lst11_1.getLfs().size());
304         assertEquals(1, lst11_1.getConts().size());
305         assertEquals(1, lst11_1.getLsts().size());
306         assertEquals(lst11_1.getLsts().get("lst111"), new Lst("lst111").addLstItem(new LstItem().addLf("lf1111", 35))
307                 .addLstItem(new LstItem().addLf("lf1111", 34)).addLstItem(new LstItem()).addLstItem(new LstItem()));
308         assertEquals(lst11_1.getConts().get("cont111"), new Cont("cont111"));
309         // : lst11_1
310
311         // lst11_2
312         assertTrue(lst11_2.getLfLsts().isEmpty());
313         assertEquals(1, lst11_2.getLfs().size());
314         assertEquals(1, lst11_2.getConts().size());
315         assertEquals(1, lst11_2.getLsts().size());
316
317         final Cont lst11_2_cont111 = lst11_2.getConts().get("cont111");
318
319         // -cont111
320         assertNotNull(lst11_2_cont111);
321         assertTrue(lst11_2_cont111.getLfs().isEmpty());
322         assertEquals(1, lst11_2_cont111.getLfLsts().size());
323         assertEquals(1, lst11_2_cont111.getLsts().size());
324         assertTrue(lst11_2_cont111.getConts().isEmpty());
325
326         assertEquals(new LfLst("lflst1111").addLf(1024).addLf(4096), lst11_2_cont111.getLfLsts().get("lflst1111"));
327         assertEquals(
328                 new Lst("lst1111").addLstItem(new LstItem().addLf("lf1111B", 4)).addLstItem(
329                         new LstItem().addLf("lf1111A", "lf1111A str12")), lst11_2_cont111.getLsts().get("lst1111"));
330         // :-cont111
331         assertEquals(lst11_2.getLsts().get("lst112"), new Lst("lst112").addLstItem(new LstItem()));
332         // : lst11_2
333
334         // lst11_3
335         assertEquals(1, lst11_3.getLfs().size());
336         assertTrue(lst11_3.getLfLsts().isEmpty());
337         assertTrue(lst11_3.getLsts().isEmpty());
338         assertTrue(lst11_3.getLsts().isEmpty());
339
340         // -cont111
341         final Cont lst11_3_cont111 = lst11_3.getConts().get("cont111");
342         assertEquals(0, lst11_3_cont111.getLfs().size());
343         assertEquals(0, lst11_3_cont111.getLfLsts().size());
344         assertEquals(1, lst11_3_cont111.getLsts().size());
345         assertTrue(lst11_3_cont111.getConts().isEmpty());
346
347         assertEquals(new Lst("lst1111").addLstItem(new LstItem()).addLstItem(new LstItem()), lst11_3_cont111.getLsts()
348                 .get("lst1111"));
349         // :-cont111
350         // : lst11_3
351
352     }
353
354     private void checkDataFromJson(final Cont dataFromJson) {
355         assertNotNull(dataFromJson.getLfs().get("lf11"));
356         assertEquals(dataFromJson.getLfs().get("lf11"), new Lf("lf11", "lf"));
357
358         LfLst lflst11 = null;
359         LfLst lflst12 = null;
360
361         lflst11 = dataFromJson.getLfLsts().get("lflst11");
362         lflst12 = dataFromJson.getLfLsts().get("lflst12");
363
364         assertNotNull(lflst11);
365         assertNotNull(lflst12);
366
367         assertEquals(3, lflst11.getLfs().size());
368         assertTrue(lflst11.getLfs().contains(new Lf(55)));
369         assertTrue(lflst11.getLfs().contains(new Lf(56)));
370         assertTrue(lflst11.getLfs().contains(new Lf(57)));
371
372         assertEquals(3, lflst12.getLfs().size());
373         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str1")));
374         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str2")));
375         assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str3")));
376
377         assertEquals(1, dataFromJson.getLsts().size());
378         final Lst lst11 = dataFromJson.getLsts().get("lst11");
379         assertNotNull(lst11);
380         assertEquals(2, lst11.getLstItems().size());
381
382         LstItem lst11_1 = null;
383         LstItem lst11_2 = null;
384         for (final LstItem lstItem : lst11.getLstItems()) {
385             final Lf lf = lstItem.getLfs().get("lf111");
386             if (lf != null && lf.getValue().equals(140)) {
387                 lst11_1 = lstItem;
388             } else if (lf != null && lf.getValue().equals(141)) {
389                 lst11_2 = lstItem;
390             }
391         }
392
393         checkLst11_1(lst11_1);
394         checkLst11_2(lst11_2);
395     }
396
397     private void checkLst11_2(final LstItem lst11_2) {
398         assertNotNull(lst11_2);
399         assertEquals(2, lst11_2.getLfs().size());
400         assertEquals(1, lst11_2.getConts().size());
401         assertEquals(2, lst11_2.getLsts().size());
402
403         assertEquals(lst11_2.getLfs().get("lf112"), new Lf("lf112", "lf112 str2"));
404
405         final Cont lst11_2_cont = lst11_2.getConts().get("cont111");
406         assertEquals(0, lst11_2_cont.getConts().size());
407         assertEquals(1, lst11_2_cont.getLfLsts().size());
408         assertEquals(1, lst11_2_cont.getLfs().size());
409         assertEquals(1, lst11_2_cont.getLsts().size());
410
411         // cont111 check
412         assertEquals(new Lf("lf1111", "lf1111 str2"), lst11_2_cont.getLfs().get("lf1111"));
413         assertEquals(new LfLst("lflst1111").addLf(new Lf(2049)).addLf(new Lf(1025)).addLf(new Lf(4097)), lst11_2_cont
414                 .getLfLsts().get("lflst1111"));
415
416         assertNotNull(lst11_2_cont.getLsts().get("lst1111"));
417         checkLst1111(lst11_2_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str21"), new Lf(
418                 "lf1111B", 5), new Lf("lf1111A", "lf1111A str22"), new Lf("lf1111B", 8));
419
420         checkLst11x(lst11_2.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", 55)),
421                 new LstItem().addLf(new Lf("lf1111", 56)));
422         checkLst11x(lst11_2.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str22")),
423                 new LstItem().addLf(new Lf("lf1121", "lf1121 str21")));
424     }
425
426     private void checkLst11_1(final LstItem lst11_1) {
427         assertNotNull(lst11_1);
428
429         assertEquals(2, lst11_1.getLfs().size());
430         assertEquals(1, lst11_1.getConts().size());
431         assertEquals(2, lst11_1.getLsts().size());
432
433         assertEquals(lst11_1.getLfs().get("lf112"), new Lf("lf112", "lf112 str"));
434
435         final Cont lst11_1_cont = lst11_1.getConts().get("cont111");
436         assertEquals(0, lst11_1_cont.getConts().size());
437         assertEquals(1, lst11_1_cont.getLfLsts().size());
438         assertEquals(1, lst11_1_cont.getLfs().size());
439         assertEquals(1, lst11_1_cont.getLsts().size());
440
441         // cont111 check
442         assertEquals(new Lf("lf1111", "lf1111 str"), lst11_1_cont.getLfs().get("lf1111"));
443         assertEquals(new LfLst("lflst1111").addLf(new Lf(2048)).addLf(new Lf(1024)).addLf(new Lf(4096)), lst11_1_cont
444                 .getLfLsts().get("lflst1111"));
445
446         assertNotNull(lst11_1_cont.getLsts().get("lst1111"));
447         checkLst1111(lst11_1_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str11"), new Lf(
448                 "lf1111B", 4), new Lf("lf1111A", "lf1111A str12"), new Lf("lf1111B", 7));
449
450         checkLst11x(lst11_1.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", 65)));
451         checkLst11x(lst11_1.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str11")));
452     }
453
454     private void checkLst11x(final Lst lst, final LstItem... lstItems) {
455         assertNotNull(lst);
456
457         final Lst requiredLst = new Lst(lst.getName());
458         for (final LstItem lstItem : lstItems) {
459             requiredLst.addLstItem(lstItem);
460         }
461
462         assertEquals(requiredLst, lst);
463
464     }
465
466     private void checkLst1111(final Set<LstItem> lstItems, final Lf lf11, final Lf lf12, final Lf lf21, final Lf lf22) {
467         LstItem lst11_1_cont_lst1111_1 = null;
468         LstItem lst11_1_cont_lst1111_2 = null;
469         for (final LstItem lstItem : lstItems) {
470             if (new LstItem().addLf(lf11).addLf(lf12).equals(lstItem)) {
471                 lst11_1_cont_lst1111_1 = lstItem;
472             } else if (new LstItem().addLf(lf21).addLf(lf22).equals(lstItem)) {
473                 lst11_1_cont_lst1111_2 = lstItem;
474             }
475         }
476
477         assertNotNull(lst11_1_cont_lst1111_1);
478         assertNotNull(lst11_1_cont_lst1111_2);
479     }
480
481 }