14d44ee8bf9c784c562fa2d3362322f1fed23898
[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 org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.SchemaHelper;
30 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.SchemaParameters;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.basicmodel.service.ModelService;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38 import org.onap.policy.common.parameters.ParameterService;
39
40 /**
41  * JavaSchemaHelperInstanceCreationTest.
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  * @version
44  */
45 public class JavaSchemaHelperInstanceCreationTest {
46     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
47     private AxContextSchemas schemas;
48
49     /**
50      * Set ups everything for the test.
51      */
52     @Before
53     public void initTest() {
54         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
55         ModelService.registerModel(AxContextSchemas.class, schemas);
56
57         final SchemaParameters schemaParameters = new SchemaParameters();
58         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
59         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
60
61         ParameterService.register(schemaParameters);
62     }
63
64     @AfterClass
65     public static void cleanUpAfterTest() {
66         ParameterService.clear();
67     }
68
69
70     @Test
71     public void testNullEncoding() {
72         final AxContextSchema javaBooleanSchema =
73                 new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean");
74         final AxContextSchema javaLongSchema =
75                 new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long");
76         final AxContextSchema javaStringSchema =
77                 new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String");
78
79         schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema);
80         schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema);
81         schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema);
82
83         final SchemaHelper schemaHelper0 =
84                 new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey());
85         final SchemaHelper schemaHelper1 =
86                 new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey());
87         final SchemaHelper schemaHelper2 =
88                 new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey());
89
90         try {
91             schemaHelper0.createNewInstance();
92             fail("this test should throw an exception here");
93         } catch (final Exception e) {
94             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" using the default"
95                     + " constructor \"Boolean()\"", e.getMessage());
96         }
97         assertEquals(true, schemaHelper0.createNewInstance("true"));
98
99         try {
100             schemaHelper1.createNewInstance();
101             fail("this test should throw an exception here");
102         } catch (final Exception e) {
103             assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" using the default "
104                     + "constructor \"Long()\"", e.getMessage());
105         }
106         assertEquals(65536L, schemaHelper1.createNewInstance("65536"));
107
108         assertEquals("", schemaHelper2.createNewInstance());
109         assertEquals("true", schemaHelper2.createNewInstance("true"));
110
111         try {
112             schemaHelper1.createNewSubInstance("SomeSubtype");
113             fail("this test should throw an exception here");
114         } catch (final Exception e) {
115             assertEquals("sub types are not supported on this schema helper", e.getMessage());
116         }
117     }
118 }