2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.context.schema.avro;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
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));
112 assertThatThrownBy(() -> schemaHelper1.marshal2String(0))
113 .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed.");
114 assertThatThrownBy(() -> schemaHelper1.marshal2String("0"))
115 .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed.");
122 public void testIntMarshal() {
123 final AxContextSchema avroIntSchema =
124 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", "{\"type\": \"int\"}");
126 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
127 final SchemaHelper schemaHelper2 =
128 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
130 assertEquals("0", schemaHelper2.marshal2String(0));
131 assertEquals("1", schemaHelper2.marshal2String(1));
132 assertEquals("-1", schemaHelper2.marshal2String(-1));
133 assertEquals("1", schemaHelper2.marshal2String(1.23));
134 assertEquals("-1", schemaHelper2.marshal2String(-1.23));
135 assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
136 assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
137 assertThatThrownBy(() -> schemaHelper2.marshal2String("Hello"))
138 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed.");
139 assertThatThrownBy(() -> schemaHelper2.marshal2String(null))
140 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\"");
147 public void testLongMarshal() {
148 final AxContextSchema avroLongSchema =
149 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", "{\"type\": \"long\"}");
151 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
152 final SchemaHelper schemaHelper3 =
153 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
155 assertEquals("0", schemaHelper3.marshal2String(0L));
156 assertEquals("1", schemaHelper3.marshal2String(1L));
157 assertEquals("-1", schemaHelper3.marshal2String(-1L));
158 assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
159 assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
160 assertThatThrownBy(() -> schemaHelper3.marshal2String("Hello"))
161 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed.");
162 assertThatThrownBy(() -> schemaHelper3.marshal2String(null))
163 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\"");
167 * Test float marshal.
170 public void testFloatMarshal() {
171 final AxContextSchema avroFloatSchema =
172 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", "{\"type\": \"float\"}");
174 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
175 final SchemaHelper schemaHelper4 =
176 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
178 assertEquals("0.0", schemaHelper4.marshal2String(0F));
179 assertEquals("1.0", schemaHelper4.marshal2String(1F));
180 assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
181 assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
182 assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
183 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
184 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
185 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
186 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
187 assertThatThrownBy(() -> schemaHelper4.marshal2String("Hello"))
188 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed.");
189 assertThatThrownBy(() -> schemaHelper4.marshal2String(null))
190 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\"");
194 * Test double marshal.
197 public void testDoubleMarshal() {
198 final AxContextSchema avroDoubleSchema =
199 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", "{\"type\": \"double\"}");
201 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
202 final SchemaHelper schemaHelper5 =
203 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
205 assertEquals("0.0", schemaHelper5.marshal2String(0D));
206 assertEquals("1.0", schemaHelper5.marshal2String(1D));
207 assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
208 assertEquals("1.23", schemaHelper5.marshal2String(1.23));
209 assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
210 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
211 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
212 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
213 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
214 assertThatThrownBy(() -> schemaHelper5.marshal2String("Hello"))
215 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed.");
216 assertThatThrownBy(() -> schemaHelper5.marshal2String(null))
217 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\"");
221 * Test string marshal.
224 public void testStringMarshal() {
225 final AxContextSchema avroStringSchema =
226 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"string\"}");
228 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
229 final SchemaHelper schemaHelper7 =
230 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
232 assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
233 assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
234 assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
235 assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
236 assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
237 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
238 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
239 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
240 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
241 assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
242 assertThatThrownBy(() -> schemaHelper7.marshal2String(null))
243 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\"");
247 * Test bytes marshal.
250 public void testBytesMarshal() {
251 final AxContextSchema avroSchema =
252 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"bytes\"}");
254 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
255 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
257 final byte[] helloBytes = {104, 101, 108, 108, 111};
258 final String helloOut = schemaHelper.marshal2String(helloBytes);
259 assertEquals("\"hello\"", helloOut);
261 assertThatThrownBy(() -> schemaHelper.marshal2String(null))
262 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\"");