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.ArrayList;
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.acm.participant.intermediary.comm.ParticipantMessagePublisher;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
35 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
36 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
37 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
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 = "AutomationCompositionParticipantGroup";
57 public static final String DESCRIPTION = "Participant description";
58 public static final long TIME_INTERVAL = 2000;
59 public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
60 public static final Coder CODER = new StandardCoder();
61 private static final Object lockit = new Object();
62 public static final UUID AC_ID_0 = UUID.randomUUID();
63 public static final UUID AC_ID_1 = UUID.randomUUID();
64 public static final ToscaConceptIdentifier PARTCICIPANT_ID =
65 new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
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("participantType", getDefinition());
118 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
119 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
120 map.put("participantSupportedElementTypes", new ArrayList<>());
126 * Returns a property map for a TopicParameters map for test cases.
128 * @param isEmpty boolean value to represent that object created should be empty or not
129 * @return a property map suitable for constructing an object
131 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
132 final Map<String, Object> map = new TreeMap<>();
134 map.put("topicSources", TOPIC_PARAMS);
135 map.put("topicSinks", TOPIC_PARAMS);
141 * Returns topic parameters for test cases.
143 * @return topic parameters
145 public static TopicParameters getTopicParams() {
146 final var topicParams = new TopicParameters();
147 topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
148 topicParams.setTopicCommInfrastructure("dmaap");
149 topicParams.setServers(List.of("localhost"));
154 * Returns participantId for test cases.
156 * @return participant Id
158 public static ToscaConceptIdentifier getParticipantId() {
159 return PARTCICIPANT_ID;
162 public static ToscaConceptIdentifier getRndParticipantId() {
163 return new ToscaConceptIdentifier("diff", "0.0.0");
166 public static ToscaConceptIdentifier getDefinition() {
167 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
171 * Returns a participantMessagePublisher for MessageSender.
173 * @return participant Message Publisher
175 private ParticipantMessagePublisher getParticipantMessagePublisher() {
176 synchronized (lockit) {
177 var participantMessagePublisher = new ParticipantMessagePublisher();
178 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
179 return participantMessagePublisher;
184 * Returns a mocked AutomationCompositionHandler for test cases.
186 * @return AutomationCompositionHandler
188 public AutomationCompositionHandler getMockAutomationCompositionHandler() {
189 return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
193 * Returns a mocked ParticipantHandler for test cases.
195 * @return participant Handler
197 public ParticipantHandler getMockParticipantHandler() {
198 var parameters = getParticipantParameters();
199 var automationCompositionHandler = getMockAutomationCompositionHandler();
200 var publisher = new ParticipantMessagePublisher();
201 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
202 return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
206 * Returns a mocked ParticipantHandler for test cases.
208 * @return participant Handler
210 * @throws CoderException if there is an error with .json file.
212 public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
213 var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
214 Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
215 .getAutomationCompositionMap();
216 var publisher = new ParticipantMessagePublisher();
217 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
218 var parameters = getParticipantParameters();
219 var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
220 participantHandler.sendParticipantRegister();
221 participantHandler.handleParticipantStatusReq(null);
222 participantHandler.sendParticipantDeregister();
223 var participantDeregisterAckMsg = new ParticipantDeregisterAck();
224 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
225 participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
226 return participantHandler;
230 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
232 * @return automationCompositionMap
234 * @throws CoderException if there is an error with .json file.
236 public Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
237 var automationCompositions = getTestAutomationCompositions();
238 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
239 Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
240 automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
241 return automationCompositionMap;
245 * Returns List of AutomationComposition for test cases.
247 * @return AutomationCompositions
249 * @throws CoderException if there is an error with .json file.
251 public AutomationCompositions getTestAutomationCompositions() {
253 var automationCompositions =
254 new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
255 AutomationCompositions.class);
256 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
257 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
258 return automationCompositions;
259 } catch (Exception e) {
260 throw new RuntimeException("cannot read TestAutomationCompositions.json");
265 * Returns a map for a elementsOnThisParticipant for test cases.
268 * @param definition ToscaConceptIdentifier
269 * @return a map suitable for elementsOnThisParticipant
271 public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
272 ToscaConceptIdentifier definition, ToscaConceptIdentifier participantId) {
273 var acElement = new AutomationCompositionElement();
274 acElement.setId(uuid);
275 acElement.setParticipantId(participantId);
276 acElement.setDefinition(definition);
277 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
279 Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
280 elementsOnThisParticipant.put(uuid, acElement);
281 return elementsOnThisParticipant;
285 * Returns a AutomationCompositionHandler with elements on the definition,uuid.
287 * @param definition ToscaConceptIdentifier
289 * @return a AutomationCompositionHander with elements
291 public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier definition,
292 UUID uuid, ToscaConceptIdentifier participantId) {
293 var ach = getMockAutomationCompositionHandler();
295 var key = getTestAutomationCompositionMap().keySet().iterator().next();
296 var value = getTestAutomationCompositionMap().get(key);
297 ach.getAutomationCompositionMap().put(key, value);
299 var keyElem = setAutomationCompositionElementTest(uuid, definition, participantId).keySet().iterator().next();
300 var valueElem = setAutomationCompositionElementTest(uuid, definition, participantId).get(keyElem);
301 ach.getElementsOnThisParticipant().put(keyElem, valueElem);
307 * Return a AutomationCompositionStateChange.
309 * @param participantId the participantId
311 * @param state a AutomationCompositionOrderedState
312 * @return a AutomationCompositionStateChange
314 public AutomationCompositionStateChange getStateChange(ToscaConceptIdentifier participantId, UUID uuid,
315 AutomationCompositionOrderedState state) {
316 var stateChange = new AutomationCompositionStateChange();
317 stateChange.setAutomationCompositionId(UUID.randomUUID());
318 stateChange.setParticipantId(participantId);
319 stateChange.setMessageId(uuid);
320 stateChange.setOrderedState(state);
321 stateChange.setCurrentState(AutomationCompositionState.UNINITIALISED);
322 stateChange.setTimestamp(Instant.ofEpochMilli(3000));