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