a710a2376290b57170fd2402ade2162e24f9bd64
[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.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;
37
38 /**
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  * @version
41  */
42 public class TestAvroSchemaHelperMarshal {
43     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
44     private AxContextSchemas schemas;
45
46     @Before
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());
51     }
52
53     @Test
54     public void testNullMarshal() {
55         final AxContextSchema avroNullSchema =
56                 new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "Avro", "{\"type\": \"null\"}");
57
58         schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
59         final SchemaHelper schemaHelper0 =
60                 new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());
61
62         assertEquals("null", schemaHelper0.marshal2String(null));
63         assertEquals("null", schemaHelper0.marshal2String(123));
64         assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is"));
65     }
66
67     @Test
68     public void testBooleanMarshal() {
69         final AxContextSchema avroBooleanSchema =
70                 new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "Avro", "{\"type\": \"boolean\"}");
71
72         schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
73         final SchemaHelper schemaHelper1 =
74                 new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());
75
76         assertEquals("true", schemaHelper1.marshal2String(true));
77         assertEquals("false", schemaHelper1.marshal2String(false));
78         try {
79             schemaHelper1.marshal2String(0);
80             fail("Test should throw an exception here");
81         } catch (final Exception e) {
82             e.printStackTrace();
83             assertEquals(
84                     "AvroTest:0.0.1: object \"0\" Avro marshalling failed: java.lang.Integer cannot be cast to java.lang.Boolean",
85                     e.getMessage());
86         }
87         try {
88             schemaHelper1.marshal2String("0");
89             fail("Test should throw an exception here");
90         } catch (final Exception e) {
91             e.printStackTrace();
92             assertEquals(
93                     "AvroTest:0.0.1: object \"0\" Avro marshalling failed: java.lang.String cannot be cast to java.lang.Boolean",
94                     e.getMessage());
95         }
96     }
97
98     @Test
99     public void testIntMarshal() {
100         final AxContextSchema avroIntSchema =
101                 new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "Avro", "{\"type\": \"int\"}");
102
103         schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
104         final SchemaHelper schemaHelper2 =
105                 new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());
106
107         assertEquals("0", schemaHelper2.marshal2String(0));
108         assertEquals("1", schemaHelper2.marshal2String(1));
109         assertEquals("-1", schemaHelper2.marshal2String(-1));
110         assertEquals("1", schemaHelper2.marshal2String(1.23));
111         assertEquals("-1", schemaHelper2.marshal2String(-1.23));
112         assertEquals("2147483647", schemaHelper2.marshal2String(2147483647));
113         assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648));
114         try {
115             schemaHelper2.marshal2String("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"));
120         }
121         try {
122             schemaHelper2.marshal2String(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\""));
127         }
128     }
129
130     @Test
131     public void testLongMarshal() {
132         final AxContextSchema avroLongSchema =
133                 new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "Avro", "{\"type\": \"long\"}");
134
135         schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
136         final SchemaHelper schemaHelper3 =
137                 new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());
138
139         assertEquals("0", schemaHelper3.marshal2String(0L));
140         assertEquals("1", schemaHelper3.marshal2String(1L));
141         assertEquals("-1", schemaHelper3.marshal2String(-1L));
142         assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L));
143         assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L));
144         try {
145             schemaHelper3.marshal2String("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"));
150         }
151         try {
152             schemaHelper3.marshal2String(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\""));
157         }
158     }
159
160     @Test
161     public void testFloatMarshal() {
162         final AxContextSchema avroFloatSchema =
163                 new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "Avro", "{\"type\": \"float\"}");
164
165         schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
166         final SchemaHelper schemaHelper4 =
167                 new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());
168
169         assertEquals("0.0", schemaHelper4.marshal2String(0F));
170         assertEquals("1.0", schemaHelper4.marshal2String(1F));
171         assertEquals("-1.0", schemaHelper4.marshal2String(-1F));
172         assertEquals("1.23", schemaHelper4.marshal2String(1.23F));
173         assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F));
174         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
175         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
176         assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F));
177         assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F));
178         try {
179             schemaHelper4.marshal2String("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"));
184         }
185         try {
186             schemaHelper4.marshal2String(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\""));
191         }
192     }
193
194
195     @Test
196     public void testDoubleMarshal() {
197         final AxContextSchema avroDoubleSchema =
198                 new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "Avro", "{\"type\": \"double\"}");
199
200         schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
201         final SchemaHelper schemaHelper5 =
202                 new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());
203
204         assertEquals("0.0", schemaHelper5.marshal2String(0D));
205         assertEquals("1.0", schemaHelper5.marshal2String(1D));
206         assertEquals("-1.0", schemaHelper5.marshal2String(-1D));
207         assertEquals("1.23", schemaHelper5.marshal2String(1.23));
208         assertEquals("-1.23", schemaHelper5.marshal2String(-1.23));
209         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
210         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
211         assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18));
212         assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18));
213         try {
214             schemaHelper5.marshal2String("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"));
219         }
220         try {
221             schemaHelper5.marshal2String(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\""));
226         }
227     }
228
229     @Test
230     public void testStringMarshal() {
231         final AxContextSchema avroStringSchema =
232                 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"string\"}");
233
234         schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
235         final SchemaHelper schemaHelper7 =
236                 new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());
237
238         assertEquals("\"0\"", schemaHelper7.marshal2String("0"));
239         assertEquals("\"1\"", schemaHelper7.marshal2String("1"));
240         assertEquals("\"-1\"", schemaHelper7.marshal2String("-1"));
241         assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23"));
242         assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23"));
243         assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807"));
244         assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808"));
245         assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808"));
246         assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809"));
247         assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello"));
248         try {
249             schemaHelper7.marshal2String(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\""));
254         }
255     }
256
257     @Test
258     public void testBytesMarshal() {
259         final AxContextSchema avroSchema =
260                 new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"bytes\"}");
261
262         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
263         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
264
265         final byte[] helloBytes = {104, 101, 108, 108, 111};
266         final String helloOut = schemaHelper.marshal2String(helloBytes);
267         assertEquals("\"hello\"", helloOut);
268
269         try {
270             schemaHelper.marshal2String(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[]\""));
275         }
276     }
277 }