2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.schema.avro;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
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;
44 * The Class TestAvroSchemaHelperUnmarshal.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
49 public class AvroSchemaHelperUnmarshalTest {
50 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
51 private AxContextSchemas schemas;
57 public void initTest() {
58 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
59 ModelService.registerModel(AxContextSchemas.class, schemas);
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);
78 public void clearContext() {
79 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
83 * Test null unmarshal.
86 public void testNullUnmarshal() {
87 final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
88 "{\"type\": \"null\"}");
90 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
91 final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
92 avroNullSchema.getKey());
95 schemaHelper0.createNewInstance();
96 fail("test should throw an exception here");
97 } catch (final Exception e) {
98 assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null",
102 assertEquals(null, schemaHelper0.unmarshal("null"));
105 schemaHelper0.unmarshal("123");
106 fail("test should throw an exception here");
107 } catch (final Exception e) {
108 assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: "
109 + "Expected null. Got VALUE_NUMBER_INT", e.getMessage());
114 * Test boolean unmarshal.
117 public void testBooleanUnmarshal() {
118 final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
119 "{\"type\": \"boolean\"}");
121 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
122 final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
123 avroBooleanSchema.getKey());
126 schemaHelper1.createNewInstance();
127 fail("test should throw an exception here");
128 } catch (final Exception e) {
129 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" "
130 + "using the default constructor \"Boolean()\"", e.getMessage());
132 assertEquals(true, schemaHelper1.createNewInstance("true"));
134 assertEquals(true, schemaHelper1.unmarshal("true"));
135 assertEquals(false, schemaHelper1.unmarshal("false"));
137 schemaHelper1.unmarshal(0);
138 fail("Test should throw an exception here");
139 } catch (final Exception e) {
140 assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to "
141 + "\"java.lang.Boolean\" or be a Json string representation of it for "
142 + "Avro unmarshalling", e.getMessage());
147 * Test int unmarshal.
150 public void testIntUnmarshal() {
151 final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
152 "{\"type\": \"int\"}");
154 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
155 final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
156 avroIntSchema.getKey());
159 schemaHelper2.createNewInstance();
160 fail("test should throw an exception here");
161 } catch (final Exception e) {
162 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" "
163 + "using the default constructor \"Integer()\"", e.getMessage());
165 assertEquals(123, schemaHelper2.createNewInstance("123"));
167 assertEquals(0, schemaHelper2.unmarshal("0"));
168 assertEquals(1, schemaHelper2.unmarshal("1"));
169 assertEquals(-1, schemaHelper2.unmarshal("-1"));
170 assertEquals(1, schemaHelper2.unmarshal("1.23"));
171 assertEquals(-1, schemaHelper2.unmarshal("-1.23"));
172 assertEquals(2147483647, schemaHelper2.unmarshal("2147483647"));
173 assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648"));
175 schemaHelper2.unmarshal("2147483648");
176 fail("Test should throw an exception here");
177 } catch (final Exception e) {
178 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: "
179 + "Numeric value (2147483648) out of range of int"));
182 schemaHelper2.unmarshal("-2147483649");
183 fail("Test should throw an exception here");
184 } catch (final Exception e) {
185 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: "
186 + "Numeric value (-2147483649) out of range of int"));
189 schemaHelper2.unmarshal(null);
190 fail("Test should throw an exception here");
191 } catch (final Exception e) {
192 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
193 + "String to read from cannot be null!"));
198 * Test long unmarshal.
201 public void testLongUnmarshal() {
202 final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
203 "{\"type\": \"long\"}");
205 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
206 final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
207 avroLongSchema.getKey());
210 schemaHelper3.createNewInstance();
211 fail("test should throw an exception here");
212 } catch (final Exception e) {
213 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" "
214 + "using the default constructor \"Long()\"", e.getMessage());
216 assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
218 assertEquals(0L, schemaHelper3.unmarshal("0"));
219 assertEquals(1L, schemaHelper3.unmarshal("1"));
220 assertEquals(-1L, schemaHelper3.unmarshal("-1"));
221 assertEquals(1L, schemaHelper3.unmarshal("1.23"));
222 assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
223 assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
224 assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
226 schemaHelper3.unmarshal("9223372036854775808");
227 fail("Test should throw an exception here");
228 } catch (final Exception e) {
229 assertTrue(e.getMessage()
230 .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: "
231 + "Numeric value (9223372036854775808) out of range of long"));
234 schemaHelper3.unmarshal("-9223372036854775809");
235 fail("Test should throw an exception here");
236 } catch (final Exception e) {
237 assertTrue(e.getMessage()
238 .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: "
239 + "Numeric value (-9223372036854775809) out of range of long"));
242 schemaHelper3.unmarshal("\"Hello\"");
243 fail("Test should throw an exception here");
244 } catch (final Exception e) {
245 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
246 + "Expected long. Got VALUE_STRING"));
249 schemaHelper3.unmarshal(null);
250 fail("Test should throw an exception here");
251 } catch (final Exception e) {
252 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
253 + "String to read from cannot be null!"));
258 * Test float unmarshal.
261 public void testFloatUnmarshal() {
262 final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
263 "{\"type\": \"float\"}");
265 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
266 final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
267 avroFloatSchema.getKey());
270 schemaHelper4.createNewInstance();
271 fail("test should throw an exception here");
272 } catch (final Exception e) {
273 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" "
274 + "using the default constructor \"Float()\"", e.getMessage());
276 assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
278 assertEquals(0.0F, schemaHelper4.unmarshal("0"));
279 assertEquals(1.0F, schemaHelper4.unmarshal("1"));
280 assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
281 assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
282 assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
283 assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
284 assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
285 assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
286 assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
288 schemaHelper4.unmarshal("\"Hello\"");
289 fail("Test should throw an exception here");
290 } catch (final Exception e) {
291 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
292 + "Expected float. Got VALUE_STRING"));
295 schemaHelper4.unmarshal(null);
296 fail("Test should throw an exception here");
297 } catch (final Exception e) {
298 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
299 + "String to read from cannot be null!"));
304 * Test double unmarshal.
307 public void testDoubleUnmarshal() {
308 final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
309 "{\"type\": \"double\"}");
311 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
312 final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
313 avroDoubleSchema.getKey());
316 schemaHelper5.createNewInstance();
317 fail("test should throw an exception here");
318 } catch (final Exception e) {
319 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" "
320 + "using the default constructor \"Double()\"", e.getMessage());
322 assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
324 assertEquals(0.0, schemaHelper5.unmarshal("0"));
325 assertEquals(1.0, schemaHelper5.unmarshal("1"));
326 assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
327 assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
328 assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
329 assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
330 assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
331 assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
332 assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
334 schemaHelper5.unmarshal("\"Hello\"");
335 fail("Test should throw an exception here");
336 } catch (final Exception e) {
337 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
338 + "Expected double. Got VALUE_STRING"));
341 schemaHelper5.unmarshal(null);
342 fail("Test should throw an exception here");
343 } catch (final Exception e) {
344 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
345 + "String to read from cannot be null!"));
350 * Test string unmarshal.
353 public void testStringUnmarshal() {
354 final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
355 "{\"type\": \"string\"}");
357 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
358 final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
359 avroStringSchema.getKey());
361 assertEquals("", schemaHelper7.createNewInstance(""));
362 assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
364 assertEquals("0", schemaHelper7.unmarshal("0"));
365 assertEquals("1", schemaHelper7.unmarshal("1"));
366 assertEquals("-1", schemaHelper7.unmarshal("-1"));
367 assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
368 assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
369 assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
370 assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
371 assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
372 assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
373 assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
374 assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
376 schemaHelper7.unmarshal(null);
377 fail("Test should throw an exception here");
378 } catch (final Exception e) {
379 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
380 + "String to read from cannot be null!"));
385 * Test bytes unmarshal.
388 public void testBytesUnmarshal() {
389 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
390 "{\"type\": \"bytes\"}");
392 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
393 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
396 schemaHelper.createNewInstance();
397 fail("test should throw an exception here");
398 } catch (final Exception e) {
399 assertEquals("AvroTest:0.0.1: could not create an instance of class \"[Ljava.lang.Byte;\" "
400 + "using the default constructor \"Byte[]()\"", e.getMessage());
402 final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
403 assertEquals(5, newBytes.length);
404 assertEquals(104, newBytes[0]);
405 assertEquals(101, newBytes[1]);
406 assertEquals(108, newBytes[2]);
407 assertEquals(108, newBytes[3]);
408 assertEquals(111, newBytes[4]);
411 schemaHelper.unmarshal(null);
412 fail("Test should throw an exception here");
413 } catch (final Exception e) {
414 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
415 + "String to read from cannot be null!"));