8da6fdab948ede83bd4d37415fb49e439fa99c2b
[policy/apex-pdp.git] /
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
21 package org.onap.policy.apex.context.impl.schema.java;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonPrimitive;
29
30 import java.math.BigDecimal;
31
32 import org.junit.Test;
33
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;
38
39
40 public class JavaSchemaHelperTest {
41
42     @Test
43     public void testJavaSchemaHelperInit() {
44         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
45         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
46
47         AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish");
48
49         try {
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());
55         }
56
57         AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
58
59         try {
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());
66         }
67     }
68
69     @Test
70     public void testJavaSchemaHelperMethods() {
71         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
72
73         assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey());
74         assertEquals(null, intSchemaHelper.getSchema());
75         assertEquals(null, intSchemaHelper.getSchemaClass());
76         assertEquals(null, intSchemaHelper.getSchemaObject());
77
78         try {
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",
83                     e.getMessage());
84         }
85
86         try {
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",
91                     e.getMessage());
92         }
93
94         try {
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",
99                     e.getMessage());
100         }
101
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");
105
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());
111
112         try {
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());
118         }
119
120         try {
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());
127         }
128
129         try {
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());
135         }
136
137         JsonElement jsonIntElement = null;
138         assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement));
139
140         jsonIntElement = new JsonParser().parse("123");
141
142         assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement));
143         assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123")));
144
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")));
152     }
153
154     @Test
155     public void testJavaSchemaHelperUnmarshal() {
156         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
157         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
158
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");
162
163         SchemaHelper byteSchemaHelper = new JavaSchemaHelper();
164         SchemaHelper shortSchemaHelper = new JavaSchemaHelper();
165         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
166
167         byteSchemaHelper.init(userKey, byteSchema);
168         shortSchemaHelper.init(userKey, shortSchema);
169         intSchemaHelper.init(userKey, intSchema);
170
171         SchemaHelper longSchemaHelper = new JavaSchemaHelper();
172         SchemaHelper floatSchemaHelper = new JavaSchemaHelper();
173
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);
178
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);
185
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);
190
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")));
199         try {
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());
205         }
206
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")));
215         try {
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());
221         }
222
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")));
231         try {
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());
237         }
238
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")));
247         try {
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());
253         }
254
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")));
263         try {
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());
269         }
270
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)));
280         try {
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());
286         }
287
288         assertEquals("123", stringSchemaHelper.unmarshal(123));
289
290         MySubClass subClassInstance = new MySubClass("123");
291         assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance));
292     }
293
294     @Test
295     public void testJavaSchemaHelperMarshal() {
296         AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
297         AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
298
299         AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer");
300         SchemaHelper intSchemaHelper = new JavaSchemaHelper();
301         intSchemaHelper.init(userKey, intSchema);
302
303         assertEquals("null", intSchemaHelper.marshal2String(null));
304         assertEquals("123", intSchemaHelper.marshal2String(123));
305         try {
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());
311         }
312
313         JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
314         assertEquals(123, intJsonPrimitive.getAsInt());
315     }
316 }