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