23ac9a855b2cbf7024ffff1f86e97ef1d3af455e
[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  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.context.schema.avro;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27
28 import org.apache.avro.util.Utf8;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.SchemaHelper;
33 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.SchemaParameters;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.service.ModelService;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 /**
44  * The Class TestAvroSchemaHelperUnmarshal.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  * @version
48  */
49 public class AvroSchemaHelperUnmarshalTest {
50     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
51     private AxContextSchemas schemas;
52
53     /**
54      * Inits the test.
55      */
56     @Before
57     public void initTest() {
58         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
59         ModelService.registerModel(AxContextSchemas.class, schemas);
60     }
61
62     /**
63      * Inits the context.
64      */
65     @Before
66     public void initContext() {
67         SchemaParameters schemaParameters = new SchemaParameters();
68         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
69         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
70         ParameterService.register(schemaParameters);
71
72     }
73
74     /**
75      * Clear context.
76      */
77     @After
78     public void clearContext() {
79         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
80     }
81
82     /**
83      * Test null unmarshal.
84      */
85     @Test
86     public void testNullUnmarshal() {
87         final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
88                         "{\"type\": \"null\"}");
89
90         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
91         final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
92                         avroNullSchema.getKey());
93
94         assertThatThrownBy(schemaHelper0::createNewInstance)
95             .hasMessage("AvroTest:0.0.1: could not create an instance, schema class for the schema is null");
96         assertEquals(null, schemaHelper0.unmarshal("null"));
97
98         assertThatThrownBy(() -> schemaHelper0.unmarshal("123"))
99             .hasMessage("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed.");
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         assertThatThrownBy(() -> schemaHelper2.unmarshal("-2147483649"))
154             .hasMessageStartingWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed.");
155         assertThatThrownBy(() -> schemaHelper2.unmarshal(null))
156             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
157     }
158
159     /**
160      * Test long unmarshal.
161      */
162     @Test
163     public void testLongUnmarshal() {
164         final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
165                         "{\"type\": \"long\"}");
166
167         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
168         final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
169                         avroLongSchema.getKey());
170
171         assertThatThrownBy(schemaHelper3::createNewInstance)
172             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" "
173                 + "using the default constructor \"Long()\"");
174         assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
175
176         assertEquals(0L, schemaHelper3.unmarshal("0"));
177         assertEquals(1L, schemaHelper3.unmarshal("1"));
178         assertEquals(-1L, schemaHelper3.unmarshal("-1"));
179         assertEquals(1L, schemaHelper3.unmarshal("1.23"));
180         assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
181         assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
182         assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
183         assertThatThrownBy(() -> schemaHelper3.unmarshal("9223372036854775808"))
184             .hasMessageStartingWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed.");
185         assertThatThrownBy(() -> schemaHelper3.unmarshal("-9223372036854775809"))
186             .hasMessageStartingWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed.");
187         assertThatThrownBy(() -> schemaHelper3.unmarshal("\"Hello\""))
188             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed.");
189         assertThatThrownBy(() -> schemaHelper3.unmarshal(null))
190             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
191     }
192
193     /**
194      * Test float unmarshal.
195      */
196     @Test
197     public void testFloatUnmarshal() {
198         final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
199                         "{\"type\": \"float\"}");
200
201         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
202         final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
203                         avroFloatSchema.getKey());
204
205         assertThatThrownBy(schemaHelper4::createNewInstance)
206             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" "
207                 + "using the default constructor \"Float()\"");
208         assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
209
210         assertEquals(0.0F, schemaHelper4.unmarshal("0"));
211         assertEquals(1.0F, schemaHelper4.unmarshal("1"));
212         assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
213         assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
214         assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
215         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
216         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
217         assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
218         assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
219         assertThatThrownBy(() -> schemaHelper4.unmarshal("\"Hello\""))
220             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed.");
221         assertThatThrownBy(() -> schemaHelper4.unmarshal(null))
222             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
223     }
224
225     /**
226      * Test double unmarshal.
227      */
228     @Test
229     public void testDoubleUnmarshal() {
230         final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
231                         "{\"type\": \"double\"}");
232
233         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
234         final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
235                         avroDoubleSchema.getKey());
236
237         assertThatThrownBy(schemaHelper5::createNewInstance)
238             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" "
239                 + "using the default constructor \"Double()\"");
240         assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
241
242         assertEquals(0.0, schemaHelper5.unmarshal("0"));
243         assertEquals(1.0, schemaHelper5.unmarshal("1"));
244         assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
245         assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
246         assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
247         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
248         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
249         assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
250         assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
251         assertThatThrownBy(() -> schemaHelper5.unmarshal("\"Hello\""))
252             .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed.");
253         assertThatThrownBy(() -> schemaHelper5.unmarshal(null))
254             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
255     }
256
257     /**
258      * Test string unmarshal.
259      */
260     @Test
261     public void testStringUnmarshal() {
262         final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
263                         "{\"type\": \"string\"}");
264
265         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
266         final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
267                         avroStringSchema.getKey());
268
269         assertEquals("", schemaHelper7.createNewInstance(""));
270         assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
271
272         assertEquals("0", schemaHelper7.unmarshal("0"));
273         assertEquals("1", schemaHelper7.unmarshal("1"));
274         assertEquals("-1", schemaHelper7.unmarshal("-1"));
275         assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
276         assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
277         assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
278         assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
279         assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
280         assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
281         assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
282         assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
283         assertThatThrownBy(() -> schemaHelper7.unmarshal(null))
284             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
285     }
286
287     /**
288      * Test bytes unmarshal.
289      */
290     @Test
291     public void testBytesUnmarshal() {
292         final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
293                         "{\"type\": \"bytes\"}");
294
295         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
296         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
297
298         assertThatThrownBy(schemaHelper::createNewInstance)
299             .hasMessage("AvroTest:0.0.1: could not create an instance of class \"[Ljava.lang.Byte;\" "
300                 + "using the default constructor \"Byte[]()\"");
301         final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
302         assertEquals(5, newBytes.length);
303         assertEquals(104, newBytes[0]);
304         assertEquals(101, newBytes[1]);
305         assertEquals(108, newBytes[2]);
306         assertEquals(108, newBytes[3]);
307         assertEquals(111, newBytes[4]);
308
309         assertThatThrownBy(() -> schemaHelper.unmarshal(null))
310             .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
311     }
312 }