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.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.StringReader;
22 import org.junit.BeforeClass;
23 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
28 static abstract class LeafVerifier {
31 JsonToken expectedToken;
33 LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
34 this.expectedValue = expectedValue;
35 this.expectedToken = expectedToken;
38 abstract Object getActualValue(JsonReader reader) throws IOException;
40 void verify(final JsonReader reader, final String keyName) throws IOException {
41 assertEquals("Json value for key " + keyName, this.expectedValue, getActualValue(reader));
44 JsonToken expectedTokenType() {
45 return this.expectedToken;
49 static class BooleanVerifier extends LeafVerifier {
51 public BooleanVerifier(final boolean expected) {
52 super(expected, JsonToken.BOOLEAN);
56 Object getActualValue(final JsonReader reader) throws IOException {
57 return reader.nextBoolean();
61 static class NumberVerifier extends LeafVerifier {
63 public NumberVerifier(final Number expected) {
64 super(expected, JsonToken.NUMBER);
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();
81 static class StringVerifier extends LeafVerifier {
83 StringVerifier(final String expected) {
84 super(expected, JsonToken.STRING);
88 Object getActualValue(final JsonReader reader) throws IOException {
89 return reader.nextString();
93 static class EmptyVerifier extends LeafVerifier {
100 Object getActualValue(final JsonReader reader) throws IOException {
109 static class ComplexAnyXmlVerifier extends LeafVerifier {
111 ComplexAnyXmlVerifier() {
112 super(null, JsonToken.BEGIN_OBJECT);
116 void verify(final JsonReader reader, final String keyName) throws IOException {
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());
123 reader.beginObject();
124 verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
125 verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
127 String nextName = reader.nextName();
128 assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
130 assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
131 assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
134 nextName = reader.nextName();
135 assertEquals("Json reader child key for " + innerKey, "list", nextName);
137 verifyNestedLists(reader, 1);
138 verifyNestedLists(reader, 3);
145 void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
146 reader.beginObject();
148 final String nextName = reader.nextName();
149 assertEquals("Json reader next name", "nested-list", nextName);
153 reader.beginObject();
154 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
157 reader.beginObject();
158 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
165 void verifyLeaf(final JsonReader reader, final String parent, final String name, final String value) throws IOException {
166 final String nextName = reader.nextName();
167 assertEquals("Json reader child key for " + parent, name, nextName);
168 assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
169 assertEquals("Json value for key " + nextName, value, reader.nextString());
173 Object getActualValue(final JsonReader reader) throws IOException {
179 public static void initialize() throws FileNotFoundException, ReactorException {
180 dataLoad("/cnsn-to-json/simple-data-types");
183 private void verifyJsonOutput(final String jsonOutput) {
184 final StringReader strReader = new StringReader(jsonOutput);
185 final JsonReader jReader = new JsonReader(strReader);
187 String exception = null;
189 jsonReadCont(jReader);
190 } catch (final IOException e) {
191 exception = e.getMessage();
194 assertNull("Error during reading Json output: " + exception, exception);
197 private void jsonReadCont(final JsonReader jReader) throws IOException {
198 jReader.beginObject();
199 assertNotNull("cont1 is missing.", jReader.hasNext());
201 // Cont dataFromJson = new Cont(jReader.nextName());
203 jsonReadContElements(jReader);
205 assertFalse("cont shouldn't have other element.", jReader.hasNext());
207 // return dataFromJson;
210 private void jsonReadContElements(final JsonReader jReader) throws IOException {
211 jReader.beginObject();
213 final Map<String, LeafVerifier> expectedMap = Maps.newHashMap();
214 expectedMap.put("lfnint8Min", new NumberVerifier(Integer.valueOf(-128)));
215 expectedMap.put("lfnint8Max", new NumberVerifier(Integer.valueOf(127)));
216 expectedMap.put("lfnint16Min", new NumberVerifier(Integer.valueOf(-32768)));
217 expectedMap.put("lfnint16Max", new NumberVerifier(Integer.valueOf(32767)));
218 expectedMap.put("lfnint32Min", new NumberVerifier(Integer.valueOf(-2147483648)));
219 expectedMap.put("lfnint32Max", new NumberVerifier(Long.valueOf(2147483647)));
220 expectedMap.put("lfnint64Min", new NumberVerifier(Long.valueOf(-9223372036854775808L)));
221 expectedMap.put("lfnint64Max", new NumberVerifier(Long.valueOf(9223372036854775807L)));
222 expectedMap.put("lfnuint8Max", new NumberVerifier(Integer.valueOf(255)));
223 expectedMap.put("lfnuint16Max", new NumberVerifier(Integer.valueOf(65535)));
224 expectedMap.put("lfnuint32Max", new NumberVerifier(Long.valueOf(4294967295L)));
225 expectedMap.put("lfstr", new StringVerifier("lfstr"));
226 expectedMap.put("lfstr1", new StringVerifier(""));
227 expectedMap.put("lfbool1", new BooleanVerifier(true));
228 expectedMap.put("lfbool2", new BooleanVerifier(false));
229 expectedMap.put("lfbool3", new BooleanVerifier(false));
230 expectedMap.put("lfdecimal1", new NumberVerifier(new Double(43.32)));
231 expectedMap.put("lfdecimal2", new NumberVerifier(new Double(-0.43)));
232 expectedMap.put("lfdecimal3", new NumberVerifier(new Double(43)));
233 expectedMap.put("lfdecimal4", new NumberVerifier(new Double(43E3)));
234 expectedMap.put("lfdecimal6", new NumberVerifier(new Double(33.12345)));
235 expectedMap.put("lfenum", new StringVerifier("enum3"));
236 expectedMap.put("lfbits", new StringVerifier("bit3 bit2"));
237 expectedMap.put("lfbinary", new StringVerifier("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
238 expectedMap.put("lfunion1", new StringVerifier("324"));
239 expectedMap.put("lfunion2", new StringVerifier("33.3"));
240 expectedMap.put("lfunion3", new StringVerifier("55"));
241 expectedMap.put("lfunion4", new StringVerifier("true"));
242 expectedMap.put("lfunion5", new StringVerifier("true"));
243 expectedMap.put("lfunion6", new StringVerifier("10"));
244 expectedMap.put("lfunion7", new StringVerifier(""));
245 expectedMap.put("lfunion8", new StringVerifier(""));
246 expectedMap.put("lfunion9", new StringVerifier(""));
247 expectedMap.put("lfunion10", new StringVerifier("bt1"));
248 expectedMap.put("lfunion11", new StringVerifier("33"));
249 expectedMap.put("lfunion12", new StringVerifier("false"));
250 expectedMap.put("lfunion13", new StringVerifier("b1"));
251 expectedMap.put("lfunion14", new StringVerifier("zero"));
252 expectedMap.put("lfempty", new EmptyVerifier());
253 expectedMap.put("identityref1", new StringVerifier("simple-data-types:iden"));
254 expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
255 expectedMap.put("simple-any", new StringVerifier("simple"));
256 expectedMap.put("empty-any", new StringVerifier(""));
258 while (jReader.hasNext()) {
259 final String keyName = jReader.nextName();
260 final JsonToken peek = jReader.peek();
262 final LeafVerifier verifier = expectedMap.remove(keyName);
263 assertNotNull("Found unexpected leaf: " + keyName, verifier);
265 final JsonToken expToken = verifier.expectedTokenType();
266 if (expToken != null) {
267 assertEquals("Json token type for key " + keyName, expToken, peek);
270 verifier.verify(jReader, keyName);
273 if (!expectedMap.isEmpty()) {
274 fail("Missing leaf nodes in Json output: " + expectedMap.keySet());