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