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