e3d7e3aa5b99a956cdf7aa2ba3414e0bbd5e9f83
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.participant.policy.main.parameters;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import javax.ws.rs.core.Response;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
33 import org.onap.policy.common.endpoints.parameters.TopicParameters;
34 import org.onap.policy.common.parameters.ParameterGroup;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 /**
41  * Class to hold/create all parameters for test cases.
42  */
43 public class CommonTestData {
44     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
45     public static final String DESCRIPTION = "Participant description";
46     public static final long TIME_INTERVAL = 2000;
47     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
48
49     public static final Coder coder = new StandardCoder();
50
51     /**
52      * Converts the contents of a map to a parameter class.
53      *
54      * @param source property map
55      * @param clazz class of object to be created from the map
56      * @return a new object represented by the map
57      */
58     public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
59         try {
60             return coder.convert(source, clazz);
61         } catch (final CoderException e) {
62             throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
63         }
64     }
65
66     /**
67      * Returns a property map for a ParticipantPolicyParameters map for test cases.
68      *
69      * @param name name of the parameters
70      *
71      * @return a property map suitable for constructing an object
72      */
73     public Map<String, Object> getParticipantPolicyParametersMap(final String name) {
74         final Map<String, Object> map = new TreeMap<>();
75
76         map.put("name", name);
77         map.put("intermediaryParameters", getIntermediaryParametersMap(false));
78         map.put("databaseProviderParameters", getDatabaseProviderParametersMap(false));
79         return map;
80     }
81
82     /**
83      * Returns a property map for a databaseProviderParameters map for test cases.
84      *
85      * @param isEmpty boolean value to represent that object created should be empty or not
86      * @return a property map suitable for constructing an object
87      */
88     public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
89         final Map<String, Object> map = new TreeMap<>();
90         if (!isEmpty) {
91             map.put("name", "PolicyProviderParameterGroup");
92             map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
93             map.put("databaseDriver", "org.h2.Driver");
94             map.put("databaseUrl", "jdbc:h2:mem:testdb");
95             map.put("databaseUser", "policy");
96             map.put("databasePassword", "P01icY");
97             map.put("persistenceUnit", "ToscaConceptTest");
98         }
99
100         return map;
101     }
102
103     /**
104      * Returns a property map for a intermediaryParameters map for test cases.
105      *
106      * @param isEmpty boolean value to represent that object created should be empty or not
107      * @return a property map suitable for constructing an object
108      */
109     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
110         final Map<String, Object> map = new TreeMap<>();
111         if (!isEmpty) {
112             map.put("name", "Participant parameters");
113             map.put("reportingTimeInterval", TIME_INTERVAL);
114             map.put("description", DESCRIPTION);
115             map.put("participantId", getParticipantId());
116             map.put("participantType", getParticipantId());
117             map.put("clampControlLoopTopics", getTopicParametersMap(false));
118         }
119
120         return map;
121     }
122
123     /**
124      * Returns a property map for a TopicParameters map for test cases.
125      *
126      * @param isEmpty boolean value to represent that object created should be empty or not
127      * @return a property map suitable for constructing an object
128      */
129     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
130         final Map<String, Object> map = new TreeMap<>();
131         if (!isEmpty) {
132             map.put("topicSources", TOPIC_PARAMS);
133             map.put("topicSinks", TOPIC_PARAMS);
134         }
135         return map;
136     }
137
138     /**
139      * Returns topic parameters for test cases.
140      *
141      * @return topic parameters
142      */
143     public static TopicParameters getTopicParams() {
144         final TopicParameters topicParams = new TopicParameters();
145         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
146         topicParams.setTopicCommInfrastructure("dmaap");
147         topicParams.setServers(Arrays.asList("localhost"));
148         return topicParams;
149     }
150
151     /**
152      * Returns participantId for test cases.
153      *
154      * @return participant Id
155      */
156     public static ToscaConceptIdentifier getParticipantId() {
157         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
158         return participantId;
159     }
160
161     /**
162      * Gets the standard participant parameters.
163      *
164      * @param port port to be inserted into the parameters
165      * @return the standard participant parameters
166      */
167     public ParticipantPolicyParameters getParticipantPolicyParameters(int port) {
168         try {
169             return coder.decode(getParticipantPolicyParametersAsString(port), ParticipantPolicyParameters.class);
170
171         } catch (CoderException e) {
172             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
173                     "cannot read participant parameters", e);
174         }
175     }
176
177     /**
178      * Gets the standard participant parameters, as a String.
179      *
180      * @param port port to be inserted into the parameters
181      * @return the standard participant parameters
182      */
183     public static String getParticipantPolicyParametersAsString(int port) {
184
185         try {
186             File file = new File(getParamFile());
187             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
188
189             json = json.replace("${port}", String.valueOf(port));
190             json = json.replace("${dbName}", "jdbc:h2:mem:testdb");
191
192             return json;
193
194         } catch (IOException e) {
195             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE,
196                     "cannot read participant parameters", e);
197
198         }
199     }
200
201     /**
202      * Gets the full path to the parameter file, which may vary depending on whether or
203      * not this is an end-to-end test.
204      *
205      * @return the parameter file name
206      */
207     private static String getParamFile() {
208         String paramFile = "src/test/resources/parameters/TestParametersStd.json";
209         return paramFile;
210     }
211
212     /**
213      * Nulls out a field within a JSON string.
214      * @param json JSON string
215      * @param field field to be nulled out
216      * @return a new JSON string with the field nulled out
217      */
218     public String nullifyField(String json, String field) {
219         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
220     }
221 }