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;
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;
43 public class JavaSchemaHelperTest {
45 * Initialize JSON adapters.
48 public static void registerParameters() {
49 JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters();
51 JavaSchemaHelperJsonAdapterParameters stringAdapterPars = new JavaSchemaHelperJsonAdapterParameters();
52 stringAdapterPars.setAdaptedClass("java.lang.String");
53 stringAdapterPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportJsonAdapter");
55 javaSchemaHelperPars.getJsonAdapters().put("String", stringAdapterPars);
57 SchemaParameters schemaPars = new SchemaParameters();
58 schemaPars.getSchemaHelperParameterMap().put("Java", javaSchemaHelperPars);
60 ParameterService.register(schemaPars);
64 public static void deregisterParameters() {
65 ParameterService.clear();
69 public void testJavaSchemaHelperInit() {
70 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
71 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
73 AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
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());
83 AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
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());
96 public void testJavaSchemaHelperMethods() {
97 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
99 assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
100 assertEquals(null, intSchemaHelper.getSchema());
101 assertEquals(null, intSchemaHelper.getSchemaClass());
102 assertEquals(null, intSchemaHelper.getSchemaObject());
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",
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",
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",
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");
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());
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());
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());
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());
163 JsonElement jsonIntElement = null;
164 assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
166 jsonIntElement = JsonParser.parseString("123");
168 assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
169 assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
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")));
181 public void testJavaSchemaHelperUnmarshal() {
182 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
183 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
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");
189 SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
190 SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
191 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
193 byteSchemaHelper.init(userKey, byteSchema);
194 shortSchemaHelper.init(userKey, shortSchema);
195 intSchemaHelper.init(userKey, intSchema);
197 SchemaHelper longSchemaHelper = new JavaSchemaHelper();
198 SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
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);
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);
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);
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")));
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());
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")));
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());
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")));
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());
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")));
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());
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")));
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());
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)));
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());
314 assertEquals("123", stringSchemaHelper.unmarshal(123));
316 SupportSubClass subClassInstance = new SupportSubClass("123");
317 assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
321 public void testJavaSchemaHelperMarshal() {
322 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
323 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
325 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
326 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
327 intSchemaHelper.init(userKey, intSchema);
329 assertEquals("null", intSchemaHelper.marshal2String(null));
330 assertEquals("123", intSchemaHelper.marshal2String(123));
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());
339 JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
340 assertEquals(123, intJsonPrimitive.getAsInt());
344 public void testJavaSchemaHelperAdapters() {
345 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
346 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
348 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
349 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
350 stringSchemaHelper.init(userKey, stringSchema);
352 assertEquals("null", stringSchemaHelper.marshal2String(null));
353 assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello"));
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());
362 JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String");
363 assertEquals("Another String", stringJsonPrimitive.getAsString());
367 public void testJavaSchemaHelperBadAdapter() {
368 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
369 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
371 SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
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");
378 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
379 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
380 stringSchemaHelper.init(userKey, stringSchema);
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());
393 public void testJavaSchemaHelperDefaultAdapter() {
394 SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
396 JavaSchemaHelperParameters javaShPars =
397 (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java");
399 pars.getSchemaHelperParameterMap().clear();
401 testJavaSchemaHelperAdapters();
403 pars.getSchemaHelperParameterMap().put("Java", javaShPars);