Merge "Removed `which` dependency, now using proper shell builtin."
[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     @BeforeClass
120     public static void initialize() {
121         dataLoad("/cnsn-to-json/simple-data-types");
122     }
123
124     @Test
125     public void simpleYangDataTest() throws Exception {
126
127         CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
128                 XmlToCompositeNodeProvider.INSTANCE);
129
130         TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
131
132         String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
133                     StructuredDataToJsonProvider.INSTANCE);
134
135         assertNotNull(jsonOutput);
136
137         verifyJsonOutput(jsonOutput);
138     }
139
140     private void verifyJsonOutput(String jsonOutput) {
141         StringReader strReader = new StringReader(jsonOutput);
142         JsonReader jReader = new JsonReader(strReader);
143
144         String exception = null;
145         try {
146             jsonReadCont(jReader);
147         } catch (IOException e) {
148             exception = e.getMessage();
149         }
150
151         assertNull("Error during reading Json output: " + exception, exception);
152     }
153
154     private void jsonReadCont(JsonReader jReader) throws IOException {
155         jReader.beginObject();
156         assertNotNull("cont1 is missing.", jReader.hasNext());
157
158         // Cont dataFromJson = new Cont(jReader.nextName());
159         jReader.nextName();
160         jsonReadContElements(jReader);
161
162         assertFalse("cont shouldn't have other element.", jReader.hasNext());
163         jReader.endObject();
164         // return dataFromJson;
165     }
166
167     private void jsonReadContElements(JsonReader jReader) throws IOException {
168         jReader.beginObject();
169
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" ) );
211
212         while (jReader.hasNext()) {
213             String keyName = jReader.nextName();
214             JsonToken peek = jReader.peek();
215
216             LeafVerifier verifier = expectedMap.remove( keyName );
217             assertNotNull( "Found unexpected leaf: " + keyName , verifier );
218
219             JsonToken expToken = verifier.expectedTokenType();
220             if( expToken != null ) {
221                 assertEquals( "Json token type for key " + keyName, expToken, peek );
222             }
223
224             verifier.verify( jReader, keyName );;
225         }
226
227         if( !expectedMap.isEmpty() ) {
228             fail( "Missing leaf nodes in Json output: " +expectedMap.keySet() );
229         }
230
231         jReader.endObject();
232     }
233
234     @Test
235     public void testBadData() throws Exception {
236
237         try {
238             CompositeNode compositeNode = TestUtils.readInputToCnSn(
239                                                "/cnsn-to-json/simple-data-types/xml/bad-data.xml",
240                                                XmlToCompositeNodeProvider.INSTANCE);
241
242             TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
243             fail( "Expected RestconfDocumentedException" );
244         }
245         catch( RestconfDocumentedException e ) {
246             assertEquals( "getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get( 0 ).getErrorTag() );
247         }
248     }
249 }