2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
35 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionMigration;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRestart;
51 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
52 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
53 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
55 class ParticipantHandlerTest {
58 void handleParticipantStatusReqTest() {
59 var publisher = mock(ParticipantMessagePublisher.class);
60 var cacheProvider = mock(CacheProvider.class);
61 var participantHandler =
62 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
63 participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
64 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
68 void handleAutomationCompositionDeployTest() {
69 var acHandler = mock(AutomationCompositionHandler.class);
70 var participantHandler =
71 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
72 var automationCompositionDeploy = new AutomationCompositionDeploy();
73 participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
74 verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
78 void handleAutomationCompositionStateChangeTest() {
79 var acHandler = mock(AutomationCompositionHandler.class);
80 var participantHandler =
81 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
82 var acStateChange = new AutomationCompositionStateChange();
83 participantHandler.handleAutomationCompositionStateChange(acStateChange);
84 verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
88 void handleAutomationCompositionMigrationTest() {
89 var acHandler = mock(AutomationCompositionHandler.class);
90 var participantHandler =
91 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
92 var migrationMsg = new AutomationCompositionMigration();
93 participantHandler.handleAutomationCompositionMigration(migrationMsg);
94 verify(acHandler).handleAutomationCompositionMigration(migrationMsg);
98 void handleAcPropertyUpdateTest() {
99 var acHandler = mock(AutomationCompositionHandler.class);
100 var participantHandler =
101 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
102 var propertyUpdateMsg = new PropertiesUpdate();
103 participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
104 verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
108 void appliesToTest() {
109 var cacheProvider = mock(CacheProvider.class);
110 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
111 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
112 mock(ParticipantMessagePublisher.class), cacheProvider);
114 var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
115 assertTrue(participantHandler.appliesTo(participantAckMsg));
117 var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
118 assertTrue(participantHandler.appliesTo(participantMsg));
120 participantMsg.setParticipantId(UUID.randomUUID());
121 assertFalse(participantHandler.appliesTo(participantMsg));
125 void sendParticipantRegister() {
126 var publisher = mock(ParticipantMessagePublisher.class);
127 var cacheProvider = mock(CacheProvider.class);
128 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
129 when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
130 var participantHandler =
131 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
133 participantHandler.sendParticipantRegister();
134 verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
138 void handleParticipantRegisterAckTest() {
139 var publisher = mock(ParticipantMessagePublisher.class);
140 var cacheProvider = mock(CacheProvider.class);
141 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
142 var participantHandler =
143 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
145 participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
146 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
150 void sendParticipantDeregisterTest() {
151 var publisher = mock(ParticipantMessagePublisher.class);
152 var cacheProvider = mock(CacheProvider.class);
153 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
154 var participantHandler =
155 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
157 participantHandler.sendParticipantDeregister();
158 verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
162 void handleParticipantDeregisterAckTest() {
163 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
164 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
165 var participantDeregisterAck = new ParticipantDeregisterAck();
166 assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
170 void handleParticipantPrimeTest() {
171 var cacheProvider = mock(CacheProvider.class);
172 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
174 var participantPrime = new ParticipantPrime();
175 participantPrime.setCompositionId(UUID.randomUUID());
176 participantPrime.setMessageId(UUID.randomUUID());
177 participantPrime.setParticipantDefinitionUpdates(List.of(createParticipantDefinition()));
179 var publisher = mock(ParticipantMessagePublisher.class);
180 var acHandler = mock(AutomationCompositionHandler.class);
181 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
183 participantHandler.handleParticipantPrime(participantPrime);
184 verify(cacheProvider).addElementDefinition(any(), any());
185 verify(acHandler).prime(any(), any(), any());
189 void handleParticipantRestartTest() {
190 var participantRestartMsg = new ParticipantRestart();
191 participantRestartMsg.setState(AcTypeState.PRIMED);
192 participantRestartMsg.setCompositionId(UUID.randomUUID());
193 participantRestartMsg.getParticipantDefinitionUpdates().add(new ParticipantDefinition());
195 var cacheProvider = mock(CacheProvider.class);
196 var publisher = mock(ParticipantMessagePublisher.class);
197 var acHandler = mock(AutomationCompositionHandler.class);
198 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
200 participantHandler.handleParticipantRestart(participantRestartMsg);
201 verify(cacheProvider).addElementDefinition(any(), any());
202 verify(acHandler).restarted(any(), any(), any(), any(), any());
206 void handleParticipantDeprimeTest() {
207 var cacheProvider = mock(CacheProvider.class);
208 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
209 var publisher = mock(ParticipantMessagePublisher.class);
210 var acHandler = mock(AutomationCompositionHandler.class);
211 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
212 var participantPrime = new ParticipantPrime();
213 var compositionId = UUID.randomUUID();
214 participantPrime.setCompositionId(compositionId);
215 var messageId = UUID.randomUUID();
216 participantPrime.setMessageId(messageId);
217 participantHandler.handleParticipantPrime(participantPrime);
218 verify(acHandler).deprime(messageId, compositionId);
222 void sendHeartbeatTest() {
223 var cacheProvider = mock(CacheProvider.class);
224 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
225 when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
226 var publisher = mock(ParticipantMessagePublisher.class);
227 when(publisher.isActive()).thenReturn(true);
228 var participantHandler =
229 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
230 participantHandler.sendHeartbeat();
231 verify(publisher).sendHeartbeat(any(ParticipantStatus.class));
234 private ParticipantDefinition createParticipantDefinition() {
235 var def = new ParticipantDefinition();
236 def.setParticipantId(CommonTestData.getParticipantId());