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