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.Test;
24 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
25 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
28 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
29 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
30 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
32 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
34 static abstract class LeafVerifier {
37 JsonToken expectedToken;
39 LeafVerifier(Object expectedValue, JsonToken expectedToken) {
40 this.expectedValue = expectedValue;
41 this.expectedToken = expectedToken;
44 abstract Object getActualValue(JsonReader reader) throws IOException;
46 void verify(JsonReader reader, String keyName) throws IOException {
47 assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
50 JsonToken expectedTokenType() {
55 static class BooleanVerifier extends LeafVerifier {
57 public BooleanVerifier(boolean expected) {
58 super(expected, JsonToken.BOOLEAN);
62 Object getActualValue(JsonReader reader) throws IOException {
63 return reader.nextBoolean();
67 static class NumberVerifier extends LeafVerifier {
69 public NumberVerifier(Number expected) {
70 super(expected, JsonToken.NUMBER);
74 Object getActualValue(JsonReader reader) throws IOException {
75 if (expectedValue instanceof Double) {
76 return reader.nextDouble();
77 } else if (expectedValue instanceof Long) {
78 return reader.nextLong();
79 } else if (expectedValue instanceof Integer) {
80 return reader.nextInt();
87 static class StringVerifier extends LeafVerifier {
89 StringVerifier(String expected) {
90 super(expected, JsonToken.STRING);
94 Object getActualValue(JsonReader reader) throws IOException {
95 return reader.nextString();
99 static class EmptyVerifier extends LeafVerifier {
106 Object getActualValue(JsonReader reader) throws IOException {
115 static class ComplexAnyXmlVerifier extends LeafVerifier {
117 ComplexAnyXmlVerifier() {
118 super(null, JsonToken.BEGIN_OBJECT);
122 void verify(JsonReader reader, String keyName) throws IOException {
124 reader.beginObject();
125 String innerKey = reader.nextName();
126 assertEquals("Json reader child key for " + keyName, "data", innerKey);
127 assertEquals("Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek());
129 reader.beginObject();
130 verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
131 verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
133 String nextName = reader.nextName();
134 assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
136 assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
137 assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
140 nextName = reader.nextName();
141 assertEquals("Json reader child key for " + innerKey, "list", nextName);
143 verifyNestedLists(reader, 1);
144 verifyNestedLists(reader, 3);
151 void verifyNestedLists(JsonReader reader, int leafNum) throws IOException {
152 reader.beginObject();
154 String nextName = reader.nextName();
155 assertEquals("Json reader next name", "nested-list", nextName);
159 reader.beginObject();
160 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
163 reader.beginObject();
164 verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
171 void verifyLeaf(JsonReader reader, String parent, String name, String value) throws IOException {
172 String nextName = reader.nextName();
173 assertEquals("Json reader child key for " + parent, name, nextName);
174 assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
175 assertEquals("Json value for key " + nextName, value, reader.nextString());
179 Object getActualValue(JsonReader reader) throws IOException {
185 public static void initialize() {
186 dataLoad("/cnsn-to-json/simple-data-types");
190 public void simpleYangDataTest() throws Exception {
192 CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
193 XmlToCompositeNodeProvider.INSTANCE);
195 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
197 String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
198 StructuredDataToJsonProvider.INSTANCE);
200 assertNotNull(jsonOutput);
202 verifyJsonOutput(jsonOutput);
205 private void verifyJsonOutput(String jsonOutput) {
206 StringReader strReader = new StringReader(jsonOutput);
207 JsonReader jReader = new JsonReader(strReader);
209 String exception = null;
211 jsonReadCont(jReader);
212 } catch (IOException e) {
213 exception = e.getMessage();
216 assertNull("Error during reading Json output: " + exception, exception);
219 private void jsonReadCont(JsonReader jReader) throws IOException {
220 jReader.beginObject();
221 assertNotNull("cont1 is missing.", jReader.hasNext());
223 // Cont dataFromJson = new Cont(jReader.nextName());
225 jsonReadContElements(jReader);
227 assertFalse("cont shouldn't have other element.", jReader.hasNext());
229 // return dataFromJson;
232 private void jsonReadContElements(JsonReader jReader) throws IOException {
233 jReader.beginObject();
235 Map<String, LeafVerifier> expectedMap = Maps.newHashMap();
236 expectedMap.put("lfnint8Min", new NumberVerifier(Integer.valueOf(-128)));
237 expectedMap.put("lfnint8Max", new NumberVerifier(Integer.valueOf(127)));
238 expectedMap.put("lfnint16Min", new NumberVerifier(Integer.valueOf(-32768)));
239 expectedMap.put("lfnint16Max", new NumberVerifier(Integer.valueOf(32767)));
240 expectedMap.put("lfnint32Min", new NumberVerifier(Integer.valueOf(-2147483648)));
241 expectedMap.put("lfnint32Max", new NumberVerifier(Long.valueOf(2147483647)));
242 expectedMap.put("lfnint64Min", new NumberVerifier(Long.valueOf(-9223372036854775808L)));
243 expectedMap.put("lfnint64Max", new NumberVerifier(Long.valueOf(9223372036854775807L)));
244 expectedMap.put("lfnuint8Max", new NumberVerifier(Integer.valueOf(255)));
245 expectedMap.put("lfnuint16Max", new NumberVerifier(Integer.valueOf(65535)));
246 expectedMap.put("lfnuint32Max", new NumberVerifier(Long.valueOf(4294967295L)));
247 expectedMap.put("lfstr", new StringVerifier("lfstr"));
248 expectedMap.put("lfstr1", new StringVerifier(""));
249 expectedMap.put("lfbool1", new BooleanVerifier(true));
250 expectedMap.put("lfbool2", new BooleanVerifier(false));
251 expectedMap.put("lfbool3", new BooleanVerifier(false));
252 expectedMap.put("lfdecimal1", new NumberVerifier(new Double(43.32)));
253 expectedMap.put("lfdecimal2", new NumberVerifier(new Double(-0.43)));
254 expectedMap.put("lfdecimal3", new NumberVerifier(new Double(43)));
255 expectedMap.put("lfdecimal4", new NumberVerifier(new Double(43E3)));
256 expectedMap.put("lfdecimal6", new NumberVerifier(new Double(33.12345)));
257 expectedMap.put("lfenum", new StringVerifier("enum3"));
258 expectedMap.put("lfbits", new StringVerifier("bit3 bit2"));
259 expectedMap.put("lfbinary", new StringVerifier("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
260 expectedMap.put("lfunion1", new StringVerifier("324"));
261 expectedMap.put("lfunion2", new StringVerifier("33.3"));
262 expectedMap.put("lfunion3", new StringVerifier("55"));
263 expectedMap.put("lfunion4", new StringVerifier("true"));
264 expectedMap.put("lfunion5", new StringVerifier("true"));
265 expectedMap.put("lfunion6", new StringVerifier("10"));
266 expectedMap.put("lfunion7", new StringVerifier(""));
267 expectedMap.put("lfunion8", new StringVerifier(""));
268 expectedMap.put("lfunion9", new StringVerifier(""));
269 expectedMap.put("lfunion10", new StringVerifier("bt1"));
270 expectedMap.put("lfunion11", new StringVerifier("33"));
271 expectedMap.put("lfunion12", new StringVerifier("false"));
272 expectedMap.put("lfunion13", new StringVerifier("b1"));
273 expectedMap.put("lfunion14", new StringVerifier("zero"));
274 expectedMap.put("lfempty", new EmptyVerifier());
275 expectedMap.put("identityref1", new StringVerifier("simple-data-types:iden"));
276 expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
277 expectedMap.put("simple-any", new StringVerifier("simple"));
278 expectedMap.put("empty-any", new StringVerifier(""));
280 while (jReader.hasNext()) {
281 String keyName = jReader.nextName();
282 JsonToken peek = jReader.peek();
284 LeafVerifier verifier = expectedMap.remove(keyName);
285 assertNotNull("Found unexpected leaf: " + keyName, verifier);
287 JsonToken expToken = verifier.expectedTokenType();
288 if (expToken != null) {
289 assertEquals("Json token type for key " + keyName, expToken, peek);
292 verifier.verify(jReader, keyName);
295 if (!expectedMap.isEmpty()) {
296 fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
303 public void testBadData() throws Exception {
306 CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/bad-data.xml",
307 XmlToCompositeNodeProvider.INSTANCE);
309 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
310 fail("Expected RestconfDocumentedException");
311 } catch (RestconfDocumentedException e) {
312 assertEquals("getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());