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