a5aa2f48a3d72cb34a7ee7d766e4fb24a428e4c6
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.main.parameters;
22
23 import java.io.File;
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
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;
50
51 /**
52  * Class to hold/create all parameters for test cases.
53  */
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();
61
62     /**
63      * Get ParticipantIntermediaryParameters.
64      *
65      * @return ParticipantIntermediaryParameters
66      */
67     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
68         try {
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);
73         }
74     }
75
76     /**
77      * Get ParticipantParameters.
78      *
79      * @return ParticipantParameters
80      */
81     public static DummyParticipantParameters getParticipantParameters() {
82         try {
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);
86         }
87     }
88
89     /**
90      * Returns a property map for a Parameters map for test cases.
91      *
92      * @param name name of the parameters
93      * @return a property map suitable for constructing an object
94      */
95     public static Map<String, Object> getParametersMap(final String name) {
96         final Map<String, Object> map = new TreeMap<>();
97         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
98         return map;
99     }
100
101     /**
102      * Returns a property map for a intermediaryParameters map for test cases.
103      *
104      * @param name name of the parameters
105      * @return a property map suitable for constructing an object
106      */
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));
115
116         return map;
117     }
118
119     /**
120      * Returns a property map for a TopicParameters map for test cases.
121      *
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
124      */
125     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
126         final Map<String, Object> map = new TreeMap<>();
127         if (!isEmpty) {
128             map.put("topicSources", TOPIC_PARAMS);
129             map.put("topicSinks", TOPIC_PARAMS);
130         }
131         return map;
132     }
133
134     /**
135      * Returns topic parameters for test cases.
136      *
137      * @return topic parameters
138      */
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"));
144         return topicParams;
145     }
146
147     /**
148      * Returns participantId for test cases.
149      *
150      * @return participant Id
151      */
152     public static ToscaConceptIdentifier getParticipantId() {
153         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
154     }
155
156     /**
157      * Returns a participantMessagePublisher for MessageSender.
158      *
159      * @return participant Message Publisher
160      */
161     private ParticipantMessagePublisher getParticipantMessagePublisher() {
162         synchronized (lockit) {
163             var participantMessagePublisher = new ParticipantMessagePublisher();
164             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
165             return participantMessagePublisher;
166         }
167     }
168
169     /**
170      * Returns a mocked AutomationCompositionHandler for test cases.
171      *
172      * @return AutomationCompositionHandler
173      */
174     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
175         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
176     }
177
178     /**
179      * Returns a mocked ParticipantHandler for test cases.
180      *
181      * @return participant Handler
182      */
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);
189     }
190
191     /**
192      * Returns a mocked ParticipantHandler for test cases.
193      *
194      * @return participant Handler
195      *
196      * @throws CoderException if there is an error with .json file.
197      */
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;
215     }
216
217     /**
218      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
219      *
220      * @return automationCompositionMap
221      *
222      * @throws CoderException if there is an error with .json file.
223      */
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;
231     }
232
233     /**
234      * Returns List of AutomationComposition for test cases.
235      *
236      * @return AutomationCompositions
237      *
238      * @throws CoderException if there is an error with .json file.
239      */
240     public AutomationCompositions getTestAutomationCompositions() throws CoderException {
241         return new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
242             AutomationCompositions.class);
243     }
244
245     /**
246      * Returns a map for a elementsOnThisParticipant for test cases.
247      *
248      * @param uuid UUID and id ToscaConceptIdentifier
249      * @return a map suitable for elementsOnThisParticipant
250      */
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);
258
259         var acElementStatistics = new AcElementStatistics();
260         acElementStatistics.setParticipantId(id);
261         acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
262         acElementStatistics.setTimeStamp(Instant.now());
263
264         acElement.setAcElementStatistics(acElementStatistics);
265
266         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
267         elementsOnThisParticipant.put(uuid, acElement);
268         return elementsOnThisParticipant;
269     }
270
271     /**
272      * Returns a AutomationCompositionHandler with elements on the id,uuid.
273      *
274      * @param id ToscaConceptIdentifier and uuid UUID
275      * @return a AutomationCompositionHander with elements
276      */
277     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
278         throws CoderException {
279         var ach = getMockAutomationCompositionHandler();
280
281         var key = getTestAutomationCompositionMap().keySet().iterator().next();
282         var value = getTestAutomationCompositionMap().get(key);
283         ach.getAutomationCompositionMap().put(key, value);
284
285         var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
286         var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
287         ach.getElementsOnThisParticipant().put(keyElem, valueElem);
288
289         return ach;
290     }
291
292 }