a34c9cda5087e843b965022d6a96bf9d6e309c1c
[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.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 import org.onap.policy.common.endpoints.parameters.TopicParameters;
28 import org.onap.policy.common.utils.coder.Coder;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
32
33 /**
34  * Class to hold/create all parameters for test cases.
35  */
36 public class CommonTestData {
37     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
38     public static final String DESCRIPTION = "Participant description";
39     public static final long TIME_INTERVAL = 2000;
40     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
41
42     public static final Coder CODER = new StandardCoder();
43
44     /**
45      * Get ParticipantPolicyParameters.
46      *
47      * @return ParticipantPolicyParameters
48      */
49     public ParticipantPolicyParameters getParticipantPolicyParameters() {
50         try {
51             return CODER.convert(getParticipantPolicyParametersMap(PARTICIPANT_GROUP_NAME),
52                     ParticipantPolicyParameters.class);
53         } catch (final CoderException e) {
54             throw new RuntimeException("cannot create ParticipantPolicyParameters from map", e);
55         }
56     }
57
58     /**
59      * Returns a property map for a ParticipantPolicyParameters map for test cases.
60      *
61      * @param name name of the parameters
62      *
63      * @return a property map suitable for constructing an object
64      */
65     public Map<String, Object> getParticipantPolicyParametersMap(final String name) {
66         final Map<String, Object> map = new TreeMap<>();
67
68         map.put("name", name);
69         map.put("intermediaryParameters", getIntermediaryParametersMap(false));
70         map.put("policyApiParameters", getPolicyApiParametersMap());
71         return map;
72     }
73
74     /**
75      * Returns a property map for a policyApiParameters map for test cases.
76      *
77      * @return a property map suitable for constructing an object
78      */
79     public Map<String, Object> getPolicyApiParametersMap() {
80         final Map<String, Object> map = new TreeMap<>();
81         map.put("clientName", "api");
82         map.put("hostname", "localhost");
83         map.put("port", 6969);
84         map.put("userName", "healthcheck");
85         map.put("password", "zb!XztG34");
86         map.put("https", false);
87         map.put("allowSelfSignedCerts", true);
88
89         return map;
90     }
91
92     /**
93      * Returns a property map for a intermediaryParameters map for test cases.
94      *
95      * @param isEmpty boolean value to represent that object created should be empty or not
96      * @return a property map suitable for constructing an object
97      */
98     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
99         final Map<String, Object> map = new TreeMap<>();
100         if (!isEmpty) {
101             map.put("name", "Participant parameters");
102             map.put("reportingTimeIntervalMs", TIME_INTERVAL);
103             map.put("description", DESCRIPTION);
104             map.put("participantId", getParticipantId());
105             map.put("participantType", getParticipantId());
106             map.put("clampControlLoopTopics", getTopicParametersMap(false));
107         }
108
109         return map;
110     }
111
112     /**
113      * Returns a property map for a TopicParameters map for test cases.
114      *
115      * @param isEmpty boolean value to represent that object created should be empty or not
116      * @return a property map suitable for constructing an object
117      */
118     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
119         final Map<String, Object> map = new TreeMap<>();
120         if (!isEmpty) {
121             map.put("topicSources", TOPIC_PARAMS);
122             map.put("topicSinks", TOPIC_PARAMS);
123         }
124         return map;
125     }
126
127     /**
128      * Returns topic parameters for test cases.
129      *
130      * @return topic parameters
131      */
132     public static TopicParameters getTopicParams() {
133         final TopicParameters topicParams = new TopicParameters();
134         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
135         topicParams.setTopicCommInfrastructure("dmaap");
136         topicParams.setServers(Arrays.asList("localhost"));
137         return topicParams;
138     }
139
140     /**
141      * Returns participantId for test cases.
142      *
143      * @return participant Id
144      */
145     public static ToscaConceptIdentifier getParticipantId() {
146         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
147         return participantId;
148     }
149
150     /**
151      * Nulls out a field within a JSON string.
152      *
153      * @param json JSON string
154      * @param field field to be nulled out
155      * @return a new JSON string with the field nulled out
156      */
157     public String nullifyField(String json, String field) {
158         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
159     }
160 }