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