004521c6c18f777ded8b2810c7833772368b154f
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
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;
41
42 /**
43  * The Class TestAvroSchemaHelperMarshal.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  * @version
47  */
48 public class AvroSchemaHelperMarshalTest {
49     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
50     private AxContextSchemas schemas;
51
52     /**
53      * Inits the test.
54      */
55     @Before
56     public void initTest() {
57         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
58         ModelService.registerModel(AxContextSchemas.class, schemas);
59     }
60
61     /**
62      * Inits the context.
63      */
64     @Before
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);
70
71     }
72
73     /**
74      * Clear context.
75      */
76     @After
77     public void clearContext() {
78         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
79     }
80
81     /**
82      * Test null marshal.
83      */
84     @Test
85     public void testNullMarshal() {
86         final AxContextSchema avroNullSchema =
87                 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", "{\"type\": \"null\"}");
88
89         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
90         final SchemaHelper schemaHelper0 =
91                 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
92
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"));
96     }
97
98     /**
99      * Test boolean marshal.
100      */
101     @Test
102     public void testBooleanMarshal() {
103         final AxContextSchema avroBooleanSchema =
104                 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", "{\"type\": \"boolean\"}");
105
106         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
107         final SchemaHelper schemaHelper1 =
108                 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
109
110         assertEquals("true", schemaHelper1.marshal2String(true));
111         assertEquals("false", schemaHelper1.marshal2String(false));
112         try {
113             schemaHelper1.marshal2String(0);
114             fail("Test should throw an exception here");
115         } catch (final Exception e) {
116             e.printStackTrace();
117             assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
118                     + "class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and "
119                     + "java.lang.Boolean are in module java.base of loader 'bootstrap')", e.getMessage());
120         }
121         try {
122             schemaHelper1.marshal2String("0");
123             fail("Test should throw an exception here");
124         } catch (final Exception e) {
125             e.printStackTrace();
126             assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: class java.lang.String cannot be cast "
127                     + "to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of "
128                     + "loader 'bootstrap')", e.getMessage());
129         }
130     }
131
132     /**
133      * Test int marshal.
134      */
135     @Test
136     public void testIntMarshal() {
137         final AxContextSchema avroIntSchema =
138                 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", "{\"type\": \"int\"}");
139
140         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
141         final SchemaHelper schemaHelper2 =
142                 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
143
144         assertEquals("0", schemaHelper2.marshal2String(0));
145         assertEquals("1", schemaHelper2.marshal2String(1));
146         assertEquals("-1", schemaHelper2.marshal2String(-1));
147         assertEquals("1", schemaHelper2.marshal2String(1.23));
148         assertEquals("-1", schemaHelper2.marshal2String(-1.23));
149         assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
150         assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
151         try {
152             schemaHelper2.marshal2String("Hello");
153             fail("Test should throw an exception here");
154         } catch (final Exception e) {
155             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
156                     + "class java.lang.String cannot be cast to class java.lang.Number"));
157         }
158         try {
159             schemaHelper2.marshal2String(null);
160             fail("Test should throw an exception here");
161         } catch (final Exception e) {
162             assertTrue(e.getMessage()
163                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
164         }
165     }
166
167     /**
168      * Test long marshal.
169      */
170     @Test
171     public void testLongMarshal() {
172         final AxContextSchema avroLongSchema =
173                 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", "{\"type\": \"long\"}");
174
175         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
176         final SchemaHelper schemaHelper3 =
177                 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
178
179         assertEquals("0", schemaHelper3.marshal2String(0L));
180         assertEquals("1", schemaHelper3.marshal2String(1L));
181         assertEquals("-1", schemaHelper3.marshal2String(-1L));
182         assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
183         assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
184         try {
185             schemaHelper3.marshal2String("Hello");
186             fail("Test should throw an exception here");
187         } catch (final Exception e) {
188             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
189                     + "class java.lang.String cannot be cast to class java.lang.Long"));
190         }
191         try {
192             schemaHelper3.marshal2String(null);
193             fail("Test should throw an exception here");
194         } catch (final Exception e) {
195             assertTrue(e.getMessage()
196                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
197         }
198     }
199
200     /**
201      * Test float marshal.
202      */
203     @Test
204     public void testFloatMarshal() {
205         final AxContextSchema avroFloatSchema =
206                 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", "{\"type\": \"float\"}");
207
208         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
209         final SchemaHelper schemaHelper4 =
210                 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
211
212         assertEquals("0.0", schemaHelper4.marshal2String(0F));
213         assertEquals("1.0", schemaHelper4.marshal2String(1F));
214         assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
215         assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
216         assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
217         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
218         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
219         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
220         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
221         try {
222             schemaHelper4.marshal2String("Hello");
223             fail("Test should throw an exception here");
224         } catch (final Exception e) {
225             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
226                     + "class java.lang.String cannot be cast to class java.lang.Float"));
227         }
228         try {
229             schemaHelper4.marshal2String(null);
230             fail("Test should throw an exception here");
231         } catch (final Exception e) {
232             assertTrue(e.getMessage()
233                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
234         }
235     }
236
237     /**
238      * Test double marshal.
239      */
240     @Test
241     public void testDoubleMarshal() {
242         final AxContextSchema avroDoubleSchema =
243                 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", "{\"type\": \"double\"}");
244
245         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
246         final SchemaHelper schemaHelper5 =
247                 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
248
249         assertEquals("0.0", schemaHelper5.marshal2String(0D));
250         assertEquals("1.0", schemaHelper5.marshal2String(1D));
251         assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
252         assertEquals("1.23", schemaHelper5.marshal2String(1.23));
253         assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
254         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
255         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
256         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
257         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
258         try {
259             schemaHelper5.marshal2String("Hello");
260             fail("Test should throw an exception here");
261         } catch (final Exception e) {
262             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
263                     + "class java.lang.String cannot be cast to class java.lang.Double"));
264         }
265         try {
266             schemaHelper5.marshal2String(null);
267             fail("Test should throw an exception here");
268         } catch (final Exception e) {
269             assertTrue(e.getMessage()
270                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
271         }
272     }
273
274     /**
275      * Test string marshal.
276      */
277     @Test
278     public void testStringMarshal() {
279         final AxContextSchema avroStringSchema =
280                 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"string\"}");
281
282         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
283         final SchemaHelper schemaHelper7 =
284                 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
285
286         assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
287         assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
288         assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
289         assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
290         assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
291         assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
292         assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
293         assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
294         assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
295         assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
296         try {
297             schemaHelper7.marshal2String(null);
298             fail("Test should throw an exception here");
299         } catch (final Exception e) {
300             assertTrue(e.getMessage()
301                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
302         }
303     }
304
305     /**
306      * Test bytes marshal.
307      */
308     @Test
309     public void testBytesMarshal() {
310         final AxContextSchema avroSchema =
311                 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", "{\"type\": \"bytes\"}");
312
313         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
314         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
315
316         final byte[] helloBytes = {104, 101, 108, 108, 111};
317         final String helloOut = schemaHelper.marshal2String(helloBytes);
318         assertEquals("\"hello\"", helloOut);
319
320         try {
321             schemaHelper.marshal2String(null);
322             fail("Test should throw an exception here");
323         } catch (final Exception e) {
324             assertTrue(e.getMessage()
325                     .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\""));
326         }
327     }
328 }