4e862c4e992d908d990659bc6b403029f7c3bb89
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.supervision.comm;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.clearInvocations;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
38 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
39 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionAcHandler;
40 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
41 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionParticipantHandler;
42 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
43 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
45 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
46 import org.onap.policy.clamp.models.acm.concepts.DeployState;
47 import org.onap.policy.clamp.models.acm.concepts.LockState;
48 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
49 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
50 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
54 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
55 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
56 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
57 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
58 import org.onap.policy.common.message.bus.event.Topic.CommInfrastructure;
59 import org.onap.policy.common.message.bus.event.TopicSink;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
61
62 class SupervisionMessagesTest {
63
64     private static final String AC_INSTANTIATION_UPDATE_JSON =
65             "src/test/resources/rest/acm/AutomationCompositionUpdate.json";
66     private static final String NOT_ACTIVE = "Not Active!";
67     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
68     private static final String TOPIC = "my-topic";
69
70     private static final String TOSCA_ELEMENT_NAME = "org.onap.policy.clamp.acm.AutomationCompositionElement";
71
72     @Test
73     void testSendParticipantRegisterAck() {
74         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
75         var topicSink = mock(TopicSink.class);
76         acRegisterAckPublisher.active(topicSink);
77         acRegisterAckPublisher.send(new ParticipantRegisterAck());
78         verify(topicSink).send(anyString());
79         acRegisterAckPublisher.stop();
80     }
81
82     @Test
83     void testSendParticipantRegisterAckNoActive() {
84         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
85         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
86                 .hasMessageMatching(NOT_ACTIVE);
87     }
88
89     @Test
90     void testReceiveParticipantDeregister() {
91         final var participantDeregisterMsg = new ParticipantDeregister();
92         var supervisionHandler = mock(SupervisionParticipantHandler.class);
93         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
94         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
95         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
96     }
97
98     @Test
99     void testSendParticipantDeregisterAck() {
100         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
101         var topicSink = mock(TopicSink.class);
102         acDeregisterAckPublisher.active(topicSink);
103         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
104         verify(topicSink).send(anyString());
105         acDeregisterAckPublisher.stop();
106     }
107
108     @Test
109     void testSendParticipantDeregisterAckNoActive() {
110         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
111         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
112                 .hasMessageMatching(NOT_ACTIVE);
113     }
114
115     @Test
116     void testReceiveParticipantPrimeAckMessage() {
117         final var participantPrimeAckMsg = new ParticipantPrimeAck();
118         var supervisionHandler = mock(SupervisionHandler.class);
119         var participantPrimeAckListener = new ParticipantPrimeAckListener(supervisionHandler);
120         participantPrimeAckListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeAckMsg);
121         verify(supervisionHandler).handleParticipantMessage(participantPrimeAckMsg);
122     }
123
124     @Test
125     void testSendAutomationCompositionStateChangePublisherNotActive() {
126         var publisher = new AutomationCompositionStateChangePublisher();
127         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0, true)).hasMessage(NOT_ACTIVE);
128     }
129
130     private AutomationComposition getAutomationComposition() {
131         var automationComposition = new AutomationComposition();
132         automationComposition.setName("NAME");
133         automationComposition.setVersion("0.0.1");
134         automationComposition.setDeployState(DeployState.DEPLOYED);
135         automationComposition.setLockState(LockState.UNLOCKING);
136         return automationComposition;
137     }
138
139     @Test
140     void testSendAutomationCompositionStateChangePublisher() {
141         var publisher = new AutomationCompositionStateChangePublisher();
142         var topicSink = mock(TopicSink.class);
143         publisher.active(topicSink);
144         publisher.send(getAutomationComposition(), 0, true);
145         verify(topicSink).send(anyString());
146         publisher.stop();
147     }
148
149     @Test
150     void testParticipantPrimePublisherDecommissioning() {
151         var publisher = new ParticipantPrimePublisher(mock(ParticipantProvider.class),
152                 mock(AcRuntimeParameterGroup.class));
153         var topicSink = mock(TopicSink.class);
154         publisher.active(topicSink);
155         publisher.sendDepriming(UUID.randomUUID());
156         verify(topicSink).send(anyString());
157     }
158
159     @Test
160     void testParticipantPrimePublisherPriming() {
161         var participantId = UUID.randomUUID();
162         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
163         supportedElementMap.put(
164                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.PolicyAutomationCompositionElement", "1.0.0"),
165                 participantId);
166         supportedElementMap.put(new ToscaConceptIdentifier(
167                 "org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement", "1.0.0"), participantId);
168         supportedElementMap.put(
169                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.HttpAutomationCompositionElement", "1.0.0"),
170                 participantId);
171         var participantProvider = mock(ParticipantProvider.class);
172         when(participantProvider.getSupportedElementMap()).thenReturn(supportedElementMap);
173         var publisher = new ParticipantPrimePublisher(participantProvider, CommonTestData.getTestParamaterGroup());
174         var topicSink = mock(TopicSink.class);
175         publisher.active(topicSink);
176         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
177         serviceTemplate.setName("Name");
178         serviceTemplate.setVersion("1.0.0");
179         var acmDefinition = new AutomationCompositionDefinition();
180         acmDefinition.setCompositionId(UUID.randomUUID());
181         acmDefinition.setServiceTemplate(serviceTemplate);
182         var acElements = AcmUtils
183                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
184         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
185         var preparation = publisher.prepareParticipantPriming(acmDefinition);
186         publisher.sendPriming(preparation, acmDefinition.getCompositionId(), null);
187         verify(topicSink).send(anyString());
188     }
189
190     @Test
191     void testParticipantStatusReqPublisher() {
192         var publisher = new ParticipantStatusReqPublisher();
193         var topicSink = mock(TopicSink.class);
194         publisher.active(topicSink);
195         publisher.send(CommonTestData.getParticipantId());
196         verify(topicSink).send(anyString());
197     }
198
199     @Test
200     void testParticipantRegisterAckPublisher() {
201         var publisher = new ParticipantRegisterAckPublisher();
202         var topicSink = mock(TopicSink.class);
203         publisher.active(topicSink);
204         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId(), CommonTestData.getReplicaId());
205         verify(topicSink).send(anyString());
206     }
207
208     @Test
209     void testParticipantDeregisterAckPublisher() {
210         var publisher = new ParticipantDeregisterAckPublisher();
211         var topicSink = mock(TopicSink.class);
212         publisher.active(topicSink);
213         publisher.send(UUID.randomUUID());
214         verify(topicSink).send(anyString());
215     }
216
217     @Test
218     void testAcElementPropertiesPublisher() {
219         var publisher = new AcElementPropertiesPublisher();
220         var topicSink = mock(TopicSink.class);
221         publisher.active(topicSink);
222         var automationComposition =
223                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
224         publisher.send(automationComposition);
225         verify(topicSink).send(anyString());
226     }
227
228     @Test
229     void testAutomationCompositionMigrationPublisher() {
230         var publisher = new AutomationCompositionMigrationPublisher();
231         var topicSink = mock(TopicSink.class);
232         publisher.active(topicSink);
233         var automationComposition =
234                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
235         publisher.send(automationComposition, 0);
236         verify(topicSink).send(anyString());
237     }
238
239     @Test
240     void testAcPreparePublisher() {
241         var publisher = new AcPreparePublisher();
242         var topicSink = mock(TopicSink.class);
243         publisher.active(topicSink);
244         var automationComposition =
245                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
246         publisher.sendPrepare(automationComposition);
247         verify(topicSink).send(anyString());
248     }
249
250     @Test
251     void testAcReviewPublisher() {
252         var publisher = new AcPreparePublisher();
253         var topicSink = mock(TopicSink.class);
254         publisher.active(topicSink);
255         var automationComposition =
256                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
257         publisher.sendRevew(automationComposition);
258         verify(topicSink).send(anyString());
259     }
260
261     @Test
262     void testParticipantSyncPublisherAutomationComposition() {
263         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
264         var topicSink = mock(TopicSink.class);
265         publisher.active(topicSink);
266
267         var automationComposition =
268                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
269         publisher.sendSync(automationComposition);
270         verify(topicSink).send(anyString());
271
272         clearInvocations(topicSink);
273         automationComposition.setDeployState(DeployState.DELETED);
274         publisher.sendSync(automationComposition);
275         verify(topicSink).send(anyString());
276     }
277
278     @Test
279     void testParticipantSyncPublisherAcDefinition() {
280         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
281         var topicSink = mock(TopicSink.class);
282         publisher.active(topicSink);
283
284         var acmDefinition = getAcmDefinition();
285         publisher.sendSync(acmDefinition, null);
286         verify(topicSink).send(anyString());
287     }
288
289     @Test
290     void testParticipantSyncPublisherAcDefinitionCommissioned() {
291         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
292         var topicSink = mock(TopicSink.class);
293         publisher.active(topicSink);
294
295         var acmDefinition = getAcmDefinition();
296         acmDefinition.setState(AcTypeState.COMMISSIONED);
297         publisher.sendSync(acmDefinition, UUID.randomUUID());
298         verify(topicSink).send(anyString());
299     }
300
301     @Test
302     void testParticipantSyncPublisherRestart() {
303         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
304         var topicSink = mock(TopicSink.class);
305         publisher.active(topicSink);
306
307         var automationComposition =
308                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
309         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
310         var acmDefinition = getAcmDefinition();
311         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
312         var replicaId = UUID.randomUUID();
313         publisher.sendRestartMsg(participantId, replicaId, acmDefinition, List.of(automationComposition));
314         verify(topicSink).send(anyString());
315     }
316
317     private AutomationCompositionDefinition getAcmDefinition() {
318         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
319         var acmDefinition = new AutomationCompositionDefinition();
320         acmDefinition.setCompositionId(UUID.randomUUID());
321         acmDefinition.setState(AcTypeState.PRIMED);
322         acmDefinition.setServiceTemplate(serviceTemplate);
323         var acElements = AcmUtils
324                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
325         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
326         acmDefinition.getElementStateMap().values().forEach(element -> element.setParticipantId(UUID.randomUUID()));
327         return acmDefinition;
328     }
329
330     @Test
331     void testParticipantRegisterListener() {
332         final var participantRegister = new ParticipantRegister();
333         var supervisionHandler = mock(SupervisionParticipantHandler.class);
334         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
335         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
336         verify(supervisionHandler).handleParticipantMessage(participantRegister);
337     }
338
339     @Test
340     void testParticipantStatusListener() {
341         final var participantStatus = new ParticipantStatus();
342         var supervisionHandler = mock(SupervisionParticipantHandler.class);
343         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
344         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
345         verify(supervisionHandler).handleParticipantMessage(participantStatus);
346     }
347
348     @Test
349     void testAutomationCompositionUpdateAckListener() {
350         final var automationCompositionAck =
351                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
352         var supervisionHandler = mock(SupervisionAcHandler.class);
353         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
354         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
355         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
356     }
357
358     @Test
359     void testAutomationCompositionStateChangeAckListener() {
360         final var automationCompositionAck =
361                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
362         var supervisionHandler = mock(SupervisionAcHandler.class);
363         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
364         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
365         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
366     }
367 }