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