2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema.java;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.SchemaHelper;
31 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.common.parameters.ParameterService;
42 * JavaSchemaHelperInstanceCreationTest.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class JavaSchemaHelperInstanceCreationTest {
47 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48 private AxContextSchemas schemas;
51 * Set ups everything for the test.
54 public void initTest() {
55 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
56 ModelService.registerModel(AxContextSchemas.class, schemas);
58 final SchemaParameters schemaParameters = new SchemaParameters();
59 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
60 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
62 ParameterService.register(schemaParameters);
66 public static void cleanUpAfterTest() {
67 ParameterService.clear();
72 public void testNullEncoding() {
73 final AxContextSchema javaBooleanSchema =
74 new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean");
75 final AxContextSchema javaLongSchema =
76 new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long");
77 final AxContextSchema javaStringSchema =
78 new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String");
80 schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema);
81 schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema);
82 schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema);
84 final SchemaHelper schemaHelper0 =
85 new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey());
86 final SchemaHelper schemaHelper1 =
87 new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey());
88 final SchemaHelper schemaHelper2 =
89 new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey());
91 assertThatThrownBy(schemaHelper0::createNewInstance)
92 .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\""
93 + " using the default constructor \"Boolean()\"");
94 assertEquals(true, schemaHelper0.createNewInstance("true"));
96 assertThatThrownBy(schemaHelper1::createNewInstance)
97 .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\""
98 + " using the default constructor \"Long()\"");
99 assertEquals(65536L, schemaHelper1.createNewInstance("65536"));
101 assertEquals("", schemaHelper2.createNewInstance());
102 assertEquals("true", schemaHelper2.createNewInstance("true"));
104 assertThatThrownBy(() -> schemaHelper1.createNewSubInstance("SomeSubtype"))
105 .hasMessage("sub types are not supported on this schema helper");