cd28d41fb90ae1a452b7ac978bd4abc353c59434
[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.ParticipantRestart;
51 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus;
52 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq;
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         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
129                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
130                 cacheProvider);
131
132         var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
133         assertTrue(participantHandler.appliesTo(participantAckMsg));
134
135         var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
136         assertTrue(participantHandler.appliesTo(participantMsg));
137
138         participantMsg.setParticipantId(UUID.randomUUID());
139         assertFalse(participantHandler.appliesTo(participantMsg));
140     }
141
142     @Test
143     void sendParticipantRegister() {
144         var publisher = mock(ParticipantMessagePublisher.class);
145         var cacheProvider = mock(CacheProvider.class);
146         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
147         when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
148         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
149                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
150
151         participantHandler.sendParticipantRegister();
152         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
153     }
154
155     @Test
156     void handleParticipantRegisterAckTest() {
157         var publisher = mock(ParticipantMessagePublisher.class);
158         var cacheProvider = mock(CacheProvider.class);
159         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
160         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
161                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
162
163         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
164         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
165     }
166
167     @Test
168     void sendParticipantDeregisterTest() {
169         var publisher = mock(ParticipantMessagePublisher.class);
170         var cacheProvider = mock(CacheProvider.class);
171         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
172         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
173                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), publisher, cacheProvider);
174
175         participantHandler.sendParticipantDeregister();
176         verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
177     }
178
179     @Test
180     void handleParticipantDeregisterAckTest() {
181         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
182                 mock(AcLockHandler.class), mock(AcDefinitionHandler.class), mock(ParticipantMessagePublisher.class),
183                 mock(CacheProvider.class));
184         var participantDeregisterAck = new ParticipantDeregisterAck();
185         assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
186     }
187
188     @Test
189     void handleParticipantPrimeTest() {
190         var participantPrime = new ParticipantPrime();
191         participantPrime.setCompositionId(UUID.randomUUID());
192         participantPrime.setMessageId(UUID.randomUUID());
193
194         var acHandler = mock(AcDefinitionHandler.class);
195         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
196                 mock(AcLockHandler.class), acHandler, mock(ParticipantMessagePublisher.class),
197                 mock(CacheProvider.class));
198
199         participantHandler.handleParticipantPrime(participantPrime);
200         verify(acHandler).handlePrime(participantPrime);
201     }
202
203     @Test
204     void handleParticipantRestartTest() {
205         var participantRestartMsg = new ParticipantRestart();
206         participantRestartMsg.setState(AcTypeState.PRIMED);
207         participantRestartMsg.setCompositionId(UUID.randomUUID());
208
209         var cacheProvider = mock(CacheProvider.class);
210         var publisher = mock(ParticipantMessagePublisher.class);
211         var acHandler = mock(AcDefinitionHandler.class);
212         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
213                 mock(AcLockHandler.class), acHandler, publisher, cacheProvider);
214
215         participantHandler.handleParticipantRestart(participantRestartMsg);
216         verify(acHandler).handleParticipantRestart(participantRestartMsg);
217     }
218
219     @Test
220     void sendHeartbeatTest() {
221         var cacheProvider = mock(CacheProvider.class);
222         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
223         when(cacheProvider.isRegistered()).thenReturn(false);
224         when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
225         var publisher = mock(ParticipantMessagePublisher.class);
226         when(publisher.isActive()).thenReturn(true);
227         var acHandler = mock(AcDefinitionHandler.class);
228         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
229                 mock(AcLockHandler.class), acHandler, publisher, cacheProvider);
230         participantHandler.sendHeartbeat();
231         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
232
233         when(cacheProvider.isRegistered()).thenReturn(true);
234         clearInvocations(publisher);
235         participantHandler.sendHeartbeat();
236         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
237     }
238 }