766380ac4f3d94a36c36d16c04b0bfbfa0ba9692
[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.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
37 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
38 import org.onap.policy.clamp.acm.runtime.participants.AcmParticipantProvider;
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.endpoints.event.comm.Topic.CommInfrastructure;
59 import org.onap.policy.common.endpoints.event.comm.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(AcmParticipantProvider.class), 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, mock(AcmParticipantProvider.class),
174                 CommonTestData.getTestParamaterGroup());
175         var topicSink = mock(TopicSink.class);
176         publisher.active(topicSink);
177         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
178         serviceTemplate.setName("Name");
179         serviceTemplate.setVersion("1.0.0");
180         var acmDefinition = new AutomationCompositionDefinition();
181         acmDefinition.setCompositionId(UUID.randomUUID());
182         acmDefinition.setServiceTemplate(serviceTemplate);
183         var acElements = AcmUtils
184                 .extractAcElementsFromServiceTemplate(serviceTemplate, TOSCA_ELEMENT_NAME);
185         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
186         var preparation = publisher.prepareParticipantPriming(acmDefinition);
187         publisher.sendPriming(preparation, acmDefinition.getCompositionId(), null);
188         verify(topicSink).send(anyString());
189     }
190
191     @Test
192     void testParticipantStatusReqPublisher() {
193         var publisher = new ParticipantStatusReqPublisher();
194         var topicSink = mock(TopicSink.class);
195         publisher.active(topicSink);
196         publisher.send(CommonTestData.getParticipantId());
197         verify(topicSink).send(anyString());
198     }
199
200     @Test
201     void testParticipantRegisterAckPublisher() {
202         var publisher = new ParticipantRegisterAckPublisher();
203         var topicSink = mock(TopicSink.class);
204         publisher.active(topicSink);
205         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId());
206         verify(topicSink).send(anyString());
207     }
208
209     @Test
210     void testParticipantDeregisterAckPublisher() {
211         var publisher = new ParticipantDeregisterAckPublisher();
212         var topicSink = mock(TopicSink.class);
213         publisher.active(topicSink);
214         publisher.send(UUID.randomUUID());
215         verify(topicSink).send(anyString());
216     }
217
218     @Test
219     void testAcElementPropertiesPublisher() {
220         var publisher = new AcElementPropertiesPublisher();
221         var topicSink = mock(TopicSink.class);
222         publisher.active(topicSink);
223         var automationComposition =
224                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
225         publisher.send(automationComposition);
226         verify(topicSink).send(anyString());
227     }
228
229     @Test
230     void testAutomationCompositionMigrationPublisher() {
231         var publisher = new AutomationCompositionMigrationPublisher();
232         var topicSink = mock(TopicSink.class);
233         publisher.active(topicSink);
234         var automationComposition =
235                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
236         publisher.send(automationComposition, UUID.randomUUID());
237         verify(topicSink).send(anyString());
238     }
239
240     @Test
241     void testParticipantRestartPublisher() {
242         var publisher = new ParticipantRestartPublisher(CommonTestData.getTestParamaterGroup());
243         var topicSink = mock(TopicSink.class);
244         publisher.active(topicSink);
245
246         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
247         var acmDefinition = new AutomationCompositionDefinition();
248         acmDefinition.setCompositionId(UUID.randomUUID());
249         acmDefinition.setServiceTemplate(serviceTemplate);
250         var acElements = AcmUtils
251                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
252         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
253
254         var automationComposition =
255                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
256
257         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
258         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
259
260         publisher.send(participantId, acmDefinition, List.of(automationComposition));
261         verify(topicSink).send(anyString());
262     }
263
264     @Test
265     void testParticipantSyncPublisher() {
266         var publisher = new ParticipantSyncPublisher(CommonTestData.getTestParamaterGroup());
267         var topicSink = mock(TopicSink.class);
268         publisher.active(topicSink);
269
270         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
271         var acmDefinition = new AutomationCompositionDefinition();
272         acmDefinition.setCompositionId(UUID.randomUUID());
273         acmDefinition.setServiceTemplate(serviceTemplate);
274         var acElements = AcmUtils
275                 .extractAcElementsFromServiceTemplate(serviceTemplate, "");
276         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
277
278         var automationComposition =
279                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
280
281         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
282         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
283
284         publisher.send(participantId, acmDefinition, List.of(automationComposition));
285         verify(topicSink).send(anyString());
286     }
287
288     @Test
289     void testParticipantRegisterListener() {
290         final var participantRegister = new ParticipantRegister();
291         var supervisionHandler = mock(SupervisionParticipantHandler.class);
292         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
293         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
294         verify(supervisionHandler).handleParticipantMessage(participantRegister);
295     }
296
297     @Test
298     void testParticipantStatusListener() {
299         final var participantStatus = new ParticipantStatus();
300         var supervisionHandler = mock(SupervisionParticipantHandler.class);
301         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
302         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
303         verify(supervisionHandler).handleParticipantMessage(participantStatus);
304     }
305
306     @Test
307     void testAutomationCompositionUpdateAckListener() {
308         final var automationCompositionAck =
309                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
310         var supervisionHandler = mock(SupervisionAcHandler.class);
311         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
312         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
313         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
314     }
315
316     @Test
317     void testAutomationCompositionStateChangeAckListener() {
318         final var automationCompositionAck =
319                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
320         var supervisionHandler = mock(SupervisionAcHandler.class);
321         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
322         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
323         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
324     }
325 }