ce03678c9f3555191a67f92c6f3d760ec9bd6b9a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada. 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.plugins.context.schema.json;
22
23 import java.io.IOException;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.onap.policy.apex.context.SchemaHelper;
28 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
29 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
30 import org.onap.policy.apex.context.parameters.SchemaParameters;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
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 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.TextFileUtils;
39
40 public class CommonTestData {
41
42     protected static final String VERSION = "0.0.1";
43     protected static final String JSON = "JSON";
44     protected static final String TEST_ID = "testId";
45     protected final AxKey testKey = new AxArtifactKey("JsonTest", VERSION);
46     protected AxContextSchemas schemas;
47     protected final StandardCoder coder = new StandardCoder();
48
49     protected static final String BOOLEAN_SCHEMA =
50         "{\"$schema\": \"http://json-schema.org/draft-07/schema#\",\"type\": \"boolean\"}";
51     protected static final String BOOLEAN_DATA = "true";
52     protected static final String NULL_SCHEMA =
53         "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"null\"}";
54     protected static final String NULL_DATA = "null";
55     protected static String COMMONHEADERTYPE_DRAFT04;
56     protected static String COMMONHEADERTYPE_DRAFT07;
57     protected static String COMMONHEADERTYPE_WITH_OPTIONAL;
58     protected static String COMMONHEADER;
59     protected static String MEASUREMENTGROUPS_TYPE;
60     protected static String MEASUREMENTGROUPS;
61
62     /**
63      * Setup before all tests.
64      */
65     @BeforeClass
66     public static void setUpBeforeClass() throws IOException {
67         COMMONHEADERTYPE_DRAFT04 =
68             TextFileUtils.getTextFileAsString("src/test/resources/schema/commonHeaderType_draft04.json");
69         COMMONHEADERTYPE_DRAFT07 =
70             TextFileUtils.getTextFileAsString("src/test/resources/schema/commonHeaderType_draft07.json");
71         COMMONHEADERTYPE_WITH_OPTIONAL =
72             TextFileUtils.getTextFileAsString("src/test/resources/schema/commonHeaderTypeWithOptional.json");
73         COMMONHEADER =
74             TextFileUtils.getTextFileAsString("src/test/resources/data/commonHeader.json").replaceAll("\r", "").trim();
75         MEASUREMENTGROUPS_TYPE =
76             TextFileUtils.getTextFileAsString("src/test/resources/schema/measurementGroupsType.json");
77         MEASUREMENTGROUPS = TextFileUtils.getTextFileAsString("src/test/resources/data/measurementGroups.json")
78             .replaceAll("\r", "").trim();
79     }
80
81     /**
82      * Setup before test.
83      */
84     @Before
85     public void setUp() {
86         schemas = new AxContextSchemas(new AxArtifactKey("JsonSchema", VERSION));
87         ModelService.registerModel(AxContextSchemas.class, schemas);
88
89         SchemaParameters schemaParameters = new SchemaParameters();
90         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
91         schemaParameters.getSchemaHelperParameterMap().put(JSON, new JsonSchemaHelperParameters());
92         ParameterService.register(schemaParameters);
93     }
94
95     /**
96      * Teardown after test.
97      */
98     @After
99     public void tearDown() {
100         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
101         ModelService.clear();
102     }
103
104     /**
105      * Method to create JsonSchemaHelper instance from schema content.
106      *
107      * @param schema the schema content as string
108      * @return schemaHelper instance
109      */
110     protected SchemaHelper createSchema(String schema) {
111         final AxContextSchema jsonSchema = new AxContextSchema(new AxArtifactKey("SchemaTest", VERSION), JSON, schema);
112         schemas.getSchemasMap().put(jsonSchema.getKey(), jsonSchema);
113         return new SchemaHelperFactory().createSchemaHelper(testKey, jsonSchema.getKey());
114     }
115 }