7785739398a9b64b19dfd7b06b343df0f5c6a5e0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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 = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO",
87                         "{\"type\": \"null\"}");
88
89         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
90         final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey,
91                         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 = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO",
104                         "{\"type\": \"boolean\"}");
105
106         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
107         final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey,
108                         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                             + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage());
119         }
120         try {
121             schemaHelper1.marshal2String("0");
122             fail("Test should throw an exception here");
123         } catch (final Exception e) {
124             e.printStackTrace();
125             assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: "
126                             + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage());
127         }
128     }
129
130     /**
131      * Test int marshal.
132      */
133     @Test
134     public void testIntMarshal() {
135         final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO",
136                         "{\"type\": \"int\"}");
137
138         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
139         final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey,
140                         avroIntSchema.getKey());
141
142         assertEquals("0", schemaHelper2.marshal2String(0));
143         assertEquals("1", schemaHelper2.marshal2String(1));
144         assertEquals("-1", schemaHelper2.marshal2String(-1));
145         assertEquals("1", schemaHelper2.marshal2String(1.23));
146         assertEquals("-1", schemaHelper2.marshal2String(-1.23));
147         assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
148         assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
149         try {
150             schemaHelper2.marshal2String("Hello");
151             fail("Test should throw an exception here");
152         } catch (final Exception e) {
153             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
154                             + "java.lang.String cannot be cast to java.lang.Number"));
155         }
156         try {
157             schemaHelper2.marshal2String(null);
158             fail("Test should throw an exception here");
159         } catch (final Exception e) {
160             assertTrue(e.getMessage()
161                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""));
162         }
163     }
164
165     /**
166      * Test long marshal.
167      */
168     @Test
169     public void testLongMarshal() {
170         final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO",
171                         "{\"type\": \"long\"}");
172
173         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
174         final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey,
175                         avroLongSchema.getKey());
176
177         assertEquals("0", schemaHelper3.marshal2String(0L));
178         assertEquals("1", schemaHelper3.marshal2String(1L));
179         assertEquals("-1", schemaHelper3.marshal2String(-1L));
180         assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
181         assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
182         try {
183             schemaHelper3.marshal2String("Hello");
184             fail("Test should throw an exception here");
185         } catch (final Exception e) {
186             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
187                             + "java.lang.String cannot be cast to java.lang.Long"));
188         }
189         try {
190             schemaHelper3.marshal2String(null);
191             fail("Test should throw an exception here");
192         } catch (final Exception e) {
193             assertTrue(e.getMessage()
194                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""));
195         }
196     }
197
198     /**
199      * Test float marshal.
200      */
201     @Test
202     public void testFloatMarshal() {
203         final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO",
204                         "{\"type\": \"float\"}");
205
206         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
207         final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey,
208                         avroFloatSchema.getKey());
209
210         assertEquals("0.0", schemaHelper4.marshal2String(0F));
211         assertEquals("1.0", schemaHelper4.marshal2String(1F));
212         assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
213         assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
214         assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
215         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
216         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
217         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
218         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
219         try {
220             schemaHelper4.marshal2String("Hello");
221             fail("Test should throw an exception here");
222         } catch (final Exception e) {
223             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
224                             + "java.lang.String cannot be cast to java.lang.Float"));
225         }
226         try {
227             schemaHelper4.marshal2String(null);
228             fail("Test should throw an exception here");
229         } catch (final Exception e) {
230             assertTrue(e.getMessage()
231                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""));
232         }
233     }
234
235     /**
236      * Test double marshal.
237      */
238     @Test
239     public void testDoubleMarshal() {
240         final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO",
241                         "{\"type\": \"double\"}");
242
243         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
244         final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey,
245                         avroDoubleSchema.getKey());
246
247         assertEquals("0.0", schemaHelper5.marshal2String(0D));
248         assertEquals("1.0", schemaHelper5.marshal2String(1D));
249         assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
250         assertEquals("1.23", schemaHelper5.marshal2String(1.23));
251         assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
252         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
253         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
254         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
255         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
256         try {
257             schemaHelper5.marshal2String("Hello");
258             fail("Test should throw an exception here");
259         } catch (final Exception e) {
260             assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: "
261                             + "java.lang.String cannot be cast to java.lang.Double"));
262         }
263         try {
264             schemaHelper5.marshal2String(null);
265             fail("Test should throw an exception here");
266         } catch (final Exception e) {
267             assertTrue(e.getMessage()
268                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""));
269         }
270     }
271
272     /**
273      * Test string marshal.
274      */
275     @Test
276     public void testStringMarshal() {
277         final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
278                         "{\"type\": \"string\"}");
279
280         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
281         final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey,
282                         avroStringSchema.getKey());
283
284         assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
285         assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
286         assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
287         assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
288         assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
289         assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
290         assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
291         assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
292         assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
293         assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
294         try {
295             schemaHelper7.marshal2String(null);
296             fail("Test should throw an exception here");
297         } catch (final Exception e) {
298             assertTrue(e.getMessage()
299                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""));
300         }
301     }
302
303     /**
304      * Test bytes marshal.
305      */
306     @Test
307     public void testBytesMarshal() {
308         final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO",
309                         "{\"type\": \"bytes\"}");
310
311         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
312         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
313
314         final byte[] helloBytes =
315             { 104, 101, 108, 108, 111 };
316         final String helloOut = schemaHelper.marshal2String(helloBytes);
317         assertEquals("\"hello\"", helloOut);
318
319         try {
320             schemaHelper.marshal2String(null);
321             fail("Test should throw an exception here");
322         } catch (final Exception e) {
323             assertTrue(e.getMessage()
324                             .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\""));
325         }
326     }
327 }