aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/parameters/CommonTestData.java
blob: bcfaf8bb9003c8ae1976cade8823489ee9cc6640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.participant.dcae.main.parameters;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.core.Response;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
import org.onap.policy.common.endpoints.parameters.TopicParameters;
import org.onap.policy.common.parameters.ParameterGroup;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

/**
 * Class to hold/create all parameters for test cases.
 */
public class CommonTestData {
    public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
    public static final String DESCRIPTION = "Participant description";
    public static final long TIME_INTERVAL = 2000;
    public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
    private static final String REST_CLIENT_PASSWORD = "password";
    private static final String REST_CLIENT_USER = "admin";
    private static final int REST_CLAMP_PORT = 8443;
    private static final int REST_CONSUL_PORT = 31321;
    private static final String REST_CLAMP_HOST = "localhost";
    private static final String REST_CONSUL_HOST = "consul";
    private static final boolean REST_CLAMP_HTTPS = false;
    private static final boolean REST_CONSUL_HTTPS = false;
    private static final boolean REST_CLIENT_AAF = false;

    public static final Coder coder = new StandardCoder();

    /**
     * Converts the contents of a map to a parameter class.
     *
     * @param source property map
     * @param clazz class of object to be created from the map
     * @return a new object represented by the map
     */
    public <T extends ParameterGroup> T toObject(final Map<String, Object> source, final Class<T> clazz) {
        try {
            return coder.convert(source, clazz);

        } catch (final CoderException e) {
            throw new RuntimeException("cannot create " + clazz.getName() + " from map", e);
        }
    }

    /**
     * Returns a property map for a ApexStarterParameterGroup map for test cases.
     *
     * @param name name of the parameters
     *
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getParticipantParameterGroupMap(final String name) {
        final Map<String, Object> map = new TreeMap<>();

        map.put("name", name);
        map.put("clampClientParameters", getClampClientParametersMap(false));
        map.put("consulClientParameters", getConsulClientParametersMap(false));
        map.put("intermediaryParameters", getIntermediaryParametersMap(false));
        map.put("databaseProviderParameters", getDatabaseProviderParametersMap(false));
        return map;
    }

    /**
     * Returns a property map for a RestServerParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getClampClientParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        map.put("https", REST_CLAMP_HTTPS);
        map.put("aaf", REST_CLIENT_AAF);

        if (!isEmpty) {
            map.put("host", REST_CLAMP_HOST);
            map.put("port", REST_CLAMP_PORT);
            map.put("userName", REST_CLIENT_USER);
            map.put("password", REST_CLIENT_PASSWORD);
        }

        return map;
    }

    /**
     * Returns a property map for a RestServerParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getConsulClientParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        map.put("https", REST_CONSUL_HTTPS);
        map.put("aaf", REST_CLIENT_AAF);

        if (!isEmpty) {
            map.put("host", REST_CONSUL_HOST);
            map.put("port", REST_CONSUL_PORT);
            map.put("userName", REST_CLIENT_USER);
            map.put("password", REST_CLIENT_PASSWORD);
        }

        return map;
    }

    /**
     * Returns a property map for a databaseProviderParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        if (!isEmpty) {
            map.put("name", "PolicyProviderParameterGroup");
            map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
            map.put("databaseDriver", "org.h2.Driver");
            map.put("databaseUrl", "jdbc:h2:mem:testdb");
            map.put("databaseUser", "policy");
            map.put("databasePassword", "P01icY");
            map.put("persistenceUnit", "ToscaConceptTest");
        }

        return map;
    }

    /**
     * Returns a property map for a intermediaryParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        if (!isEmpty) {
            map.put("name", "Participant parameters");
            map.put("reportingTimeInterval", TIME_INTERVAL);
            map.put("description", DESCRIPTION);
            map.put("participantId", getParticipantId());
            map.put("participantType", getParticipantId());
            map.put("clampControlLoopTopics", getTopicParametersMap(false));
        }

        return map;
    }

    /**
     * Returns a property map for a TopicParameters map for test cases.
     *
     * @param isEmpty boolean value to represent that object created should be empty or not
     * @return a property map suitable for constructing an object
     */
    public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
        final Map<String, Object> map = new TreeMap<>();
        if (!isEmpty) {
            map.put("topicSources", TOPIC_PARAMS);
            map.put("topicSinks", TOPIC_PARAMS);
        }
        return map;
    }

    /**
     * Returns topic parameters for test cases.
     *
     * @return topic parameters
     */
    public static TopicParameters getTopicParams() {
        final TopicParameters topicParams = new TopicParameters();
        topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
        topicParams.setTopicCommInfrastructure("dmaap");
        topicParams.setServers(Arrays.asList("localhost"));
        return topicParams;
    }

    /**
     * Returns participantId for test cases.
     *
     * @return participant Id
     */
    public static ToscaConceptIdentifier getParticipantId() {
        final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
        participantId.setName("CDSParticipant0");
        participantId.setVersion("1.0.0");
        return participantId;
    }

    /**
     * Gets the standard participant parameters.
     *
     * @param port port to be inserted into the parameters
     * @return the standard participant parameters
     */
    public ParticipantDcaeParameters getParticipantParameterGroup(int port) {
        try {
            return coder.decode(getParticipantParameterGroupAsString(port), ParticipantDcaeParameters.class);

        } catch (CoderException e) {
            throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
                    e);
        }
    }

    /**
     * Gets the standard participant parameters, as a String.
     *
     * @param port port to be inserted into the parameters
     * @return the standard participant parameters
     */
    public static String getParticipantParameterGroupAsString(int port) {

        try {
            File file = new File(getParamFile());
            String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

            json = json.replace("${port}", String.valueOf(port));
            json = json.replace("${dbName}", "jdbc:h2:mem:testdb");

            return json;

        } catch (IOException e) {
            throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read participant parameters",
                    e);

        }
    }

    /**
     * Gets the full path to the parameter file, which may vary depending on whether or
     * not this is an end-to-end test.
     *
     * @return the parameter file name
     */
    private static String getParamFile() {
        return "src/test/resources/parameters/TestParametersStd.json";
    }

    /**
     * Nulls out a field within a JSON string.
     *
     * @param json JSON string
     * @param field field to be nulled out
     * @return a new JSON string with the field nulled out
     */
    public String nullifyField(String json, String field) {
        return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
    }

    /**
     * create Json response from getstatus call.
     *
     * @param status the status of Partecipant
     * @return the JSON
     */
    public static String createJsonStatus(String status) {
        try {
            File file = new File("src/test/resources/rest/status.json");
            String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
            return json.replace("${status}", status);

        } catch (IOException e) {
            throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);
        }
    }
}