Merge "Implement finding a primary based on the shard name and do basic wiring of...
[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 java.io.IOException;
17 import java.io.StringReader;
18 import java.util.Map;
19
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
23 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
24 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
26 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
27 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29
30 import com.google.common.collect.Maps;
31 import com.google.gson.stream.JsonReader;
32 import com.google.gson.stream.JsonToken;
33
34 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
35
36     static abstract class LeafVerifier {
37
38         Object expectedValue;
39         JsonToken expectedToken;
40
41         LeafVerifier( Object expectedValue, JsonToken expectedToken ) {
42             this.expectedValue = expectedValue;
43             this.expectedToken = expectedToken;
44         }
45
46         abstract Object getActualValue( JsonReader reader ) throws IOException;
47
48         void verify( JsonReader reader, String keyName ) throws IOException {
49             assertEquals( "Json value for key " + keyName, expectedValue, getActualValue( reader ) );
50         }
51
52         JsonToken expectedTokenType() {
53             return expectedToken;
54         }
55     }
56
57     static class BooleanVerifier extends LeafVerifier {
58
59         public BooleanVerifier( boolean expected ) {
60             super( expected, JsonToken.BOOLEAN );
61         }
62
63         @Override
64         Object getActualValue( JsonReader reader ) throws IOException {
65             return reader.nextBoolean();
66         }
67     }
68
69     static class NumberVerifier extends LeafVerifier {
70
71         public NumberVerifier( Number expected ) {
72             super( expected, JsonToken.NUMBER );
73         }
74
75         @Override
76         Object getActualValue( JsonReader reader ) throws IOException {
77             if( expectedValue instanceof Double ) {
78                 return reader.nextDouble();
79             }
80             else if( expectedValue instanceof Long ) {
81                 return reader.nextLong();
82             }
83             else if( expectedValue instanceof Integer ) {
84                 return reader.nextInt();
85             }
86
87             return null;
88         }
89     }
90
91     static class StringVerifier extends LeafVerifier {
92
93         StringVerifier( String expected ) {
94             super( expected, JsonToken.STRING );
95         }
96
97         @Override
98         Object getActualValue( JsonReader reader ) throws IOException {
99             return reader.nextString();
100         }
101     }
102
103     static class EmptyVerifier extends LeafVerifier {
104
105         EmptyVerifier() {
106             super( null, null );
107         }
108
109         @Override
110         Object getActualValue( JsonReader reader ) throws IOException {
111             reader.beginArray();
112             reader.nextNull();
113             reader.endArray();
114             return null;
115         }
116
117     }
118
119     static class ComplexAnyXmlVerifier extends LeafVerifier {
120
121         ComplexAnyXmlVerifier() {
122             super( null, JsonToken.BEGIN_OBJECT );
123         }
124
125         @Override
126         void verify( JsonReader reader, String keyName ) throws IOException {
127
128             reader.beginObject();
129             String innerKey = reader.nextName();
130             assertEquals( "Json reader child key for " + keyName, "data", innerKey );
131             assertEquals( "Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek() );
132
133             reader.beginObject();
134             verifyLeaf( reader, innerKey, "leaf1", "leaf1-value" );
135             verifyLeaf( reader, innerKey, "leaf2", "leaf2-value" );
136
137             String nextName = reader.nextName();
138             assertEquals( "Json reader child key for " + innerKey, "leaf-list", nextName );
139             reader.beginArray();
140             assertEquals( "Json value for key " + nextName, "leaf-list-value1", reader.nextString() );
141             assertEquals( "Json value for key " + nextName, "leaf-list-value2", reader.nextString() );
142             reader.endArray();
143
144             nextName = reader.nextName();
145             assertEquals( "Json reader child key for " + innerKey, "list", nextName );
146             reader.beginArray();
147             verifyNestedLists( reader, 1 );
148             verifyNestedLists( reader, 3 );
149             reader.endArray();
150
151             reader.endObject();
152             reader.endObject();
153         }
154
155         void verifyNestedLists( JsonReader reader, int leafNum ) throws IOException {
156             reader.beginObject();
157
158             String nextName = reader.nextName();
159             assertEquals( "Json reader next name", "nested-list", nextName );
160
161             reader.beginArray();
162
163             reader.beginObject();
164             verifyLeaf( reader, "nested-list", "nested-leaf", "nested-value" + leafNum++ );
165             reader.endObject();
166
167             reader.beginObject();
168             verifyLeaf( reader, "nested-list", "nested-leaf", "nested-value" + leafNum );
169             reader.endObject();
170
171             reader.endArray();
172             reader.endObject();
173         }
174
175         void verifyLeaf( JsonReader reader, String parent, String name, String value ) throws IOException {
176             String nextName = reader.nextName();
177             assertEquals( "Json reader child key for " + parent, name, nextName );
178             assertEquals( "Json token type for key " + parent, JsonToken.STRING, reader.peek() );
179             assertEquals( "Json value for key " + nextName, value, reader.nextString() );
180         }
181
182         @Override
183         Object getActualValue( JsonReader reader ) throws IOException {
184             return null;
185         }
186     }
187
188     @BeforeClass
189     public static void initialize() {
190         dataLoad("/cnsn-to-json/simple-data-types");
191     }
192
193     @Test
194     public void simpleYangDataTest() throws Exception {
195
196         CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
197                 XmlToCompositeNodeProvider.INSTANCE);
198
199         TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
200
201         String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
202                     StructuredDataToJsonProvider.INSTANCE);
203
204         assertNotNull(jsonOutput);
205
206         verifyJsonOutput(jsonOutput);
207     }
208
209     private void verifyJsonOutput(String jsonOutput) {
210         StringReader strReader = new StringReader(jsonOutput);
211         JsonReader jReader = new JsonReader(strReader);
212
213         String exception = null;
214         try {
215             jsonReadCont(jReader);
216         } catch (IOException e) {
217             exception = e.getMessage();
218         }
219
220         assertNull("Error during reading Json output: " + exception, exception);
221     }
222
223     private void jsonReadCont(JsonReader jReader) throws IOException {
224         jReader.beginObject();
225         assertNotNull("cont1 is missing.", jReader.hasNext());
226
227         // Cont dataFromJson = new Cont(jReader.nextName());
228         jReader.nextName();
229         jsonReadContElements(jReader);
230
231         assertFalse("cont shouldn't have other element.", jReader.hasNext());
232         jReader.endObject();
233         // return dataFromJson;
234     }
235
236     private void jsonReadContElements(JsonReader jReader) throws IOException {
237         jReader.beginObject();
238
239         Map<String,LeafVerifier> expectedMap = Maps.newHashMap();
240         expectedMap.put( "lfnint8Min", new NumberVerifier( Integer.valueOf( -128 ) ) );
241         expectedMap.put( "lfnint8Max", new NumberVerifier( Integer.valueOf( 127 ) ) );
242         expectedMap.put( "lfnint16Min", new NumberVerifier( Integer.valueOf( -32768 ) ) );
243         expectedMap.put( "lfnint16Max", new NumberVerifier( Integer.valueOf( 32767 ) ) );
244         expectedMap.put( "lfnint32Min", new NumberVerifier( Integer.valueOf( -2147483648 ) ) );
245         expectedMap.put( "lfnint32Max", new NumberVerifier( Long.valueOf( 2147483647 ) ) );
246         expectedMap.put( "lfnint64Min", new NumberVerifier( Long.valueOf( -9223372036854775808L ) ) );
247         expectedMap.put( "lfnint64Max", new NumberVerifier( Long.valueOf( 9223372036854775807L ) ) );
248         expectedMap.put( "lfnuint8Max", new NumberVerifier( Integer.valueOf( 255 ) ) );
249         expectedMap.put( "lfnuint16Max", new NumberVerifier( Integer.valueOf( 65535 ) ) );
250         expectedMap.put( "lfnuint32Max", new NumberVerifier( Long.valueOf( 4294967295L ) ) );
251         expectedMap.put( "lfstr", new StringVerifier( "lfstr" ) );
252         expectedMap.put( "lfstr1", new StringVerifier( "" ) );
253         expectedMap.put( "lfbool1", new BooleanVerifier( true ) );
254         expectedMap.put( "lfbool2", new BooleanVerifier( false ) );
255         expectedMap.put( "lfbool3", new BooleanVerifier( false ) );
256         expectedMap.put( "lfdecimal1", new NumberVerifier( new Double( 43.32 ) ) );
257         expectedMap.put( "lfdecimal2", new NumberVerifier( new Double( -0.43 ) ) );
258         expectedMap.put( "lfdecimal3", new NumberVerifier( new Double( 43 ) ) );
259         expectedMap.put( "lfdecimal4", new NumberVerifier( new Double( 43E3 ) ) );
260         expectedMap.put( "lfdecimal6", new NumberVerifier( new Double( 33.12345 ) ) );
261         expectedMap.put( "lfenum", new StringVerifier( "enum3" ) );
262         expectedMap.put( "lfbits", new StringVerifier( "bit3 bit2" ) );
263         expectedMap.put( "lfbinary", new StringVerifier( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ) );
264         expectedMap.put( "lfunion1", new StringVerifier( "324" ) );
265         expectedMap.put( "lfunion2", new StringVerifier( "33.3" ) );
266         expectedMap.put( "lfunion3", new StringVerifier( "55" ) );
267         expectedMap.put( "lfunion4", new StringVerifier( "true" ) );
268         expectedMap.put( "lfunion5", new StringVerifier( "true" ) );
269         expectedMap.put( "lfunion6", new StringVerifier( "10" ) );
270         expectedMap.put( "lfunion7", new StringVerifier( "" ) );
271         expectedMap.put( "lfunion8", new StringVerifier( "" ) );
272         expectedMap.put( "lfunion9", new StringVerifier( "" ) );
273         expectedMap.put( "lfunion10", new StringVerifier( "bt1" ) );
274         expectedMap.put( "lfunion11", new StringVerifier( "33" ) );
275         expectedMap.put( "lfunion12", new StringVerifier( "false" ) );
276         expectedMap.put( "lfunion13", new StringVerifier( "b1" ) );
277         expectedMap.put( "lfunion14", new StringVerifier( "zero" ) );
278         expectedMap.put( "lfempty", new EmptyVerifier() );
279         expectedMap.put( "identityref1", new StringVerifier( "simple-data-types:iden" ) );
280         expectedMap.put( "complex-any", new ComplexAnyXmlVerifier() );
281         expectedMap.put( "simple-any", new StringVerifier( "simple" ) );
282         expectedMap.put( "empty-any", new StringVerifier( "" ) );
283
284         while (jReader.hasNext()) {
285             String keyName = jReader.nextName();
286             JsonToken peek = jReader.peek();
287
288             LeafVerifier verifier = expectedMap.remove( keyName );
289             assertNotNull( "Found unexpected leaf: " + keyName , verifier );
290
291             JsonToken expToken = verifier.expectedTokenType();
292             if( expToken != null ) {
293                 assertEquals( "Json token type for key " + keyName, expToken, peek );
294             }
295
296             verifier.verify( jReader, keyName );
297         }
298
299         if( !expectedMap.isEmpty() ) {
300             fail( "Missing leaf nodes in Json output: " +expectedMap.keySet() );
301         }
302
303         jReader.endObject();
304     }
305
306     @Test
307     public void testBadData() throws Exception {
308
309         try {
310             CompositeNode compositeNode = TestUtils.readInputToCnSn(
311                                                "/cnsn-to-json/simple-data-types/xml/bad-data.xml",
312                                                XmlToCompositeNodeProvider.INSTANCE);
313
314             TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
315             fail( "Expected RestconfDocumentedException" );
316         }
317         catch( RestconfDocumentedException e ) {
318             assertEquals( "getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get( 0 ).getErrorTag() );
319         }
320     }
321 }