06684d86323415a104112d73593104f7860eb66a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.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.dmaap.participant.AutomationCompositionDeployAck;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
51 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
52 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
53 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
54 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
55 import org.onap.policy.clamp.models.acm.messages.dmaap.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     @Test
71     void testSendParticipantRegisterAck() {
72         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
73         var topicSink = mock(TopicSink.class);
74         acRegisterAckPublisher.active(List.of(topicSink));
75         acRegisterAckPublisher.send(new ParticipantRegisterAck());
76         verify(topicSink).send(anyString());
77         acRegisterAckPublisher.stop();
78     }
79
80     @Test
81     void testSendParticipantRegisterAckNoActive() {
82         var acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
83         assertThatThrownBy(() -> acRegisterAckPublisher.send(new ParticipantRegisterAck()))
84                 .hasMessageMatching(NOT_ACTIVE);
85     }
86
87     @Test
88     void testReceiveParticipantDeregister() {
89         final var participantDeregisterMsg = new ParticipantDeregister();
90         var supervisionHandler = mock(SupervisionParticipantHandler.class);
91         var participantDeregisterListener = new ParticipantDeregisterListener(supervisionHandler);
92         participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg);
93         verify(supervisionHandler).handleParticipantMessage(participantDeregisterMsg);
94     }
95
96     @Test
97     void testSendParticipantDeregisterAck() {
98         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
99         var topicSink = mock(TopicSink.class);
100         acDeregisterAckPublisher.active(Collections.singletonList(topicSink));
101         acDeregisterAckPublisher.send(new ParticipantDeregisterAck());
102         verify(topicSink).send(anyString());
103         acDeregisterAckPublisher.stop();
104     }
105
106     void testSendParticipantDeregisterAckNoActive() {
107         var acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
108         assertThatThrownBy(() -> acDeregisterAckPublisher.send(new ParticipantDeregisterAck()))
109                 .hasMessageMatching(NOT_ACTIVE);
110     }
111
112     @Test
113     void testReceiveParticipantPrimeAckMessage() {
114         final var participantPrimeAckMsg = new ParticipantPrimeAck();
115         var supervisionHandler = mock(SupervisionHandler.class);
116         var participantPrimeAckListener = new ParticipantPrimeAckListener(supervisionHandler);
117         participantPrimeAckListener.onTopicEvent(INFRA, TOPIC, null, participantPrimeAckMsg);
118         verify(supervisionHandler).handleParticipantMessage(participantPrimeAckMsg);
119     }
120
121     @Test
122     void testSendAutomationCompositionStateChangePublisherNotActive() {
123         var publisher = new AutomationCompositionStateChangePublisher();
124         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0, true)).hasMessage(NOT_ACTIVE);
125     }
126
127     private AutomationComposition getAutomationComposition() {
128         var automationComposition = new AutomationComposition();
129         automationComposition.setName("NAME");
130         automationComposition.setVersion("0.0.1");
131         automationComposition.setDeployState(DeployState.DEPLOYED);
132         automationComposition.setLockState(LockState.UNLOCKING);
133         return automationComposition;
134     }
135
136     @Test
137     void testSendAutomationCompositionStateChangePublisher() {
138         var publisher = new AutomationCompositionStateChangePublisher();
139         var topicSink = mock(TopicSink.class);
140         publisher.active(List.of(topicSink));
141         publisher.send(getAutomationComposition(), 0, true);
142         verify(topicSink).send(anyString());
143         publisher.stop();
144     }
145
146     @Test
147     void testParticipantPrimePublisherDecommissioning() {
148         var publisher = new ParticipantPrimePublisher(mock(ParticipantProvider.class),
149                 mock(AcmParticipantProvider.class));
150         var topicSink = mock(TopicSink.class);
151         publisher.active(List.of(topicSink));
152         publisher.sendDepriming(UUID.randomUUID());
153         verify(topicSink).send(anyString());
154     }
155
156     @Test
157     void testParticipantPrimePublisherPriming() {
158         var participantId = UUID.randomUUID();
159         Map<ToscaConceptIdentifier, UUID> supportedElementMap = new HashMap<>();
160         supportedElementMap.put(
161                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.PolicyAutomationCompositionElement", "1.0.0"),
162                 participantId);
163         supportedElementMap.put(new ToscaConceptIdentifier(
164                 "org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement", "1.0.0"), participantId);
165         supportedElementMap.put(
166                 new ToscaConceptIdentifier("org.onap.policy.clamp.acm.HttpAutomationCompositionElement", "1.0.0"),
167                 participantId);
168         var participantProvider = mock(ParticipantProvider.class);
169         when(participantProvider.getSupportedElementMap()).thenReturn(supportedElementMap);
170         var publisher = new ParticipantPrimePublisher(participantProvider, mock(AcmParticipantProvider.class));
171         var topicSink = mock(TopicSink.class);
172         publisher.active(List.of(topicSink));
173         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
174         serviceTemplate.setName("Name");
175         serviceTemplate.setVersion("1.0.0");
176         var acmDefinition = new AutomationCompositionDefinition();
177         acmDefinition.setCompositionId(UUID.randomUUID());
178         acmDefinition.setServiceTemplate(serviceTemplate);
179         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
180         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED));
181         var preparation = publisher.prepareParticipantPriming(acmDefinition);
182         publisher.sendPriming(preparation, acmDefinition.getCompositionId(), null);
183         verify(topicSink).send(anyString());
184     }
185
186     @Test
187     void testParticipantStatusReqPublisher() {
188         var publisher = new ParticipantStatusReqPublisher();
189         var topicSink = mock(TopicSink.class);
190         publisher.active(List.of(topicSink));
191         publisher.send(CommonTestData.getParticipantId());
192         verify(topicSink).send(anyString());
193     }
194
195     @Test
196     void testParticipantRegisterAckPublisher() {
197         var publisher = new ParticipantRegisterAckPublisher();
198         var topicSink = mock(TopicSink.class);
199         publisher.active(List.of(topicSink));
200         publisher.send(UUID.randomUUID(), CommonTestData.getParticipantId());
201         verify(topicSink).send(anyString());
202     }
203
204     @Test
205     void testParticipantDeregisterAckPublisher() {
206         var publisher = new ParticipantDeregisterAckPublisher();
207         var topicSink = mock(TopicSink.class);
208         publisher.active(List.of(topicSink));
209         publisher.send(UUID.randomUUID());
210         verify(topicSink).send(anyString());
211     }
212
213     @Test
214     void testAcElementPropertiesPublisher() {
215         var publisher = new AcElementPropertiesPublisher();
216         var topicSink = mock(TopicSink.class);
217         publisher.active(List.of(topicSink));
218         var automationComposition =
219                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
220         publisher.send(automationComposition);
221         verify(topicSink).send(anyString());
222     }
223
224     @Test
225     void testParticipantRestartPublisher() {
226         var publisher = new ParticipantRestartPublisher();
227         var topicSink = mock(TopicSink.class);
228         publisher.active(List.of(topicSink));
229
230         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
231         // serviceTemplate.setName("Name");
232         // serviceTemplate.setVersion("1.0.0");
233         var acmDefinition = new AutomationCompositionDefinition();
234         acmDefinition.setCompositionId(UUID.randomUUID());
235         acmDefinition.setServiceTemplate(serviceTemplate);
236         var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate);
237         acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.PRIMED));
238
239         var automationComposition =
240                 InstantiationUtils.getAutomationCompositionFromResource(AC_INSTANTIATION_UPDATE_JSON, "Crud");
241
242         var participantId = automationComposition.getElements().values().iterator().next().getParticipantId();
243         acmDefinition.getElementStateMap().values().iterator().next().setParticipantId(participantId);
244
245         publisher.send(participantId, acmDefinition, List.of(automationComposition));
246         verify(topicSink).send(anyString());
247     }
248
249     @Test
250     void testParticipantRegisterListener() {
251         final var participantRegister = new ParticipantRegister();
252         var supervisionHandler = mock(SupervisionParticipantHandler.class);
253         var participantRegisterListener = new ParticipantRegisterListener(supervisionHandler);
254         participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegister);
255         verify(supervisionHandler).handleParticipantMessage(participantRegister);
256     }
257
258     @Test
259     void testParticipantStatusListener() {
260         final var participantStatus = new ParticipantStatus();
261         var supervisionHandler = mock(SupervisionParticipantHandler.class);
262         var participantStatusListener = new ParticipantStatusListener(supervisionHandler);
263         participantStatusListener.onTopicEvent(INFRA, TOPIC, null, participantStatus);
264         verify(supervisionHandler).handleParticipantMessage(participantStatus);
265     }
266
267     @Test
268     void testAutomationCompositionUpdateAckListener() {
269         final var automationCompositionAck =
270                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
271         var supervisionHandler = mock(SupervisionAcHandler.class);
272         var acUpdateAckListener = new AutomationCompositionUpdateAckListener(supervisionHandler);
273         acUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
274         verify(supervisionHandler).handleAutomationCompositionUpdateAckMessage(automationCompositionAck);
275     }
276
277     @Test
278     void testAutomationCompositionStateChangeAckListener() {
279         final var automationCompositionAck =
280                 new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
281         var supervisionHandler = mock(SupervisionAcHandler.class);
282         var acStateChangeAckListener = new AutomationCompositionStateChangeAckListener(supervisionHandler);
283         acStateChangeAckListener.onTopicEvent(INFRA, TOPIC, null, automationCompositionAck);
284         verify(supervisionHandler).handleAutomationCompositionStateChangeAckMessage(automationCompositionAck);
285     }
286
287 }