2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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.AutomationCompositionElementDefinition;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantAckMessage;
44 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
45 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
46 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage;
47 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
48 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
49 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
50 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRestart;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
54 import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate;
56 class ParticipantHandlerTest {
59 void handleParticipantStatusReqTest() {
60 var publisher = mock(ParticipantMessagePublisher.class);
61 var cacheProvider = mock(CacheProvider.class);
62 var participantHandler =
63 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
64 participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
65 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
69 void handleAutomationCompositionDeployTest() {
70 var acHandler = mock(AutomationCompositionHandler.class);
71 var participantHandler =
72 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
73 var automationCompositionDeploy = new AutomationCompositionDeploy();
74 participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
75 verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
79 void handleAutomationCompositionStateChangeTest() {
80 var acHandler = mock(AutomationCompositionHandler.class);
81 var participantHandler =
82 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
83 var acStateChange = new AutomationCompositionStateChange();
84 participantHandler.handleAutomationCompositionStateChange(acStateChange);
85 verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
89 void handleAutomationCompositionMigrationTest() {
90 var acHandler = mock(AutomationCompositionHandler.class);
91 var participantHandler =
92 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
93 var migrationMsg = new AutomationCompositionMigration();
94 participantHandler.handleAutomationCompositionMigration(migrationMsg);
95 verify(acHandler).handleAutomationCompositionMigration(migrationMsg);
99 void handleAcPropertyUpdateTest() {
100 var acHandler = mock(AutomationCompositionHandler.class);
101 var participantHandler =
102 new ParticipantHandler(acHandler, mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
103 var propertyUpdateMsg = new PropertiesUpdate();
104 participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
105 verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
109 void appliesToTest() {
110 var cacheProvider = mock(CacheProvider.class);
111 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
112 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
113 mock(ParticipantMessagePublisher.class), cacheProvider);
115 var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
116 assertTrue(participantHandler.appliesTo(participantAckMsg));
118 var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
119 assertTrue(participantHandler.appliesTo(participantMsg));
121 participantMsg.setParticipantId(UUID.randomUUID());
122 assertFalse(participantHandler.appliesTo(participantMsg));
126 void sendParticipantRegister() {
127 var publisher = mock(ParticipantMessagePublisher.class);
128 var cacheProvider = mock(CacheProvider.class);
129 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
130 when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
131 var participantHandler =
132 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
134 participantHandler.sendParticipantRegister();
135 verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
139 void handleParticipantRegisterAckTest() {
140 var publisher = mock(ParticipantMessagePublisher.class);
141 var cacheProvider = mock(CacheProvider.class);
142 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
143 var participantHandler =
144 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
146 participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
147 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
151 void sendParticipantDeregisterTest() {
152 var publisher = mock(ParticipantMessagePublisher.class);
153 var cacheProvider = mock(CacheProvider.class);
154 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
155 var participantHandler =
156 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
158 participantHandler.sendParticipantDeregister();
159 verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
163 void handleParticipantDeregisterAckTest() {
164 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
165 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
166 var participantDeregisterAck = new ParticipantDeregisterAck();
167 assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
171 void handleParticipantPrimeTest() {
172 var cacheProvider = mock(CacheProvider.class);
173 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
175 var participantPrime = new ParticipantPrime();
176 participantPrime.setCompositionId(UUID.randomUUID());
177 participantPrime.setMessageId(UUID.randomUUID());
178 participantPrime.setParticipantDefinitionUpdates(List.of(createParticipantDefinition()));
180 var publisher = mock(ParticipantMessagePublisher.class);
181 var acHandler = mock(AutomationCompositionHandler.class);
182 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
184 participantHandler.handleParticipantPrime(participantPrime);
185 verify(cacheProvider).addElementDefinition(any(), any());
186 verify(acHandler).prime(any(), any(), any());
190 void handleParticipantRestartTest() {
191 var participantRestartMsg = new ParticipantRestart();
192 participantRestartMsg.setState(AcTypeState.PRIMED);
193 participantRestartMsg.setCompositionId(UUID.randomUUID());
194 participantRestartMsg.getParticipantDefinitionUpdates().add(new ParticipantDefinition());
196 var cacheProvider = mock(CacheProvider.class);
197 var publisher = mock(ParticipantMessagePublisher.class);
198 var acHandler = mock(AutomationCompositionHandler.class);
199 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
201 participantHandler.handleParticipantRestart(participantRestartMsg);
202 verify(cacheProvider).addElementDefinition(any(), any());
203 verify(acHandler).restarted(any(), any(), any(), any(), any());
207 void handleParticipantDeprimeTest() {
208 var cacheProvider = mock(CacheProvider.class);
209 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
210 var publisher = mock(ParticipantMessagePublisher.class);
211 var acHandler = mock(AutomationCompositionHandler.class);
212 var participantHandler = new ParticipantHandler(acHandler, publisher, cacheProvider);
213 var participantPrime = new ParticipantPrime();
214 var compositionId = UUID.randomUUID();
215 participantPrime.setCompositionId(compositionId);
216 var messageId = UUID.randomUUID();
217 participantPrime.setMessageId(messageId);
218 participantHandler.handleParticipantPrime(participantPrime);
219 verify(acHandler).deprime(messageId, compositionId);
223 void sendHeartbeatTest() {
224 var cacheProvider = mock(CacheProvider.class);
225 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
226 when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
227 var publisher = mock(ParticipantMessagePublisher.class);
228 when(publisher.isActive()).thenReturn(true);
229 var participantHandler =
230 new ParticipantHandler(mock(AutomationCompositionHandler.class), publisher, cacheProvider);
231 participantHandler.sendHeartbeat();
232 verify(publisher).sendHeartbeat(any(ParticipantStatus.class));
235 private ParticipantDefinition createParticipantDefinition() {
236 var def = new ParticipantDefinition();
237 def.setParticipantId(CommonTestData.getParticipantId());
238 def.setAutomationCompositionElementDefinitionList(List.of(new AutomationCompositionElementDefinition()));