Merge "Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to...
[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
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;
21 import java.util.Map;
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;
32
33 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
34
35     static abstract class LeafVerifier {
36
37         Object expectedValue;
38         JsonToken expectedToken;
39
40         LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
41             this.expectedValue = expectedValue;
42             this.expectedToken = expectedToken;
43         }
44
45         abstract Object getActualValue(JsonReader reader) throws IOException;
46
47         void verify(final JsonReader reader, final String keyName) throws IOException {
48             assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
49         }
50
51         JsonToken expectedTokenType() {
52             return expectedToken;
53         }
54     }
55
56     static class BooleanVerifier extends LeafVerifier {
57
58         public BooleanVerifier(final boolean expected) {
59             super(expected, JsonToken.BOOLEAN);
60         }
61
62         @Override
63         Object getActualValue(final JsonReader reader) throws IOException {
64             return reader.nextBoolean();
65         }
66     }
67
68     static class NumberVerifier extends LeafVerifier {
69
70         public NumberVerifier(final Number expected) {
71             super(expected, JsonToken.NUMBER);
72         }
73
74         @Override
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();
82             }
83
84             return null;
85         }
86     }
87
88     static class StringVerifier extends LeafVerifier {
89
90         StringVerifier(final String expected) {
91             super(expected, JsonToken.STRING);
92         }
93
94         @Override
95         Object getActualValue(final JsonReader reader) throws IOException {
96             return reader.nextString();
97         }
98     }
99
100     static class EmptyVerifier extends LeafVerifier {
101
102         EmptyVerifier() {
103             super(null, null);
104         }
105
106         @Override
107         Object getActualValue(final JsonReader reader) throws IOException {
108             reader.beginArray();
109             reader.nextNull();
110             reader.endArray();
111             return null;
112         }
113
114     }
115
116     static class ComplexAnyXmlVerifier extends LeafVerifier {
117
118         ComplexAnyXmlVerifier() {
119             super(null, JsonToken.BEGIN_OBJECT);
120         }
121
122         @Override
123         void verify(final JsonReader reader, final String keyName) throws IOException {
124
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());
129
130             reader.beginObject();
131             verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
132             verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
133
134             String nextName = reader.nextName();
135             assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
136             reader.beginArray();
137             assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
138             assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
139             reader.endArray();
140
141             nextName = reader.nextName();
142             assertEquals("Json reader child key for " + innerKey, "list", nextName);
143             reader.beginArray();
144             verifyNestedLists(reader, 1);
145             verifyNestedLists(reader, 3);
146             reader.endArray();
147
148             reader.endObject();
149             reader.endObject();
150         }
151
152         void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
153             reader.beginObject();
154
155             final String nextName = reader.nextName();
156             assertEquals("Json reader next name", "nested-list", nextName);
157
158             reader.beginArray();
159
160             reader.beginObject();
161             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
162             reader.endObject();
163
164             reader.beginObject();
165             verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
166             reader.endObject();
167
168             reader.endArray();
169             reader.endObject();
170         }
171
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());
177         }
178
179         @Override
180         Object getActualValue(final JsonReader reader) throws IOException {
181             return null;
182         }
183     }
184
185     @BeforeClass
186     public static void initialize() {
187         dataLoad("/cnsn-to-json/simple-data-types");
188     }
189
190     @Test
191     @Ignore
192     public void simpleYangDataTest() throws Exception {
193
194         final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
195                 XmlToCompositeNodeProvider.INSTANCE);
196
197         TestUtils.normalizeCompositeNode(node, modules, "simple-data-types:cont");
198
199         final String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(node, modules, dataSchemaNode,
200                 StructuredDataToJsonProvider.INSTANCE);
201
202         assertNotNull(jsonOutput);
203
204         verifyJsonOutput(jsonOutput);
205     }
206
207     private void verifyJsonOutput(final String jsonOutput) {
208         final StringReader strReader = new StringReader(jsonOutput);
209         final JsonReader jReader = new JsonReader(strReader);
210
211         String exception = null;
212         try {
213             jsonReadCont(jReader);
214         } catch (final IOException e) {
215             exception = e.getMessage();
216         }
217
218         assertNull("Error during reading Json output: " + exception, exception);
219     }
220
221     private void jsonReadCont(final JsonReader jReader) throws IOException {
222         jReader.beginObject();
223         assertNotNull("cont1 is missing.", jReader.hasNext());
224
225         // Cont dataFromJson = new Cont(jReader.nextName());
226         jReader.nextName();
227         jsonReadContElements(jReader);
228
229         assertFalse("cont shouldn't have other element.", jReader.hasNext());
230         jReader.endObject();
231         // return dataFromJson;
232     }
233
234     private void jsonReadContElements(final JsonReader jReader) throws IOException {
235         jReader.beginObject();
236
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(""));
281
282         while (jReader.hasNext()) {
283             final String keyName = jReader.nextName();
284             final JsonToken peek = jReader.peek();
285
286             final LeafVerifier verifier = expectedMap.remove(keyName);
287             assertNotNull("Found unexpected leaf: " + keyName, verifier);
288
289             final JsonToken expToken = verifier.expectedTokenType();
290             if (expToken != null) {
291                 assertEquals("Json token type for key " + keyName, expToken, peek);
292             }
293
294             verifier.verify(jReader, keyName);
295         }
296
297         if (!expectedMap.isEmpty()) {
298             fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
299         }
300
301         jReader.endObject();
302     }
303
304     @Test
305     public void testBadData() throws Exception {
306
307         try {
308             final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/bad-data.xml",
309                     XmlToCompositeNodeProvider.INSTANCE);
310
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());
315         }
316     }
317 }