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.ParticipantDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
51 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
53 class ParticipantHandlerTest {
56 void handleParticipantStatusReqTest() {
57 var publisher = mock(ParticipantMessagePublisher.class);
58 var cacheProvider = mock(CacheProvider.class);
59 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
60 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
61 participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
62 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
66 void handleAutomationCompositionDeployTest() {
67 var acHandler = mock(AutomationCompositionHandler.class);
68 var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
69 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
70 var automationCompositionDeploy = new AutomationCompositionDeploy();
71 participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
72 verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
76 void handleAutomationCompositionStateChangeTest() {
77 var acHandler = mock(AutomationCompositionHandler.class);
78 var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
79 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
80 var acStateChange = new AutomationCompositionStateChange();
81 participantHandler.handleAutomationCompositionStateChange(acStateChange);
82 verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
86 void handleAcPropertyUpdateTest() {
87 var acHandler = mock(AutomationCompositionHandler.class);
88 var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
89 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
90 var propertyUpdateMsg = new PropertiesUpdate();
91 participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
92 verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
96 void appliesToTest() {
97 var cacheProvider = mock(CacheProvider.class);
98 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
99 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
100 mock(AutomationCompositionOutHandler.class), mock(ParticipantMessagePublisher.class), cacheProvider);
102 var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
103 assertTrue(participantHandler.appliesTo(participantAckMsg));
105 var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
106 assertTrue(participantHandler.appliesTo(participantMsg));
108 participantMsg.setParticipantId(UUID.randomUUID());
109 assertFalse(participantHandler.appliesTo(participantMsg));
113 void sendParticipantRegister() {
114 var publisher = mock(ParticipantMessagePublisher.class);
115 var cacheProvider = mock(CacheProvider.class);
116 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
117 when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
118 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
119 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
121 participantHandler.sendParticipantRegister();
122 verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
126 void handleParticipantRegisterAckTest() {
127 var publisher = mock(ParticipantMessagePublisher.class);
128 var cacheProvider = mock(CacheProvider.class);
129 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
130 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
131 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
133 participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
134 verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
138 void sendParticipantDeregisterTest() {
139 var publisher = mock(ParticipantMessagePublisher.class);
140 var cacheProvider = mock(CacheProvider.class);
141 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
142 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
143 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
145 participantHandler.sendParticipantDeregister();
146 verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
150 void handleParticipantDeregisterAckTest() {
151 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
152 mock(AutomationCompositionOutHandler.class), mock(ParticipantMessagePublisher.class),
153 mock(CacheProvider.class));
154 var participantDeregisterAck = new ParticipantDeregisterAck();
155 assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
159 void handleParticipantPrimeTest() {
160 var cacheProvider = mock(CacheProvider.class);
161 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
162 var publisher = mock(ParticipantMessagePublisher.class);
163 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
164 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
166 var participantPrime = new ParticipantPrime();
167 participantPrime.setCompositionId(UUID.randomUUID());
168 participantPrime.setParticipantDefinitionUpdates(List.of(createParticipantDefinition()));
169 participantHandler.handleParticipantPrime(participantPrime);
170 verify(cacheProvider).addElementDefinition(any(), any());
171 verify(publisher).sendParticipantPrimeAck(any(ParticipantPrimeAck.class));
175 void handleParticipantDeprimeTest() {
176 var cacheProvider = mock(CacheProvider.class);
177 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
178 var publisher = mock(ParticipantMessagePublisher.class);
179 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
180 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
181 var participantPrime = new ParticipantPrime();
182 var compositionId = UUID.randomUUID();
183 participantPrime.setCompositionId(compositionId);
184 participantHandler.handleParticipantPrime(participantPrime);
185 verify(cacheProvider).removeElementDefinition(compositionId);
186 verify(publisher).sendParticipantPrimeAck(any(ParticipantPrimeAck.class));
190 void sendHeartbeatTest() {
191 var cacheProvider = mock(CacheProvider.class);
192 when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
193 when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
194 var publisher = mock(ParticipantMessagePublisher.class);
195 var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
196 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
197 participantHandler.sendHeartbeat();
198 verify(publisher).sendHeartbeat(any(ParticipantStatus.class));
201 private ParticipantDefinition createParticipantDefinition() {
202 var def = new ParticipantDefinition();
203 def.setParticipantId(CommonTestData.getParticipantId());