2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.TreeMap;
29 import java.util.UUID;
30 import org.mockito.Mockito;
31 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
32 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
35 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
42 import org.onap.policy.common.endpoints.parameters.TopicParameters;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 * Class to hold/create all parameters for test cases.
51 public class CommonTestData {
52 public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
53 public static final String DESCRIPTION = "Participant description";
54 public static final long TIME_INTERVAL = 2000;
55 public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
56 public static final Coder CODER = new StandardCoder();
57 private static final Object lockit = new Object();
58 public static final UUID AC_ID_0 = UUID.randomUUID();
59 public static final UUID AC_ID_1 = UUID.randomUUID();
62 * Get ParticipantIntermediaryParameters.
64 * @return ParticipantIntermediaryParameters
66 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
68 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
69 ParticipantIntermediaryParameters.class);
70 } catch (final CoderException e) {
71 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
76 * Get ParticipantParameters.
78 * @return ParticipantParameters
80 public static DummyParticipantParameters getParticipantParameters() {
82 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
83 } catch (final CoderException e) {
84 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
89 * Returns a property map for a Parameters map for test cases.
91 * @param name name of the parameters
92 * @return a property map suitable for constructing an object
94 public static Map<String, Object> getParametersMap(final String name) {
95 final Map<String, Object> map = new TreeMap<>();
96 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
101 * Returns a property map for a intermediaryParameters map for test cases.
103 * @param name name of the parameters
104 * @return a property map suitable for constructing an object
106 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
107 final Map<String, Object> map = new TreeMap<>();
108 map.put("name", name);
109 map.put("participantId", getParticipantId());
110 map.put("description", DESCRIPTION);
111 map.put("participantType", getParticipantId());
112 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
113 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
119 * Returns a property map for a TopicParameters map for test cases.
121 * @param isEmpty boolean value to represent that object created should be empty or not
122 * @return a property map suitable for constructing an object
124 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
125 final Map<String, Object> map = new TreeMap<>();
127 map.put("topicSources", TOPIC_PARAMS);
128 map.put("topicSinks", TOPIC_PARAMS);
134 * Returns topic parameters for test cases.
136 * @return topic parameters
138 public static TopicParameters getTopicParams() {
139 final var topicParams = new TopicParameters();
140 topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
141 topicParams.setTopicCommInfrastructure("dmaap");
142 topicParams.setServers(List.of("localhost"));
147 * Returns participantId for test cases.
149 * @return participant Id
151 public static ToscaConceptIdentifier getParticipantId() {
152 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
156 * Returns a participantMessagePublisher for MessageSender.
158 * @return participant Message Publisher
160 private ParticipantMessagePublisher getParticipantMessagePublisher() {
161 synchronized (lockit) {
162 var participantMessagePublisher = new ParticipantMessagePublisher();
163 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
164 return participantMessagePublisher;
169 * Returns a mocked AutomationCompositionHandler for test cases.
171 * @return AutomationCompositionHandler
173 public AutomationCompositionHandler getMockAutomationCompositionHandler() {
174 return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
178 * Returns a mocked ParticipantHandler for test cases.
180 * @return participant Handler
182 public ParticipantHandler getMockParticipantHandler() {
183 var parameters = getParticipantParameters();
184 var automationCompositionHandler = getMockAutomationCompositionHandler();
185 var publisher = new ParticipantMessagePublisher();
186 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
187 return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
191 * Returns a mocked ParticipantHandler for test cases.
193 * @return participant Handler
195 * @throws CoderException if there is an error with .json file.
197 public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
198 var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
199 Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
200 .getAutomationCompositionMap();
201 var publisher = new ParticipantMessagePublisher();
202 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
203 var parameters = getParticipantParameters();
204 var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
205 participantHandler.sendParticipantRegister();
206 participantHandler.handleParticipantStatusReq(null);
207 participantHandler.sendParticipantDeregister();
208 var participantDeregisterAckMsg = new ParticipantDeregisterAck();
209 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
210 participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
211 return participantHandler;
215 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
217 * @return automationCompositionMap
219 * @throws CoderException if there is an error with .json file.
221 public Map<UUID, AutomationComposition> getTestAutomationCompositionMap() throws CoderException {
222 var automationCompositions = getTestAutomationCompositions();
223 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
224 Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
225 automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
226 return automationCompositionMap;
230 * Returns List of AutomationComposition for test cases.
232 * @return AutomationCompositions
234 * @throws CoderException if there is an error with .json file.
236 public AutomationCompositions getTestAutomationCompositions() throws CoderException {
237 var automationCompositions = new StandardCoder().decode(
238 new File("src/test/resources/providers/TestAutomationCompositions.json"), AutomationCompositions.class);
239 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
240 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
241 return automationCompositions;
245 * Returns a map for a elementsOnThisParticipant for test cases.
247 * @param uuid UUID and id ToscaConceptIdentifier
248 * @return a map suitable for elementsOnThisParticipant
250 public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
251 ToscaConceptIdentifier id) {
252 var acElement = new AutomationCompositionElement();
253 acElement.setId(uuid);
254 acElement.setParticipantId(id);
255 acElement.setDefinition(id);
256 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
258 Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
259 elementsOnThisParticipant.put(uuid, acElement);
260 return elementsOnThisParticipant;
264 * Returns a AutomationCompositionHandler with elements on the id,uuid.
266 * @param id ToscaConceptIdentifier and uuid UUID
267 * @return a AutomationCompositionHander with elements
269 public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
270 throws CoderException {
271 var ach = getMockAutomationCompositionHandler();
273 var key = getTestAutomationCompositionMap().keySet().iterator().next();
274 var value = getTestAutomationCompositionMap().get(key);
275 ach.getAutomationCompositionMap().put(key, value);
277 var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
278 var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
279 ach.getElementsOnThisParticipant().put(keyElem, valueElem);