2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.schema.java;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonPrimitive;
30 import java.math.BigDecimal;
32 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextRuntimeException;
35 import org.onap.policy.apex.context.SchemaHelper;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 public class JavaSchemaHelperTest {
43 public void testJavaSchemaHelperInit() {
44 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
45 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
47 AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
50 new JavaSchemaHelper().init(userKey, badJavaTypeSchema);
51 fail("test should throw an exception here");
52 } catch (ContextRuntimeException e) {
53 assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
54 + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
57 AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
60 new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema);
61 fail("test should throw an exception here");
62 } catch (ContextRuntimeException e) {
63 assertEquals("UserKey:0.0.1: class/type short for context schema "
64 + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
65 + " Use the appropriate Java boxing type instead.", e.getMessage());
70 public void testJavaSchemaHelperMethods() {
71 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
73 assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
74 assertEquals(null, intSchemaHelper.getSchema());
75 assertEquals(null, intSchemaHelper.getSchemaClass());
76 assertEquals(null, intSchemaHelper.getSchemaObject());
79 intSchemaHelper.createNewInstance();
80 fail("test should throw an exception here");
81 } catch (ContextRuntimeException e) {
82 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
87 intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
88 fail("test should throw an exception here");
89 } catch (ContextRuntimeException e) {
90 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
95 intSchemaHelper.createNewInstance("hello");
96 fail("test should throw an exception here");
97 } catch (ContextRuntimeException e) {
98 assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
102 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
103 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
104 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
106 intSchemaHelper.init(userKey, intSchema);
107 assertEquals(userKey, intSchemaHelper.getUserKey());
108 assertEquals(intSchema, intSchemaHelper.getSchema());
109 assertEquals(Integer.class, intSchemaHelper.getSchemaClass());
110 assertEquals(null, intSchemaHelper.getSchemaObject());
113 intSchemaHelper.createNewInstance();
114 fail("test should throw an exception here");
115 } catch (ContextRuntimeException e) {
116 assertEquals("UserKey:0.0.1: could not create an instance of class "
117 + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
121 intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
122 fail("test should throw an exception here");
123 } catch (ContextRuntimeException e) {
124 assertEquals("UserKey:0.0.1: the object \"1.23\" of type "
125 + "\"java.lang.Float\" is not an instance of JsonObject and is not "
126 + "assignable to \"java.lang.Integer\"", e.getMessage());
130 intSchemaHelper.createNewInstance("hello");
131 fail("test should throw an exception here");
132 } catch (ContextRuntimeException e) {
133 assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
134 + "using the string constructor \"Integer(String)\"", e.getMessage());
137 JsonElement jsonIntElement = null;
138 assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
140 jsonIntElement = new JsonParser().parse("123");
142 assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
143 assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
145 assertEquals(null, intSchemaHelper.unmarshal(null));
146 assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
147 assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
148 assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
149 assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
150 assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
151 assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
155 public void testJavaSchemaHelperUnmarshal() {
156 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
157 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
159 AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte");
160 AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short");
161 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
163 SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
164 SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
165 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
167 byteSchemaHelper.init(userKey, byteSchema);
168 shortSchemaHelper.init(userKey, shortSchema);
169 intSchemaHelper.init(userKey, intSchema);
171 SchemaHelper longSchemaHelper = new JavaSchemaHelper();
172 SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
174 AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long");
175 AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float");
176 longSchemaHelper.init(userKey, longSchema);
177 floatSchemaHelper.init(userKey, floatSchema);
179 SchemaHelper doubleSchemaHelper = new JavaSchemaHelper();
180 SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
181 AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double");
182 AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
183 doubleSchemaHelper.init(userKey, doubleSchema);
184 stringSchemaHelper.init(userKey, stringSchema);
186 AxContextSchema myBaseClassSchema =
187 new AxContextSchema(schemaKey, "Java", "org.onap.policy.apex.context.impl.schema.java.MyBaseClass");
188 SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
189 myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
191 assertEquals(null, byteSchemaHelper.unmarshal(null));
192 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal("123"));
193 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123")));
194 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123")));
195 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Short.parseShort("123")));
196 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Long.parseLong("123")));
197 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123")));
198 assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123")));
200 byteSchemaHelper.unmarshal("one two three");
201 fail("test should throw an exception here");
202 } catch (ContextRuntimeException e) {
203 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
204 + "compatible with class \"java.lang.Byte\"", e.getMessage());
207 assertEquals(null, shortSchemaHelper.unmarshal(null));
208 assertEquals(new Short("123"), shortSchemaHelper.unmarshal("123"));
209 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123")));
210 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123")));
211 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Short.parseShort("123")));
212 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Long.parseLong("123")));
213 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123")));
214 assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123")));
216 shortSchemaHelper.unmarshal("one two three");
217 fail("test should throw an exception here");
218 } catch (ContextRuntimeException e) {
219 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
220 + "compatible with class \"java.lang.Short\"", e.getMessage());
223 assertEquals(null, intSchemaHelper.unmarshal(null));
224 assertEquals(123, intSchemaHelper.unmarshal("123"));
225 assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
226 assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
227 assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
228 assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
229 assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
230 assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
232 intSchemaHelper.unmarshal("one two three");
233 fail("test should throw an exception here");
234 } catch (ContextRuntimeException e) {
235 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
236 + "compatible with class \"java.lang.Integer\"", e.getMessage());
239 assertEquals(null, longSchemaHelper.unmarshal(null));
240 assertEquals(123L, longSchemaHelper.unmarshal("123"));
241 assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123")));
242 assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123")));
243 assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123")));
244 assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123")));
245 assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123")));
246 assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123")));
248 longSchemaHelper.unmarshal("one two three");
249 fail("test should throw an exception here");
250 } catch (ContextRuntimeException e) {
251 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
252 + "compatible with class \"java.lang.Long\"", e.getMessage());
255 assertEquals(null, floatSchemaHelper.unmarshal(null));
256 assertEquals(new Float("123"), floatSchemaHelper.unmarshal("123"));
257 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123")));
258 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123")));
259 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Short.parseShort("123")));
260 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Long.parseLong("123")));
261 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123")));
262 assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123")));
264 floatSchemaHelper.unmarshal("one two three");
265 fail("test should throw an exception here");
266 } catch (ContextRuntimeException e) {
267 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
268 + "compatible with class \"java.lang.Float\"", e.getMessage());
271 assertEquals(null, doubleSchemaHelper.unmarshal(null));
272 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal("123"));
273 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123")));
274 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123")));
275 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123")));
276 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123")));
277 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123")));
278 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123")));
279 assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123)));
281 doubleSchemaHelper.unmarshal("one two three");
282 fail("test should throw an exception here");
283 } catch (ContextRuntimeException e) {
284 assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
285 + "compatible with class \"java.lang.Double\"", e.getMessage());
288 assertEquals("123", stringSchemaHelper.unmarshal(123));
290 MySubClass subClassInstance = new MySubClass("123");
291 assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
295 public void testJavaSchemaHelperMarshal() {
296 AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
297 AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
299 AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
300 SchemaHelper intSchemaHelper = new JavaSchemaHelper();
301 intSchemaHelper.init(userKey, intSchema);
303 assertEquals("null", intSchemaHelper.marshal2String(null));
304 assertEquals("123", intSchemaHelper.marshal2String(123));
306 intSchemaHelper.marshal2String(123.45);
307 fail("test should throw an exception here");
308 } catch (ContextRuntimeException e) {
309 assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
310 + "compatible with class \"java.lang.Integer\"", e.getMessage());
313 JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
314 assertEquals(123, intJsonPrimitive.getAsInt());