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.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 = 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());
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 = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
104 "{\"type\": \"boolean\"}");
106 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
107 final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
108 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 + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage());
121 schemaHelper1.marshal2String("0");
122 fail("Test should throw an exception here");
123 } catch (final Exception e) {
125 assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
126 + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage());
134 public void testIntMarshal() {
135 final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
136 "{\"type\": \"int\"}");
138 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
139 final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
140 avroIntSchema.getKey());
142 assertEquals("0", schemaHelper2.marshal2String(0));
143 assertEquals("1", schemaHelper2.marshal2String(1));
144 assertEquals("-1", schemaHelper2.marshal2String(-1));
145 assertEquals("1", schemaHelper2.marshal2String(1.23));
146 assertEquals("-1", schemaHelper2.marshal2String(-1.23));
147 assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
148 assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
150 schemaHelper2.marshal2String("Hello");
151 fail("Test should throw an exception here");
152 } catch (final Exception e) {
153 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
154 + "java.lang.String cannot be cast to java.lang.Number"));
157 schemaHelper2.marshal2String(null);
158 fail("Test should throw an exception here");
159 } catch (final Exception e) {
160 assertTrue(e.getMessage()
161 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
169 public void testLongMarshal() {
170 final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
171 "{\"type\": \"long\"}");
173 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
174 final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
175 avroLongSchema.getKey());
177 assertEquals("0", schemaHelper3.marshal2String(0L));
178 assertEquals("1", schemaHelper3.marshal2String(1L));
179 assertEquals("-1", schemaHelper3.marshal2String(-1L));
180 assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
181 assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
183 schemaHelper3.marshal2String("Hello");
184 fail("Test should throw an exception here");
185 } catch (final Exception e) {
186 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
187 + "java.lang.String cannot be cast to java.lang.Long"));
190 schemaHelper3.marshal2String(null);
191 fail("Test should throw an exception here");
192 } catch (final Exception e) {
193 assertTrue(e.getMessage()
194 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
199 * Test float marshal.
202 public void testFloatMarshal() {
203 final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
204 "{\"type\": \"float\"}");
206 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
207 final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
208 avroFloatSchema.getKey());
210 assertEquals("0.0", schemaHelper4.marshal2String(0F));
211 assertEquals("1.0", schemaHelper4.marshal2String(1F));
212 assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
213 assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
214 assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
215 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
216 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
217 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
218 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
220 schemaHelper4.marshal2String("Hello");
221 fail("Test should throw an exception here");
222 } catch (final Exception e) {
223 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
224 + "java.lang.String cannot be cast to java.lang.Float"));
227 schemaHelper4.marshal2String(null);
228 fail("Test should throw an exception here");
229 } catch (final Exception e) {
230 assertTrue(e.getMessage()
231 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
236 * Test double marshal.
239 public void testDoubleMarshal() {
240 final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
241 "{\"type\": \"double\"}");
243 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
244 final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
245 avroDoubleSchema.getKey());
247 assertEquals("0.0", schemaHelper5.marshal2String(0D));
248 assertEquals("1.0", schemaHelper5.marshal2String(1D));
249 assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
250 assertEquals("1.23", schemaHelper5.marshal2String(1.23));
251 assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
252 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
253 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
254 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
255 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
257 schemaHelper5.marshal2String("Hello");
258 fail("Test should throw an exception here");
259 } catch (final Exception e) {
260 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
261 + "java.lang.String cannot be cast to java.lang.Double"));
264 schemaHelper5.marshal2String(null);
265 fail("Test should throw an exception here");
266 } catch (final Exception e) {
267 assertTrue(e.getMessage()
268 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
273 * Test string marshal.
276 public void testStringMarshal() {
277 final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
278 "{\"type\": \"string\"}");
280 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
281 final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
282 avroStringSchema.getKey());
284 assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
285 assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
286 assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
287 assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
288 assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
289 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
290 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
291 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
292 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
293 assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
295 schemaHelper7.marshal2String(null);
296 fail("Test should throw an exception here");
297 } catch (final Exception e) {
298 assertTrue(e.getMessage()
299 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
304 * Test bytes marshal.
307 public void testBytesMarshal() {
308 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
309 "{\"type\": \"bytes\"}");
311 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
312 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
314 final byte[] helloBytes =
315 { 104, 101, 108, 108, 111 };
316 final String helloOut = schemaHelper.marshal2String(helloBytes);
317 assertEquals("\"hello\"", helloOut);
320 schemaHelper.marshal2String(null);
321 fail("Test should throw an exception here");
322 } catch (final Exception e) {
323 assertTrue(e.getMessage()
324 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\""));