c53410b28a386c0eb043a780f718801c80fb4d59
[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.simulator.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 ParticipantSimulatorParameters.
46      *
47      * @return ParticipantSimulatorParameters
48      */
49     public ParticipantSimulatorParameters getParticipantSimulatorParameters() {
50         try {
51             return CODER.convert(getParticipantParameterGroupMap(PARTICIPANT_GROUP_NAME),
52                     ParticipantSimulatorParameters.class);
53         } catch (final CoderException e) {
54             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
55         }
56     }
57
58     /**
59      * Returns a property map for a ApexStarterParameterGroup 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> getParticipantParameterGroupMap(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("databaseProviderParameters", getDatabaseProviderParametersMap(false));
71         return map;
72     }
73
74     /**
75      * Returns a property map for a databaseProviderParameters map for test cases.
76      *
77      * @param isEmpty boolean value to represent that object created should be empty or not
78      * @return a property map suitable for constructing an object
79      */
80     public Map<String, Object> getDatabaseProviderParametersMap(final boolean isEmpty) {
81         final Map<String, Object> map = new TreeMap<>();
82         if (!isEmpty) {
83             map.put("name", "PolicyProviderParameterGroup");
84             map.put("implementation", "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
85             map.put("databaseDriver", "org.h2.Driver");
86             map.put("databaseUrl", "jdbc:h2:mem:testdb");
87             map.put("databaseUser", "policy");
88             map.put("databasePassword", "P01icY");
89             map.put("persistenceUnit", "ToscaConceptTest");
90         }
91
92         return map;
93     }
94
95     /**
96      * Returns a property map for a intermediaryParameters map for test cases.
97      *
98      * @param isEmpty boolean value to represent that object created should be empty or not
99      * @return a property map suitable for constructing an object
100      */
101     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
102         final Map<String, Object> map = new TreeMap<>();
103         if (!isEmpty) {
104             map.put("name", "Participant parameters");
105             map.put("reportingTimeInterval", TIME_INTERVAL);
106             map.put("description", DESCRIPTION);
107             map.put("participantId", getParticipantId());
108             map.put("participantType", getParticipantId());
109             map.put("clampControlLoopTopics", getTopicParametersMap(false));
110         }
111
112         return map;
113     }
114
115     /**
116      * Returns a property map for a TopicParameters map for test cases.
117      *
118      * @param isEmpty boolean value to represent that object created should be empty or not
119      * @return a property map suitable for constructing an object
120      */
121     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
122         final Map<String, Object> map = new TreeMap<>();
123         if (!isEmpty) {
124             map.put("topicSources", TOPIC_PARAMS);
125             map.put("topicSinks", TOPIC_PARAMS);
126         }
127         return map;
128     }
129
130     /**
131      * Returns topic parameters for test cases.
132      *
133      * @return topic parameters
134      */
135     public static TopicParameters getTopicParams() {
136         final TopicParameters topicParams = new TopicParameters();
137         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
138         topicParams.setTopicCommInfrastructure("dmaap");
139         topicParams.setServers(Arrays.asList("localhost"));
140         return topicParams;
141     }
142
143     /**
144      * Returns participantId for test cases.
145      *
146      * @return participant Id
147      */
148     public static ToscaConceptIdentifier getParticipantId() {
149         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
150         return participantId;
151     }
152
153     /**
154      * Nulls out a field within a JSON string.
155      *
156      * @param json JSON string
157      * @param field field to be nulled out
158      * @return a new JSON string with the field nulled out
159      */
160     public String nullifyField(String json, String field) {
161         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
162     }
163 }