Changes for checkstyle 8.32
[policy/apex-pdp.git] / context / context-management / src / test / java / org / onap / policy / apex / context / impl / schema / java / JavaSchemaHelperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 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.context.impl.schema.java;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonParser;
29 import com.google.gson.JsonPrimitive;
30 import java.math.BigDecimal;
31 import java.time.Instant;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.ContextRuntimeException;
36 import org.onap.policy.apex.context.SchemaHelper;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.SchemaParameters;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 public class JavaSchemaHelperTest {
44     /**
45      * Initialize JSON adapters.
46      */
47     @BeforeClass
48     public static void registerParameters() {
49         JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters();
50
51         JavaSchemaHelperJsonAdapterParameters stringAdapterPars = new JavaSchemaHelperJsonAdapterParameters();
52         stringAdapterPars.setAdaptedClass("java.lang.String");
53         stringAdapterPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter");
54
55         javaSchemaHelperPars.getJsonAdapters().put("String", stringAdapterPars);
56
57         SchemaParameters schemaPars = new SchemaParameters();
58         schemaPars.getSchemaHelperParameterMap().put("Java", javaSchemaHelperPars);
59
60         ParameterService.register(schemaPars);
61     }
62
63     @AfterClass
64     public static void deregisterParameters() {
65         ParameterService.clear();
66     }
67
68     @Test
69     public void testJavaSchemaHelperInit() {
70         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
71         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
72
73         AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
74
75         try {
76             new JavaSchemaHelper().init(userKey, badJavaTypeSchema);
77             fail("test should throw an exception here");
78         } catch (ContextRuntimeException e) {
79             assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
80                     + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
81         }
82
83         AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
84
85         try {
86             new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema);
87             fail("test should throw an exception here");
88         } catch (ContextRuntimeException e) {
89             assertEquals("UserKey:0.0.1: class/type short for context schema "
90                     + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
91                     + " Use the appropriate Java boxing type instead.", e.getMessage());
92         }
93     }
94
95     @Test
96     public void testJavaSchemaHelperMethods() {
97         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
98
99         assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
100         assertEquals(null, intSchemaHelper.getSchema());
101         assertEquals(null, intSchemaHelper.getSchemaClass());
102         assertEquals(null, intSchemaHelper.getSchemaObject());
103
104         try {
105             intSchemaHelper.createNewInstance();
106             fail("test should throw an exception here");
107         } catch (ContextRuntimeException e) {
108             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
109                     e.getMessage());
110         }
111
112         try {
113             intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
114             fail("test should throw an exception here");
115         } catch (ContextRuntimeException e) {
116             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
117                     e.getMessage());
118         }
119
120         try {
121             intSchemaHelper.createNewInstance("hello");
122             fail("test should throw an exception here");
123         } catch (ContextRuntimeException e) {
124             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
125                     e.getMessage());
126         }
127
128         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
129         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
130         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
131
132         intSchemaHelper.init(userKey, intSchema);
133         assertEquals(userKey, intSchemaHelper.getUserKey());
134         assertEquals(intSchema, intSchemaHelper.getSchema());
135         assertEquals(Integer.class, intSchemaHelper.getSchemaClass());
136         assertEquals(null, intSchemaHelper.getSchemaObject());
137
138         try {
139             intSchemaHelper.createNewInstance();
140             fail("test should throw an exception here");
141         } catch (ContextRuntimeException e) {
142             assertEquals("UserKey:0.0.1: could not create an instance of class "
143                     + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
144         }
145
146         try {
147             intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
148             fail("test should throw an exception here");
149         } catch (ContextRuntimeException e) {
150             assertEquals("UserKey:0.0.1: the object \"1.23\" of type "
151                     + "\"java.lang.Float\" is not an instance of JsonObject and is not "
152                     + "assignable to \"java.lang.Integer\"", e.getMessage());
153         }
154
155         try {
156             intSchemaHelper.createNewInstance("hello");
157             fail("test should throw an exception here");
158         } catch (ContextRuntimeException e) {
159             assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
160                     + "using the string constructor \"Integer(String)\"", e.getMessage());
161         }
162
163         JsonElement jsonIntElement = null;
164         assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
165
166         jsonIntElement = JsonParser.parseString("123");
167
168         assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
169         assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
170
171         assertEquals(null, intSchemaHelper.unmarshal(null));
172         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
173         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
174         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
175         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
176         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
177         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
178     }
179
180     @Test
181     public void testJavaSchemaHelperUnmarshal() {
182         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
183         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
184
185         AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte");
186         AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short");
187         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
188
189         SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
190         SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
191         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
192
193         byteSchemaHelper.init(userKey, byteSchema);
194         shortSchemaHelper.init(userKey, shortSchema);
195         intSchemaHelper.init(userKey, intSchema);
196
197         SchemaHelper longSchemaHelper = new JavaSchemaHelper();
198         SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
199
200         AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long");
201         AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float");
202         longSchemaHelper.init(userKey, longSchema);
203         floatSchemaHelper.init(userKey, floatSchema);
204
205         SchemaHelper doubleSchemaHelper = new JavaSchemaHelper();
206         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
207         AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double");
208         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
209         doubleSchemaHelper.init(userKey, doubleSchema);
210         stringSchemaHelper.init(userKey, stringSchema);
211
212         AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java",
213                 "org.onap.policy.apex.context.impl.schema.java.SupportBaseClass");
214         SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
215         myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
216
217         assertEquals(null, byteSchemaHelper.unmarshal(null));
218         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal("123"));
219         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123")));
220         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123")));
221         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Short.parseShort("123")));
222         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Long.parseLong("123")));
223         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123")));
224         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123")));
225         try {
226             byteSchemaHelper.unmarshal("one two three");
227             fail("test should throw an exception here");
228         } catch (ContextRuntimeException e) {
229             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
230                     + "compatible with class \"java.lang.Byte\"", e.getMessage());
231         }
232
233         assertEquals(null, shortSchemaHelper.unmarshal(null));
234         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal("123"));
235         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123")));
236         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123")));
237         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Short.parseShort("123")));
238         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Long.parseLong("123")));
239         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123")));
240         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123")));
241         try {
242             shortSchemaHelper.unmarshal("one two three");
243             fail("test should throw an exception here");
244         } catch (ContextRuntimeException e) {
245             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
246                     + "compatible with class \"java.lang.Short\"", e.getMessage());
247         }
248
249         assertEquals(null, intSchemaHelper.unmarshal(null));
250         assertEquals(123, intSchemaHelper.unmarshal("123"));
251         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
252         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
253         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
254         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
255         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
256         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
257         try {
258             intSchemaHelper.unmarshal("one two three");
259             fail("test should throw an exception here");
260         } catch (ContextRuntimeException e) {
261             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
262                     + "compatible with class \"java.lang.Integer\"", e.getMessage());
263         }
264
265         assertEquals(null, longSchemaHelper.unmarshal(null));
266         assertEquals(123L, longSchemaHelper.unmarshal("123"));
267         assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123")));
268         assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123")));
269         assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123")));
270         assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123")));
271         assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123")));
272         assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123")));
273         try {
274             longSchemaHelper.unmarshal("one two three");
275             fail("test should throw an exception here");
276         } catch (ContextRuntimeException e) {
277             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
278                     + "compatible with class \"java.lang.Long\"", e.getMessage());
279         }
280
281         assertEquals(null, floatSchemaHelper.unmarshal(null));
282         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal("123"));
283         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123")));
284         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123")));
285         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Short.parseShort("123")));
286         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Long.parseLong("123")));
287         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123")));
288         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123")));
289         try {
290             floatSchemaHelper.unmarshal("one two three");
291             fail("test should throw an exception here");
292         } catch (ContextRuntimeException e) {
293             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
294                     + "compatible with class \"java.lang.Float\"", e.getMessage());
295         }
296
297         assertEquals(null, doubleSchemaHelper.unmarshal(null));
298         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal("123"));
299         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123")));
300         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123")));
301         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123")));
302         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123")));
303         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123")));
304         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123")));
305         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123)));
306         try {
307             doubleSchemaHelper.unmarshal("one two three");
308             fail("test should throw an exception here");
309         } catch (ContextRuntimeException e) {
310             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
311                     + "compatible with class \"java.lang.Double\"", e.getMessage());
312         }
313
314         assertEquals("123", stringSchemaHelper.unmarshal(123));
315
316         SupportSubClass subClassInstance = new SupportSubClass("123");
317         assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
318     }
319
320     @Test
321     public void testJavaSchemaHelperMarshal() {
322         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
323         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
324
325         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
326         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
327         intSchemaHelper.init(userKey, intSchema);
328
329         assertEquals("null", intSchemaHelper.marshal2String(null));
330         assertEquals("123", intSchemaHelper.marshal2String(123));
331         try {
332             intSchemaHelper.marshal2String(123.45);
333             fail("test should throw an exception here");
334         } catch (ContextRuntimeException e) {
335             assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
336                     + "compatible with class \"java.lang.Integer\"", e.getMessage());
337         }
338
339         JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
340         assertEquals(123, intJsonPrimitive.getAsInt());
341     }
342
343     @Test
344     public void testJavaSchemaHelperAdapters() {
345         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
346         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
347
348         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
349         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
350         stringSchemaHelper.init(userKey, stringSchema);
351
352         assertEquals("null", stringSchemaHelper.marshal2String(null));
353         assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello"));
354         try {
355             stringSchemaHelper.marshal2String(Instant.ofEpochMilli(1000));
356             fail("test should throw an exception here");
357         } catch (ContextRuntimeException e) {
358             assertEquals("UserKey:0.0.1: object \"1970-01-01T00:00:01Z\" of class \"java.time.Instant\" "
359                     + "not compatible with class \"java.lang.String\"", e.getMessage());
360         }
361
362         JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String");
363         assertEquals("Another String", stringJsonPrimitive.getAsString());
364     }
365
366     @Test
367     public void testJavaSchemaHelperBadAdapter() {
368         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
369         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
370
371         SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
372
373         JavaSchemaHelperParameters javaShPars =
374                 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
375         javaShPars.getJsonAdapters().get("String")
376                 .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter");
377
378         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
379         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
380         stringSchemaHelper.init(userKey, stringSchema);
381
382         try {
383             stringSchemaHelper.marshal2String("Hello");
384             fail("test should throw an exception");
385         } catch (ContextRuntimeException pre) {
386             assertEquals("UserKey:0.0.1: instantiation of adapter class "
387                     + "\"org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter\"  to decode and encode "
388                     + "class \"java.lang.String\" failed: null", pre.getMessage());
389         }
390     }
391
392     @Test
393     public void testJavaSchemaHelperDefaultAdapter() {
394         SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
395
396         JavaSchemaHelperParameters javaShPars =
397                 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
398
399         pars.getSchemaHelperParameterMap().clear();
400
401         testJavaSchemaHelperAdapters();
402
403         pars.getSchemaHelperParameterMap().put("Java", javaShPars);
404     }
405 }