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