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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
27 import org.junit.After;
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.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.common.parameters.ParameterService;
42 * The Class TestAvroSchemaHelperMarshal.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class AvroSchemaHelperMarshalTest {
48 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
49 private AxContextSchemas schemas;
55 public void initTest() {
56 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
57 ModelService.registerModel(AxContextSchemas.class, schemas);
64 public void initContext() {
65 SchemaParameters schemaParameters = new SchemaParameters();
66 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
67 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
68 ParameterService.register(schemaParameters);
76 public void clearContext() {
77 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
84 public void testNullMarshal() {
85 final AxContextSchema avroNullSchema =
86 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", "{\"type\": \"null\"}");
88 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
89 final SchemaHelper schemaHelper0 =
90 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
92 assertEquals("null", schemaHelper0.marshal2String(null));
93 assertEquals("null", schemaHelper0.marshal2String(123));
94 assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is"));
98 * Test boolean marshal.
101 public void testBooleanMarshal() {
102 final AxContextSchema avroBooleanSchema =
103 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", "{\"type\": \"boolean\"}");
105 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
106 final SchemaHelper schemaHelper1 =
107 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
109 assertEquals("true", schemaHelper1.marshal2String(true));
110 assertEquals("false", schemaHelper1.marshal2String(false));
111 assertThatThrownBy(() -> schemaHelper1.marshal2String(0))
112 .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
113 + "class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and "
114 + "java.lang.Boolean are in module java.base of loader 'bootstrap')");
115 assertThatThrownBy(() -> schemaHelper1.marshal2String("0"))
116 .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed: class java.lang.String "
117 + "cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module"
118 + " java.base of loader 'bootstrap')");
125 public void testIntMarshal() {
126 final AxContextSchema avroIntSchema =
127 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", "{\"type\": \"int\"}");
129 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
130 final SchemaHelper schemaHelper2 =
131 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
133 assertEquals("0", schemaHelper2.marshal2String(0));
134 assertEquals("1", schemaHelper2.marshal2String(1));
135 assertEquals("-1", schemaHelper2.marshal2String(-1));
136 assertEquals("1", schemaHelper2.marshal2String(1.23));
137 assertEquals("-1", schemaHelper2.marshal2String(-1.23));
138 assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
139 assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
140 assertThatThrownBy(() -> schemaHelper2.marshal2String("Hello"))
141 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
142 + "class java.lang.String cannot be cast to class java.lang.Number");
143 assertThatThrownBy(() -> schemaHelper2.marshal2String(null))
144 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\"");
151 public void testLongMarshal() {
152 final AxContextSchema avroLongSchema =
153 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", "{\"type\": \"long\"}");
155 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
156 final SchemaHelper schemaHelper3 =
157 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
159 assertEquals("0", schemaHelper3.marshal2String(0L));
160 assertEquals("1", schemaHelper3.marshal2String(1L));
161 assertEquals("-1", schemaHelper3.marshal2String(-1L));
162 assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
163 assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
164 assertThatThrownBy(() -> schemaHelper3.marshal2String("Hello"))
165 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
166 + "class java.lang.String cannot be cast to class java.lang.Long");
167 assertThatThrownBy(() -> schemaHelper3.marshal2String(null))
168 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\"");
172 * Test float marshal.
175 public void testFloatMarshal() {
176 final AxContextSchema avroFloatSchema =
177 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", "{\"type\": \"float\"}");
179 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
180 final SchemaHelper schemaHelper4 =
181 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
183 assertEquals("0.0", schemaHelper4.marshal2String(0F));
184 assertEquals("1.0", schemaHelper4.marshal2String(1F));
185 assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
186 assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
187 assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
188 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
189 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
190 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
191 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
192 assertThatThrownBy(() -> schemaHelper4.marshal2String("Hello"))
193 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
194 + "class java.lang.String cannot be cast to class java.lang.Float");
195 assertThatThrownBy(() -> schemaHelper4.marshal2String(null))
196 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\"");
200 * Test double marshal.
203 public void testDoubleMarshal() {
204 final AxContextSchema avroDoubleSchema =
205 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", "{\"type\": \"double\"}");
207 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
208 final SchemaHelper schemaHelper5 =
209 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
211 assertEquals("0.0", schemaHelper5.marshal2String(0D));
212 assertEquals("1.0", schemaHelper5.marshal2String(1D));
213 assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
214 assertEquals("1.23", schemaHelper5.marshal2String(1.23));
215 assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
216 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
217 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
218 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
219 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
220 assertThatThrownBy(() -> schemaHelper5.marshal2String("Hello"))
221 .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
222 + "class java.lang.String cannot be cast to class java.lang.Double");
223 assertThatThrownBy(() -> schemaHelper5.marshal2String(null))
224 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\"");
228 * Test string marshal.
231 public void testStringMarshal() {
232 final AxContextSchema avroStringSchema =
233 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"string\"}");
235 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
236 final SchemaHelper schemaHelper7 =
237 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
239 assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
240 assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
241 assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
242 assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
243 assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
244 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
245 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
246 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
247 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
248 assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
249 assertThatThrownBy(() -> schemaHelper7.marshal2String(null))
250 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\"");
254 * Test bytes marshal.
257 public void testBytesMarshal() {
258 final AxContextSchema avroSchema =
259 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"bytes\"}");
261 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
262 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
264 final byte[] helloBytes = {104, 101, 108, 108, 111};
265 final String helloOut = schemaHelper.marshal2String(helloBytes);
266 assertEquals("\"hello\"", helloOut);
268 assertThatThrownBy(() -> schemaHelper.marshal2String(null))
269 .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\"");