ad54d402cdb3d2518970c2fbabab39f9c3d4fa2f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.Arrays;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
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.AcElementStatistics;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
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;
51
52 /**
53  * Class to hold/create all parameters for test cases.
54  */
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 = Arrays.asList(getTopicParams());
60     public static final Coder CODER = new StandardCoder();
61     private static final Object lockit = new Object();
62
63     /**
64      * Get ParticipantIntermediaryParameters.
65      *
66      * @return ParticipantIntermediaryParameters
67      */
68     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
69         try {
70             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
71                 ParticipantIntermediaryParameters.class);
72         } catch (final CoderException e) {
73             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
74         }
75     }
76
77     /**
78      * Get ParticipantParameters.
79      *
80      * @return ParticipantParameters
81      */
82     public static DummyParticipantParameters getParticipantParameters() {
83         try {
84             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
85         } catch (final CoderException e) {
86             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
87         }
88     }
89
90     /**
91      * Returns a property map for a Parameters map for test cases.
92      *
93      * @param name name of the parameters
94      * @return a property map suitable for constructing an object
95      */
96     public static Map<String, Object> getParametersMap(final String name) {
97         final Map<String, Object> map = new TreeMap<>();
98         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
99         return map;
100     }
101
102     /**
103      * Returns a property map for a intermediaryParameters map for test cases.
104      *
105      * @param name name of the parameters
106      * @return a property map suitable for constructing an object
107      */
108     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
109         final Map<String, Object> map = new TreeMap<>();
110         map.put("name", name);
111         map.put("participantId", getParticipantId());
112         map.put("description", DESCRIPTION);
113         map.put("participantType", getParticipantId());
114         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
115         map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
116
117         return map;
118     }
119
120     /**
121      * Returns a property map for a TopicParameters map for test cases.
122      *
123      * @param isEmpty boolean value to represent that object created should be empty or not
124      * @return a property map suitable for constructing an object
125      */
126     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
127         final Map<String, Object> map = new TreeMap<>();
128         if (!isEmpty) {
129             map.put("topicSources", TOPIC_PARAMS);
130             map.put("topicSinks", TOPIC_PARAMS);
131         }
132         return map;
133     }
134
135     /**
136      * Returns topic parameters for test cases.
137      *
138      * @return topic parameters
139      */
140     public static TopicParameters getTopicParams() {
141         final var topicParams = new TopicParameters();
142         topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
143         topicParams.setTopicCommInfrastructure("dmaap");
144         topicParams.setServers(Arrays.asList("localhost"));
145         return topicParams;
146     }
147
148     /**
149      * Returns participantId for test cases.
150      *
151      * @return participant Id
152      */
153     public static ToscaConceptIdentifier getParticipantId() {
154         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
155     }
156
157     /**
158      * Returns a participantMessagePublisher for MessageSender.
159      *
160      * @return participant Message Publisher
161      */
162     private ParticipantMessagePublisher getParticipantMessagePublisher() {
163         synchronized (lockit) {
164             var participantMessagePublisher = new ParticipantMessagePublisher();
165             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
166             return participantMessagePublisher;
167         }
168     }
169
170     /**
171      * Returns a mocked AutomationCompositionHandler for test cases.
172      *
173      * @return AutomationCompositionHandler
174      */
175     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
176         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
177     }
178
179     /**
180      * Returns a mocked ParticipantHandler for test cases.
181      *
182      * @return participant Handler
183      */
184     public ParticipantHandler getMockParticipantHandler() {
185         var parameters = getParticipantParameters();
186         var automationCompositionHandler = getMockAutomationCompositionHandler();
187         var publisher = new ParticipantMessagePublisher();
188         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
189         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
190         return participantHandler;
191     }
192
193     /**
194      * Returns a mocked ParticipantHandler for test cases.
195      *
196      * @return participant Handler
197      *
198      * @throws CoderException if there is an error with .json file.
199      */
200     public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
201         var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
202         Mockito.doReturn(getTestAutomationCompositions()).when(automationCompositionHandler)
203             .getAutomationCompositions();
204         Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
205             .getAutomationCompositionMap();
206         var publisher = new ParticipantMessagePublisher();
207         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
208         var parameters = getParticipantParameters();
209         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
210         participantHandler.sendParticipantRegister();
211         participantHandler.handleParticipantStatusReq(null);
212         participantHandler.sendParticipantDeregister();
213         var participantDeregisterAckMsg = new ParticipantDeregisterAck();
214         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
215         participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
216         return participantHandler;
217     }
218
219     /**
220      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
221      *
222      * @return automationCompositionMap
223      *
224      * @throws CoderException if there is an error with .json file.
225      */
226     public Map<ToscaConceptIdentifier, AutomationComposition> getTestAutomationCompositionMap() throws CoderException {
227         var automationCompositions = getTestAutomationCompositions();
228         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
229         var id = getParticipantId();
230         Map<ToscaConceptIdentifier, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
231         automationCompositionMap.put(id, automationComposition);
232         return automationCompositionMap;
233     }
234
235     /**
236      * Returns List of AutomationComposition for test cases.
237      *
238      * @return AutomationCompositions
239      *
240      * @throws CoderException if there is an error with .json file.
241      */
242     public AutomationCompositions getTestAutomationCompositions() throws CoderException {
243         return new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
244             AutomationCompositions.class);
245     }
246
247     /**
248      * Returns a map for a elementsOnThisParticipant for test cases.
249      *
250      * @param uuid UUID and id ToscaConceptIdentifier
251      * @return a map suitable for elementsOnThisParticipant
252      */
253     public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
254         ToscaConceptIdentifier id) {
255         var acElement = new AutomationCompositionElement();
256         acElement.setId(uuid);
257         acElement.setParticipantId(id);
258         acElement.setDefinition(id);
259         acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
260
261         var acElementStatistics = new AcElementStatistics();
262         acElementStatistics.setParticipantId(id);
263         acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
264         acElementStatistics.setTimeStamp(Instant.now());
265
266         acElement.setAcElementStatistics(acElementStatistics);
267
268         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
269         elementsOnThisParticipant.put(uuid, acElement);
270         return elementsOnThisParticipant;
271     }
272
273     /**
274      * Returns a AutomationCompositionHandler with elements on the id,uuid.
275      *
276      * @param id ToscaConceptIdentifier and uuid UUID
277      * @return a AutomationCompositionHander with elements
278      */
279     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
280         throws CoderException {
281         var ach = getMockAutomationCompositionHandler();
282
283         var key = getTestAutomationCompositionMap().keySet().iterator().next();
284         var value = getTestAutomationCompositionMap().get(key);
285         ach.getAutomationCompositionMap().put(key, value);
286
287         var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
288         var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
289         ach.getElementsOnThisParticipant().put(keyElem, valueElem);
290
291         return ach;
292     }
293
294 }