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 java.io.IOException;
17 import java.io.StringReader;
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;
30 import com.google.common.collect.Maps;
31 import com.google.gson.stream.JsonReader;
32 import com.google.gson.stream.JsonToken;
34 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
36 static abstract class LeafVerifier {
39 JsonToken expectedToken;
41 LeafVerifier( Object expectedValue, JsonToken expectedToken ) {
42 this.expectedValue = expectedValue;
43 this.expectedToken = expectedToken;
46 abstract Object getActualValue( JsonReader reader ) throws IOException;
48 void verify( JsonReader reader, String keyName ) throws IOException {
49 assertEquals( "Json value for key " + keyName, expectedValue, getActualValue( reader ) );
52 JsonToken expectedTokenType() {
57 static class BooleanVerifier extends LeafVerifier {
59 public BooleanVerifier( boolean expected ) {
60 super( expected, JsonToken.BOOLEAN );
64 Object getActualValue( JsonReader reader ) throws IOException {
65 return reader.nextBoolean();
69 static class NumberVerifier extends LeafVerifier {
71 public NumberVerifier( Number expected ) {
72 super( expected, JsonToken.NUMBER );
76 Object getActualValue( JsonReader reader ) throws IOException {
77 if( expectedValue instanceof Double ) {
78 return reader.nextDouble();
80 else if( expectedValue instanceof Long ) {
81 return reader.nextLong();
83 else if( expectedValue instanceof Integer ) {
84 return reader.nextInt();
91 static class StringVerifier extends LeafVerifier {
93 StringVerifier( String expected ) {
94 super( expected, JsonToken.STRING );
98 Object getActualValue( JsonReader reader ) throws IOException {
99 return reader.nextString();
103 static class EmptyVerifier extends LeafVerifier {
110 Object getActualValue( JsonReader reader ) throws IOException {
119 static class ComplexAnyXmlVerifier extends LeafVerifier {
121 ComplexAnyXmlVerifier() {
122 super( null, JsonToken.BEGIN_OBJECT );
126 void verify( JsonReader reader, String keyName ) throws IOException {
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() );
133 reader.beginObject();
134 verifyLeaf( reader, innerKey, "leaf1", "leaf1-value" );
135 verifyLeaf( reader, innerKey, "leaf2", "leaf2-value" );
137 String nextName = reader.nextName();
138 assertEquals( "Json reader child key for " + innerKey, "leaf-list", nextName );
140 assertEquals( "Json value for key " + nextName, "leaf-list-value1", reader.nextString() );
141 assertEquals( "Json value for key " + nextName, "leaf-list-value2", reader.nextString() );
144 nextName = reader.nextName();
145 assertEquals( "Json reader child key for " + innerKey, "list", nextName );
147 verifyNestedLists( reader, 1 );
148 verifyNestedLists( reader, 3 );
155 void verifyNestedLists( JsonReader reader, int leafNum ) throws IOException {
156 reader.beginObject();
158 String nextName = reader.nextName();
159 assertEquals( "Json reader next name", "nested-list", nextName );
163 reader.beginObject();
164 verifyLeaf( reader, "nested-list", "nested-leaf", "nested-value" + leafNum++ );
167 reader.beginObject();
168 verifyLeaf( reader, "nested-list", "nested-leaf", "nested-value" + leafNum );
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() );
183 Object getActualValue( JsonReader reader ) throws IOException {
189 public static void initialize() {
190 dataLoad("/cnsn-to-json/simple-data-types");
194 public void simpleYangDataTest() throws Exception {
196 CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
197 XmlToCompositeNodeProvider.INSTANCE);
199 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
201 String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
202 StructuredDataToJsonProvider.INSTANCE);
204 assertNotNull(jsonOutput);
206 verifyJsonOutput(jsonOutput);
209 private void verifyJsonOutput(String jsonOutput) {
210 StringReader strReader = new StringReader(jsonOutput);
211 JsonReader jReader = new JsonReader(strReader);
213 String exception = null;
215 jsonReadCont(jReader);
216 } catch (IOException e) {
217 exception = e.getMessage();
220 assertNull("Error during reading Json output: " + exception, exception);
223 private void jsonReadCont(JsonReader jReader) throws IOException {
224 jReader.beginObject();
225 assertNotNull("cont1 is missing.", jReader.hasNext());
227 // Cont dataFromJson = new Cont(jReader.nextName());
229 jsonReadContElements(jReader);
231 assertFalse("cont shouldn't have other element.", jReader.hasNext());
233 // return dataFromJson;
236 private void jsonReadContElements(JsonReader jReader) throws IOException {
237 jReader.beginObject();
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( "" ) );
284 while (jReader.hasNext()) {
285 String keyName = jReader.nextName();
286 JsonToken peek = jReader.peek();
288 LeafVerifier verifier = expectedMap.remove( keyName );
289 assertNotNull( "Found unexpected leaf: " + keyName , verifier );
291 JsonToken expToken = verifier.expectedTokenType();
292 if( expToken != null ) {
293 assertEquals( "Json token type for key " + keyName, expToken, peek );
296 verifier.verify( jReader, keyName );
299 if( !expectedMap.isEmpty() ) {
300 fail( "Missing leaf nodes in Json output: " +expectedMap.keySet() );
307 public void testBadData() throws Exception {
310 CompositeNode compositeNode = TestUtils.readInputToCnSn(
311 "/cnsn-to-json/simple-data-types/xml/bad-data.xml",
312 XmlToCompositeNodeProvider.INSTANCE);
314 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
315 fail( "Expected RestconfDocumentedException" );
317 catch( RestconfDocumentedException e ) {
318 assertEquals( "getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get( 0 ).getErrorTag() );