2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
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 import com.google.common.collect.Maps;
16 import com.google.gson.stream.JsonReader;
17 import com.google.gson.stream.JsonToken;
18 import java.io.IOException;
19 import java.io.StringReader;
21 import org.junit.BeforeClass;
22 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
24 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
26 static abstract class LeafVerifier {
29 JsonToken expectedToken;
31 LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
32 this.expectedValue = expectedValue;
33 this.expectedToken = expectedToken;
36 abstract Object getActualValue(JsonReader reader) throws IOException;
38 void verify(final JsonReader reader, final String keyName) throws IOException {
39 assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
42 JsonToken expectedTokenType() {
47 static class BooleanVerifier extends LeafVerifier {
49 public BooleanVerifier(final boolean expected) {
50 super(expected, JsonToken.BOOLEAN);
54 Object getActualValue(final JsonReader reader) throws IOException {
55 return reader.nextBoolean();
59 static class NumberVerifier extends LeafVerifier {
61 public NumberVerifier(final Number expected) {
62 super(expected, JsonToken.NUMBER);
66 Object getActualValue(final JsonReader reader) throws IOException {
67 if (expectedValue instanceof Double) {
68 return reader.nextDouble();
69 } else if (expectedValue instanceof Long) {
70 return reader.nextLong();
71 } else if (expectedValue instanceof Integer) {
72 return reader.nextInt();
79 static class StringVerifier extends LeafVerifier {
81 StringVerifier(final String expected) {
82 super(expected, JsonToken.STRING);
86 Object getActualValue(final JsonReader reader) throws IOException {
87 return reader.nextString();
91 static class EmptyVerifier extends LeafVerifier {
98 Object getActualValue(final JsonReader reader) throws IOException {
107 static class ComplexAnyXmlVerifier extends LeafVerifier {
109 ComplexAnyXmlVerifier() {
110 super(null, JsonToken.BEGIN_OBJECT);
114 void verify(final JsonReader reader, final String keyName) throws IOException {
116 reader.beginObject();
117 final String innerKey = reader.nextName();
118 assertEquals("Json reader child key for " + keyName, "data", innerKey);
119 assertEquals("Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek());
121 reader.beginObject();
122 verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
123 verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
125 String nextName = reader.nextName();
126 assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
128 assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
129 assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
132 nextName = reader.nextName();
133 assertEquals("Json reader child key for " + innerKey, "list", nextName);
135 verifyNestedLists(reader, 1);
136 verifyNestedLists(reader, 3);
143 void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
144 reader.beginObject();
146 final String nextName = reader.nextName();
147 assertEquals("Json reader next name", "nested-list", nextName);
151 reader.beginObject();
152 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
155 reader.beginObject();
156 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
163 void verifyLeaf(final JsonReader reader, final String parent, final String name, final String value) throws IOException {
164 final String nextName = reader.nextName();
165 assertEquals("Json reader child key for " + parent, name, nextName);
166 assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
167 assertEquals("Json value for key " + nextName, value, reader.nextString());
171 Object getActualValue(final JsonReader reader) throws IOException {
177 public static void initialize() {
178 dataLoad("/cnsn-to-json/simple-data-types");
181 private void verifyJsonOutput(final String jsonOutput) {
182 final StringReader strReader = new StringReader(jsonOutput);
183 final JsonReader jReader = new JsonReader(strReader);
185 String exception = null;
187 jsonReadCont(jReader);
188 } catch (final IOException e) {
189 exception = e.getMessage();
192 assertNull("Error during reading Json output: " + exception, exception);
195 private void jsonReadCont(final JsonReader jReader) throws IOException {
196 jReader.beginObject();
197 assertNotNull("cont1 is missing.", jReader.hasNext());
199 // Cont dataFromJson = new Cont(jReader.nextName());
201 jsonReadContElements(jReader);
203 assertFalse("cont shouldn't have other element.", jReader.hasNext());
205 // return dataFromJson;
208 private void jsonReadContElements(final JsonReader jReader) throws IOException {
209 jReader.beginObject();
211 final Map<String, LeafVerifier> expectedMap = Maps.newHashMap();
212 expectedMap.put("lfnint8Min", new NumberVerifier(Integer.valueOf(-128)));
213 expectedMap.put("lfnint8Max", new NumberVerifier(Integer.valueOf(127)));
214 expectedMap.put("lfnint16Min", new NumberVerifier(Integer.valueOf(-32768)));
215 expectedMap.put("lfnint16Max", new NumberVerifier(Integer.valueOf(32767)));
216 expectedMap.put("lfnint32Min", new NumberVerifier(Integer.valueOf(-2147483648)));
217 expectedMap.put("lfnint32Max", new NumberVerifier(Long.valueOf(2147483647)));
218 expectedMap.put("lfnint64Min", new NumberVerifier(Long.valueOf(-9223372036854775808L)));
219 expectedMap.put("lfnint64Max", new NumberVerifier(Long.valueOf(9223372036854775807L)));
220 expectedMap.put("lfnuint8Max", new NumberVerifier(Integer.valueOf(255)));
221 expectedMap.put("lfnuint16Max", new NumberVerifier(Integer.valueOf(65535)));
222 expectedMap.put("lfnuint32Max", new NumberVerifier(Long.valueOf(4294967295L)));
223 expectedMap.put("lfstr", new StringVerifier("lfstr"));
224 expectedMap.put("lfstr1", new StringVerifier(""));
225 expectedMap.put("lfbool1", new BooleanVerifier(true));
226 expectedMap.put("lfbool2", new BooleanVerifier(false));
227 expectedMap.put("lfbool3", new BooleanVerifier(false));
228 expectedMap.put("lfdecimal1", new NumberVerifier(new Double(43.32)));
229 expectedMap.put("lfdecimal2", new NumberVerifier(new Double(-0.43)));
230 expectedMap.put("lfdecimal3", new NumberVerifier(new Double(43)));
231 expectedMap.put("lfdecimal4", new NumberVerifier(new Double(43E3)));
232 expectedMap.put("lfdecimal6", new NumberVerifier(new Double(33.12345)));
233 expectedMap.put("lfenum", new StringVerifier("enum3"));
234 expectedMap.put("lfbits", new StringVerifier("bit3 bit2"));
235 expectedMap.put("lfbinary", new StringVerifier("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
236 expectedMap.put("lfunion1", new StringVerifier("324"));
237 expectedMap.put("lfunion2", new StringVerifier("33.3"));
238 expectedMap.put("lfunion3", new StringVerifier("55"));
239 expectedMap.put("lfunion4", new StringVerifier("true"));
240 expectedMap.put("lfunion5", new StringVerifier("true"));
241 expectedMap.put("lfunion6", new StringVerifier("10"));
242 expectedMap.put("lfunion7", new StringVerifier(""));
243 expectedMap.put("lfunion8", new StringVerifier(""));
244 expectedMap.put("lfunion9", new StringVerifier(""));
245 expectedMap.put("lfunion10", new StringVerifier("bt1"));
246 expectedMap.put("lfunion11", new StringVerifier("33"));
247 expectedMap.put("lfunion12", new StringVerifier("false"));
248 expectedMap.put("lfunion13", new StringVerifier("b1"));
249 expectedMap.put("lfunion14", new StringVerifier("zero"));
250 expectedMap.put("lfempty", new EmptyVerifier());
251 expectedMap.put("identityref1", new StringVerifier("simple-data-types:iden"));
252 expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
253 expectedMap.put("simple-any", new StringVerifier("simple"));
254 expectedMap.put("empty-any", new StringVerifier(""));
256 while (jReader.hasNext()) {
257 final String keyName = jReader.nextName();
258 final JsonToken peek = jReader.peek();
260 final LeafVerifier verifier = expectedMap.remove(keyName);
261 assertNotNull("Found unexpected leaf: " + keyName, verifier);
263 final JsonToken expToken = verifier.expectedTokenType();
264 if (expToken != null) {
265 assertEquals("Json token type for key " + keyName, expToken, peek);
268 verifier.verify(jReader, keyName);
271 if (!expectedMap.isEmpty()) {
272 fail("Missing leaf nodes in Json output: " + expectedMap.keySet());