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.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.AcElementStatistics;
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.ParticipantDeregisterAck;
44 import org.onap.policy.common.endpoints.event.comm.TopicSink;
45 import org.onap.policy.common.endpoints.parameters.TopicParameters;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 * Class to hold/create all parameters for test cases.
54 public class CommonTestData {
55 public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
56 public static final String DESCRIPTION = "Participant description";
57 public static final long TIME_INTERVAL = 2000;
58 public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
59 public static final Coder CODER = new StandardCoder();
60 private static final Object lockit = new Object();
63 * Get ParticipantIntermediaryParameters.
65 * @return ParticipantIntermediaryParameters
67 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
69 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
70 ParticipantIntermediaryParameters.class);
71 } catch (final CoderException e) {
72 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
77 * Get ParticipantParameters.
79 * @return ParticipantParameters
81 public static DummyParticipantParameters getParticipantParameters() {
83 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
84 } catch (final CoderException e) {
85 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
90 * Returns a property map for a Parameters map for test cases.
92 * @param name name of the parameters
93 * @return a property map suitable for constructing an object
95 public static Map<String, Object> getParametersMap(final String name) {
96 final Map<String, Object> map = new TreeMap<>();
97 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
102 * Returns a property map for a intermediaryParameters map for test cases.
104 * @param name name of the parameters
105 * @return a property map suitable for constructing an object
107 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
108 final Map<String, Object> map = new TreeMap<>();
109 map.put("name", name);
110 map.put("participantId", getParticipantId());
111 map.put("description", DESCRIPTION);
112 map.put("participantType", getParticipantId());
113 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
114 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
120 * Returns a property map for a TopicParameters map for test cases.
122 * @param isEmpty boolean value to represent that object created should be empty or not
123 * @return a property map suitable for constructing an object
125 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
126 final Map<String, Object> map = new TreeMap<>();
128 map.put("topicSources", TOPIC_PARAMS);
129 map.put("topicSinks", TOPIC_PARAMS);
135 * Returns topic parameters for test cases.
137 * @return topic parameters
139 public static TopicParameters getTopicParams() {
140 final var topicParams = new TopicParameters();
141 topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
142 topicParams.setTopicCommInfrastructure("dmaap");
143 topicParams.setServers(List.of("localhost"));
148 * Returns participantId for test cases.
150 * @return participant Id
152 public static ToscaConceptIdentifier getParticipantId() {
153 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
157 * Returns a participantMessagePublisher for MessageSender.
159 * @return participant Message Publisher
161 private ParticipantMessagePublisher getParticipantMessagePublisher() {
162 synchronized (lockit) {
163 var participantMessagePublisher = new ParticipantMessagePublisher();
164 participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
165 return participantMessagePublisher;
170 * Returns a mocked AutomationCompositionHandler for test cases.
172 * @return AutomationCompositionHandler
174 public AutomationCompositionHandler getMockAutomationCompositionHandler() {
175 return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
179 * Returns a mocked ParticipantHandler for test cases.
181 * @return participant Handler
183 public ParticipantHandler getMockParticipantHandler() {
184 var parameters = getParticipantParameters();
185 var automationCompositionHandler = getMockAutomationCompositionHandler();
186 var publisher = new ParticipantMessagePublisher();
187 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
188 return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
192 * Returns a mocked ParticipantHandler for test cases.
194 * @return participant Handler
196 * @throws CoderException if there is an error with .json file.
198 public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
199 var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
200 Mockito.doReturn(getTestAutomationCompositions()).when(automationCompositionHandler)
201 .getAutomationCompositions();
202 Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
203 .getAutomationCompositionMap();
204 var publisher = new ParticipantMessagePublisher();
205 publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
206 var parameters = getParticipantParameters();
207 var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
208 participantHandler.sendParticipantRegister();
209 participantHandler.handleParticipantStatusReq(null);
210 participantHandler.sendParticipantDeregister();
211 var participantDeregisterAckMsg = new ParticipantDeregisterAck();
212 participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
213 participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
214 return participantHandler;
218 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
220 * @return automationCompositionMap
222 * @throws CoderException if there is an error with .json file.
224 public Map<ToscaConceptIdentifier, AutomationComposition> getTestAutomationCompositionMap() throws CoderException {
225 var automationCompositions = getTestAutomationCompositions();
226 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
227 var id = getParticipantId();
228 Map<ToscaConceptIdentifier, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
229 automationCompositionMap.put(id, automationComposition);
230 return automationCompositionMap;
234 * Returns List of AutomationComposition for test cases.
236 * @return AutomationCompositions
238 * @throws CoderException if there is an error with .json file.
240 public AutomationCompositions getTestAutomationCompositions() throws CoderException {
241 return new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
242 AutomationCompositions.class);
246 * Returns a map for a elementsOnThisParticipant for test cases.
248 * @param uuid UUID and id ToscaConceptIdentifier
249 * @return a map suitable for elementsOnThisParticipant
251 public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
252 ToscaConceptIdentifier id) {
253 var acElement = new AutomationCompositionElement();
254 acElement.setId(uuid);
255 acElement.setParticipantId(id);
256 acElement.setDefinition(id);
257 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
259 var acElementStatistics = new AcElementStatistics();
260 acElementStatistics.setParticipantId(id);
261 acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
262 acElementStatistics.setTimeStamp(Instant.now());
264 acElement.setAcElementStatistics(acElementStatistics);
266 Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
267 elementsOnThisParticipant.put(uuid, acElement);
268 return elementsOnThisParticipant;
272 * Returns a AutomationCompositionHandler with elements on the id,uuid.
274 * @param id ToscaConceptIdentifier and uuid UUID
275 * @return a AutomationCompositionHander with elements
277 public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
278 throws CoderException {
279 var ach = getMockAutomationCompositionHandler();
281 var key = getTestAutomationCompositionMap().keySet().iterator().next();
282 var value = getTestAutomationCompositionMap().get(key);
283 ach.getAutomationCompositionMap().put(key, value);
285 var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
286 var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
287 ach.getElementsOnThisParticipant().put(keyElem, valueElem);