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
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.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 TestAvroSchemaHelperMarshal.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class AvroSchemaHelperMarshalTest {
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);
85 public void testNullMarshal() {
86 final AxContextSchema avroNullSchema =
87 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", "{\"type\": \"null\"}");
89 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
90 final SchemaHelper schemaHelper0 =
91 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
93 assertEquals("null", schemaHelper0.marshal2String(null));
94 assertEquals("null", schemaHelper0.marshal2String(123));
95 assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is"));
99 * Test boolean marshal.
102 public void testBooleanMarshal() {
103 final AxContextSchema avroBooleanSchema =
104 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", "{\"type\": \"boolean\"}");
106 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
107 final SchemaHelper schemaHelper1 =
108 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
110 assertEquals("true", schemaHelper1.marshal2String(true));
111 assertEquals("false", schemaHelper1.marshal2String(false));
113 schemaHelper1.marshal2String(0);
114 fail("Test should throw an exception here");
115 } catch (final Exception e) {
117 assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
118 + "class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and "
119 + "java.lang.Boolean are in module java.base of loader 'bootstrap')", e.getMessage());
122 schemaHelper1.marshal2String("0");
123 fail("Test should throw an exception here");
124 } catch (final Exception e) {
126 assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: class java.lang.String cannot be cast "
127 + "to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of "
128 + "loader 'bootstrap')", e.getMessage());
136 public void testIntMarshal() {
137 final AxContextSchema avroIntSchema =
138 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", "{\"type\": \"int\"}");
140 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
141 final SchemaHelper schemaHelper2 =
142 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
144 assertEquals("0", schemaHelper2.marshal2String(0));
145 assertEquals("1", schemaHelper2.marshal2String(1));
146 assertEquals("-1", schemaHelper2.marshal2String(-1));
147 assertEquals("1", schemaHelper2.marshal2String(1.23));
148 assertEquals("-1", schemaHelper2.marshal2String(-1.23));
149 assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
150 assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
152 schemaHelper2.marshal2String("Hello");
153 fail("Test should throw an exception here");
154 } catch (final Exception e) {
155 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
156 + "class java.lang.String cannot be cast to class java.lang.Number"));
159 schemaHelper2.marshal2String(null);
160 fail("Test should throw an exception here");
161 } catch (final Exception e) {
162 assertTrue(e.getMessage()
163 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
171 public void testLongMarshal() {
172 final AxContextSchema avroLongSchema =
173 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", "{\"type\": \"long\"}");
175 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
176 final SchemaHelper schemaHelper3 =
177 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
179 assertEquals("0", schemaHelper3.marshal2String(0L));
180 assertEquals("1", schemaHelper3.marshal2String(1L));
181 assertEquals("-1", schemaHelper3.marshal2String(-1L));
182 assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
183 assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
185 schemaHelper3.marshal2String("Hello");
186 fail("Test should throw an exception here");
187 } catch (final Exception e) {
188 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
189 + "class java.lang.String cannot be cast to class java.lang.Long"));
192 schemaHelper3.marshal2String(null);
193 fail("Test should throw an exception here");
194 } catch (final Exception e) {
195 assertTrue(e.getMessage()
196 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
201 * Test float marshal.
204 public void testFloatMarshal() {
205 final AxContextSchema avroFloatSchema =
206 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", "{\"type\": \"float\"}");
208 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
209 final SchemaHelper schemaHelper4 =
210 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
212 assertEquals("0.0", schemaHelper4.marshal2String(0F));
213 assertEquals("1.0", schemaHelper4.marshal2String(1F));
214 assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
215 assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
216 assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
217 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
218 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
219 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
220 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
222 schemaHelper4.marshal2String("Hello");
223 fail("Test should throw an exception here");
224 } catch (final Exception e) {
225 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
226 + "class java.lang.String cannot be cast to class java.lang.Float"));
229 schemaHelper4.marshal2String(null);
230 fail("Test should throw an exception here");
231 } catch (final Exception e) {
232 assertTrue(e.getMessage()
233 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
238 * Test double marshal.
241 public void testDoubleMarshal() {
242 final AxContextSchema avroDoubleSchema =
243 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", "{\"type\": \"double\"}");
245 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
246 final SchemaHelper schemaHelper5 =
247 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
249 assertEquals("0.0", schemaHelper5.marshal2String(0D));
250 assertEquals("1.0", schemaHelper5.marshal2String(1D));
251 assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
252 assertEquals("1.23", schemaHelper5.marshal2String(1.23));
253 assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
254 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
255 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
256 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
257 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
259 schemaHelper5.marshal2String("Hello");
260 fail("Test should throw an exception here");
261 } catch (final Exception e) {
262 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
263 + "class java.lang.String cannot be cast to class java.lang.Double"));
266 schemaHelper5.marshal2String(null);
267 fail("Test should throw an exception here");
268 } catch (final Exception e) {
269 assertTrue(e.getMessage()
270 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
275 * Test string marshal.
278 public void testStringMarshal() {
279 final AxContextSchema avroStringSchema =
280 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"string\"}");
282 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
283 final SchemaHelper schemaHelper7 =
284 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
286 assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
287 assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
288 assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
289 assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
290 assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
291 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
292 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
293 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
294 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
295 assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
297 schemaHelper7.marshal2String(null);
298 fail("Test should throw an exception here");
299 } catch (final Exception e) {
300 assertTrue(e.getMessage()
301 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
306 * Test bytes marshal.
309 public void testBytesMarshal() {
310 final AxContextSchema avroSchema =
311 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"bytes\"}");
313 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
314 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
316 final byte[] helloBytes = {104, 101, 108, 108, 111};
317 final String helloOut = schemaHelper.marshal2String(helloBytes);
318 assertEquals("\"hello\"", helloOut);
321 schemaHelper.marshal2String(null);
322 fail("Test should throw an exception here");
323 } catch (final Exception e) {
324 assertTrue(e.getMessage()
325 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\""));