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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters;
24 import java.time.Instant;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.TreeMap;
31 import java.util.UUID;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.DummyParticipantParameters;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
45 import org.onap.policy.common.endpoints.event.comm.TopicSink;
46 import org.onap.policy.common.endpoints.parameters.TopicParameters;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 * Class to hold/create all parameters for test cases.
55 public class CommonTestData {
56 public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
57 public static final String DESCRIPTION = "Participant description";
58 public static final long TIME_INTERVAL = 2000;
59 public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
60 public static final Coder CODER = new StandardCoder();
61 private static final Object lockit = new Object();
64 * Get ParticipantIntermediaryParameters.
66 * @return ParticipantIntermediaryParameters
68 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
70 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
71 ParticipantIntermediaryParameters.class);
72 } catch (final CoderException e) {
73 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
78 * Get ParticipantParameters.
80 * @return ParticipantParameters
82 public static DummyParticipantParameters getParticipantParameters() {
84 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME),
85 DummyParticipantParameters.class);
86 } catch (final CoderException e) {
87 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
92 * Returns a property map for a Parameters map for test cases.
94 * @param name name of the parameters
95 * @return a property map suitable for constructing an object
97 public static Map<String, Object> getParametersMap(final String name) {
98 final Map<String, Object> map = new TreeMap<>();
99 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
104 * Returns a property map for a intermediaryParameters map for test cases.
106 * @param name name of the parameters
107 * @return a property map suitable for constructing an object
109 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
110 final Map<String, Object> map = new TreeMap<>();
111 map.put("name", name);
112 map.put("participantId", getParticipantId());
113 map.put("description", DESCRIPTION);
114 map.put("participantType", getParticipantId());
115 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
116 map.put("clampControlLoopTopics", getTopicParametersMap(false));
122 * Returns a property map for a TopicParameters map for test cases.
124 * @param isEmpty boolean value to represent that object created should be empty or not
125 * @return a property map suitable for constructing an object
127 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
128 final Map<String, Object> map = new TreeMap<>();
130 map.put("topicSources", TOPIC_PARAMS);
131 map.put("topicSinks", TOPIC_PARAMS);
137 * Returns topic parameters for test cases.
139 * @return topic parameters
141 public static TopicParameters getTopicParams() {
142 final var topicParams = new TopicParameters();
143 topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
144 topicParams.setTopicCommInfrastructure("dmaap");
145 topicParams.setServers(Arrays.asList("localhost"));
150 * Returns participantId for test cases.
152 * @return participant Id
154 public static ToscaConceptIdentifier getParticipantId() {
155 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
159 * Returns a participantMessagePublisher for MessageSender.
161 * @return participant Message Publisher
163 private ParticipantMessagePublisher getParticipantMessagePublisher() {
164 synchronized (lockit) {
165 var participantMessagePublisher = new ParticipantMessagePublisher();
166 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
167 return participantMessagePublisher;
172 * Returns a mocked ControlLoopHandler for test cases.
174 * @return ControlLoopHandler
176 public ControlLoopHandler getMockControlLoopHandler() {
177 return new ControlLoopHandler(
178 getParticipantParameters(),
179 getParticipantMessagePublisher());
183 * Returns a mocked ParticipantHandler for test cases.
185 * @return participant Handler
187 public ParticipantHandler getMockParticipantHandler() {
188 var parameters = getParticipantParameters();
189 var controlLoopHandler = getMockControlLoopHandler();
190 var publisher = new ParticipantMessagePublisher();
191 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
192 var participantHandler = new ParticipantHandler(parameters, publisher, controlLoopHandler);
193 return participantHandler;
197 * Returns a mocked ParticipantHandler for test cases.
199 * @return participant Handler
201 * @throws CoderException if there is an error with .json file.
203 public ParticipantHandler getParticipantHandlerControlLoops() throws CoderException {
204 var controlLoopHandler = Mockito.mock(ControlLoopHandler.class);
205 Mockito.doReturn(getTestControlLoops()).when(controlLoopHandler).getControlLoops();
206 Mockito.doReturn(getTestControlLoopMap()).when(controlLoopHandler).getControlLoopMap();
207 var publisher = new ParticipantMessagePublisher();
208 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
209 var parameters = getParticipantParameters();
210 var participantHandler = new ParticipantHandler(parameters, publisher, controlLoopHandler);
211 participantHandler.sendParticipantRegister();
212 participantHandler.handleParticipantStatusReq(null);
213 participantHandler.sendParticipantDeregister();
214 var participantDeregisterAckMsg = new ParticipantDeregisterAck();
215 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
216 participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
217 return participantHandler;
221 * Returns a Map of ToscaConceptIdentifier and ControlLoop for test cases.
223 * @return controlLoopMap
225 * @throws CoderException if there is an error with .json file.
227 public Map<ToscaConceptIdentifier, ControlLoop> getTestControlLoopMap() throws CoderException {
228 var controlLoops = getTestControlLoops();
229 var controlLoop = controlLoops.getControlLoopList().get(1);
230 var id = getParticipantId();
231 Map<ToscaConceptIdentifier, ControlLoop> controlLoopMap = new LinkedHashMap<>();
232 controlLoopMap.put(id, controlLoop);
233 return controlLoopMap;
237 * Returns List of ControlLoop for test cases.
239 * @return ControlLoops
241 * @throws CoderException if there is an error with .json file.
243 public ControlLoops getTestControlLoops() throws CoderException {
244 return new StandardCoder()
245 .decode(new File("src/test/resources/providers/TestControlLoops.json"), ControlLoops.class);
249 * Returns a map for a elementsOnThisParticipant for test cases.
251 * @param uuid UUID and id ToscaConceptIdentifier
252 * @return a map suitable for elementsOnThisParticipant
254 public Map<UUID, ControlLoopElement> setControlLoopElementTest(UUID uuid, ToscaConceptIdentifier id) {
255 var clElement = new ControlLoopElement();
256 clElement.setId(uuid);
257 clElement.setParticipantId(id);
258 clElement.setDefinition(id);
259 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
261 var clElementStatistics = new ClElementStatistics();
262 clElementStatistics.setParticipantId(id);
263 clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
264 clElementStatistics.setTimeStamp(Instant.now());
266 clElement.setClElementStatistics(clElementStatistics);
268 Map<UUID, ControlLoopElement> elementsOnThisParticipant = new LinkedHashMap<>();
269 elementsOnThisParticipant.put(uuid, clElement);
270 return elementsOnThisParticipant;
274 * Returns a ControlLoopHandler with elements on the id,uuid.
276 * @param id ToscaConceptIdentifier and uuid UUID
277 * @return a ControlLoopHander with elements
279 public ControlLoopHandler setTestControlLoopHandler(ToscaConceptIdentifier id, UUID uuid) throws CoderException {
280 var clh = getMockControlLoopHandler();
282 var key = getTestControlLoopMap().keySet().iterator().next();
283 var value = getTestControlLoopMap().get(key);
284 clh.getControlLoopMap().put(key, value);
286 var keyElem = setControlLoopElementTest(uuid, id).keySet().iterator().next();
287 var valueElem = setControlLoopElementTest(uuid, id).get(keyElem);
288 clh.getElementsOnThisParticipant().put(keyElem, valueElem);