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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.schema.java;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
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.ContextParameters;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.common.parameters.ParameterService;
43 * JavaSchemaHelperInstanceCreationTest.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class JavaSchemaHelperInstanceCreationTest {
48 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
49 private AxContextSchemas schemas;
52 * Set ups everything for the test.
55 public void initTest() {
56 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
57 ModelService.registerModel(AxContextSchemas.class, schemas);
59 final SchemaParameters schemaParameters = new SchemaParameters();
60 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
61 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
63 ParameterService.register(schemaParameters);
67 public static void cleanUpAfterTest() {
68 ParameterService.clear();
73 public void testNullEncoding() {
74 final AxContextSchema javaBooleanSchema =
75 new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean");
76 final AxContextSchema javaLongSchema =
77 new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long");
78 final AxContextSchema javaStringSchema =
79 new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String");
81 schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema);
82 schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema);
83 schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema);
85 final SchemaHelper schemaHelper0 =
86 new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey());
87 final SchemaHelper schemaHelper1 =
88 new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey());
89 final SchemaHelper schemaHelper2 =
90 new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey());
93 schemaHelper0.createNewInstance();
94 fail("this test should throw an exception here");
95 } catch (final Exception e) {
96 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" using the default"
97 + " constructor \"Boolean()\"", e.getMessage());
99 assertEquals(true, schemaHelper0.createNewInstance("true"));
103 schemaHelper1.createNewInstance();
104 fail("this test should throw an exception here");
105 } catch (final Exception e) {
106 assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" using the default "
107 + "constructor \"Long()\"", e.getMessage());
109 assertEquals(65536L, schemaHelper1.createNewInstance("65536"));
111 assertEquals("", schemaHelper2.createNewInstance());
112 assertEquals("true", schemaHelper2.createNewInstance("true"));