10c438f93e38de0fe9adc0a2b9a3733a6f46771b
[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.util.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
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;
47
48 /**
49  * Class to hold/create all parameters for test cases.
50  */
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();
60
61     /**
62      * Get ParticipantIntermediaryParameters.
63      *
64      * @return ParticipantIntermediaryParameters
65      */
66     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
67         try {
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);
72         }
73     }
74
75     /**
76      * Get ParticipantParameters.
77      *
78      * @return ParticipantParameters
79      */
80     public static DummyParticipantParameters getParticipantParameters() {
81         try {
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);
85         }
86     }
87
88     /**
89      * Returns a property map for a Parameters map for test cases.
90      *
91      * @param name name of the parameters
92      * @return a property map suitable for constructing an object
93      */
94     public static Map<String, Object> getParametersMap(final String name) {
95         final Map<String, Object> map = new TreeMap<>();
96         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
97         return map;
98     }
99
100     /**
101      * Returns a property map for a intermediaryParameters map for test cases.
102      *
103      * @param name name of the parameters
104      * @return a property map suitable for constructing an object
105      */
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));
114
115         return map;
116     }
117
118     /**
119      * Returns a property map for a TopicParameters map for test cases.
120      *
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
123      */
124     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
125         final Map<String, Object> map = new TreeMap<>();
126         if (!isEmpty) {
127             map.put("topicSources", TOPIC_PARAMS);
128             map.put("topicSinks", TOPIC_PARAMS);
129         }
130         return map;
131     }
132
133     /**
134      * Returns topic parameters for test cases.
135      *
136      * @return topic parameters
137      */
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"));
143         return topicParams;
144     }
145
146     /**
147      * Returns participantId for test cases.
148      *
149      * @return participant Id
150      */
151     public static ToscaConceptIdentifier getParticipantId() {
152         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
153     }
154
155     /**
156      * Returns a participantMessagePublisher for MessageSender.
157      *
158      * @return participant Message Publisher
159      */
160     private ParticipantMessagePublisher getParticipantMessagePublisher() {
161         synchronized (lockit) {
162             var participantMessagePublisher = new ParticipantMessagePublisher();
163             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
164             return participantMessagePublisher;
165         }
166     }
167
168     /**
169      * Returns a mocked AutomationCompositionHandler for test cases.
170      *
171      * @return AutomationCompositionHandler
172      */
173     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
174         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
175     }
176
177     /**
178      * Returns a mocked ParticipantHandler for test cases.
179      *
180      * @return participant Handler
181      */
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);
188     }
189
190     /**
191      * Returns a mocked ParticipantHandler for test cases.
192      *
193      * @return participant Handler
194      *
195      * @throws CoderException if there is an error with .json file.
196      */
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;
212     }
213
214     /**
215      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
216      *
217      * @return automationCompositionMap
218      *
219      * @throws CoderException if there is an error with .json file.
220      */
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;
227     }
228
229     /**
230      * Returns List of AutomationComposition for test cases.
231      *
232      * @return AutomationCompositions
233      *
234      * @throws CoderException if there is an error with .json file.
235      */
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;
242     }
243
244     /**
245      * Returns a map for a elementsOnThisParticipant for test cases.
246      *
247      * @param uuid UUID and id ToscaConceptIdentifier
248      * @return a map suitable for elementsOnThisParticipant
249      */
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);
257
258         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
259         elementsOnThisParticipant.put(uuid, acElement);
260         return elementsOnThisParticipant;
261     }
262
263     /**
264      * Returns a AutomationCompositionHandler with elements on the id,uuid.
265      *
266      * @param id ToscaConceptIdentifier and uuid UUID
267      * @return a AutomationCompositionHander with elements
268      */
269     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
270             throws CoderException {
271         var ach = getMockAutomationCompositionHandler();
272
273         var key = getTestAutomationCompositionMap().keySet().iterator().next();
274         var value = getTestAutomationCompositionMap().get(key);
275         ach.getAutomationCompositionMap().put(key, value);
276
277         var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
278         var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
279         ach.getElementsOnThisParticipant().put(keyElem, valueElem);
280
281         return ach;
282     }
283
284 }