Merge "Fix modules Restconf call for mounted devices"
[controller.git] / opendaylight / md-sal / sal-rest-connector / 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 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;
20 import java.util.Map;
21 import org.junit.BeforeClass;
22 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
23
24 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
25
26     static abstract class LeafVerifier {
27
28         Object expectedValue;
29         JsonToken expectedToken;
30
31         LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
32             this.expectedValue = expectedValue;
33             this.expectedToken = expectedToken;
34         }
35
36         abstract Object getActualValue(JsonReader reader) throws IOException;
37
38         void verify(final JsonReader reader, final String keyName) throws IOException {
39             assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
40         }
41
42         JsonToken expectedTokenType() {
43             return expectedToken;
44         }
45     }
46
47     static class BooleanVerifier extends LeafVerifier {
48
49         public BooleanVerifier(final boolean expected) {
50             super(expected, JsonToken.BOOLEAN);
51         }
52
53         @Override
54         Object getActualValue(final JsonReader reader) throws IOException {
55             return reader.nextBoolean();
56         }
57     }
58
59     static class NumberVerifier extends LeafVerifier {
60
61         public NumberVerifier(final Number expected) {
62             super(expected, JsonToken.NUMBER);
63         }
64
65         @Override
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();
73             }
74
75             return null;
76         }
77     }
78
79     static class StringVerifier extends LeafVerifier {
80
81         StringVerifier(final String expected) {
82             super(expected, JsonToken.STRING);
83         }
84
85         @Override
86         Object getActualValue(final JsonReader reader) throws IOException {
87             return reader.nextString();
88         }
89     }
90
91     static class EmptyVerifier extends LeafVerifier {
92
93         EmptyVerifier() {
94             super(null, null);
95         }
96
97         @Override
98         Object getActualValue(final JsonReader reader) throws IOException {
99             reader.beginArray();
100             reader.nextNull();
101             reader.endArray();
102             return null;
103         }
104
105     }
106
107     static class ComplexAnyXmlVerifier extends LeafVerifier {
108
109         ComplexAnyXmlVerifier() {
110             super(null, JsonToken.BEGIN_OBJECT);
111         }
112
113         @Override
114         void verify(final JsonReader reader, final String keyName) throws IOException {
115
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());
120
121             reader.beginObject();
122             verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
123             verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
124
125             String nextName = reader.nextName();
126             assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
127             reader.beginArray();
128             assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
129             assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
130             reader.endArray();
131
132             nextName = reader.nextName();
133             assertEquals("Json reader child key for " + innerKey, "list", nextName);
134             reader.beginArray();
135             verifyNestedLists(reader, 1);
136             verifyNestedLists(reader, 3);
137             reader.endArray();
138
139             reader.endObject();
140             reader.endObject();
141         }
142
143         void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
144             reader.beginObject();
145
146             final String nextName = reader.nextName();
147             assertEquals("Json reader next name", "nested-list", nextName);
148
149             reader.beginArray();
150
151             reader.beginObject();
152             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
153             reader.endObject();
154
155             reader.beginObject();
156             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
157             reader.endObject();
158
159             reader.endArray();
160             reader.endObject();
161         }
162
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());
168         }
169
170         @Override
171         Object getActualValue(final JsonReader reader) throws IOException {
172             return null;
173         }
174     }
175
176     @BeforeClass
177     public static void initialize() {
178         dataLoad("/cnsn-to-json/simple-data-types");
179     }
180
181     private void verifyJsonOutput(final String jsonOutput) {
182         final StringReader strReader = new StringReader(jsonOutput);
183         final JsonReader jReader = new JsonReader(strReader);
184
185         String exception = null;
186         try {
187             jsonReadCont(jReader);
188         } catch (final IOException e) {
189             exception = e.getMessage();
190         }
191
192         assertNull("Error during reading Json output: " + exception, exception);
193     }
194
195     private void jsonReadCont(final JsonReader jReader) throws IOException {
196         jReader.beginObject();
197         assertNotNull("cont1 is missing.", jReader.hasNext());
198
199         // Cont dataFromJson = new Cont(jReader.nextName());
200         jReader.nextName();
201         jsonReadContElements(jReader);
202
203         assertFalse("cont shouldn't have other element.", jReader.hasNext());
204         jReader.endObject();
205         // return dataFromJson;
206     }
207
208     private void jsonReadContElements(final JsonReader jReader) throws IOException {
209         jReader.beginObject();
210
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(""));
255
256         while (jReader.hasNext()) {
257             final String keyName = jReader.nextName();
258             final JsonToken peek = jReader.peek();
259
260             final LeafVerifier verifier = expectedMap.remove(keyName);
261             assertNotNull("Found unexpected leaf: " + keyName, verifier);
262
263             final JsonToken expToken = verifier.expectedTokenType();
264             if (expToken != null) {
265                 assertEquals("Json token type for key " + keyName, expToken, peek);
266             }
267
268             verifier.verify(jReader, keyName);
269         }
270
271         if (!expectedMap.isEmpty()) {
272             fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
273         }
274
275         jReader.endObject();
276     }
277
278 }