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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.schema.avro;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
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;
43 * The Class TestAvroSchemaHelperUnmarshal.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class AvroSchemaHelperUnmarshalTest {
49 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
50 private AxContextSchemas schemas;
56 public void initTest() {
57 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
58 ModelService.registerModel(AxContextSchemas.class, schemas);
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);
77 public void clearContext() {
78 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
82 * Test null unmarshal.
85 public void testNullUnmarshal() {
86 final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
87 "{\"type\": \"null\"}");
89 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
90 final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
91 avroNullSchema.getKey());
94 schemaHelper0.createNewInstance();
95 fail("test should throw an exception here");
96 } catch (final Exception e) {
97 assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null",
101 assertEquals(null, schemaHelper0.unmarshal("null"));
104 schemaHelper0.unmarshal("123");
105 fail("test should throw an exception here");
106 } catch (final Exception e) {
107 assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: "
108 + "Expected null. Got VALUE_NUMBER_INT", e.getMessage());
113 * Test boolean unmarshal.
116 public void testBooleanUnmarshal() {
117 final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
118 "{\"type\": \"boolean\"}");
120 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
121 final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
122 avroBooleanSchema.getKey());
125 schemaHelper1.createNewInstance();
126 fail("test should throw an exception here");
127 } catch (final Exception e) {
128 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" "
129 + "using the default constructor \"Boolean()\"", e.getMessage());
131 assertEquals(true, schemaHelper1.createNewInstance("true"));
133 assertEquals(true, schemaHelper1.unmarshal("true"));
134 assertEquals(false, schemaHelper1.unmarshal("false"));
136 schemaHelper1.unmarshal(0);
137 fail("Test should throw an exception here");
138 } catch (final Exception e) {
139 assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to "
140 + "\"java.lang.Boolean\" or be a Json string representation of it for "
141 + "Avro unmarshalling", e.getMessage());
146 * Test int unmarshal.
149 public void testIntUnmarshal() {
150 final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
151 "{\"type\": \"int\"}");
153 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
154 final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
155 avroIntSchema.getKey());
158 schemaHelper2.createNewInstance();
159 fail("test should throw an exception here");
160 } catch (final Exception e) {
161 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" "
162 + "using the default constructor \"Integer()\"", e.getMessage());
164 assertEquals(123, schemaHelper2.createNewInstance("123"));
166 assertEquals(0, schemaHelper2.unmarshal("0"));
167 assertEquals(1, schemaHelper2.unmarshal("1"));
168 assertEquals(-1, schemaHelper2.unmarshal("-1"));
169 assertEquals(1, schemaHelper2.unmarshal("1.23"));
170 assertEquals(-1, schemaHelper2.unmarshal("-1.23"));
171 assertEquals(2147483647, schemaHelper2.unmarshal("2147483647"));
172 assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648"));
174 schemaHelper2.unmarshal("2147483648");
175 fail("Test should throw an exception here");
176 } catch (final Exception e) {
177 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: "
178 + "Numeric value (2147483648) out of range of int"));
181 schemaHelper2.unmarshal("-2147483649");
182 fail("Test should throw an exception here");
183 } catch (final Exception e) {
184 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: "
185 + "Numeric value (-2147483649) out of range of int"));
188 schemaHelper2.unmarshal(null);
189 fail("Test should throw an exception here");
190 } catch (final Exception e) {
191 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
192 + "String to read from cannot be null!"));
197 * Test long unmarshal.
200 public void testLongUnmarshal() {
201 final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
202 "{\"type\": \"long\"}");
204 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
205 final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
206 avroLongSchema.getKey());
209 schemaHelper3.createNewInstance();
210 fail("test should throw an exception here");
211 } catch (final Exception e) {
212 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" "
213 + "using the default constructor \"Long()\"", e.getMessage());
215 assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
217 assertEquals(0L, schemaHelper3.unmarshal("0"));
218 assertEquals(1L, schemaHelper3.unmarshal("1"));
219 assertEquals(-1L, schemaHelper3.unmarshal("-1"));
220 assertEquals(1L, schemaHelper3.unmarshal("1.23"));
221 assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
222 assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
223 assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
225 schemaHelper3.unmarshal("9223372036854775808");
226 fail("Test should throw an exception here");
227 } catch (final Exception e) {
228 assertTrue(e.getMessage()
229 .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: "
230 + "Numeric value (9223372036854775808) out of range of long"));
233 schemaHelper3.unmarshal("-9223372036854775809");
234 fail("Test should throw an exception here");
235 } catch (final Exception e) {
236 assertTrue(e.getMessage()
237 .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: "
238 + "Numeric value (-9223372036854775809) out of range of long"));
241 schemaHelper3.unmarshal("\"Hello\"");
242 fail("Test should throw an exception here");
243 } catch (final Exception e) {
244 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
245 + "Expected long. Got VALUE_STRING"));
248 schemaHelper3.unmarshal(null);
249 fail("Test should throw an exception here");
250 } catch (final Exception e) {
251 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
252 + "String to read from cannot be null!"));
257 * Test float unmarshal.
260 public void testFloatUnmarshal() {
261 final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
262 "{\"type\": \"float\"}");
264 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
265 final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
266 avroFloatSchema.getKey());
269 schemaHelper4.createNewInstance();
270 fail("test should throw an exception here");
271 } catch (final Exception e) {
272 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" "
273 + "using the default constructor \"Float()\"", e.getMessage());
275 assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
277 assertEquals(0.0F, schemaHelper4.unmarshal("0"));
278 assertEquals(1.0F, schemaHelper4.unmarshal("1"));
279 assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
280 assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
281 assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
282 assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
283 assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
284 assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
285 assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
287 schemaHelper4.unmarshal("\"Hello\"");
288 fail("Test should throw an exception here");
289 } catch (final Exception e) {
290 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
291 + "Expected float. Got VALUE_STRING"));
294 schemaHelper4.unmarshal(null);
295 fail("Test should throw an exception here");
296 } catch (final Exception e) {
297 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
298 + "String to read from cannot be null!"));
303 * Test double unmarshal.
306 public void testDoubleUnmarshal() {
307 final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
308 "{\"type\": \"double\"}");
310 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
311 final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
312 avroDoubleSchema.getKey());
315 schemaHelper5.createNewInstance();
316 fail("test should throw an exception here");
317 } catch (final Exception e) {
318 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" "
319 + "using the default constructor \"Double()\"", e.getMessage());
321 assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
323 assertEquals(0.0, schemaHelper5.unmarshal("0"));
324 assertEquals(1.0, schemaHelper5.unmarshal("1"));
325 assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
326 assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
327 assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
328 assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
329 assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
330 assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
331 assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
333 schemaHelper5.unmarshal("\"Hello\"");
334 fail("Test should throw an exception here");
335 } catch (final Exception e) {
336 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: "
337 + "Expected double. Got VALUE_STRING"));
340 schemaHelper5.unmarshal(null);
341 fail("Test should throw an exception here");
342 } catch (final Exception e) {
343 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
344 + "String to read from cannot be null!"));
349 * Test string unmarshal.
352 public void testStringUnmarshal() {
353 final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
354 "{\"type\": \"string\"}");
356 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
357 final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
358 avroStringSchema.getKey());
360 assertEquals("", schemaHelper7.createNewInstance(""));
361 assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
363 assertEquals("0", schemaHelper7.unmarshal("0"));
364 assertEquals("1", schemaHelper7.unmarshal("1"));
365 assertEquals("-1", schemaHelper7.unmarshal("-1"));
366 assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
367 assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
368 assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
369 assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
370 assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
371 assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
372 assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
373 assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
375 schemaHelper7.unmarshal(null);
376 fail("Test should throw an exception here");
377 } catch (final Exception e) {
378 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
379 + "String to read from cannot be null!"));
384 * Test bytes unmarshal.
387 public void testBytesUnmarshal() {
388 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
389 "{\"type\": \"bytes\"}");
391 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
392 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
395 schemaHelper.createNewInstance();
396 fail("test should throw an exception here");
397 } catch (final Exception e) {
398 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" "
399 + "using the default constructor \"Byte[]()\"", e.getMessage());
401 final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
402 assertEquals(5, newBytes.length);
403 assertEquals(104, newBytes[0]);
404 assertEquals(101, newBytes[1]);
405 assertEquals(108, newBytes[2]);
406 assertEquals(108, newBytes[3]);
407 assertEquals(111, newBytes[4]);
410 schemaHelper.unmarshal(null);
411 fail("Test should throw an exception here");
412 } catch (final Exception e) {
413 assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: "
414 + "String to read from cannot be null!"));