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