e2638733c1e3d4480a1a78c03fbc2150039bc32d
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonBasicDataTypesTest.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.fail;
15
16 import com.google.gson.stream.JsonReader;
17 import com.google.gson.stream.JsonToken;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.StringReader;
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.junit.BeforeClass;
24 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
25
26 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
27
28     abstract static class LeafVerifier {
29
30         Object expectedValue;
31         JsonToken expectedToken;
32
33         LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
34             this.expectedValue = expectedValue;
35             this.expectedToken = expectedToken;
36         }
37
38         abstract Object getActualValue(JsonReader reader) throws IOException;
39
40         void verify(final JsonReader reader, final String keyName) throws IOException {
41             assertEquals("Json value for key " + keyName, this.expectedValue, getActualValue(reader));
42         }
43
44         JsonToken expectedTokenType() {
45             return this.expectedToken;
46         }
47     }
48
49     static class BooleanVerifier extends LeafVerifier {
50
51         BooleanVerifier(final boolean expected) {
52             super(expected, JsonToken.BOOLEAN);
53         }
54
55         @Override
56         Object getActualValue(final JsonReader reader) throws IOException {
57             return reader.nextBoolean();
58         }
59     }
60
61     static class NumberVerifier extends LeafVerifier {
62
63         NumberVerifier(final Number expected) {
64             super(expected, JsonToken.NUMBER);
65         }
66
67         @Override
68         Object getActualValue(final JsonReader reader) throws IOException {
69             if (this.expectedValue instanceof Double) {
70                 return reader.nextDouble();
71             } else if (this.expectedValue instanceof Long) {
72                 return reader.nextLong();
73             } else if (this.expectedValue instanceof Integer) {
74                 return reader.nextInt();
75             }
76
77             return null;
78         }
79     }
80
81     static class StringVerifier extends LeafVerifier {
82
83         StringVerifier(final String expected) {
84             super(expected, JsonToken.STRING);
85         }
86
87         @Override
88         Object getActualValue(final JsonReader reader) throws IOException {
89             return reader.nextString();
90         }
91     }
92
93     static class EmptyVerifier extends LeafVerifier {
94
95         EmptyVerifier() {
96             super(null, null);
97         }
98
99         @Override
100         Object getActualValue(final JsonReader reader) throws IOException {
101             reader.beginArray();
102             reader.nextNull();
103             reader.endArray();
104             return null;
105         }
106
107     }
108
109     static class ComplexAnyXmlVerifier extends LeafVerifier {
110
111         ComplexAnyXmlVerifier() {
112             super(null, JsonToken.BEGIN_OBJECT);
113         }
114
115         @Override
116         void verify(final JsonReader reader, final String keyName) throws IOException {
117
118             reader.beginObject();
119             final String innerKey = reader.nextName();
120             assertEquals("Json reader child key for " + keyName, "data", innerKey);
121             assertEquals("Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek());
122
123             reader.beginObject();
124             verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
125             verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
126
127             String nextName = reader.nextName();
128             assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
129             reader.beginArray();
130             assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
131             assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
132             reader.endArray();
133
134             nextName = reader.nextName();
135             assertEquals("Json reader child key for " + innerKey, "list", nextName);
136             reader.beginArray();
137             verifyNestedLists(reader, 1);
138             verifyNestedLists(reader, 3);
139             reader.endArray();
140
141             reader.endObject();
142             reader.endObject();
143         }
144
145         void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
146             reader.beginObject();
147
148             final String nextName = reader.nextName();
149             assertEquals("Json reader next name", "nested-list", nextName);
150
151             reader.beginArray();
152
153             reader.beginObject();
154             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
155             reader.endObject();
156
157             reader.beginObject();
158             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
159             reader.endObject();
160
161             reader.endArray();
162             reader.endObject();
163         }
164
165         void verifyLeaf(final JsonReader reader, final String parent, final String name,
166                         final String value) throws IOException {
167             final String nextName = reader.nextName();
168             assertEquals("Json reader child key for " + parent, name, nextName);
169             assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
170             assertEquals("Json value for key " + nextName, value, reader.nextString());
171         }
172
173         @Override
174         Object getActualValue(final JsonReader reader) throws IOException {
175             return null;
176         }
177     }
178
179     @BeforeClass
180     public static void initialize() throws FileNotFoundException {
181         dataLoad("/cnsn-to-json/simple-data-types");
182     }
183
184     private static void verifyJsonOutput(final String jsonOutput) {
185         final StringReader strReader = new StringReader(jsonOutput);
186         final JsonReader jReader = new JsonReader(strReader);
187
188         String exception = null;
189         try {
190             jsonReadCont(jReader);
191         } catch (final IOException e) {
192             exception = e.getMessage();
193         }
194
195         assertNull("Error during reading Json output: " + exception, exception);
196     }
197
198     private static void jsonReadCont(final JsonReader jsonReader) throws IOException {
199         jsonReader.beginObject();
200         assertNotNull("cont1 is missing.", jsonReader.hasNext());
201
202         // Cont dataFromJson = new Cont(jReader.nextName());
203         jsonReader.nextName();
204         jsonReadContElements(jsonReader);
205
206         assertFalse("cont shouldn't have other element.", jsonReader.hasNext());
207         jsonReader.endObject();
208         // return dataFromJson;
209     }
210
211     private static void jsonReadContElements(final JsonReader jsonReader) throws IOException {
212         jsonReader.beginObject();
213
214         final Map<String, LeafVerifier> expectedMap = new HashMap<>();
215         expectedMap.put("lfnint8Min", new NumberVerifier(-128));
216         expectedMap.put("lfnint8Max", new NumberVerifier(127));
217         expectedMap.put("lfnint16Min", new NumberVerifier(-32768));
218         expectedMap.put("lfnint16Max", new NumberVerifier(32767));
219         expectedMap.put("lfnint32Min", new NumberVerifier(-2147483648));
220         expectedMap.put("lfnint32Max", new NumberVerifier(2147483647L));
221         expectedMap.put("lfnint64Min", new NumberVerifier(-9223372036854775808L));
222         expectedMap.put("lfnint64Max", new NumberVerifier(9223372036854775807L));
223         expectedMap.put("lfnuint8Max", new NumberVerifier(255));
224         expectedMap.put("lfnuint16Max", new NumberVerifier(65535));
225         expectedMap.put("lfnuint32Max", new NumberVerifier(4294967295L));
226         expectedMap.put("lfstr", new StringVerifier("lfstr"));
227         expectedMap.put("lfstr1", new StringVerifier(""));
228         expectedMap.put("lfbool1", new BooleanVerifier(true));
229         expectedMap.put("lfbool2", new BooleanVerifier(false));
230         expectedMap.put("lfbool3", new BooleanVerifier(false));
231         expectedMap.put("lfdecimal1", new NumberVerifier(43.32));
232         expectedMap.put("lfdecimal2", new NumberVerifier(-0.43));
233         expectedMap.put("lfdecimal3", new NumberVerifier(43d));
234         expectedMap.put("lfdecimal4", new NumberVerifier(43E3));
235         expectedMap.put("lfdecimal6", new NumberVerifier(33.12345));
236         expectedMap.put("lfenum", new StringVerifier("enum3"));
237         expectedMap.put("lfbits", new StringVerifier("bit3 bit2"));
238         expectedMap.put("lfbinary", new StringVerifier("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
239         expectedMap.put("lfunion1", new StringVerifier("324"));
240         expectedMap.put("lfunion2", new StringVerifier("33.3"));
241         expectedMap.put("lfunion3", new StringVerifier("55"));
242         expectedMap.put("lfunion4", new StringVerifier("true"));
243         expectedMap.put("lfunion5", new StringVerifier("true"));
244         expectedMap.put("lfunion6", new StringVerifier("10"));
245         expectedMap.put("lfunion7", new StringVerifier(""));
246         expectedMap.put("lfunion8", new StringVerifier(""));
247         expectedMap.put("lfunion9", new StringVerifier(""));
248         expectedMap.put("lfunion10", new StringVerifier("bt1"));
249         expectedMap.put("lfunion11", new StringVerifier("33"));
250         expectedMap.put("lfunion12", new StringVerifier("false"));
251         expectedMap.put("lfunion13", new StringVerifier("b1"));
252         expectedMap.put("lfunion14", new StringVerifier("zero"));
253         expectedMap.put("lfempty", new EmptyVerifier());
254         expectedMap.put("identityref1", new StringVerifier("simple-data-types:iden"));
255         expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
256         expectedMap.put("simple-any", new StringVerifier("simple"));
257         expectedMap.put("empty-any", new StringVerifier(""));
258
259         while (jsonReader.hasNext()) {
260             final String keyName = jsonReader.nextName();
261             final JsonToken peek = jsonReader.peek();
262
263             final LeafVerifier verifier = expectedMap.remove(keyName);
264             assertNotNull("Found unexpected leaf: " + keyName, verifier);
265
266             final JsonToken expToken = verifier.expectedTokenType();
267             if (expToken != null) {
268                 assertEquals("Json token type for key " + keyName, expToken, peek);
269             }
270
271             verifier.verify(jsonReader, keyName);
272         }
273
274         if (!expectedMap.isEmpty()) {
275             fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
276         }
277
278         jsonReader.endObject();
279     }
280
281 }