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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema.java;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonParser;
29 import com.google.gson.JsonPrimitive;
31 import java.math.BigDecimal;
32 import java.time.Instant;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.apex.context.ContextRuntimeException;
38 import org.onap.policy.apex.context.SchemaHelper;
39 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
40 import org.onap.policy.apex.context.parameters.SchemaParameters;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.common.parameters.ParameterService;
45 public class JavaSchemaHelperTest {
47 * Initialize JSON adapters.
50 public static void registerParameters() {
51 JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters();
53 JavaSchemaHelperJsonAdapterParameters stringAdapterPars = new JavaSchemaHelperJsonAdapterParameters();
54 stringAdapterPars.setAdaptedClass("java.lang.String");
55 stringAdapterPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter");
57 javaSchemaHelperPars.getJsonAdapters().put("String", stringAdapterPars);
59 SchemaParameters schemaPars = new SchemaParameters();
60 schemaPars.getSchemaHelperParameterMap().put("Java", javaSchemaHelperPars);
62 ParameterService.register(schemaPars);
66 public static void deregisterParameters() {
67 ParameterService.clear();
71 public void testJavaSchemaHelperInit() {
72 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
73 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
75 AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
78 new JavaSchemaHelper().init(userKey, badJavaTypeSchema);
79 fail("test should throw an exception here");
80 } catch (ContextRuntimeException e) {
81 assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
82 + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
85 AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
88 new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema);
89 fail("test should throw an exception here");
90 } catch (ContextRuntimeException e) {
91 assertEquals("UserKey:0.0.1: class/type short for context schema "
92 + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
93 + " Use the appropriate Java boxing type instead.", e.getMessage());
98 public void testJavaSchemaHelperMethods() {
99 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
101 assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
102 assertEquals(null, intSchemaHelper.getSchema());
103 assertEquals(null, intSchemaHelper.getSchemaClass());
104 assertEquals(null, intSchemaHelper.getSchemaObject());
107 intSchemaHelper.createNewInstance();
108 fail("test should throw an exception here");
109 } catch (ContextRuntimeException e) {
110 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
115 intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
116 fail("test should throw an exception here");
117 } catch (ContextRuntimeException e) {
118 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
123 intSchemaHelper.createNewInstance("hello");
124 fail("test should throw an exception here");
125 } catch (ContextRuntimeException e) {
126 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
130 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
131 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
132 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
134 intSchemaHelper.init(userKey, intSchema);
135 assertEquals(userKey, intSchemaHelper.getUserKey());
136 assertEquals(intSchema, intSchemaHelper.getSchema());
137 assertEquals(Integer.class, intSchemaHelper.getSchemaClass());
138 assertEquals(null, intSchemaHelper.getSchemaObject());
141 intSchemaHelper.createNewInstance();
142 fail("test should throw an exception here");
143 } catch (ContextRuntimeException e) {
144 assertEquals("UserKey:0.0.1: could not create an instance of class "
145 + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
149 intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
150 fail("test should throw an exception here");
151 } catch (ContextRuntimeException e) {
152 assertEquals("UserKey:0.0.1: the object \"1.23\" of type "
153 + "\"java.lang.Float\" is not an instance of JsonObject and is not "
154 + "assignable to \"java.lang.Integer\"", e.getMessage());
158 intSchemaHelper.createNewInstance("hello");
159 fail("test should throw an exception here");
160 } catch (ContextRuntimeException e) {
161 assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
162 + "using the string constructor \"Integer(String)\"", e.getMessage());
165 JsonElement jsonIntElement = null;
166 assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
168 jsonIntElement = JsonParser.parseString("123");
170 assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
171 assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
173 assertEquals(null, intSchemaHelper.unmarshal(null));
174 assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
175 assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
176 assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
177 assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
178 assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
179 assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
183 public void testJavaSchemaHelperUnmarshal() {
184 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
185 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
187 AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte");
188 AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short");
189 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
191 SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
192 SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
193 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
195 byteSchemaHelper.init(userKey, byteSchema);
196 shortSchemaHelper.init(userKey, shortSchema);
197 intSchemaHelper.init(userKey, intSchema);
199 SchemaHelper longSchemaHelper = new JavaSchemaHelper();
200 SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
202 AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long");
203 AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float");
204 longSchemaHelper.init(userKey, longSchema);
205 floatSchemaHelper.init(userKey, floatSchema);
207 SchemaHelper doubleSchemaHelper = new JavaSchemaHelper();
208 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
209 AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double");
210 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
211 doubleSchemaHelper.init(userKey, doubleSchema);
212 stringSchemaHelper.init(userKey, stringSchema);
214 AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java",
215 "org.onap.policy.apex.context.impl.schema.java.SupportBaseClass");
216 SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
217 myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
219 assertEquals(null, byteSchemaHelper.unmarshal(null));
220 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal("123"));
221 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123")));
222 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123")));
223 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Short.parseShort("123")));
224 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Long.parseLong("123")));
225 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123")));
226 assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123")));
228 byteSchemaHelper.unmarshal("one two three");
229 fail("test should throw an exception here");
230 } catch (ContextRuntimeException e) {
231 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
232 + "compatible with class \"java.lang.Byte\"", e.getMessage());
235 assertEquals(null, shortSchemaHelper.unmarshal(null));
236 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal("123"));
237 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123")));
238 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123")));
239 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Short.parseShort("123")));
240 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Long.parseLong("123")));
241 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123")));
242 assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123")));
244 shortSchemaHelper.unmarshal("one two three");
245 fail("test should throw an exception here");
246 } catch (ContextRuntimeException e) {
247 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
248 + "compatible with class \"java.lang.Short\"", e.getMessage());
251 assertEquals(null, intSchemaHelper.unmarshal(null));
252 assertEquals(123, intSchemaHelper.unmarshal("123"));
253 assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
254 assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
255 assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
256 assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
257 assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
258 assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
260 intSchemaHelper.unmarshal("one two three");
261 fail("test should throw an exception here");
262 } catch (ContextRuntimeException e) {
263 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
264 + "compatible with class \"java.lang.Integer\"", e.getMessage());
267 assertEquals(null, longSchemaHelper.unmarshal(null));
268 assertEquals(123L, longSchemaHelper.unmarshal("123"));
269 assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123")));
270 assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123")));
271 assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123")));
272 assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123")));
273 assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123")));
274 assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123")));
276 longSchemaHelper.unmarshal("one two three");
277 fail("test should throw an exception here");
278 } catch (ContextRuntimeException e) {
279 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
280 + "compatible with class \"java.lang.Long\"", e.getMessage());
283 assertEquals(null, floatSchemaHelper.unmarshal(null));
284 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal("123"));
285 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123")));
286 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123")));
287 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Short.parseShort("123")));
288 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Long.parseLong("123")));
289 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123")));
290 assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123")));
292 floatSchemaHelper.unmarshal("one two three");
293 fail("test should throw an exception here");
294 } catch (ContextRuntimeException e) {
295 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
296 + "compatible with class \"java.lang.Float\"", e.getMessage());
299 assertEquals(null, doubleSchemaHelper.unmarshal(null));
300 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal("123"));
301 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123")));
302 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123")));
303 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123")));
304 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123")));
305 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123")));
306 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123")));
307 assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123)));
309 doubleSchemaHelper.unmarshal("one two three");
310 fail("test should throw an exception here");
311 } catch (ContextRuntimeException e) {
312 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
313 + "compatible with class \"java.lang.Double\"", e.getMessage());
316 assertEquals("123", stringSchemaHelper.unmarshal(123));
318 SupportSubClass subClassInstance = new SupportSubClass("123");
319 assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
323 public void testJavaSchemaHelperMarshal() {
324 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
325 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
327 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
328 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
329 intSchemaHelper.init(userKey, intSchema);
331 assertEquals("null", intSchemaHelper.marshal2String(null));
332 assertEquals("123", intSchemaHelper.marshal2String(123));
334 intSchemaHelper.marshal2String(123.45);
335 fail("test should throw an exception here");
336 } catch (ContextRuntimeException e) {
337 assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
338 + "compatible with class \"java.lang.Integer\"", e.getMessage());
341 JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
342 assertEquals(123, intJsonPrimitive.getAsInt());
346 public void testJavaSchemaHelperAdapters() {
347 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
348 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
350 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
351 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
352 stringSchemaHelper.init(userKey, stringSchema);
354 assertEquals("null", stringSchemaHelper.marshal2String(null));
355 assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello"));
357 stringSchemaHelper.marshal2String(Instant.ofEpochMilli(1000));
358 fail("test should throw an exception here");
359 } catch (ContextRuntimeException e) {
360 assertEquals("UserKey:0.0.1: object \"1970-01-01T00:00:01Z\" of class \"java.time.Instant\" "
361 + "not compatible with class \"java.lang.String\"", e.getMessage());
364 JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String");
365 assertEquals("Another String", stringJsonPrimitive.getAsString());
369 public void testJavaSchemaHelperBadAdapter() {
370 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
371 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
373 SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
375 JavaSchemaHelperParameters javaShPars =
376 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
377 javaShPars.getJsonAdapters().get("String")
378 .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter");
380 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
381 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
382 stringSchemaHelper.init(userKey, stringSchema);
385 stringSchemaHelper.marshal2String("Hello");
386 fail("test should throw an exception");
387 } catch (ContextRuntimeException pre) {
388 assertEquals("UserKey:0.0.1: instantiation of adapter class "
389 + "\"org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter\" to decode and encode "
390 + "class \"java.lang.String\" failed: null", pre.getMessage());
395 public void testJavaSchemaHelperDefaultAdapter() {
396 SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
398 JavaSchemaHelperParameters javaShPars =
399 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
401 pars.getSchemaHelperParameterMap().clear();
403 testJavaSchemaHelperAdapters();
405 pars.getSchemaHelperParameterMap().put("Java", javaShPars);