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