d06ac7469779ede8dde84a2ecc2b420830c95403
[policy/apex-pdp.git] /
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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
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.SchemaHelper;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 import org.onap.policy.common.parameters.ParameterService;
41
42 public class JavaSchemaHelperTest {
43     /**
44      * Initialize JSON adapters.
45      */
46     @BeforeClass
47     public static void registerParameters() {
48         JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters();
49
50         JavaSchemaHelperJsonAdapterParameters stringAdapterPars = new JavaSchemaHelperJsonAdapterParameters();
51         stringAdapterPars.setAdaptedClass("java.lang.String");
52         stringAdapterPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter");
53
54         javaSchemaHelperPars.getJsonAdapters().put("String", stringAdapterPars);
55
56         SchemaParameters schemaPars = new SchemaParameters();
57         schemaPars.getSchemaHelperParameterMap().put("Java", javaSchemaHelperPars);
58
59         ParameterService.register(schemaPars);
60     }
61
62     @AfterClass
63     public static void deregisterParameters() {
64         ParameterService.clear();
65     }
66
67     @Test
68     public void testJavaSchemaHelperInit() {
69         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
70         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
71
72         AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
73
74         assertThatThrownBy(() -> new JavaSchemaHelper().init(userKey, badJavaTypeSchema))
75             .hasMessage("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
76                     + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM");
77         AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
78
79         assertThatThrownBy(() -> new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema))
80             .hasMessage("UserKey:0.0.1: class/type short for context schema "
81                     + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
82                     + " Use the appropriate Java boxing type instead.");
83     }
84
85     @Test
86     public void testJavaSchemaHelperMethods() {
87         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
88
89         assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
90         assertEquals(null, intSchemaHelper.getSchema());
91         assertEquals(null, intSchemaHelper.getSchemaClass());
92         assertEquals(null, intSchemaHelper.getSchemaObject());
93
94         assertThatThrownBy(intSchemaHelper::createNewInstance)
95             .hasMessage("NULL:0.0.0: could not create an instance, schema class for the schema is null");
96         assertThatThrownBy(() -> intSchemaHelper.createNewInstance(Float.parseFloat("1.23")))
97             .hasMessage("NULL:0.0.0: could not create an instance, schema class for the schema is null");
98         assertThatThrownBy(() -> intSchemaHelper.createNewInstance("hello"))
99             .hasMessage("NULL:0.0.0: could not create an instance, schema class for the schema is null");
100         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
101         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
102         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
103
104         intSchemaHelper.init(userKey, intSchema);
105         assertEquals(userKey, intSchemaHelper.getUserKey());
106         assertEquals(intSchema, intSchemaHelper.getSchema());
107         assertEquals(Integer.class, intSchemaHelper.getSchemaClass());
108         assertEquals(null, intSchemaHelper.getSchemaObject());
109
110         assertThatThrownBy(intSchemaHelper::createNewInstance)
111             .hasMessage("UserKey:0.0.1: could not create an instance of class "
112                     + "\"java.lang.Integer\" using the default constructor \"Integer()\"");
113         assertThatThrownBy(() -> intSchemaHelper.createNewInstance(Float.parseFloat("1.23")))
114             .hasMessage("UserKey:0.0.1: the object \"1.23\" of type "
115                     + "\"java.lang.Float\" is not an instance of JsonObject and is not "
116                     + "assignable to \"java.lang.Integer\"");
117         assertThatThrownBy(() -> intSchemaHelper.createNewInstance("hello"))
118             .hasMessage("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
119                     + "using the string constructor \"Integer(String)\"");
120         JsonElement jsonIntElement = null;
121         assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
122
123         jsonIntElement = JsonParser.parseString("123");
124
125         assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
126         assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
127
128         assertEquals(null, intSchemaHelper.unmarshal(null));
129         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
130         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
131         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
132         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
133         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
134         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
135     }
136
137     @Test
138     public void testJavaSchemaHelperUnmarshal() {
139         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
140         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
141
142         AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte");
143         AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short");
144         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
145
146         SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
147         SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
148         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
149
150         byteSchemaHelper.init(userKey, byteSchema);
151         shortSchemaHelper.init(userKey, shortSchema);
152         intSchemaHelper.init(userKey, intSchema);
153
154         SchemaHelper longSchemaHelper = new JavaSchemaHelper();
155         SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
156
157         AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long");
158         AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float");
159         longSchemaHelper.init(userKey, longSchema);
160         floatSchemaHelper.init(userKey, floatSchema);
161
162         SchemaHelper doubleSchemaHelper = new JavaSchemaHelper();
163         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
164         AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double");
165         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
166         doubleSchemaHelper.init(userKey, doubleSchema);
167         stringSchemaHelper.init(userKey, stringSchema);
168
169         AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java",
170                 "org.onap.policy.apex.context.impl.schema.java.SupportBaseClass");
171         SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
172         myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
173
174         assertEquals(null, byteSchemaHelper.unmarshal(null));
175         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal("123"));
176         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123")));
177         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123")));
178         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Short.parseShort("123")));
179         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Long.parseLong("123")));
180         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123")));
181         assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123")));
182         assertThatThrownBy(() -> byteSchemaHelper.unmarshal("one two three"))
183             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
184                     + "compatible with class \"java.lang.Byte\"");
185         assertEquals(null, shortSchemaHelper.unmarshal(null));
186         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal("123"));
187         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123")));
188         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123")));
189         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Short.parseShort("123")));
190         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Long.parseLong("123")));
191         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123")));
192         assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123")));
193         assertThatThrownBy(() -> shortSchemaHelper.unmarshal("one two three"))
194             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
195                     + "compatible with class \"java.lang.Short\"");
196         assertEquals(null, intSchemaHelper.unmarshal(null));
197         assertEquals(123, intSchemaHelper.unmarshal("123"));
198         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
199         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
200         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
201         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
202         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
203         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
204         assertThatThrownBy(() -> intSchemaHelper.unmarshal("one two three"))
205             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
206                     + "compatible with class \"java.lang.Integer\"");
207         assertEquals(null, longSchemaHelper.unmarshal(null));
208         assertEquals(123L, longSchemaHelper.unmarshal("123"));
209         assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123")));
210         assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123")));
211         assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123")));
212         assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123")));
213         assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123")));
214         assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123")));
215         assertThatThrownBy(() -> longSchemaHelper.unmarshal("one two three"))
216             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
217                     + "compatible with class \"java.lang.Long\"");
218         assertEquals(null, floatSchemaHelper.unmarshal(null));
219         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal("123"));
220         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123")));
221         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123")));
222         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Short.parseShort("123")));
223         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Long.parseLong("123")));
224         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123")));
225         assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123")));
226         assertThatThrownBy(() -> floatSchemaHelper.unmarshal("one two three"))
227             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
228                     + "compatible with class \"java.lang.Float\"");
229         assertEquals(null, doubleSchemaHelper.unmarshal(null));
230         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal("123"));
231         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123")));
232         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123")));
233         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123")));
234         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123")));
235         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123")));
236         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123")));
237         assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123)));
238         assertThatThrownBy(() -> doubleSchemaHelper.unmarshal("one two three"))
239             .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
240                     + "compatible with class \"java.lang.Double\"");
241         assertEquals("123", stringSchemaHelper.unmarshal(123));
242
243         SupportSubClass subClassInstance = new SupportSubClass("123");
244         assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
245     }
246
247     @Test
248     public void testJavaSchemaHelperMarshal() {
249         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
250         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
251
252         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
253         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
254         intSchemaHelper.init(userKey, intSchema);
255
256         assertEquals("null", intSchemaHelper.marshal2String(null));
257         assertEquals("123", intSchemaHelper.marshal2String(123));
258         assertThatThrownBy(() -> intSchemaHelper.marshal2String(123.45))
259             .hasMessage("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
260                     + "compatible with class \"java.lang.Integer\"");
261         JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
262         assertEquals(123, intJsonPrimitive.getAsInt());
263     }
264
265     @Test
266     public void testJavaSchemaHelperAdapters() {
267         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
268         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
269
270         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
271         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
272         stringSchemaHelper.init(userKey, stringSchema);
273
274         assertEquals("null", stringSchemaHelper.marshal2String(null));
275         assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello"));
276         assertThatThrownBy(() -> stringSchemaHelper.marshal2String(Instant.ofEpochMilli(1000)))
277             .hasMessage("UserKey:0.0.1: object \"1970-01-01T00:00:01Z\" of class \"java.time.Instant\" "
278                     + "not compatible with class \"java.lang.String\"");
279         JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String");
280         assertEquals("Another String", stringJsonPrimitive.getAsString());
281     }
282
283     @Test
284     public void testJavaSchemaHelperBadAdapter() {
285         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
286         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
287
288         SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
289
290         JavaSchemaHelperParameters javaShPars =
291                 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
292         javaShPars.getJsonAdapters().get("String")
293                 .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter");
294
295         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
296         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
297         stringSchemaHelper.init(userKey, stringSchema);
298
299         assertThatThrownBy(() -> stringSchemaHelper.marshal2String("Hello"))
300             .hasMessage("UserKey:0.0.1: instantiation of adapter class "
301                     + "\"org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter\"  to decode and encode "
302                     + "class \"java.lang.String\" failed: null");
303     }
304
305     @Test
306     public void testJavaSchemaHelperDefaultAdapter() {
307         SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
308
309         JavaSchemaHelperParameters javaShPars =
310                 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
311
312         pars.getSchemaHelperParameterMap().clear();
313
314         testJavaSchemaHelperAdapters();
315
316         pars.getSchemaHelperParameterMap().put("Java", javaShPars);
317     }
318 }