dd49ee318da6f648b0418f2d745b26732b05c232
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
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.AcElementDeploy;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
44 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
45 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 class AutomationCompositionHandlerTest {
50
51     @Test
52     void handleAutomationCompositionStateChangeNullTest() {
53         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
54         var cacheProvider = mock(CacheProvider.class);
55         var ach =
56                 new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, mock(ThreadHandler.class));
57
58         var automationCompositionStateChange = new AutomationCompositionStateChange();
59         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
60
61         automationCompositionStateChange.setAutomationCompositionId(UUID.randomUUID());
62         automationCompositionStateChange.setDeployOrderedState(DeployOrder.DELETE);
63         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
64         verify(participantMessagePublisher).sendAutomationCompositionAck(any(AutomationCompositionDeployAck.class));
65
66         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
67         automationCompositionStateChange.setAutomationCompositionId(automationComposition.getInstanceId());
68         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
69                 .thenReturn(automationComposition);
70         automationCompositionStateChange.setDeployOrderedState(DeployOrder.UPDATE);
71         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(automationCompositionStateChange));
72     }
73
74     @Test
75     void handleAutomationCompositionStateChangeUndeployTest() {
76         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
77         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
78                 automationComposition.getInstanceId(), DeployOrder.UNDEPLOY, LockOrder.NONE);
79         var cacheProvider = mock(CacheProvider.class);
80         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
81                 .thenReturn(automationComposition);
82         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
83
84         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
85         var listener = mock(ThreadHandler.class);
86         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
87         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
88         verify(listener, times(automationComposition.getElements().size())).undeploy(any(), any(), any());
89     }
90
91     @Test
92     void handleAutomationCompositionStateChangeLockTest() {
93         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
94         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
95                 automationComposition.getInstanceId(), DeployOrder.NONE, LockOrder.LOCK);
96         var cacheProvider = mock(CacheProvider.class);
97         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
98                 .thenReturn(automationComposition);
99         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
100
101         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
102         var listener = mock(ThreadHandler.class);
103         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
104         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
105         verify(listener, times(automationComposition.getElements().size())).lock(any(), any(), any());
106     }
107
108     @Test
109     void handleAutomationCompositionStateChangeUnlockTest() {
110         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
111         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
112                 automationComposition.getInstanceId(), DeployOrder.NONE, LockOrder.UNLOCK);
113         var cacheProvider = mock(CacheProvider.class);
114         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
115                 .thenReturn(automationComposition);
116         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
117
118         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
119         var listener = mock(ThreadHandler.class);
120         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
121         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
122         verify(listener, times(automationComposition.getElements().size())).unlock(any(), any(), any());
123     }
124
125     @Test
126     void handleAutomationCompositionStateChangeDeleteTest() {
127         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
128         var automationCompositionStateChange = CommonTestData.getStateChange(CommonTestData.getParticipantId(),
129                 automationComposition.getInstanceId(), DeployOrder.DELETE, LockOrder.NONE);
130         var cacheProvider = mock(CacheProvider.class);
131         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
132                 .thenReturn(automationComposition);
133         when(cacheProvider.getCommonProperties(any(UUID.class), any(UUID.class))).thenReturn(Map.of());
134
135         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
136         var listener = mock(ThreadHandler.class);
137         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
138         ach.handleAutomationCompositionStateChange(automationCompositionStateChange);
139         verify(listener, times(automationComposition.getElements().size())).delete(any(), any(), any());
140     }
141
142     @Test
143     void handleAcPropertyUpdateTest() {
144         var cacheProvider = mock(CacheProvider.class);
145         var listener = mock(ThreadHandler.class);
146         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
147         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
148
149         var updateMsg = new PropertiesUpdate();
150         assertDoesNotThrow(() -> ach.handleAcPropertyUpdate(updateMsg));
151
152         updateMsg.setParticipantId(CommonTestData.getParticipantId());
153         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
154         var participantDeploy = new ParticipantDeploy();
155         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
156         updateMsg.getParticipantUpdatesList().add(participantDeploy);
157
158         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
159         updateMsg.setAutomationCompositionId(automationComposition.getInstanceId());
160         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
161                 .thenReturn(automationComposition);
162         var acElementDeploy = new AcElementDeploy();
163         acElementDeploy.setProperties(Map.of());
164         acElementDeploy.setId(automationComposition.getElements().values().iterator().next().getId());
165         participantDeploy.getAcElementList().add(acElementDeploy);
166
167         ach.handleAcPropertyUpdate(updateMsg);
168         verify(listener).update(any(), any(), any(), any());
169     }
170
171     @Test
172     void handleAutomationCompositionDeployTest() {
173         var cacheProvider = mock(CacheProvider.class);
174         var listener = mock(ThreadHandler.class);
175         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
176         var ach = new AutomationCompositionHandler(cacheProvider, participantMessagePublisher, listener);
177
178         var deployMsg = new AutomationCompositionDeploy();
179         assertDoesNotThrow(() -> ach.handleAutomationCompositionDeploy(deployMsg));
180
181         deployMsg.setParticipantId(CommonTestData.getParticipantId());
182         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
183         var participantDeploy = new ParticipantDeploy();
184         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
185         deployMsg.getParticipantUpdatesList().add(participantDeploy);
186
187         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
188         deployMsg.setAutomationCompositionId(automationComposition.getInstanceId());
189         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
190                 .thenReturn(automationComposition);
191         for (var element : automationComposition.getElements().values()) {
192             var acElementDeploy = new AcElementDeploy();
193             acElementDeploy.setProperties(Map.of());
194             acElementDeploy.setId(element.getId());
195             participantDeploy.getAcElementList().add(acElementDeploy);
196         }
197         ach.handleAutomationCompositionDeploy(deployMsg);
198         verify(listener, times(automationComposition.getElements().size())).deploy(any(), any(), any(), any());
199     }
200
201     @Test
202     void handleComposiotPrimeTest() {
203         var listener = mock(ThreadHandler.class);
204         var ach = new AutomationCompositionHandler(mock(CacheProvider.class), mock(ParticipantMessagePublisher.class),
205                 listener);
206         var compositionId = UUID.randomUUID();
207         var list = List.of(new AutomationCompositionElementDefinition());
208         var messageId = UUID.randomUUID();
209         ach.prime(messageId, compositionId, list);
210         verify(listener).prime(messageId, compositionId, list);
211     }
212
213     @Test
214     void handleComposiotDeprimeTest() {
215         var listener = mock(ThreadHandler.class);
216         var ach = new AutomationCompositionHandler(mock(CacheProvider.class), mock(ParticipantMessagePublisher.class),
217                 listener);
218         var compositionId = UUID.randomUUID();
219         var messageId = UUID.randomUUID();
220         ach.deprime(messageId, compositionId);
221         verify(listener).deprime(messageId, compositionId);
222     }
223 }