663424411f775baa3e0c0f63bfceb3824b6ea04f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.schema;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertNotNull;
26
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.SchemaParameters;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.service.ModelService;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
37 import org.onap.policy.common.parameters.ParameterService;
38
39 public class SchemaHelperFactoryTest {
40     private static AxContextSchema intSchema;
41     private static AxContextSchemas schemas;
42     private static AxContextSchema badSchema;
43
44     /**
45      * Set ups schema for the test.
46      */
47     @BeforeClass
48     public static void setupSchema() {
49         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
50         ModelService.registerModel(AxContextSchemas.class, schemas);
51
52         intSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
53         badSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Bad");
54     }
55
56     @AfterClass
57     public static void clearParameters() {
58         ParameterService.clear();
59     }
60
61     @Test
62     public void testSchemaHelperFactory() {
63         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(null, null))
64             .hasMessage("Parameter \"owningEntityKey\" may not be null");
65         AxArtifactKey ownerKey = new AxArtifactKey("Owner", "0.0.1");
66         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(ownerKey, null))
67             .hasMessage("Parameter \"schemaKey\" may not be null");
68         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()))
69             .hasMessage("schema \"IntSchema:0.0.1\" for entity Owner:0.0.1 does not exist");
70         schemas.getSchemasMap().put(intSchema.getKey(), intSchema);
71         SchemaParameters schemaParameters0 = new SchemaParameters();
72         schemaParameters0.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
73         ParameterService.register(schemaParameters0);
74         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()))
75             .hasMessage("context schema helper parameters not found for context schema  \"JAVA\"");
76         ParameterService.deregister(schemaParameters0);
77
78         SchemaParameters schemaParameters1 = new SchemaParameters();
79         schemaParameters1.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
80         ParameterService.register(schemaParameters1);
81         schemaParameters1.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
82         assertNotNull(new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()));
83
84         schemas.getSchemasMap().put(intSchema.getKey(), badSchema);
85         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(ownerKey, badSchema.getKey()))
86             .hasMessage("Owner:0.0.1: class/type java.lang.Bad for context schema \"IntSchema:0.0.1\" "
87                             + "not found. Check the class path of the JVM");
88     }
89 }