8c2b2473c5aaeef3e18597c47cc6d187ff40f50d
[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(AcDefinitionHandler.class), publisher, cacheProvider);
66         participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
67         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
68
69         when(cacheProvider.isRegistered()).thenReturn(true);
70         clearInvocations(publisher);
71         participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
72         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
73     }
74
75     @Test
76     void handleAutomationCompositionDeployTest() {
77         var acHandler = mock(AutomationCompositionHandler.class);
78         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
79                 mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
80         var automationCompositionDeploy = new AutomationCompositionDeploy();
81         participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
82         verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
83     }
84
85     @Test
86     void handleAutomationCompositionStateChangeTest() {
87         var acHandler = mock(AutomationCompositionHandler.class);
88         var acLockHandler = mock(AcLockHandler.class);
89         var participantHandler = new ParticipantHandler(acHandler, acLockHandler, mock(AcDefinitionHandler.class),
90                 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
91         var acStateChange = new AutomationCompositionStateChange();
92
93         acStateChange.setDeployOrderedState(DeployOrder.DEPLOY);
94         acStateChange.setLockOrderedState(LockOrder.NONE);
95         participantHandler.handleAutomationCompositionStateChange(acStateChange);
96         verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
97
98         acStateChange.setDeployOrderedState(DeployOrder.NONE);
99         acStateChange.setLockOrderedState(LockOrder.LOCK);
100         participantHandler.handleAutomationCompositionStateChange(acStateChange);
101         verify(acLockHandler).handleAutomationCompositionStateChange(acStateChange);
102     }
103
104     @Test
105     void handleAutomationCompositionMigrationTest() {
106         var acHandler = mock(AutomationCompositionHandler.class);
107         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
108                 mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
109         var migrationMsg = new AutomationCompositionMigration();
110         participantHandler.handleAutomationCompositionMigration(migrationMsg);
111         verify(acHandler).handleAutomationCompositionMigration(migrationMsg);
112     }
113
114     @Test
115     void handleAcPropertyUpdateTest() {
116         var acHandler = mock(AutomationCompositionHandler.class);
117         var participantHandler = new ParticipantHandler(acHandler, mock(AcLockHandler.class),
118                 mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
119         var propertyUpdateMsg = new PropertiesUpdate();
120         participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
121         verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
122     }
123
124     @Test
125     void appliesToTest() {
126         var cacheProvider = mock(CacheProvider.class);
127         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
128         when(cacheProvider.getReplicaId()).thenReturn(CommonTestData.getReplicaId());
129         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
130                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
131                 cacheProvider);
132
133         var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
134         assertTrue(participantHandler.appliesTo(participantAckMsg));
135
136         var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
137         assertTrue(participantHandler.appliesTo(participantMsg));
138
139         participantMsg.setParticipantId(UUID.randomUUID());
140         assertFalse(participantHandler.appliesTo(participantMsg));
141     }
142
143     @Test
144     void sendParticipantRegister() {
145         var publisher = mock(ParticipantMessagePublisher.class);
146         var cacheProvider = mock(CacheProvider.class);
147         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
148         when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
149         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
150                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
151
152         participantHandler.sendParticipantRegister();
153         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
154     }
155
156     @Test
157     void handleParticipantRegisterAckTest() {
158         var publisher = mock(ParticipantMessagePublisher.class);
159         var cacheProvider = mock(CacheProvider.class);
160         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
161         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
162                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
163
164         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
165         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
166     }
167
168     @Test
169     void sendParticipantDeregisterTest() {
170         var publisher = mock(ParticipantMessagePublisher.class);
171         var cacheProvider = mock(CacheProvider.class);
172         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
173         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
174                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
175
176         participantHandler.sendParticipantDeregister();
177         verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
178     }
179
180     @Test
181     void handleParticipantDeregisterAckTest() {
182         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
183                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
184                 mock(CacheProvider.class));
185         var participantDeregisterAck = new ParticipantDeregisterAck();
186         assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
187     }
188
189     @Test
190     void handleParticipantPrimeTest() {
191         var participantPrime = new ParticipantPrime();
192         participantPrime.setCompositionId(UUID.randomUUID());
193         participantPrime.setMessageId(UUID.randomUUID());
194
195         var acHandler = mock(AcDefinitionHandler.class);
196         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
197                 mock(AcLockHandler.class), acHandler, mock(ParticipantMessagePublisher.class),
198                 mock(CacheProvider.class));
199
200         participantHandler.handleParticipantPrime(participantPrime);
201         verify(acHandler).handlePrime(participantPrime);
202     }
203
204     @Test
205     void handleParticipantRestartTest() {
206         var participantSyncMsg = new ParticipantSync();
207         participantSyncMsg.setState(AcTypeState.PRIMED);
208         participantSyncMsg.setCompositionId(UUID.randomUUID());
209         participantSyncMsg.setReplicaId(CommonTestData.getReplicaId());
210
211         var cacheProvider = mock(CacheProvider.class);
212         when(cacheProvider.getReplicaId()).thenReturn(CommonTestData.getReplicaId());
213         var publisher = mock(ParticipantMessagePublisher.class);
214         var acHandler = mock(AcDefinitionHandler.class);
215         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
216                 mock(AcLockHandler.class), acHandler, publisher, cacheProvider);
217
218         participantHandler.handleParticipantSync(participantSyncMsg);
219         verify(acHandler).handleParticipantSync(participantSyncMsg);
220     }
221
222     @Test
223     void sendHeartbeatTest() {
224         var cacheProvider = mock(CacheProvider.class);
225         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
226         when(cacheProvider.isRegistered()).thenReturn(false);
227         when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
228         var publisher = mock(ParticipantMessagePublisher.class);
229         when(publisher.isActive()).thenReturn(true);
230         var acHandler = mock(AcDefinitionHandler.class);
231         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
232                 mock(AcLockHandler.class), acHandler, publisher, cacheProvider);
233         participantHandler.sendHeartbeat();
234         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
235
236         when(cacheProvider.isRegistered()).thenReturn(true);
237         clearInvocations(publisher);
238         participantHandler.sendHeartbeat();
239         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
240     }
241 }