2b6050acc0cc93bac73ffc52fe65a29066dd0fc9
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
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         assertThatThrownBy(schemaHelper0::createNewInstance)
94             .hasMessage("AvroTest:0.0.1: could not create an instance, schema class for the schema is null");
95         assertEquals(null, schemaHelper0.unmarshal("null"));
96
97         assertThatThrownBy(() -> schemaHelper0.unmarshal("123"))
98             .hasMessage("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: "
99                 + "Expected null. Got VALUE_NUMBER_INT");
100     }
101
102     /**
103      * Test boolean unmarshal.
104      */
105     @Test
106     public void testBooleanUnmarshal() {
107         final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
108                         "{\"type\": \"boolean\"}");
109
110         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
111         final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
112                         avroBooleanSchema.getKey());
113
114         assertThatThrownBy(schemaHelper1::createNewInstance)
115             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" "
116                 + "using the default constructor \"Boolean()\"");
117         assertEquals(true, schemaHelper1.createNewInstance("true"));
118
119         assertEquals(true, schemaHelper1.unmarshal("true"));
120         assertEquals(false, schemaHelper1.unmarshal("false"));
121         assertThatThrownBy(() -> schemaHelper1.unmarshal(0))
122             .hasMessage("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");
125     }
126
127     /**
128      * Test int unmarshal.
129      */
130     @Test
131     public void testIntUnmarshal() {
132         final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
133                         "{\"type\": \"int\"}");
134
135         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
136         final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
137                         avroIntSchema.getKey());
138
139         assertThatThrownBy(schemaHelper2::createNewInstance)
140             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" "
141                 + "using the default constructor \"Integer()\"");
142         assertEquals(123, schemaHelper2.createNewInstance("123"));
143
144         assertEquals(0, schemaHelper2.unmarshal("0"));
145         assertEquals(1, schemaHelper2.unmarshal("1"));
146         assertEquals(-1, schemaHelper2.unmarshal("-1"));
147         assertEquals(1, schemaHelper2.unmarshal("1.23"));
148         assertEquals(-1, schemaHelper2.unmarshal("-1.23"));
149         assertEquals(2147483647, schemaHelper2.unmarshal("2147483647"));
150         assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648"));
151         assertThatThrownBy(() -> schemaHelper2.unmarshal("2147483648"))
152             .hasMessageStartingWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: "
153                 + "Numeric value (2147483648) out of range of int");
154         assertThatThrownBy(() -> schemaHelper2.unmarshal("-2147483649"))
155             .hasMessageStartingWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: "
156                 + "Numeric value (-2147483649) out of range of int");
157         assertThatThrownBy(() -> schemaHelper2.unmarshal(null))
158             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
159                 + "String to read from cannot be null!");
160     }
161
162     /**
163      * Test long unmarshal.
164      */
165     @Test
166     public void testLongUnmarshal() {
167         final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
168                         "{\"type\": \"long\"}");
169
170         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
171         final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
172                         avroLongSchema.getKey());
173
174         assertThatThrownBy(schemaHelper3::createNewInstance)
175             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" "
176                 + "using the default constructor \"Long()\"");
177         assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
178
179         assertEquals(0L, schemaHelper3.unmarshal("0"));
180         assertEquals(1L, schemaHelper3.unmarshal("1"));
181         assertEquals(-1L, schemaHelper3.unmarshal("-1"));
182         assertEquals(1L, schemaHelper3.unmarshal("1.23"));
183         assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
184         assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
185         assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
186         assertThatThrownBy(() -> schemaHelper3.unmarshal("9223372036854775808"))
187             .hasMessageStartingWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: "
188                 + "Numeric value (9223372036854775808) out of range of long");
189         assertThatThrownBy(() -> schemaHelper3.unmarshal("-9223372036854775809"))
190             .hasMessageStartingWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: "
191                 + "Numeric value (-9223372036854775809) out of range of long");
192         assertThatThrownBy(() -> schemaHelper3.unmarshal("\"Hello\""))
193             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
194                 + "Expected long. Got VALUE_STRING");
195         assertThatThrownBy(() -> schemaHelper3.unmarshal(null))
196             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
197                 + "String to read from cannot be null!");
198     }
199
200     /**
201      * Test float unmarshal.
202      */
203     @Test
204     public void testFloatUnmarshal() {
205         final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
206                         "{\"type\": \"float\"}");
207
208         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
209         final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
210                         avroFloatSchema.getKey());
211
212         assertThatThrownBy(schemaHelper4::createNewInstance)
213             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" "
214                 + "using the default constructor \"Float()\"");
215         assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
216
217         assertEquals(0.0F, schemaHelper4.unmarshal("0"));
218         assertEquals(1.0F, schemaHelper4.unmarshal("1"));
219         assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
220         assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
221         assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
222         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
223         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
224         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
225         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
226         assertThatThrownBy(() -> schemaHelper4.unmarshal("\"Hello\""))
227             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
228                 + "Expected float. Got VALUE_STRING");
229         assertThatThrownBy(() -> schemaHelper4.unmarshal(null))
230             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
231                 + "String to read from cannot be null!");
232     }
233
234     /**
235      * Test double unmarshal.
236      */
237     @Test
238     public void testDoubleUnmarshal() {
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         assertThatThrownBy(schemaHelper5::createNewInstance)
247             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" "
248                 + "using the default constructor \"Double()\"");
249         assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
250
251         assertEquals(0.0, schemaHelper5.unmarshal("0"));
252         assertEquals(1.0, schemaHelper5.unmarshal("1"));
253         assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
254         assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
255         assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
256         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
257         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
258         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
259         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
260         assertThatThrownBy(() -> schemaHelper5.unmarshal("\"Hello\""))
261             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
262                 + "Expected double. Got VALUE_STRING");
263         assertThatThrownBy(() -> schemaHelper5.unmarshal(null))
264             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
265                 + "String to read from cannot be null!");
266     }
267
268     /**
269      * Test string unmarshal.
270      */
271     @Test
272     public void testStringUnmarshal() {
273         final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
274                         "{\"type\": \"string\"}");
275
276         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
277         final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
278                         avroStringSchema.getKey());
279
280         assertEquals("", schemaHelper7.createNewInstance(""));
281         assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
282
283         assertEquals("0", schemaHelper7.unmarshal("0"));
284         assertEquals("1", schemaHelper7.unmarshal("1"));
285         assertEquals("-1", schemaHelper7.unmarshal("-1"));
286         assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
287         assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
288         assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
289         assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
290         assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
291         assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
292         assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
293         assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
294         assertThatThrownBy(() -> schemaHelper7.unmarshal(null))
295             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
296                 + "String to read from cannot be null!");
297     }
298
299     /**
300      * Test bytes unmarshal.
301      */
302     @Test
303     public void testBytesUnmarshal() {
304         final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
305                         "{\"type\": \"bytes\"}");
306
307         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
308         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
309
310         assertThatThrownBy(schemaHelper::createNewInstance)
311             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"[Ljava.lang.Byte;\" "
312                 + "using the default constructor \"Byte[]()\"");
313         final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
314         assertEquals(5, newBytes.length);
315         assertEquals(104, newBytes[0]);
316         assertEquals(101, newBytes[1]);
317         assertEquals(108, newBytes[2]);
318         assertEquals(108, newBytes[3]);
319         assertEquals(111, newBytes[4]);
320
321         assertThatThrownBy(() -> schemaHelper.unmarshal(null))
322             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
323                 + "String to read from cannot be null!");
324     }
325 }