11a060029ead0f3ededee42359143480f03fe052
[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.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import java.time.Instant;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
39 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
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.ParticipantRegisterAck;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck;
46 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
47 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
48 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
49 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.common.endpoints.event.comm.TopicSink;
51 import org.onap.policy.models.base.PfModelException;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53
54 class SupervisionMessagesTest extends CommonRestController {
55
56     private static final String NOT_ACTIVE = "Not Active!";
57     private static final Object lockit = new Object();
58     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
59     private static final String TOPIC = "my-topic";
60     private static SupervisionHandler supervisionHandler;
61
62     /**
63      * setup Db Provider Parameters.
64      *
65      * @throws PfModelException if an error occurs
66      */
67     @BeforeAll
68     public static void setupDbProviderParameters() throws PfModelException {
69         var acProvider = mock(AutomationCompositionProvider.class);
70         var participantProvider = mock(ParticipantProvider.class);
71         var acDefinitionProvider = Mockito.mock(AcDefinitionProvider.class);
72         var automationCompositionUpdatePublisher = Mockito.mock(AutomationCompositionUpdatePublisher.class);
73         var automationCompositionStateChangePublisher = Mockito.mock(AutomationCompositionStateChangePublisher.class);
74         var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class);
75         var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class);
76         var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class);
77         supervisionHandler = new SupervisionHandler(acProvider, participantProvider,
78             acDefinitionProvider, automationCompositionUpdatePublisher, automationCompositionStateChangePublisher,
79             participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher);
80     }
81
82     @Test
83     void testSendParticipantRegisterAck() throws Exception {
84         final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck();
85         participantRegisterAckMsg.setMessage("ParticipantRegisterAck message");
86         participantRegisterAckMsg.setResponseTo(UUID.randomUUID());
87         participantRegisterAckMsg.setResult(true);
88
89         synchronized (lockit) {
90             ParticipantRegisterAckPublisher acRegisterAckPublisher = new ParticipantRegisterAckPublisher();
91             acRegisterAckPublisher.active(List.of(Mockito.mock(TopicSink.class)));
92             assertThatCode(() -> acRegisterAckPublisher.send(participantRegisterAckMsg)).doesNotThrowAnyException();
93         }
94     }
95
96     @Test
97     void testReceiveParticipantDeregister() throws Exception {
98         final ParticipantDeregister participantDeregisterMsg = new ParticipantDeregister();
99         participantDeregisterMsg.setParticipantId(getParticipantId());
100         participantDeregisterMsg.setTimestamp(Instant.now());
101         participantDeregisterMsg.setParticipantType(getParticipantType());
102
103         synchronized (lockit) {
104             ParticipantDeregisterListener participantDeregisterListener =
105                 new ParticipantDeregisterListener(supervisionHandler);
106             assertThatCode(
107                 () -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg))
108                     .doesNotThrowAnyException();
109         }
110     }
111
112     @Test
113     void testSendParticipantDeregisterAck() throws Exception {
114         final ParticipantDeregisterAck participantDeregisterAckMsg = new ParticipantDeregisterAck();
115         participantDeregisterAckMsg.setMessage("ParticipantDeregisterAck message");
116         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
117         participantDeregisterAckMsg.setResult(true);
118
119         synchronized (lockit) {
120             ParticipantDeregisterAckPublisher acDeregisterAckPublisher = new ParticipantDeregisterAckPublisher();
121             acDeregisterAckPublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
122             assertThatCode(() -> acDeregisterAckPublisher.send(participantDeregisterAckMsg)).doesNotThrowAnyException();
123         }
124     }
125
126     @Test
127     void testReceiveParticipantUpdateAckMessage() throws Exception {
128         final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck();
129         participantUpdateAckMsg.setMessage("ParticipantUpdateAck message");
130         participantUpdateAckMsg.setResponseTo(UUID.randomUUID());
131         participantUpdateAckMsg.setResult(true);
132         participantUpdateAckMsg.setParticipantId(getParticipantId());
133         participantUpdateAckMsg.setParticipantType(getParticipantType());
134
135         synchronized (lockit) {
136             ParticipantUpdateAckListener participantUpdateAckListener =
137                 new ParticipantUpdateAckListener(supervisionHandler);
138             assertThatCode(() -> participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg))
139                 .doesNotThrowAnyException();
140         }
141     }
142
143     @Test
144     void testSendAutomationCompositionStateChangePublisherNotActive() {
145         var publisher = new AutomationCompositionStateChangePublisher();
146         assertThatThrownBy(() -> publisher.send(getAutomationComposition(), 0)).hasMessage(NOT_ACTIVE);
147     }
148
149     @Test
150     void testSendAutomationCompositionStateChangePublisher() {
151         var publisher = new AutomationCompositionStateChangePublisher();
152         var topicSink = mock(TopicSink.class);
153         publisher.active(List.of(topicSink));
154         publisher.send(getAutomationComposition(), 0);
155         verify(topicSink).send(anyString());
156     }
157
158     @Test
159     void testParticipantUpdatePublisherDecomisioning() {
160         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
161         var topicSink = mock(TopicSink.class);
162         publisher.active(List.of(topicSink));
163         publisher.sendDecomisioning();
164         verify(topicSink).send(anyString());
165     }
166
167     @Test
168     void testParticipantUpdatePublisherComissioning() {
169         var publisher = new ParticipantUpdatePublisher(mock(AcDefinitionProvider.class));
170         var topicSink = mock(TopicSink.class);
171         publisher.active(List.of(topicSink));
172         publisher.sendComissioningBroadcast("NAME", "1.0.0");
173         verify(topicSink, times(0)).send(anyString());
174     }
175
176     @Test
177     void testParticipantStatusReqPublisher() {
178         var publisher = new ParticipantStatusReqPublisher();
179         var topicSink = mock(TopicSink.class);
180         publisher.active(List.of(topicSink));
181         publisher.send(getParticipantId());
182         verify(topicSink).send(anyString());
183     }
184
185     @Test
186     void testParticipantRegisterAckPublisher() {
187         var publisher = new ParticipantRegisterAckPublisher();
188         var topicSink = mock(TopicSink.class);
189         publisher.active(List.of(topicSink));
190         publisher.send(UUID.randomUUID(), getParticipantId(), getParticipantType());
191         verify(topicSink).send(anyString());
192     }
193
194     @Test
195     void testParticipantDeregisterAckPublisher() {
196         var publisher = new ParticipantDeregisterAckPublisher();
197         var topicSink = mock(TopicSink.class);
198         publisher.active(List.of(topicSink));
199         publisher.send(UUID.randomUUID());
200         verify(topicSink).send(anyString());
201     }
202
203     private AutomationComposition getAutomationComposition() {
204         var automationComposition = new AutomationComposition();
205         automationComposition.setName("NAME");
206         automationComposition.setVersion("0.0.1");
207         automationComposition.setState(AutomationCompositionState.UNINITIALISED);
208         return automationComposition;
209     }
210
211     private ToscaConceptIdentifier getParticipantId() {
212         return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
213     }
214
215     private ToscaConceptIdentifier getParticipantType() {
216         return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
217     }
218 }