7bf836c3c8057c580e505da6292c88a8272fc71d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
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.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 public class SchemaHelperFactoryTest {
41     private static AxContextSchema intSchema;
42     private static AxContextSchemas schemas;
43     private static AxContextSchema badSchema;
44
45     /**
46      * Set ups schema for the test.
47      */
48     @BeforeClass
49     public static void setupSchema() {
50         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
51         ModelService.registerModel(AxContextSchemas.class, schemas);
52
53         intSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
54         badSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Bad");
55     }
56
57     @AfterClass
58     public static void clearParameters() {
59         ParameterService.clear();
60     }
61
62     @Test
63     public void testSchemaHelperFactory() {
64         try {
65             new SchemaHelperFactory().createSchemaHelper(null, null);
66             fail("this test should throw an exception");
67         } catch (IllegalArgumentException e) {
68             assertEquals("Parameter \"owningEntityKey\" may not be null", e.getMessage());
69         }
70
71         AxArtifactKey ownerKey = new AxArtifactKey("Owner", "0.0.1");
72         try {
73             new SchemaHelperFactory().createSchemaHelper(ownerKey, null);
74             fail("this test should throw an exception");
75         } catch (IllegalArgumentException e) {
76             assertEquals("Parameter \"schemaKey\" may not be null", e.getMessage());
77         }
78
79         try {
80             new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey());
81             fail("this test should throw an exception");
82         } catch (ContextRuntimeException e) {
83             assertEquals("schema \"IntSchema:0.0.1\" for entity Owner:0.0.1 does not exist", e.getMessage());
84         }
85
86         schemas.getSchemasMap().put(intSchema.getKey(), intSchema);
87         SchemaParameters schemaParameters0 = new SchemaParameters();
88         schemaParameters0.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
89         ParameterService.register(schemaParameters0);
90         try {
91             new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey());
92             fail("this test should throw an exception");
93         } catch (ContextRuntimeException e) {
94             assertEquals("context schema helper parameters not found for context schema  \"JAVA\"", e.getMessage());
95         }
96         ParameterService.deregister(schemaParameters0);
97
98         SchemaParameters schemaParameters1 = new SchemaParameters();
99         schemaParameters1.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
100         ParameterService.register(schemaParameters1);
101         schemaParameters1.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
102         assertNotNull(new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()));
103
104         schemas.getSchemasMap().put(intSchema.getKey(), badSchema);
105         try {
106             new SchemaHelperFactory().createSchemaHelper(ownerKey, badSchema.getKey());
107             fail("this test should throw an exception");
108         } catch (ContextRuntimeException e) {
109             assertEquals("Owner:0.0.1: class/type java.lang.Bad for context schema \"IntSchema:0.0.1\" "
110                             + "not found. Check the class path of the JVM", e.getMessage());
111         }
112     }
113 }