Merge "Adding Junit for plugins-persistence-jpa-hibernate"
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.apex.context.impl.schema.java;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24
25 import java.math.BigDecimal;
26
27 import org.junit.Test;
28 import org.onap.policy.apex.context.ContextRuntimeException;
29 import org.onap.policy.apex.context.SchemaHelper;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
32
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonParser;
35 import com.google.gson.JsonPrimitive;
36
37 public class JavaSchemaHelperTest {
38
39     @Test
40     public void testJavaSchemaHelperInit() {
41         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
42         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
43
44         AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
45
46         try {
47             new JavaSchemaHelper().init(userKey, badJavaTypeSchema);
48             fail("test should throw an exception here");
49         } catch (ContextRuntimeException e) {
50             assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
51                             + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
52         }
53
54         AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
55
56         try {
57             new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema);
58             fail("test should throw an exception here");
59         } catch (ContextRuntimeException e) {
60             assertEquals("UserKey:0.0.1: class/type short for context schema "
61                             + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
62                             + " Use the appropriate Java boxing type instead.", e.getMessage());
63         }
64     }
65
66     @Test
67     public void testJavaSchemaHelperMethods() {
68         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
69         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
70
71         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
72
73         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
74
75         assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
76         assertEquals(null, intSchemaHelper.getSchema());
77         assertEquals(null, intSchemaHelper.getSchemaClass());
78         assertEquals(null, intSchemaHelper.getSchemaObject());
79
80         try {
81             intSchemaHelper.createNewInstance();
82             fail("test should throw an exception here");
83         } catch (ContextRuntimeException e) {
84             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
85                             e.getMessage());
86         }
87
88         try {
89             intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
90             fail("test should throw an exception here");
91         } catch (ContextRuntimeException e) {
92             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
93                             e.getMessage());
94         }
95
96         try {
97             intSchemaHelper.createNewInstance("hello");
98             fail("test should throw an exception here");
99         } catch (ContextRuntimeException e) {
100             assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
101                             e.getMessage());
102         }
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         try {
111             intSchemaHelper.createNewInstance();
112             fail("test should throw an exception here");
113         } catch (ContextRuntimeException e) {
114             assertEquals("UserKey:0.0.1: could not create an instance of class "
115                             + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
116         }
117
118         try {
119             intSchemaHelper.createNewInstance(Float.parseFloat("1.23"));
120             fail("test should throw an exception here");
121         } catch (ContextRuntimeException e) {
122             assertEquals("UserKey:0.0.1: the object \"1.23\" of type "
123                             + "\"java.lang.Float\" is not an instance of JsonObject and is not "
124                             + "assignable to \"java.lang.Integer\"", e.getMessage());
125         }
126
127         try {
128             intSchemaHelper.createNewInstance("hello");
129             fail("test should throw an exception here");
130         } catch (ContextRuntimeException e) {
131             assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
132                             + "using the string constructor \"Integer(String)\"", e.getMessage());
133         }
134
135         JsonElement jsonIntElement = null;
136         assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
137
138         jsonIntElement = new JsonParser().parse("123");
139
140         assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
141         assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
142
143         assertEquals(null, intSchemaHelper.unmarshal(null));
144         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
145         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
146         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
147         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
148         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
149         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
150     }
151
152     @Test
153     public void testJavaSchemaHelperUnmarshal() {
154         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
155         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
156
157         AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte");
158         AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short");
159         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
160         AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long");
161         AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float");
162         AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double");
163         AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
164         AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java",
165                         "org.onap.policy.apex.context.impl.schema.java.MyBaseClass");
166
167         SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
168         SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
169         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
170         SchemaHelper longSchemaHelper = new JavaSchemaHelper();
171         SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
172         SchemaHelper doubleSchemaHelper = new JavaSchemaHelper();
173         SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
174         SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
175
176         byteSchemaHelper.init(userKey, byteSchema);
177         shortSchemaHelper.init(userKey, shortSchema);
178         intSchemaHelper.init(userKey, intSchema);
179         longSchemaHelper.init(userKey, longSchema);
180         floatSchemaHelper.init(userKey, floatSchema);
181         doubleSchemaHelper.init(userKey, doubleSchema);
182         stringSchemaHelper.init(userKey, stringSchema);
183         myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
184
185         assertEquals(null, byteSchemaHelper.unmarshal(null));
186         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal("123"));
187         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123")));
188         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123")));
189         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Short.parseShort("123")));
190         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Long.parseLong("123")));
191         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123")));
192         assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123")));
193         try {
194             byteSchemaHelper.unmarshal("one two three");
195             fail("test should throw an exception here");
196         } catch (ContextRuntimeException e) {
197             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
198                             + "compatible with class \"java.lang.Byte\"", e.getMessage());
199         }
200
201         assertEquals(null, shortSchemaHelper.unmarshal(null));
202         assertEquals(new Short("123"), shortSchemaHelper.unmarshal("123"));
203         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123")));
204         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123")));
205         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Short.parseShort("123")));
206         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Long.parseLong("123")));
207         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123")));
208         assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123")));
209         try {
210             shortSchemaHelper.unmarshal("one two three");
211             fail("test should throw an exception here");
212         } catch (ContextRuntimeException e) {
213             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
214                             + "compatible with class \"java.lang.Short\"", e.getMessage());
215         }
216
217         assertEquals(null, intSchemaHelper.unmarshal(null));
218         assertEquals(123, intSchemaHelper.unmarshal("123"));
219         assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123")));
220         assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123")));
221         assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123")));
222         assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123")));
223         assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123")));
224         assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123")));
225         try {
226             intSchemaHelper.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.Integer\"", e.getMessage());
231         }
232
233         assertEquals(null, longSchemaHelper.unmarshal(null));
234         assertEquals(123L, longSchemaHelper.unmarshal("123"));
235         assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123")));
236         assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123")));
237         assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123")));
238         assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123")));
239         assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123")));
240         assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123")));
241         try {
242             longSchemaHelper.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.Long\"", e.getMessage());
247         }
248
249         assertEquals(null, floatSchemaHelper.unmarshal(null));
250         assertEquals(new Float("123"), floatSchemaHelper.unmarshal("123"));
251         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123")));
252         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123")));
253         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Short.parseShort("123")));
254         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Long.parseLong("123")));
255         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123")));
256         assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123")));
257         try {
258             floatSchemaHelper.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.Float\"", e.getMessage());
263         }
264
265         assertEquals(null, doubleSchemaHelper.unmarshal(null));
266         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal("123"));
267         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123")));
268         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123")));
269         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123")));
270         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123")));
271         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123")));
272         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123")));
273         assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123)));
274         try {
275             doubleSchemaHelper.unmarshal("one two three");
276             fail("test should throw an exception here");
277         } catch (ContextRuntimeException e) {
278             assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
279                             + "compatible with class \"java.lang.Double\"", e.getMessage());
280         }
281
282         assertEquals("123", stringSchemaHelper.unmarshal(123));
283
284         MySubClass subClassInstance = new MySubClass("123");
285         assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
286     }
287
288     @Test
289     public void testJavaSchemaHelperMarshal() {
290         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
291         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
292
293         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
294         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
295         intSchemaHelper.init(userKey, intSchema);
296
297         assertEquals("null", intSchemaHelper.marshal2String(null));
298         assertEquals("123", intSchemaHelper.marshal2String(123));
299         try {
300             intSchemaHelper.marshal2String(123.45);
301             fail("test should throw an exception here");
302         } catch (ContextRuntimeException e) {
303             assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
304                             + "compatible with class \"java.lang.Integer\"", e.getMessage());
305         }
306         
307         JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
308         assertEquals(123, intJsonPrimitive.getAsInt());
309     }
310 }