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;
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.IOException;
20 import java.io.StringReader;
22 import org.junit.BeforeClass;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
26 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
29 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
30 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
31 import org.opendaylight.yangtools.yang.data.api.Node;
33 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
35 static abstract class LeafVerifier {
38 JsonToken expectedToken;
40 LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
41 this.expectedValue = expectedValue;
42 this.expectedToken = expectedToken;
45 abstract Object getActualValue(JsonReader reader) throws IOException;
47 void verify(final JsonReader reader, final String keyName) throws IOException {
48 assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
51 JsonToken expectedTokenType() {
56 static class BooleanVerifier extends LeafVerifier {
58 public BooleanVerifier(final boolean expected) {
59 super(expected, JsonToken.BOOLEAN);
63 Object getActualValue(final JsonReader reader) throws IOException {
64 return reader.nextBoolean();
68 static class NumberVerifier extends LeafVerifier {
70 public NumberVerifier(final Number expected) {
71 super(expected, JsonToken.NUMBER);
75 Object getActualValue(final JsonReader reader) throws IOException {
76 if (expectedValue instanceof Double) {
77 return reader.nextDouble();
78 } else if (expectedValue instanceof Long) {
79 return reader.nextLong();
80 } else if (expectedValue instanceof Integer) {
81 return reader.nextInt();
88 static class StringVerifier extends LeafVerifier {
90 StringVerifier(final String expected) {
91 super(expected, JsonToken.STRING);
95 Object getActualValue(final JsonReader reader) throws IOException {
96 return reader.nextString();
100 static class EmptyVerifier extends LeafVerifier {
107 Object getActualValue(final JsonReader reader) throws IOException {
116 static class ComplexAnyXmlVerifier extends LeafVerifier {
118 ComplexAnyXmlVerifier() {
119 super(null, JsonToken.BEGIN_OBJECT);
123 void verify(final JsonReader reader, final String keyName) throws IOException {
125 reader.beginObject();
126 final String innerKey = reader.nextName();
127 assertEquals("Json reader child key for " + keyName, "data", innerKey);
128 assertEquals("Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek());
130 reader.beginObject();
131 verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
132 verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
134 String nextName = reader.nextName();
135 assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
137 assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
138 assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
141 nextName = reader.nextName();
142 assertEquals("Json reader child key for " + innerKey, "list", nextName);
144 verifyNestedLists(reader, 1);
145 verifyNestedLists(reader, 3);
152 void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
153 reader.beginObject();
155 final String nextName = reader.nextName();
156 assertEquals("Json reader next name", "nested-list", nextName);
160 reader.beginObject();
161 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
164 reader.beginObject();
165 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
172 void verifyLeaf(final JsonReader reader, final String parent, final String name, final String value) throws IOException {
173 final String nextName = reader.nextName();
174 assertEquals("Json reader child key for " + parent, name, nextName);
175 assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
176 assertEquals("Json value for key " + nextName, value, reader.nextString());
180 Object getActualValue(final JsonReader reader) throws IOException {
186 public static void initialize() {
187 dataLoad("/cnsn-to-json/simple-data-types");
192 public void simpleYangDataTest() throws Exception {
194 final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
195 XmlToCompositeNodeProvider.INSTANCE);
197 TestUtils.normalizeCompositeNode(node, modules, "simple-data-types:cont");
199 final String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(node, modules, dataSchemaNode,
200 StructuredDataToJsonProvider.INSTANCE);
202 assertNotNull(jsonOutput);
204 verifyJsonOutput(jsonOutput);
207 private void verifyJsonOutput(final String jsonOutput) {
208 final StringReader strReader = new StringReader(jsonOutput);
209 final JsonReader jReader = new JsonReader(strReader);
211 String exception = null;
213 jsonReadCont(jReader);
214 } catch (final IOException e) {
215 exception = e.getMessage();
218 assertNull("Error during reading Json output: " + exception, exception);
221 private void jsonReadCont(final JsonReader jReader) throws IOException {
222 jReader.beginObject();
223 assertNotNull("cont1 is missing.", jReader.hasNext());
225 // Cont dataFromJson = new Cont(jReader.nextName());
227 jsonReadContElements(jReader);
229 assertFalse("cont shouldn't have other element.", jReader.hasNext());
231 // return dataFromJson;
234 private void jsonReadContElements(final JsonReader jReader) throws IOException {
235 jReader.beginObject();
237 final Map<String, LeafVerifier> expectedMap = Maps.newHashMap();
238 expectedMap.put("lfnint8Min", new NumberVerifier(Integer.valueOf(-128)));
239 expectedMap.put("lfnint8Max", new NumberVerifier(Integer.valueOf(127)));
240 expectedMap.put("lfnint16Min", new NumberVerifier(Integer.valueOf(-32768)));
241 expectedMap.put("lfnint16Max", new NumberVerifier(Integer.valueOf(32767)));
242 expectedMap.put("lfnint32Min", new NumberVerifier(Integer.valueOf(-2147483648)));
243 expectedMap.put("lfnint32Max", new NumberVerifier(Long.valueOf(2147483647)));
244 expectedMap.put("lfnint64Min", new NumberVerifier(Long.valueOf(-9223372036854775808L)));
245 expectedMap.put("lfnint64Max", new NumberVerifier(Long.valueOf(9223372036854775807L)));
246 expectedMap.put("lfnuint8Max", new NumberVerifier(Integer.valueOf(255)));
247 expectedMap.put("lfnuint16Max", new NumberVerifier(Integer.valueOf(65535)));
248 expectedMap.put("lfnuint32Max", new NumberVerifier(Long.valueOf(4294967295L)));
249 expectedMap.put("lfstr", new StringVerifier("lfstr"));
250 expectedMap.put("lfstr1", new StringVerifier(""));
251 expectedMap.put("lfbool1", new BooleanVerifier(true));
252 expectedMap.put("lfbool2", new BooleanVerifier(false));
253 expectedMap.put("lfbool3", new BooleanVerifier(false));
254 expectedMap.put("lfdecimal1", new NumberVerifier(new Double(43.32)));
255 expectedMap.put("lfdecimal2", new NumberVerifier(new Double(-0.43)));
256 expectedMap.put("lfdecimal3", new NumberVerifier(new Double(43)));
257 expectedMap.put("lfdecimal4", new NumberVerifier(new Double(43E3)));
258 expectedMap.put("lfdecimal6", new NumberVerifier(new Double(33.12345)));
259 expectedMap.put("lfenum", new StringVerifier("enum3"));
260 expectedMap.put("lfbits", new StringVerifier("bit3 bit2"));
261 expectedMap.put("lfbinary", new StringVerifier("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
262 expectedMap.put("lfunion1", new StringVerifier("324"));
263 expectedMap.put("lfunion2", new StringVerifier("33.3"));
264 expectedMap.put("lfunion3", new StringVerifier("55"));
265 expectedMap.put("lfunion4", new StringVerifier("true"));
266 expectedMap.put("lfunion5", new StringVerifier("true"));
267 expectedMap.put("lfunion6", new StringVerifier("10"));
268 expectedMap.put("lfunion7", new StringVerifier(""));
269 expectedMap.put("lfunion8", new StringVerifier(""));
270 expectedMap.put("lfunion9", new StringVerifier(""));
271 expectedMap.put("lfunion10", new StringVerifier("bt1"));
272 expectedMap.put("lfunion11", new StringVerifier("33"));
273 expectedMap.put("lfunion12", new StringVerifier("false"));
274 expectedMap.put("lfunion13", new StringVerifier("b1"));
275 expectedMap.put("lfunion14", new StringVerifier("zero"));
276 expectedMap.put("lfempty", new EmptyVerifier());
277 expectedMap.put("identityref1", new StringVerifier("simple-data-types:iden"));
278 expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
279 expectedMap.put("simple-any", new StringVerifier("simple"));
280 expectedMap.put("empty-any", new StringVerifier(""));
282 while (jReader.hasNext()) {
283 final String keyName = jReader.nextName();
284 final JsonToken peek = jReader.peek();
286 final LeafVerifier verifier = expectedMap.remove(keyName);
287 assertNotNull("Found unexpected leaf: " + keyName, verifier);
289 final JsonToken expToken = verifier.expectedTokenType();
290 if (expToken != null) {
291 assertEquals("Json token type for key " + keyName, expToken, peek);
294 verifier.verify(jReader, keyName);
297 if (!expectedMap.isEmpty()) {
298 fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
305 public void testBadData() throws Exception {
308 final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/bad-data.xml",
309 XmlToCompositeNodeProvider.INSTANCE);
311 TestUtils.normalizeCompositeNode(node, modules, "simple-data-types:cont");
312 fail("Expected RestconfDocumentedException");
313 } catch (final RestconfDocumentedException e) {
314 assertEquals("getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());