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