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.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.SchemaHelper;
30 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
31 import org.onap.policy.apex.context.parameters.SchemaParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.basicmodel.service.ModelService;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class TestAvroSchemaHelperMarshal {
43 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
44 private AxContextSchemas schemas;
47 public void initTest() {
48 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
49 ModelService.registerModel(AxContextSchemas.class, schemas);
50 new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
54 public void testNullMarshal() {
55 final AxContextSchema avroNullSchema =
56 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "Avro", "{\"type\": \"null\"}");
58 schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
59 final SchemaHelper schemaHelper0 =
60 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
62 assertEquals("null", schemaHelper0.marshal2Json(null));
63 assertEquals("null", schemaHelper0.marshal2Json(123));
64 assertEquals("null", schemaHelper0.marshal2Json("Everything is marshalled to Null, no matter what it is"));
68 public void testBooleanMarshal() {
69 final AxContextSchema avroBooleanSchema =
70 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "Avro", "{\"type\": \"boolean\"}");
72 schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
73 final SchemaHelper schemaHelper1 =
74 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
76 assertEquals("true", schemaHelper1.marshal2Json(true));
77 assertEquals("false", schemaHelper1.marshal2Json(false));
79 schemaHelper1.marshal2Json(0);
80 fail("Test should throw an exception here");
81 } catch (final Exception e) {
84 "AvroTest:0.0.1: object \"0\" Avro marshalling failed: java.lang.Integer cannot be cast to java.lang.Boolean",
88 schemaHelper1.marshal2Json("0");
89 fail("Test should throw an exception here");
90 } catch (final Exception e) {
93 "AvroTest:0.0.1: object \"0\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Boolean",
99 public void testIntMarshal() {
100 final AxContextSchema avroIntSchema =
101 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "Avro", "{\"type\": \"int\"}");
103 schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
104 final SchemaHelper schemaHelper2 =
105 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
107 assertEquals("0", schemaHelper2.marshal2Json(0));
108 assertEquals("1", schemaHelper2.marshal2Json(1));
109 assertEquals("-1", schemaHelper2.marshal2Json(-1));
110 assertEquals("1", schemaHelper2.marshal2Json(1.23));
111 assertEquals("-1", schemaHelper2.marshal2Json(-1.23));
112 assertEquals("2147483647", schemaHelper2.marshal2Json(2147483647));
113 assertEquals("-2147483648", schemaHelper2.marshal2Json(-2147483648));
115 schemaHelper2.marshal2Json("Hello");
116 fail("Test should throw an exception here");
117 } catch (final Exception e) {
118 assertTrue(e.getMessage().startsWith(
119 "AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Number"));
122 schemaHelper2.marshal2Json(null);
123 fail("Test should throw an exception here");
124 } catch (final Exception e) {
125 assertTrue(e.getMessage()
126 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
131 public void testLongMarshal() {
132 final AxContextSchema avroLongSchema =
133 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "Avro", "{\"type\": \"long\"}");
135 schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
136 final SchemaHelper schemaHelper3 =
137 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
139 assertEquals("0", schemaHelper3.marshal2Json(0L));
140 assertEquals("1", schemaHelper3.marshal2Json(1L));
141 assertEquals("-1", schemaHelper3.marshal2Json(-1L));
142 assertEquals("9223372036854775807", schemaHelper3.marshal2Json(9223372036854775807L));
143 assertEquals("-9223372036854775808", schemaHelper3.marshal2Json(-9223372036854775808L));
145 schemaHelper3.marshal2Json("Hello");
146 fail("Test should throw an exception here");
147 } catch (final Exception e) {
148 assertTrue(e.getMessage().startsWith(
149 "AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Long"));
152 schemaHelper3.marshal2Json(null);
153 fail("Test should throw an exception here");
154 } catch (final Exception e) {
155 assertTrue(e.getMessage()
156 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
161 public void testFloatMarshal() {
162 final AxContextSchema avroFloatSchema =
163 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "Avro", "{\"type\": \"float\"}");
165 schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
166 final SchemaHelper schemaHelper4 =
167 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
169 assertEquals("0.0", schemaHelper4.marshal2Json(0F));
170 assertEquals("1.0", schemaHelper4.marshal2Json(1F));
171 assertEquals("-1.0", schemaHelper4.marshal2Json(-1F));
172 assertEquals("1.23", schemaHelper4.marshal2Json(1.23F));
173 assertEquals("-1.23", schemaHelper4.marshal2Json(-1.23F));
174 assertEquals("9.223372E18", schemaHelper4.marshal2Json(9.223372E18F));
175 assertEquals("-9.223372E18", schemaHelper4.marshal2Json(-9.223372E18F));
176 assertEquals("9.223372E18", schemaHelper4.marshal2Json(9.223372E18F));
177 assertEquals("-9.223372E18", schemaHelper4.marshal2Json(-9.223372E18F));
179 schemaHelper4.marshal2Json("Hello");
180 fail("Test should throw an exception here");
181 } catch (final Exception e) {
182 assertTrue(e.getMessage().startsWith(
183 "AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Float"));
186 schemaHelper4.marshal2Json(null);
187 fail("Test should throw an exception here");
188 } catch (final Exception e) {
189 assertTrue(e.getMessage()
190 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
196 public void testDoubleMarshal() {
197 final AxContextSchema avroDoubleSchema =
198 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "Avro", "{\"type\": \"double\"}");
200 schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
201 final SchemaHelper schemaHelper5 =
202 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
204 assertEquals("0.0", schemaHelper5.marshal2Json(0D));
205 assertEquals("1.0", schemaHelper5.marshal2Json(1D));
206 assertEquals("-1.0", schemaHelper5.marshal2Json(-1D));
207 assertEquals("1.23", schemaHelper5.marshal2Json(1.23));
208 assertEquals("-1.23", schemaHelper5.marshal2Json(-1.23));
209 assertEquals("9.223372036854776E18", schemaHelper5.marshal2Json(9.223372036854776E18));
210 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2Json(-9.223372036854776E18));
211 assertEquals("9.223372036854776E18", schemaHelper5.marshal2Json(9.223372036854776E18));
212 assertEquals("-9.223372036854776E18", schemaHelper5.marshal2Json(-9.223372036854776E18));
214 schemaHelper5.marshal2Json("Hello");
215 fail("Test should throw an exception here");
216 } catch (final Exception e) {
217 assertTrue(e.getMessage().startsWith(
218 "AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Double"));
221 schemaHelper5.marshal2Json(null);
222 fail("Test should throw an exception here");
223 } catch (final Exception e) {
224 assertTrue(e.getMessage()
225 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
230 public void testStringMarshal() {
231 final AxContextSchema avroStringSchema =
232 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"string\"}");
234 schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
235 final SchemaHelper schemaHelper7 =
236 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
238 assertEquals("\"0\"", schemaHelper7.marshal2Json("0"));
239 assertEquals("\"1\"", schemaHelper7.marshal2Json("1"));
240 assertEquals("\"-1\"", schemaHelper7.marshal2Json("-1"));
241 assertEquals("\"1.23\"", schemaHelper7.marshal2Json("1.23"));
242 assertEquals("\"-1.23\"", schemaHelper7.marshal2Json("-1.23"));
243 assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2Json("9223372036854775807"));
244 assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2Json("-9223372036854775808"));
245 assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2Json("9223372036854775808"));
246 assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2Json("-9223372036854775809"));
247 assertEquals("\"Hello\"", schemaHelper7.marshal2Json("Hello"));
249 schemaHelper7.marshal2Json(null);
250 fail("Test should throw an exception here");
251 } catch (final Exception e) {
252 assertTrue(e.getMessage()
253 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
258 public void testBytesMarshal() {
259 final AxContextSchema avroSchema =
260 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"bytes\"}");
262 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
263 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
265 final byte[] helloBytes = {104, 101, 108, 108, 111};
266 final String helloOut = schemaHelper.marshal2Json(helloBytes);
267 assertEquals("\"hello\"", helloOut);
270 schemaHelper.marshal2Json(null);
271 fail("Test should throw an exception here");
272 } catch (final Exception e) {
273 assertTrue(e.getMessage()
274 .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Byte[]\""));