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 {
120 public static void initialize() {
121 dataLoad("/cnsn-to-json/simple-data-types");
125 public void simpleYangDataTest() throws Exception {
127 CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
128 XmlToCompositeNodeProvider.INSTANCE);
130 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
132 String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
133 StructuredDataToJsonProvider.INSTANCE);
135 assertNotNull(jsonOutput);
137 verifyJsonOutput(jsonOutput);
140 private void verifyJsonOutput(String jsonOutput) {
141 StringReader strReader = new StringReader(jsonOutput);
142 JsonReader jReader = new JsonReader(strReader);
144 String exception = null;
146 jsonReadCont(jReader);
147 } catch (IOException e) {
148 exception = e.getMessage();
151 assertNull("Error during reading Json output: " + exception, exception);
154 private void jsonReadCont(JsonReader jReader) throws IOException {
155 jReader.beginObject();
156 assertNotNull("cont1 is missing.", jReader.hasNext());
158 // Cont dataFromJson = new Cont(jReader.nextName());
160 jsonReadContElements(jReader);
162 assertFalse("cont shouldn't have other element.", jReader.hasNext());
164 // return dataFromJson;
167 private void jsonReadContElements(JsonReader jReader) throws IOException {
168 jReader.beginObject();
170 Map<String,LeafVerifier> expectedMap = Maps.newHashMap();
171 expectedMap.put( "lfnint8Min", new NumberVerifier( Integer.valueOf( -128 ) ) );
172 expectedMap.put( "lfnint8Max", new NumberVerifier( Integer.valueOf( 127 ) ) );
173 expectedMap.put( "lfnint16Min", new NumberVerifier( Integer.valueOf( -32768 ) ) );
174 expectedMap.put( "lfnint16Max", new NumberVerifier( Integer.valueOf( 32767 ) ) );
175 expectedMap.put( "lfnint32Min", new NumberVerifier( Integer.valueOf( -2147483648 ) ) );
176 expectedMap.put( "lfnint32Max", new NumberVerifier( Long.valueOf( 2147483647 ) ) );
177 expectedMap.put( "lfnint64Min", new NumberVerifier( Long.valueOf( -9223372036854775808L ) ) );
178 expectedMap.put( "lfnint64Max", new NumberVerifier( Long.valueOf( 9223372036854775807L ) ) );
179 expectedMap.put( "lfnuint8Max", new NumberVerifier( Integer.valueOf( 255 ) ) );
180 expectedMap.put( "lfnuint16Max", new NumberVerifier( Integer.valueOf( 65535 ) ) );
181 expectedMap.put( "lfnuint32Max", new NumberVerifier( Long.valueOf( 4294967295L ) ) );
182 expectedMap.put( "lfstr", new StringVerifier( "lfstr" ) );
183 expectedMap.put( "lfstr1", new StringVerifier( "" ) );
184 expectedMap.put( "lfbool1", new BooleanVerifier( true ) );
185 expectedMap.put( "lfbool2", new BooleanVerifier( false ) );
186 expectedMap.put( "lfbool3", new BooleanVerifier( false ) );
187 expectedMap.put( "lfdecimal1", new NumberVerifier( new Double( 43.32 ) ) );
188 expectedMap.put( "lfdecimal2", new NumberVerifier( new Double( -0.43 ) ) );
189 expectedMap.put( "lfdecimal3", new NumberVerifier( new Double( 43 ) ) );
190 expectedMap.put( "lfdecimal4", new NumberVerifier( new Double( 43E3 ) ) );
191 expectedMap.put( "lfdecimal6", new NumberVerifier( new Double( 33.12345 ) ) );
192 expectedMap.put( "lfenum", new StringVerifier( "enum3" ) );
193 expectedMap.put( "lfbits", new StringVerifier( "bit3 bit2" ) );
194 expectedMap.put( "lfbinary", new StringVerifier( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ) );
195 expectedMap.put( "lfunion1", new StringVerifier( "324" ) );
196 expectedMap.put( "lfunion2", new StringVerifier( "33.3" ) );
197 expectedMap.put( "lfunion3", new StringVerifier( "55" ) );
198 expectedMap.put( "lfunion4", new StringVerifier( "true" ) );
199 expectedMap.put( "lfunion5", new StringVerifier( "true" ) );
200 expectedMap.put( "lfunion6", new StringVerifier( "10" ) );
201 expectedMap.put( "lfunion7", new StringVerifier( "" ) );
202 expectedMap.put( "lfunion8", new StringVerifier( "" ) );
203 expectedMap.put( "lfunion9", new StringVerifier( "" ) );
204 expectedMap.put( "lfunion10", new StringVerifier( "bt1" ) );
205 expectedMap.put( "lfunion11", new StringVerifier( "33" ) );
206 expectedMap.put( "lfunion12", new StringVerifier( "false" ) );
207 expectedMap.put( "lfunion13", new StringVerifier( "b1" ) );
208 expectedMap.put( "lfunion14", new StringVerifier( "zero" ) );
209 expectedMap.put( "lfempty", new EmptyVerifier() );
210 expectedMap.put( "identityref1", new StringVerifier( "simple-data-types:iden" ) );
212 while (jReader.hasNext()) {
213 String keyName = jReader.nextName();
214 JsonToken peek = jReader.peek();
216 LeafVerifier verifier = expectedMap.remove( keyName );
217 assertNotNull( "Found unexpected leaf: " + keyName , verifier );
219 JsonToken expToken = verifier.expectedTokenType();
220 if( expToken != null ) {
221 assertEquals( "Json token type for key " + keyName, expToken, peek );
224 verifier.verify( jReader, keyName );
227 if( !expectedMap.isEmpty() ) {
228 fail( "Missing leaf nodes in Json output: " +expectedMap.keySet() );
235 public void testBadData() throws Exception {
238 CompositeNode compositeNode = TestUtils.readInputToCnSn(
239 "/cnsn-to-json/simple-data-types/xml/bad-data.xml",
240 XmlToCompositeNodeProvider.INSTANCE);
242 TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
243 fail( "Expected RestconfDocumentedException" );
245 catch( RestconfDocumentedException e ) {
246 assertEquals( "getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get( 0 ).getErrorTag() );