1fb72812bc3378b85af795c6c4de19a77a267d97
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
22
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.clearInvocations;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
36 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration;
41 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
42 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantAckMessage;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister;
44 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck;
45 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage;
46 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType;
47 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
48 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister;
49 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegisterAck;
50 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
53 import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate;
54 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
55 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
56
57 class ParticipantHandlerTest {
58
59     @Test
60     void handleParticipantStatusReqTest() {
61         var publisher = mock(ParticipantMessagePublisher.class);
62         when(publisher.isActive()).thenReturn(true);
63         var cacheProvider = mock(CacheProvider.class);
64         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
65             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class),
66             publisher, cacheProvider);
67         participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
68         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
69
70         when(cacheProvider.isRegistered()).thenReturn(true);
71         clearInvocations(publisher);
72         participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
73         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
74     }
75
76     @Test
77     void handleAutomationCompositionDeployTest() {
78         var acHandler = mock(AutomationCompositionHandler.class);
79         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
80             mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
81             mock(CacheProvider.class));
82         var automationCompositionDeploy = new AutomationCompositionDeploy();
83         participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
84         verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
85     }
86
87     @Test
88     void handleAutomationCompositionStateChangeTest() {
89         var acHandler = mock(AutomationCompositionHandler.class);
90         var acLockHandler = mock(AcLockHandler.class);
91         var participantHandler = new ParticipantHandler(acHandler, acLockHandler, mock(AcSubStateHandler.class),
92             mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
93         var acStateChange = new AutomationCompositionStateChange();
94
95         acStateChange.setDeployOrderedState(DeployOrder.DEPLOY);
96         acStateChange.setLockOrderedState(LockOrder.NONE);
97         participantHandler.handleAutomationCompositionStateChange(acStateChange);
98         verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
99
100         acStateChange.setDeployOrderedState(DeployOrder.NONE);
101         acStateChange.setLockOrderedState(LockOrder.LOCK);
102         participantHandler.handleAutomationCompositionStateChange(acStateChange);
103         verify(acLockHandler).handleAutomationCompositionStateChange(acStateChange);
104     }
105
106     @Test
107     void handleAutomationCompositionMigrationTest() {
108         var acHandler = mock(AutomationCompositionHandler.class);
109         var acSubStateHandler = mock(AcSubStateHandler.class);
110         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
111             acSubStateHandler, mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
112             mock(CacheProvider.class));
113         var migrationMsg = new AutomationCompositionMigration();
114         participantHandler.handleAutomationCompositionMigration(migrationMsg);
115         verify(acHandler).handleAutomationCompositionMigration(migrationMsg);
116
117         migrationMsg.setPrecheck(true);
118         participantHandler.handleAutomationCompositionMigration(migrationMsg);
119         verify(acSubStateHandler).handleAcMigrationPrecheck(migrationMsg);
120     }
121
122     @Test
123     void handleAcPropertyUpdateTest() {
124         var acHandler = mock(AutomationCompositionHandler.class);
125         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
126             mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
127             mock(CacheProvider.class));
128         var propertyUpdateMsg = new PropertiesUpdate();
129         participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
130         verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
131     }
132
133     @Test
134     void appliesToTest() {
135         var cacheProvider = mock(CacheProvider.class);
136         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
137         when(cacheProvider.getReplicaId()).thenReturn(CommonTestData.getReplicaId());
138         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
139             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class),
140             mock(ParticipantMessagePublisher.class), cacheProvider);
141
142         var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
143         assertTrue(participantHandler.appliesTo(participantAckMsg));
144
145         var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
146         assertTrue(participantHandler.appliesTo(participantMsg));
147
148         participantMsg.setParticipantId(UUID.randomUUID());
149         assertFalse(participantHandler.appliesTo(participantMsg));
150     }
151
152     @Test
153     void sendParticipantRegister() {
154         var publisher = mock(ParticipantMessagePublisher.class);
155         var cacheProvider = mock(CacheProvider.class);
156         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
157         when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
158         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
159             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class), publisher,
160             cacheProvider);
161
162         participantHandler.sendParticipantRegister();
163         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
164     }
165
166     @Test
167     void handleParticipantRegisterAckTest() {
168         var publisher = mock(ParticipantMessagePublisher.class);
169         var cacheProvider = mock(CacheProvider.class);
170         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
171         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
172             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class), publisher,
173             cacheProvider);
174
175         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
176         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
177     }
178
179     @Test
180     void sendParticipantDeregisterTest() {
181         var publisher = mock(ParticipantMessagePublisher.class);
182         var cacheProvider = mock(CacheProvider.class);
183         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
184         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
185             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class), publisher,
186             cacheProvider);
187
188         participantHandler.sendParticipantDeregister();
189         verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
190     }
191
192     @Test
193     void handleParticipantDeregisterAckTest() {
194         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
195             mock(AcLockHandler.class), mock(AcSubStateHandler.class), mock(AcDefinitionHandler.class),
196             mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
197         var participantDeregisterAck = new ParticipantDeregisterAck();
198         assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
199     }
200
201     @Test
202     void handleParticipantPrimeTest() {
203         var participantPrime = new ParticipantPrime();
204         participantPrime.setCompositionId(UUID.randomUUID());
205         participantPrime.setMessageId(UUID.randomUUID());
206
207         var acHandler = mock(AcDefinitionHandler.class);
208         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
209             mock(AcLockHandler.class), mock(AcSubStateHandler.class), acHandler,
210             mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
211
212         participantHandler.handleParticipantPrime(participantPrime);
213         verify(acHandler).handlePrime(participantPrime);
214     }
215
216     @Test
217     void handleParticipantRestartTest() {
218         var participantSyncMsg = new ParticipantSync();
219         participantSyncMsg.setState(AcTypeState.PRIMED);
220         participantSyncMsg.setCompositionId(UUID.randomUUID());
221         participantSyncMsg.setReplicaId(CommonTestData.getReplicaId());
222
223         var cacheProvider = mock(CacheProvider.class);
224         when(cacheProvider.getReplicaId()).thenReturn(CommonTestData.getReplicaId());
225         var publisher = mock(ParticipantMessagePublisher.class);
226         var acHandler = mock(AcDefinitionHandler.class);
227         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
228             mock(AcLockHandler.class), mock(AcSubStateHandler.class), acHandler, publisher, cacheProvider);
229
230         participantHandler.handleParticipantSync(participantSyncMsg);
231         verify(acHandler).handleParticipantSync(participantSyncMsg);
232     }
233
234     @Test
235     void sendHeartbeatTest() {
236         var cacheProvider = mock(CacheProvider.class);
237         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
238         when(cacheProvider.isRegistered()).thenReturn(false);
239         when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
240         var publisher = mock(ParticipantMessagePublisher.class);
241         when(publisher.isActive()).thenReturn(true);
242         var acHandler = mock(AcDefinitionHandler.class);
243         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
244             mock(AcLockHandler.class), mock(AcSubStateHandler.class), acHandler, publisher, cacheProvider);
245         participantHandler.sendHeartbeat();
246         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
247
248         when(cacheProvider.isRegistered()).thenReturn(true);
249         clearInvocations(publisher);
250         participantHandler.sendHeartbeat();
251         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
252     }
253 }