91552ee34b4f9c7e9843128c8ad0a229ba299f2b
[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
59     /**
60      * Get ParticipantIntermediaryParameters.
61      *
62      * @return ParticipantIntermediaryParameters
63      */
64     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
65         try {
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);
70         }
71     }
72
73     /**
74      * Get ParticipantParameters.
75      *
76      * @return ParticipantParameters
77      */
78     public static DummyParticipantParameters getParticipantParameters() {
79         try {
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);
83         }
84     }
85
86     /**
87      * Returns a property map for a Parameters map for test cases.
88      *
89      * @param name name of the parameters
90      * @return a property map suitable for constructing an object
91      */
92     public static Map<String, Object> getParametersMap(final String name) {
93         final Map<String, Object> map = new TreeMap<>();
94         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
95         return map;
96     }
97
98     /**
99      * Returns a property map for a intermediaryParameters map for test cases.
100      *
101      * @param name name of the parameters
102      * @return a property map suitable for constructing an object
103      */
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));
112
113         return map;
114     }
115
116     /**
117      * Returns a property map for a TopicParameters map for test cases.
118      *
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
121      */
122     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
123         final Map<String, Object> map = new TreeMap<>();
124         if (!isEmpty) {
125             map.put("topicSources", TOPIC_PARAMS);
126             map.put("topicSinks", TOPIC_PARAMS);
127         }
128         return map;
129     }
130
131     /**
132      * Returns topic parameters for test cases.
133      *
134      * @return topic parameters
135      */
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"));
141         return topicParams;
142     }
143
144     /**
145      * Returns participantId for test cases.
146      *
147      * @return participant Id
148      */
149     public static ToscaConceptIdentifier getParticipantId() {
150         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
151     }
152
153     /**
154      * Returns a participantMessagePublisher for MessageSender.
155      *
156      * @return participant Message Publisher
157      */
158     private ParticipantMessagePublisher getParticipantMessagePublisher() {
159         synchronized (lockit) {
160             var participantMessagePublisher = new ParticipantMessagePublisher();
161             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
162             return participantMessagePublisher;
163         }
164     }
165
166     /**
167      * Returns a mocked AutomationCompositionHandler for test cases.
168      *
169      * @return AutomationCompositionHandler
170      */
171     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
172         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
173     }
174
175     /**
176      * Returns a mocked ParticipantHandler for test cases.
177      *
178      * @return participant Handler
179      */
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);
186     }
187
188     /**
189      * Returns a mocked ParticipantHandler for test cases.
190      *
191      * @return participant Handler
192      *
193      * @throws CoderException if there is an error with .json file.
194      */
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;
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<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;
228     }
229
230     /**
231      * Returns List of AutomationComposition for test cases.
232      *
233      * @return AutomationCompositions
234      *
235      * @throws CoderException if there is an error with .json file.
236      */
237     public AutomationCompositions getTestAutomationCompositions() throws CoderException {
238         return new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
239             AutomationCompositions.class);
240     }
241
242     /**
243      * Returns a map for a elementsOnThisParticipant for test cases.
244      *
245      * @param uuid UUID and id ToscaConceptIdentifier
246      * @return a map suitable for elementsOnThisParticipant
247      */
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);
255
256         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
257         elementsOnThisParticipant.put(uuid, acElement);
258         return elementsOnThisParticipant;
259     }
260
261     /**
262      * Returns a AutomationCompositionHandler with elements on the id,uuid.
263      *
264      * @param id ToscaConceptIdentifier and uuid UUID
265      * @return a AutomationCompositionHander with elements
266      */
267     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
268         throws CoderException {
269         var ach = getMockAutomationCompositionHandler();
270
271         var key = getTestAutomationCompositionMap().keySet().iterator().next();
272         var value = getTestAutomationCompositionMap().get(key);
273         ach.getAutomationCompositionMap().put(key, value);
274
275         var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
276         var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
277         ach.getElementsOnThisParticipant().put(keyElem, valueElem);
278
279         return ach;
280     }
281
282 }