31cd659b36ec7cdf747e0e4b65f2022ba54b8f47
[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.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
30
31 import java.util.Collections;
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.main.parameters.Topics;
40 import org.onap.policy.clamp.acm.runtime.participants.AcmParticipantProvider;
41 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionAcHandler;
42 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
43 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionParticipantHandler;
44 import org.onap.policy.clamp.acm.runtime.util.CommonTestData;
45 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
46 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
47 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
48 import org.onap.policy.clamp.models.acm.concepts.DeployState;
49 import org.onap.policy.clamp.models.acm.concepts.LockState;
50 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
54 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
55 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
56 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
57 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
58 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
59 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
60 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
61 import org.onap.policy.common.endpoints.event.comm.TopicSink;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
63
64 class SupervisionMessagesTest {
65
66     private static final String AC_INSTANTIATION_UPDATE_JSON =
67             "src/test/resources/rest/acm/AutomationCompositionUpdate.json";
68     private static final String NOT_ACTIVE = "Not Active!";
69     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
70     private static final String TOPIC = "my-topic";
71
72     private static final String TOSCA_ELEMENT_NAME = "org.onap.policy.clamp.acm.AutomationCompositionElement";
73
74     @Test
75     void testSendParticipantRegisterAck() {
76         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
77         var topicSink = mock(TopicSink.class);
78         acRegisterAckPublisher.active(topicSink);
79         acRegisterAckPublisher.send(new ParticipantRegisterAck());
80         verify(topicSink).send(anyString());
81         acRegisterAckPublisher.stop();
82     }
83
84     @Test
85     void testSendParticipantRegisterAckNoActive() {
86         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
87         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
88                 .hasMessageMatching(NOT_ACTIVE);
89     }
90
91     @Test
92     void testReceiveParticipantDeregister() {
93         final var participantDeregisterMsg = new ParticipantDeregister();
94         var supervisionHandler = mock(SupervisionParticipantHandler.class);
95         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
96         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
97         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
98     }
99
100     @Test
101     void testSendParticipantDeregisterAck() {
102         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
103         var topicSink = mock(TopicSink.class);
104         acDeregisterAckPublisher.active(topicSink);
105         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
106         verify(topicSink).send(anyString());
107         acDeregisterAckPublisher.stop();
108     }
109
110     void testSendParticipantDeregisterAckNoActive() {
111         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
112         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
113                 .hasMessageMatching(NOT_ACTIVE);
114     }
115
116     @Test
117     void testReceiveParticipantPrimeAckMessage() {
118         final var participantPrimeAckMsg = new ParticipantPrimeAck();
119         var supervisionHandler = mock(SupervisionHandler.class);
120         var participantPrimeAckListener = new ParticipantPrimeAckListener(supervisionHandler);
121         participantPrimeAckListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeAckMsg);
122         verify(supervisionHandler).handleParticipantMessage(participantPrimeAckMsg);
123     }
124
125     @Test
126     void testSendAutomationCompositionStateChangePublisherNotActive() {
127         var publisher = new AutomationCompositionStateChangePublisher();
128         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0, true)).hasMessage(NOT_ACTIVE);
129     }
130
131     private AutomationComposition getAutomationComposition() {
132         var automationComposition = new AutomationComposition();
133         automationComposition.setName("NAME");
134         automationComposition.setVersion("0.0.1");
135         automationComposition.setDeployState(DeployState.DEPLOYED);
136         automationComposition.setLockState(LockState.UNLOCKING);
137         return automationComposition;
138     }
139
140     @Test
141     void testSendAutomationCompositionStateChangePublisher() {
142         var publisher = new AutomationCompositionStateChangePublisher();
143         var topicSink = mock(TopicSink.class);
144         publisher.active(topicSink);
145         publisher.send(getAutomationComposition(), 0, true);
146         verify(topicSink).send(anyString());
147         publisher.stop();
148     }
149
150     @Test
151     void testParticipantPrimePublisherDecommissioning() {
152         var publisher = new ParticipantPrimePublisher(mock(ParticipantProvider.class),
153                 mock(AcmParticipantProvider.class), mock(AcRuntimeParameterGroup.class));
154         var topicSink = mock(TopicSink.class);
155         publisher.active(topicSink);
156         publisher.sendDepriming(UUID.randomUUID());
157         verify(topicSink).send(anyString());
158     }
159
160     @Test
161     void testParticipantPrimePublisherPriming() {
162         var participantId = UUID.randomUUID();
163         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
164         supportedElementMap.put(
165                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.PolicyAutomationCompositionElement", "1.0.0"),
166                 participantId);
167         supportedElementMap.put(new ToscaConceptIdentifier(
168                 "org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement", "1.0.0"), participantId);
169         supportedElementMap.put(
170                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.HttpAutomationCompositionElement", "1.0.0"),
171                 participantId);
172         var participantProvider = mock(ParticipantProvider.class);
173         when(participantProvider.getSupportedElementMap()).thenReturn(supportedElementMap);
174         var publisher = new ParticipantPrimePublisher(participantProvider, mock(AcmParticipantProvider.class),
175                 CommonTestData.getTestParamaterGroup());
176         var topicSink = mock(TopicSink.class);
177         publisher.active(topicSink);
178         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
179         serviceTemplate.setName("Name");
180         serviceTemplate.setVersion("1.0.0");
181         var acmDefinition = new AutomationCompositionDefinition();
182         acmDefinition.setCompositionId(UUID.randomUUID());
183         acmDefinition.setServiceTemplate(serviceTemplate);
184         var acElements = AcmUtils
185                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
186         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
187         var preparation = publisher.prepareParticipantPriming(acmDefinition);
188         publisher.sendPriming(preparation, acmDefinition.getCompositionId(), null);
189         verify(topicSink).send(anyString());
190     }
191
192     @Test
193     void testParticipantStatusReqPublisher() {
194         var publisher = new ParticipantStatusReqPublisher();
195         var topicSink = mock(TopicSink.class);
196         publisher.active(topicSink);
197         publisher.send(CommonTestData.getParticipantId());
198         verify(topicSink).send(anyString());
199     }
200
201     @Test
202     void testParticipantRegisterAckPublisher() {
203         var publisher = new ParticipantRegisterAckPublisher();
204         var topicSink = mock(TopicSink.class);
205         publisher.active(topicSink);
206         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId());
207         verify(topicSink).send(anyString());
208     }
209
210     @Test
211     void testParticipantDeregisterAckPublisher() {
212         var publisher = new ParticipantDeregisterAckPublisher();
213         var topicSink = mock(TopicSink.class);
214         publisher.active(topicSink);
215         publisher.send(UUID.randomUUID());
216         verify(topicSink).send(anyString());
217     }
218
219     @Test
220     void testAcElementPropertiesPublisher() {
221         var publisher = new AcElementPropertiesPublisher();
222         var topicSink = mock(TopicSink.class);
223         publisher.active(topicSink);
224         var automationComposition =
225                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
226         publisher.send(automationComposition);
227         verify(topicSink).send(anyString());
228     }
229
230     @Test
231     void testAutomationCompositionMigrationPublisher() {
232         var publisher = new AutomationCompositionMigrationPublisher();
233         var topicSink = mock(TopicSink.class);
234         publisher.active(topicSink);
235         var automationComposition =
236                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
237         publisher.send(automationComposition, UUID.randomUUID());
238         verify(topicSink).send(anyString());
239     }
240
241     @Test
242     void testParticipantRestartPublisher() {
243         var publisher = new ParticipantRestartPublisher(CommonTestData.getTestParamaterGroup());
244         var topicSink = mock(TopicSink.class);
245         publisher.active(topicSink);
246
247         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
248         var acmDefinition = new AutomationCompositionDefinition();
249         acmDefinition.setCompositionId(UUID.randomUUID());
250         acmDefinition.setServiceTemplate(serviceTemplate);
251         var acElements = AcmUtils
252                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
253         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
254
255         var automationComposition =
256                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
257
258         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
259         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
260
261         publisher.send(participantId, acmDefinition, List.of(automationComposition));
262         verify(topicSink).send(anyString());
263     }
264
265     @Test
266     void testParticipantSyncPublisher() {
267         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
268         var topicSink = mock(TopicSink.class);
269         publisher.active(topicSink);
270
271         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
272         var acmDefinition = new AutomationCompositionDefinition();
273         acmDefinition.setCompositionId(UUID.randomUUID());
274         acmDefinition.setServiceTemplate(serviceTemplate);
275         var acElements = AcmUtils
276                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
277         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
278
279         var automationComposition =
280                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
281
282         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
283         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
284
285         publisher.send(participantId, acmDefinition, List.of(automationComposition));
286         verify(topicSink).send(anyString());
287     }
288
289     @Test
290     void testParticipantRegisterListener() {
291         final var participantRegister = new ParticipantRegister();
292         var supervisionHandler = mock(SupervisionParticipantHandler.class);
293         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
294         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
295         verify(supervisionHandler).handleParticipantMessage(participantRegister);
296     }
297
298     @Test
299     void testParticipantStatusListener() {
300         final var participantStatus = new ParticipantStatus();
301         var supervisionHandler = mock(SupervisionParticipantHandler.class);
302         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
303         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
304         verify(supervisionHandler).handleParticipantMessage(participantStatus);
305     }
306
307     @Test
308     void testAutomationCompositionUpdateAckListener() {
309         final var automationCompositionAck =
310                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
311         var supervisionHandler = mock(SupervisionAcHandler.class);
312         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
313         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
314         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
315     }
316
317     @Test
318     void testAutomationCompositionStateChangeAckListener() {
319         final var automationCompositionAck =
320                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
321         var supervisionHandler = mock(SupervisionAcHandler.class);
322         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
323         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
324         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
325     }
326 }