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.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 = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
86 "{\"type\": \"null\"}");
88 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
89 final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
90 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 = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
103 "{\"type\": \"boolean\"}");
105 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
106 final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
107 avroBooleanSchema.getKey());
109 assertEquals("true", schemaHelper1.marshal2String(true));
110 assertEquals("false", schemaHelper1.marshal2String(false));
112 schemaHelper1.marshal2String(0);
113 fail("Test should throw an exception here");
114 } catch (final Exception e) {
116 assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
117 + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage());
120 schemaHelper1.marshal2String("0");
121 fail("Test should throw an exception here");
122 } catch (final Exception e) {
124 assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
125 + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage());
133 public void testIntMarshal() {
134 final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
135 "{\"type\": \"int\"}");
137 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
138 final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
139 avroIntSchema.getKey());
141 assertEquals("0", schemaHelper2.marshal2String(0));
142 assertEquals("1", schemaHelper2.marshal2String(1));
143 assertEquals("-1", schemaHelper2.marshal2String(-1));
144 assertEquals("1", schemaHelper2.marshal2String(1.23));
145 assertEquals("-1", schemaHelper2.marshal2String(-1.23));
146 assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
147 assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
149 schemaHelper2.marshal2String("Hello");
150 fail("Test should throw an exception here");
151 } catch (final Exception e) {
152 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
153 + "java.lang.String cannot be cast to java.lang.Number"));
156 schemaHelper2.marshal2String(null);
157 fail("Test should throw an exception here");
158 } catch (final Exception e) {
159 assertTrue(e.getMessage()
160 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
168 public void testLongMarshal() {
169 final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
170 "{\"type\": \"long\"}");
172 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
173 final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
174 avroLongSchema.getKey());
176 assertEquals("0", schemaHelper3.marshal2String(0L));
177 assertEquals("1", schemaHelper3.marshal2String(1L));
178 assertEquals("-1", schemaHelper3.marshal2String(-1L));
179 assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
180 assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
182 schemaHelper3.marshal2String("Hello");
183 fail("Test should throw an exception here");
184 } catch (final Exception e) {
185 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
186 + "java.lang.String cannot be cast to java.lang.Long"));
189 schemaHelper3.marshal2String(null);
190 fail("Test should throw an exception here");
191 } catch (final Exception e) {
192 assertTrue(e.getMessage()
193 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
198 * Test float marshal.
201 public void testFloatMarshal() {
202 final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
203 "{\"type\": \"float\"}");
205 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
206 final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
207 avroFloatSchema.getKey());
209 assertEquals("0.0", schemaHelper4.marshal2String(0F));
210 assertEquals("1.0", schemaHelper4.marshal2String(1F));
211 assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
212 assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
213 assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
214 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
215 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
216 assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
217 assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
219 schemaHelper4.marshal2String("Hello");
220 fail("Test should throw an exception here");
221 } catch (final Exception e) {
222 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
223 + "java.lang.String cannot be cast to java.lang.Float"));
226 schemaHelper4.marshal2String(null);
227 fail("Test should throw an exception here");
228 } catch (final Exception e) {
229 assertTrue(e.getMessage()
230 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
235 * Test double marshal.
238 public void testDoubleMarshal() {
239 final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
240 "{\"type\": \"double\"}");
242 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
243 final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
244 avroDoubleSchema.getKey());
246 assertEquals("0.0", schemaHelper5.marshal2String(0D));
247 assertEquals("1.0", schemaHelper5.marshal2String(1D));
248 assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
249 assertEquals("1.23", schemaHelper5.marshal2String(1.23));
250 assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
251 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
252 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
253 assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
254 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
256 schemaHelper5.marshal2String("Hello");
257 fail("Test should throw an exception here");
258 } catch (final Exception e) {
259 assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
260 + "java.lang.String cannot be cast to java.lang.Double"));
263 schemaHelper5.marshal2String(null);
264 fail("Test should throw an exception here");
265 } catch (final Exception e) {
266 assertTrue(e.getMessage()
267 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
272 * Test string marshal.
275 public void testStringMarshal() {
276 final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
277 "{\"type\": \"string\"}");
279 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
280 final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
281 avroStringSchema.getKey());
283 assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
284 assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
285 assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
286 assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
287 assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
288 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
289 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
290 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
291 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
292 assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
294 schemaHelper7.marshal2String(null);
295 fail("Test should throw an exception here");
296 } catch (final Exception e) {
297 assertTrue(e.getMessage()
298 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
303 * Test bytes marshal.
306 public void testBytesMarshal() {
307 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
308 "{\"type\": \"bytes\"}");
310 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
311 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
313 final byte[] helloBytes =
314 { 104, 101, 108, 108, 111 };
315 final String helloOut = schemaHelper.marshal2String(helloBytes);
316 assertEquals("\"hello\"", helloOut);
319 schemaHelper.marshal2String(null);
320 fail("Test should throw an exception here");
321 } catch (final Exception e) {
322 assertTrue(e.getMessage()
323 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Byte[]\""));