fed79e7133ce27852ec71274db90486d0a602f1a
[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.BeforeClass;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.ContextRuntimeException;
30 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
31 import org.onap.policy.apex.context.parameters.SchemaParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
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 public class SchemaHelperFactoryTest {
38     private static AxContextSchema intSchema;
39     private static AxContextSchemas schemas;
40     private static AxContextSchema badSchema;
41
42     /**
43      * Set ups schema for the test.
44      */
45     @BeforeClass
46     public static void setupSchema() {
47         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
48         ModelService.registerModel(AxContextSchemas.class, schemas);
49
50         intSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
51         badSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Bad");
52     }
53
54     @Test
55     public void testSchemaHelperFactory() {
56         try {
57             new SchemaHelperFactory().createSchemaHelper(null, null);
58             fail("this test should throw an exception");
59         } catch (IllegalArgumentException e) {
60             assertEquals("Parameter \"owningEntityKey\" may not be null", e.getMessage());
61         }
62
63         AxArtifactKey ownerKey = new AxArtifactKey("Owner", "0.0.1");
64         try {
65             new SchemaHelperFactory().createSchemaHelper(ownerKey, null);
66             fail("this test should throw an exception");
67         } catch (IllegalArgumentException e) {
68             assertEquals("Parameter \"schemaKey\" may not be null", e.getMessage());
69         }
70
71         try {
72             new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey());
73             fail("this test should throw an exception");
74         } catch (ContextRuntimeException e) {
75             assertEquals("schema \"IntSchema:0.0.1\" for entity Owner:0.0.1 does not exist", e.getMessage());
76         }
77
78         schemas.getSchemasMap().put(intSchema.getKey(), intSchema);
79         new SchemaParameters();
80         try {
81             new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey());
82             fail("this test should throw an exception");
83         } catch (ContextRuntimeException e) {
84             assertEquals("context schema helper parameters not found for context schema  \"JAVA\"", e.getMessage());
85         }
86
87         new SchemaParameters().getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
88         assertNotNull(new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()));
89
90         schemas.getSchemasMap().put(intSchema.getKey(), badSchema);
91         try {
92             new SchemaHelperFactory().createSchemaHelper(ownerKey, badSchema.getKey());
93             fail("this test should throw an exception");
94         } catch (ContextRuntimeException e) {
95             assertEquals("Owner:0.0.1: class/type java.lang.Bad for context schema \"IntSchema:0.0.1\" "
96                             + "not found. Check the class path of the JVM", e.getMessage());
97         }
98     }
99 }