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