2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.acm.participant.intermediary.main.parameters;
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.TreeMap;
30 import java.util.UUID;
31 import org.mockito.Mockito;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
35 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
36 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
44 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
45 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
46 import org.onap.policy.common.endpoints.event.comm.TopicSink;
47 import org.onap.policy.common.endpoints.parameters.TopicParameters;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
54 * Class to hold/create all parameters for test cases.
56 public class CommonTestData {
57 public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
58 public static final String DESCRIPTION = "Participant description";
59 public static final long TIME_INTERVAL = 2000;
60 public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
61 public static final Coder CODER = new StandardCoder();
62 private static final Object lockit = new Object();
63 public static final UUID AC_ID_0 = UUID.randomUUID();
64 public static final UUID AC_ID_1 = UUID.randomUUID();
65 public static final UUID PARTCICIPANT_ID = UUID.randomUUID();
68 * Get ParticipantIntermediaryParameters.
70 * @return ParticipantIntermediaryParameters
72 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
74 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
75 ParticipantIntermediaryParameters.class);
76 } catch (final CoderException e) {
77 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
82 * Get ParticipantParameters.
84 * @return ParticipantParameters
86 public static DummyParticipantParameters getParticipantParameters() {
88 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
89 } catch (final CoderException e) {
90 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
95 * Returns a property map for a Parameters map for test cases.
97 * @param name name of the parameters
98 * @return a property map suitable for constructing an object
100 public static Map<String, Object> getParametersMap(final String name) {
101 final Map<String, Object> map = new TreeMap<>();
102 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
107 * Returns a property map for a intermediaryParameters map for test cases.
109 * @param name name of the parameters
110 * @return a property map suitable for constructing an object
112 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
113 final Map<String, Object> map = new TreeMap<>();
114 map.put("name", name);
115 map.put("participantId", getParticipantId());
116 map.put("description", DESCRIPTION);
117 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
118 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
119 var supportedElementType = new ParticipantSupportedElementType();
120 supportedElementType.setTypeName("org.onap.policy.clamp.acm.HttpAutomationCompositionElement");
121 supportedElementType.setTypeVersion("1.0.0");
122 map.put("participantSupportedElementTypes", List.of(supportedElementType));
128 * Returns a property map for a TopicParameters map for test cases.
130 * @param isEmpty boolean value to represent that object created should be empty or not
131 * @return a property map suitable for constructing an object
133 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
134 final Map<String, Object> map = new TreeMap<>();
136 map.put("topicSources", TOPIC_PARAMS);
137 map.put("topicSinks", TOPIC_PARAMS);
143 * Returns topic parameters for test cases.
145 * @return topic parameters
147 public static TopicParameters getTopicParams() {
148 final var topicParams = new TopicParameters();
149 topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
150 topicParams.setTopicCommInfrastructure("dmaap");
151 topicParams.setServers(List.of("localhost"));
156 * Returns participantId for test cases.
158 * @return participant Id
160 public static UUID getParticipantId() {
161 return PARTCICIPANT_ID;
164 public static UUID getRndParticipantId() {
165 return UUID.randomUUID();
168 public static ToscaConceptIdentifier getDefinition() {
169 return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
173 * Returns a participantMessagePublisher for MessageSender.
175 * @return participant Message Publisher
177 private ParticipantMessagePublisher getParticipantMessagePublisher() {
178 synchronized (lockit) {
179 var participantMessagePublisher = new ParticipantMessagePublisher();
180 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
181 return participantMessagePublisher;
186 * Returns a mocked AutomationCompositionHandler for test cases.
188 * @return AutomationCompositionHandler
190 public AutomationCompositionHandler getMockAutomationCompositionHandler() {
191 return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
195 * Returns a mocked ParticipantHandler for test cases.
197 * @return participant Handler
199 public ParticipantHandler getMockParticipantHandler() {
200 var parameters = getParticipantParameters();
201 var automationCompositionHandler = getMockAutomationCompositionHandler();
202 var publisher = new ParticipantMessagePublisher();
203 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
204 return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
207 public ParticipantHandler getParticipantHandlerAutomationCompositions() {
208 var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
209 return getParticipantHandlerAutomationCompositions(automationCompositionHandler);
213 * Returns a mocked ParticipantHandler for test cases.
215 * @return participant Handler
217 * @throws CoderException if there is an error with .json file.
219 public ParticipantHandler getParticipantHandlerAutomationCompositions(
220 AutomationCompositionHandler automationCompositionHandler) {
221 Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
222 .getAutomationCompositionMap();
223 var publisher = new ParticipantMessagePublisher();
224 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
225 var parameters = getParticipantParameters();
226 var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
227 participantHandler.sendParticipantRegister();
228 participantHandler.handleParticipantStatusReq(null);
229 participantHandler.sendParticipantDeregister();
230 var participantDeregisterAckMsg = new ParticipantDeregisterAck();
231 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
232 participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
233 return participantHandler;
237 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
239 * @return automationCompositionMap
241 * @throws CoderException if there is an error with .json file.
243 public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
244 var automationCompositions = getTestAutomationCompositions();
245 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
246 Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
247 automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
248 return automationCompositionMap;
252 * Returns List of AutomationComposition for test cases.
254 * @return AutomationCompositions
256 * @throws CoderException if there is an error with .json file.
258 public static AutomationCompositions getTestAutomationCompositions() {
260 var automationCompositions =
261 new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
262 AutomationCompositions.class);
263 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
264 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
265 return automationCompositions;
266 } catch (Exception e) {
267 throw new RuntimeException("cannot read TestAutomationCompositions.json");
272 * Returns a map for a elementsOnThisParticipant for test cases.
275 * @param definition ToscaConceptIdentifier
276 * @return a map suitable for elementsOnThisParticipant
278 public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
279 ToscaConceptIdentifier definition, UUID participantId) {
280 var acElement = new AutomationCompositionElement();
281 acElement.setId(uuid);
282 acElement.setParticipantId(participantId);
283 acElement.setDefinition(definition);
284 acElement.setDeployState(DeployState.UNDEPLOYED);
286 Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
287 elementsOnThisParticipant.put(uuid, acElement);
288 return elementsOnThisParticipant;
292 * Returns a AutomationCompositionHandler with elements on the definition,uuid.
294 * @param definition ToscaConceptIdentifier
296 * @return a AutomationCompositionHander with elements
298 public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier definition,
299 UUID uuid, UUID participantId) {
300 var ach = getMockAutomationCompositionHandler();
301 ach.getAutomationCompositionMap().putAll(getTestAutomationCompositionMap());
302 var acKey = ach.getAutomationCompositionMap().keySet().iterator().next();
303 ach.getAutomationCompositionMap().get(acKey)
304 .setElements(setAutomationCompositionElementTest(uuid, definition, participantId));
310 * Return a AutomationCompositionStateChange.
312 * @param participantId the participantId
314 * @param deployOrder a DeployOrder
315 * @param lockOrder a LockOrder
316 * @return a AutomationCompositionStateChange
318 public AutomationCompositionStateChange getStateChange(UUID participantId, UUID uuid,
319 DeployOrder deployOrder, LockOrder lockOrder) {
320 var stateChange = new AutomationCompositionStateChange();
321 stateChange.setStartPhase(0);
322 stateChange.setAutomationCompositionId(UUID.randomUUID());
323 stateChange.setParticipantId(participantId);
324 stateChange.setMessageId(uuid);
325 stateChange.setDeployOrderedState(deployOrder);
326 stateChange.setLockOrderedState(lockOrder);
327 stateChange.setTimestamp(Instant.ofEpochMilli(3000));