6d3ebb30a4500c3f517505f373acdfa0f4d63c4d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.schema.avro;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.SchemaHelper;
31 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.common.parameters.ParameterService;
40
41 /**
42  * The Class TestAvroSchemaHelperMarshal.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  * @version
46  */
47 public class AvroSchemaHelperMarshalTest {
48     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
49     private AxContextSchemas schemas;
50
51     /**
52      * Inits the test.
53      */
54     @Before
55     public void initTest() {
56         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
57         ModelService.registerModel(AxContextSchemas.class, schemas);
58     }
59
60     /**
61      * Inits the context.
62      */
63     @Before
64     public void initContext() {
65         SchemaParameters schemaParameters = new SchemaParameters();
66         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
67         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
68         ParameterService.register(schemaParameters);
69
70     }
71
72     /**
73      * Clear context.
74      */
75     @After
76     public void clearContext() {
77         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
78     }
79
80     /**
81      * Test null marshal.
82      */
83     @Test
84     public void testNullMarshal() {
85         final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
86                         "{\"type\": \"null\"}");
87
88         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
89         final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
90                         avroNullSchema.getKey());
91
92         assertEquals("null", schemaHelper0.marshal2String(null));
93         assertEquals("null", schemaHelper0.marshal2String(123));
94         assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is"));
95     }
96
97     /**
98      * Test boolean marshal.
99      */
100     @Test
101     public void testBooleanMarshal() {
102         final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
103                         "{\"type\": \"boolean\"}");
104
105         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
106         final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
107                         avroBooleanSchema.getKey());
108
109         assertEquals("true", schemaHelper1.marshal2String(true));
110         assertEquals("false", schemaHelper1.marshal2String(false));
111         try {
112             schemaHelper1.marshal2String(0);
113             fail("Test should throw an exception here");
114         } catch (final Exception e) {
115             e.printStackTrace();
116             assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
117                             + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage());
118         }
119         try {
120             schemaHelper1.marshal2String("0");
121             fail("Test should throw an exception here");
122         } catch (final Exception e) {
123             e.printStackTrace();
124             assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
125                             + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage());
126         }
127     }
128
129     /**
130      * Test int marshal.
131      */
132     @Test
133     public void testIntMarshal() {
134         final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
135                         "{\"type\": \"int\"}");
136
137         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
138         final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
139                         avroIntSchema.getKey());
140
141         assertEquals("0", schemaHelper2.marshal2String(0));
142         assertEquals("1", schemaHelper2.marshal2String(1));
143         assertEquals("-1", schemaHelper2.marshal2String(-1));
144         assertEquals("1", schemaHelper2.marshal2String(1.23));
145         assertEquals("-1", schemaHelper2.marshal2String(-1.23));
146         assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
147         assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
148         try {
149             schemaHelper2.marshal2String("Hello");
150             fail("Test should throw an exception here");
151         } catch (final Exception e) {
152             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
153                             + "java.lang.String cannot be cast to java.lang.Number"));
154         }
155         try {
156             schemaHelper2.marshal2String(null);
157             fail("Test should throw an exception here");
158         } catch (final Exception e) {
159             assertTrue(e.getMessage()
160                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
161         }
162     }
163
164     /**
165      * Test long marshal.
166      */
167     @Test
168     public void testLongMarshal() {
169         final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
170                         "{\"type\": \"long\"}");
171
172         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
173         final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
174                         avroLongSchema.getKey());
175
176         assertEquals("0", schemaHelper3.marshal2String(0L));
177         assertEquals("1", schemaHelper3.marshal2String(1L));
178         assertEquals("-1", schemaHelper3.marshal2String(-1L));
179         assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
180         assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
181         try {
182             schemaHelper3.marshal2String("Hello");
183             fail("Test should throw an exception here");
184         } catch (final Exception e) {
185             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
186                             + "java.lang.String cannot be cast to java.lang.Long"));
187         }
188         try {
189             schemaHelper3.marshal2String(null);
190             fail("Test should throw an exception here");
191         } catch (final Exception e) {
192             assertTrue(e.getMessage()
193                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
194         }
195     }
196
197     /**
198      * Test float marshal.
199      */
200     @Test
201     public void testFloatMarshal() {
202         final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
203                         "{\"type\": \"float\"}");
204
205         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
206         final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
207                         avroFloatSchema.getKey());
208
209         assertEquals("0.0", schemaHelper4.marshal2String(0F));
210         assertEquals("1.0", schemaHelper4.marshal2String(1F));
211         assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
212         assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
213         assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
214         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
215         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
216         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
217         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
218         try {
219             schemaHelper4.marshal2String("Hello");
220             fail("Test should throw an exception here");
221         } catch (final Exception e) {
222             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
223                             + "java.lang.String cannot be cast to java.lang.Float"));
224         }
225         try {
226             schemaHelper4.marshal2String(null);
227             fail("Test should throw an exception here");
228         } catch (final Exception e) {
229             assertTrue(e.getMessage()
230                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
231         }
232     }
233
234     /**
235      * Test double marshal.
236      */
237     @Test
238     public void testDoubleMarshal() {
239         final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
240                         "{\"type\": \"double\"}");
241
242         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
243         final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
244                         avroDoubleSchema.getKey());
245
246         assertEquals("0.0", schemaHelper5.marshal2String(0D));
247         assertEquals("1.0", schemaHelper5.marshal2String(1D));
248         assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
249         assertEquals("1.23", schemaHelper5.marshal2String(1.23));
250         assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
251         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
252         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
253         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
254         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
255         try {
256             schemaHelper5.marshal2String("Hello");
257             fail("Test should throw an exception here");
258         } catch (final Exception e) {
259             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
260                             + "java.lang.String cannot be cast to java.lang.Double"));
261         }
262         try {
263             schemaHelper5.marshal2String(null);
264             fail("Test should throw an exception here");
265         } catch (final Exception e) {
266             assertTrue(e.getMessage()
267                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
268         }
269     }
270
271     /**
272      * Test string marshal.
273      */
274     @Test
275     public void testStringMarshal() {
276         final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
277                         "{\"type\": \"string\"}");
278
279         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
280         final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
281                         avroStringSchema.getKey());
282
283         assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
284         assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
285         assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
286         assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
287         assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
288         assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
289         assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
290         assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
291         assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
292         assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
293         try {
294             schemaHelper7.marshal2String(null);
295             fail("Test should throw an exception here");
296         } catch (final Exception e) {
297             assertTrue(e.getMessage()
298                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
299         }
300     }
301
302     /**
303      * Test bytes marshal.
304      */
305     @Test
306     public void testBytesMarshal() {
307         final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
308                         "{\"type\": \"bytes\"}");
309
310         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
311         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
312
313         final byte[] helloBytes =
314             { 104, 101, 108, 108, 111 };
315         final String helloOut = schemaHelper.marshal2String(helloBytes);
316         assertEquals("\"hello\"", helloOut);
317
318         try {
319             schemaHelper.marshal2String(null);
320             fail("Test should throw an exception here");
321         } catch (final Exception e) {
322             assertTrue(e.getMessage()
323                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Byte[]\""));
324         }
325     }
326 }