a8711e06d52499bb8be87d59ad545f9b701af489
[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.apache.avro.util.Utf8;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.common.parameters.ParameterService;
41
42 /**
43  * The Class TestAvroSchemaHelperUnmarshal.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  * @version
47  */
48 public class AvroSchemaHelperUnmarshalTest {
49     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
50     private AxContextSchemas schemas;
51
52     /**
53      * Inits the test.
54      */
55     @Before
56     public void initTest() {
57         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
58         ModelService.registerModel(AxContextSchemas.class, schemas);
59     }
60
61     /**
62      * Inits the context.
63      */
64     @Before
65     public void initContext() {
66         SchemaParameters schemaParameters = new SchemaParameters();
67         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
68         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
69         ParameterService.register(schemaParameters);
70
71     }
72
73     /**
74      * Clear context.
75      */
76     @After
77     public void clearContext() {
78         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
79     }
80
81     /**
82      * Test null unmarshal.
83      */
84     @Test
85     public void testNullUnmarshal() {
86         final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
87                         "{\"type\": \"null\"}");
88
89         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
90         final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
91                         avroNullSchema.getKey());
92
93         try {
94             schemaHelper0.createNewInstance();
95             fail("test should throw an exception here");
96         } catch (final Exception e) {
97             assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null",
98                             e.getMessage());
99         }
100
101         assertEquals(null, schemaHelper0.unmarshal("null"));
102
103         try {
104             schemaHelper0.unmarshal("123");
105             fail("test should throw an exception here");
106         } catch (final Exception e) {
107             assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: "
108                             + "Expected null. Got VALUE_NUMBER_INT", e.getMessage());
109         }
110     }
111
112     /**
113      * Test boolean unmarshal.
114      */
115     @Test
116     public void testBooleanUnmarshal() {
117         final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
118                         "{\"type\": \"boolean\"}");
119
120         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
121         final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
122                         avroBooleanSchema.getKey());
123
124         try {
125             schemaHelper1.createNewInstance();
126             fail("test should throw an exception here");
127         } catch (final Exception e) {
128             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" "
129                             + "using the default constructor \"Boolean()\"", e.getMessage());
130         }
131         assertEquals(true, schemaHelper1.createNewInstance("true"));
132
133         assertEquals(true, schemaHelper1.unmarshal("true"));
134         assertEquals(false, schemaHelper1.unmarshal("false"));
135         try {
136             schemaHelper1.unmarshal(0);
137             fail("Test should throw an exception here");
138         } catch (final Exception e) {
139             assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to "
140                             + "\"java.lang.Boolean\" or be a Json string representation of it for "
141                             + "Avro unmarshalling", e.getMessage());
142         }
143     }
144
145     /**
146      * Test int unmarshal.
147      */
148     @Test
149     public void testIntUnmarshal() {
150         final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
151                         "{\"type\": \"int\"}");
152
153         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
154         final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
155                         avroIntSchema.getKey());
156
157         try {
158             schemaHelper2.createNewInstance();
159             fail("test should throw an exception here");
160         } catch (final Exception e) {
161             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" "
162                             + "using the default constructor \"Integer()\"", e.getMessage());
163         }
164         assertEquals(123, schemaHelper2.createNewInstance("123"));
165
166         assertEquals(0, schemaHelper2.unmarshal("0"));
167         assertEquals(1, schemaHelper2.unmarshal("1"));
168         assertEquals(-1, schemaHelper2.unmarshal("-1"));
169         assertEquals(1, schemaHelper2.unmarshal("1.23"));
170         assertEquals(-1, schemaHelper2.unmarshal("-1.23"));
171         assertEquals(2147483647, schemaHelper2.unmarshal("2147483647"));
172         assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648"));
173         try {
174             schemaHelper2.unmarshal("2147483648");
175             fail("Test should throw an exception here");
176         } catch (final Exception e) {
177             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: "
178                             + "Numeric value (2147483648) out of range of int"));
179         }
180         try {
181             schemaHelper2.unmarshal("-2147483649");
182             fail("Test should throw an exception here");
183         } catch (final Exception e) {
184             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: "
185                             + "Numeric value (-2147483649) out of range of int"));
186         }
187         try {
188             schemaHelper2.unmarshal(null);
189             fail("Test should throw an exception here");
190         } catch (final Exception e) {
191             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
192                             + "String to read from cannot be null!"));
193         }
194     }
195
196     /**
197      * Test long unmarshal.
198      */
199     @Test
200     public void testLongUnmarshal() {
201         final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
202                         "{\"type\": \"long\"}");
203
204         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
205         final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
206                         avroLongSchema.getKey());
207
208         try {
209             schemaHelper3.createNewInstance();
210             fail("test should throw an exception here");
211         } catch (final Exception e) {
212             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" "
213                             + "using the default constructor \"Long()\"", e.getMessage());
214         }
215         assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
216
217         assertEquals(0L, schemaHelper3.unmarshal("0"));
218         assertEquals(1L, schemaHelper3.unmarshal("1"));
219         assertEquals(-1L, schemaHelper3.unmarshal("-1"));
220         assertEquals(1L, schemaHelper3.unmarshal("1.23"));
221         assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
222         assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
223         assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
224         try {
225             schemaHelper3.unmarshal("9223372036854775808");
226             fail("Test should throw an exception here");
227         } catch (final Exception e) {
228             assertTrue(e.getMessage()
229                             .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: "
230                                             + "Numeric value (9223372036854775808) out of range of long"));
231         }
232         try {
233             schemaHelper3.unmarshal("-9223372036854775809");
234             fail("Test should throw an exception here");
235         } catch (final Exception e) {
236             assertTrue(e.getMessage()
237                             .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: "
238                                             + "Numeric value (-9223372036854775809) out of range of long"));
239         }
240         try {
241             schemaHelper3.unmarshal("\"Hello\"");
242             fail("Test should throw an exception here");
243         } catch (final Exception e) {
244             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
245                             + "Expected long. Got VALUE_STRING"));
246         }
247         try {
248             schemaHelper3.unmarshal(null);
249             fail("Test should throw an exception here");
250         } catch (final Exception e) {
251             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
252                             + "String to read from cannot be null!"));
253         }
254     }
255
256     /**
257      * Test float unmarshal.
258      */
259     @Test
260     public void testFloatUnmarshal() {
261         final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
262                         "{\"type\": \"float\"}");
263
264         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
265         final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
266                         avroFloatSchema.getKey());
267
268         try {
269             schemaHelper4.createNewInstance();
270             fail("test should throw an exception here");
271         } catch (final Exception e) {
272             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" "
273                             + "using the default constructor \"Float()\"", e.getMessage());
274         }
275         assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
276
277         assertEquals(0.0F, schemaHelper4.unmarshal("0"));
278         assertEquals(1.0F, schemaHelper4.unmarshal("1"));
279         assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
280         assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
281         assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
282         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
283         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
284         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
285         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
286         try {
287             schemaHelper4.unmarshal("\"Hello\"");
288             fail("Test should throw an exception here");
289         } catch (final Exception e) {
290             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
291                             + "Expected float. Got VALUE_STRING"));
292         }
293         try {
294             schemaHelper4.unmarshal(null);
295             fail("Test should throw an exception here");
296         } catch (final Exception e) {
297             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
298                             + "String to read from cannot be null!"));
299         }
300     }
301
302     /**
303      * Test double unmarshal.
304      */
305     @Test
306     public void testDoubleUnmarshal() {
307         final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
308                         "{\"type\": \"double\"}");
309
310         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
311         final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
312                         avroDoubleSchema.getKey());
313
314         try {
315             schemaHelper5.createNewInstance();
316             fail("test should throw an exception here");
317         } catch (final Exception e) {
318             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" "
319                             + "using the default constructor \"Double()\"", e.getMessage());
320         }
321         assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
322
323         assertEquals(0.0, schemaHelper5.unmarshal("0"));
324         assertEquals(1.0, schemaHelper5.unmarshal("1"));
325         assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
326         assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
327         assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
328         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
329         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
330         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
331         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
332         try {
333             schemaHelper5.unmarshal("\"Hello\"");
334             fail("Test should throw an exception here");
335         } catch (final Exception e) {
336             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
337                             + "Expected double. Got VALUE_STRING"));
338         }
339         try {
340             schemaHelper5.unmarshal(null);
341             fail("Test should throw an exception here");
342         } catch (final Exception e) {
343             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
344                             + "String to read from cannot be null!"));
345         }
346     }
347
348     /**
349      * Test string unmarshal.
350      */
351     @Test
352     public void testStringUnmarshal() {
353         final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
354                         "{\"type\": \"string\"}");
355
356         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
357         final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
358                         avroStringSchema.getKey());
359
360         assertEquals("", schemaHelper7.createNewInstance(""));
361         assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
362
363         assertEquals("0", schemaHelper7.unmarshal("0"));
364         assertEquals("1", schemaHelper7.unmarshal("1"));
365         assertEquals("-1", schemaHelper7.unmarshal("-1"));
366         assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
367         assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
368         assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
369         assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
370         assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
371         assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
372         assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
373         assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
374         try {
375             schemaHelper7.unmarshal(null);
376             fail("Test should throw an exception here");
377         } catch (final Exception e) {
378             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
379                             + "String to read from cannot be null!"));
380         }
381     }
382
383     /**
384      * Test bytes unmarshal.
385      */
386     @Test
387     public void testBytesUnmarshal() {
388         final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
389                         "{\"type\": \"bytes\"}");
390
391         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
392         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
393
394         try {
395             schemaHelper.createNewInstance();
396             fail("test should throw an exception here");
397         } catch (final Exception e) {
398             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" "
399                             + "using the default constructor \"Byte[]()\"", e.getMessage());
400         }
401         final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
402         assertEquals(5, newBytes.length);
403         assertEquals(104, newBytes[0]);
404         assertEquals(101, newBytes[1]);
405         assertEquals(108, newBytes[2]);
406         assertEquals(108, newBytes[3]);
407         assertEquals(111, newBytes[4]);
408
409         try {
410             schemaHelper.unmarshal(null);
411             fail("Test should throw an exception here");
412         } catch (final Exception e) {
413             assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
414                             + "String to read from cannot be null!"));
415         }
416     }
417 }