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