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