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