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();
60 * Get ParticipantIntermediaryParameters.
62 * @return ParticipantIntermediaryParameters
64 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
66 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
67 ParticipantIntermediaryParameters.class);
68 } catch (final CoderException e) {
69 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
74 * Get ParticipantParameters.
76 * @return ParticipantParameters
78 public static DummyParticipantParameters getParticipantParameters() {
80 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
81 } catch (final CoderException e) {
82 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
87 * Returns a property map for a Parameters map for test cases.
89 * @param name name of the parameters
90 * @return a property map suitable for constructing an object
92 public static Map<String, Object> getParametersMap(final String name) {
93 final Map<String, Object> map = new TreeMap<>();
94 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
99 * Returns a property map for a intermediaryParameters map for test cases.
101 * @param name name of the parameters
102 * @return a property map suitable for constructing an object
104 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
105 final Map<String, Object> map = new TreeMap<>();
106 map.put("name", name);
107 map.put("participantId", getParticipantId());
108 map.put("description", DESCRIPTION);
109 map.put("participantType", getParticipantId());
110 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
111 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
117 * Returns a property map for a TopicParameters map for test cases.
119 * @param isEmpty boolean value to represent that object created should be empty or not
120 * @return a property map suitable for constructing an object
122 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
123 final Map<String, Object> map = new TreeMap<>();
125 map.put("topicSources", TOPIC_PARAMS);
126 map.put("topicSinks", TOPIC_PARAMS);
132 * Returns topic parameters for test cases.
134 * @return topic parameters
136 public static TopicParameters getTopicParams() {
137 final var topicParams = new TopicParameters();
138 topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
139 topicParams.setTopicCommInfrastructure("dmaap");
140 topicParams.setServers(List.of("localhost"));
145 * Returns participantId for test cases.
147 * @return participant Id
149 public static ToscaConceptIdentifier getParticipantId() {
150 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
154 * Returns a participantMessagePublisher for MessageSender.
156 * @return participant Message Publisher
158 private ParticipantMessagePublisher getParticipantMessagePublisher() {
159 synchronized (lockit) {
160 var participantMessagePublisher = new ParticipantMessagePublisher();
161 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
162 return participantMessagePublisher;
167 * Returns a mocked AutomationCompositionHandler for test cases.
169 * @return AutomationCompositionHandler
171 public AutomationCompositionHandler getMockAutomationCompositionHandler() {
172 return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
176 * Returns a mocked ParticipantHandler for test cases.
178 * @return participant Handler
180 public ParticipantHandler getMockParticipantHandler() {
181 var parameters = getParticipantParameters();
182 var automationCompositionHandler = getMockAutomationCompositionHandler();
183 var publisher = new ParticipantMessagePublisher();
184 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
185 return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
189 * Returns a mocked ParticipantHandler for test cases.
191 * @return participant Handler
193 * @throws CoderException if there is an error with .json file.
195 public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
196 var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
197 Mockito.doReturn(getTestAutomationCompositions()).when(automationCompositionHandler)
198 .getAutomationCompositions();
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<ToscaConceptIdentifier, AutomationComposition> getTestAutomationCompositionMap() throws CoderException {
222 var automationCompositions = getTestAutomationCompositions();
223 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
224 var id = getParticipantId();
225 Map<ToscaConceptIdentifier, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
226 automationCompositionMap.put(id, automationComposition);
227 return automationCompositionMap;
231 * Returns List of AutomationComposition for test cases.
233 * @return AutomationCompositions
235 * @throws CoderException if there is an error with .json file.
237 public AutomationCompositions getTestAutomationCompositions() throws CoderException {
238 return new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
239 AutomationCompositions.class);
243 * Returns a map for a elementsOnThisParticipant for test cases.
245 * @param uuid UUID and id ToscaConceptIdentifier
246 * @return a map suitable for elementsOnThisParticipant
248 public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
249 ToscaConceptIdentifier id) {
250 var acElement = new AutomationCompositionElement();
251 acElement.setId(uuid);
252 acElement.setParticipantId(id);
253 acElement.setDefinition(id);
254 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
256 Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
257 elementsOnThisParticipant.put(uuid, acElement);
258 return elementsOnThisParticipant;
262 * Returns a AutomationCompositionHandler with elements on the id,uuid.
264 * @param id ToscaConceptIdentifier and uuid UUID
265 * @return a AutomationCompositionHander with elements
267 public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
268 throws CoderException {
269 var ach = getMockAutomationCompositionHandler();
271 var key = getTestAutomationCompositionMap().keySet().iterator().next();
272 var value = getTestAutomationCompositionMap().get(key);
273 ach.getAutomationCompositionMap().put(key, value);
275 var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
276 var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
277 ach.getElementsOnThisParticipant().put(keyElem, valueElem);