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.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;
40 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class TestAvroSchemaHelperUnmarshal {
44 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
45 private AxContextSchemas schemas;
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());
55 public void testNullUnmarshal() {
56 final AxContextSchema avroNullSchema =
57 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "Avro", "{\"type\": \"null\"}");
59 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
60 final SchemaHelper schemaHelper0 =
61 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
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",
71 assertEquals(null, schemaHelper0.unmarshal("null"));
74 schemaHelper0.unmarshal("123");
75 fail("test should throw an exception here");
76 } catch (final Exception e) {
78 "AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: Expected null. Got VALUE_NUMBER_INT",
84 public void testBooleanUnmarshal() {
85 final AxContextSchema avroBooleanSchema =
86 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "Avro", "{\"type\": \"boolean\"}");
88 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
89 final SchemaHelper schemaHelper1 =
90 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
93 schemaHelper1.createNewInstance();
94 fail("test should throw an exception here");
95 } catch (final Exception e) {
97 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" using the default constructor \"Boolean()\"",
100 assertEquals(true, schemaHelper1.createNewInstance("true"));
102 assertEquals(true, schemaHelper1.unmarshal("true"));
103 assertEquals(false, schemaHelper1.unmarshal("false"));
105 schemaHelper1.unmarshal(0);
106 fail("Test should throw an exception here");
107 } catch (final Exception e) {
109 "AvroTest:0.0.1: object \"0\" Avro unmarshalling failed: Expected boolean. Got VALUE_NUMBER_INT",
115 public void testIntUnmarshal() {
116 final AxContextSchema avroIntSchema =
117 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "Avro", "{\"type\": \"int\"}");
119 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
120 final SchemaHelper schemaHelper2 =
121 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
124 schemaHelper2.createNewInstance();
125 fail("test should throw an exception here");
126 } catch (final Exception e) {
128 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" using the default constructor \"Integer()\"",
131 assertEquals(123, schemaHelper2.createNewInstance("123"));
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"));
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"));
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"));
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!"));
164 public void testLongUnmarshal() {
165 final AxContextSchema avroLongSchema =
166 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "Avro", "{\"type\": \"long\"}");
168 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
169 final SchemaHelper schemaHelper3 =
170 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
173 schemaHelper3.createNewInstance();
174 fail("test should throw an exception here");
175 } catch (final Exception e) {
177 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" using the default constructor \"Long()\"",
180 assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));
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"));
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"));
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"));
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"));
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!"));
220 public void testFloatUnmarshal() {
221 final AxContextSchema avroFloatSchema =
222 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "Avro", "{\"type\": \"float\"}");
224 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
225 final SchemaHelper schemaHelper4 =
226 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
229 schemaHelper4.createNewInstance();
230 fail("test should throw an exception here");
231 } catch (final Exception e) {
233 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" using the default constructor \"Float()\"",
236 assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));
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"));
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"));
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!"));
264 public void testDoubleUnmarshal() {
265 final AxContextSchema avroDoubleSchema =
266 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "Avro", "{\"type\": \"double\"}");
268 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
269 final SchemaHelper schemaHelper5 =
270 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
273 schemaHelper5.createNewInstance();
274 fail("test should throw an exception here");
275 } catch (final Exception e) {
277 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" using the default constructor \"Double()\"",
280 assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));
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"));
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"));
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!"));
308 public void testStringUnmarshal() {
309 final AxContextSchema avroStringSchema =
310 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"string\"}");
312 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
313 final SchemaHelper schemaHelper7 =
314 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
316 assertEquals("", schemaHelper7.createNewInstance(""));
317 assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));
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")));
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!"));
340 public void testBytesUnmarshal() {
341 final AxContextSchema avroSchema =
342 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"bytes\"}");
344 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
345 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
348 schemaHelper.createNewInstance();
349 fail("test should throw an exception here");
350 } catch (final Exception e) {
352 "AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" using the default constructor \"Byte[]()\"",
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]);
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!"));