d782ae167cbbddca5810e79013ff8f055434e035
[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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
39 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
40 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
41 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
43 import org.onap.policy.clamp.models.acm.concepts.DeployState;
44 import org.onap.policy.clamp.models.acm.concepts.LockState;
45 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
48 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
49 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
50 import org.onap.policy.models.base.PfModelException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53
54 @ExtendWith(SpringExtension.class)
55 class AutomationCompositionHandlerTest {
56
57     private final CommonTestData commonTestData = new CommonTestData();
58
59     private static final String STATE_VALUE = "STATE_VALUE";
60
61     @Test
62     void automationCompositionHandlerTest() {
63         var ach = commonTestData.getMockAutomationCompositionHandler();
64         assertNotNull(ach.getAutomationCompositionMap());
65     }
66
67     @Test
68     void updateNullAutomationCompositionHandlerTest() {
69         var id = UUID.randomUUID();
70
71         var ach = commonTestData.getMockAutomationCompositionHandler();
72         assertDoesNotThrow(() -> ach.updateAutomationCompositionElementState(null, null, DeployState.UNDEPLOYED, null,
73                 "Undeployed"));
74
75         assertDoesNotThrow(
76                 () -> ach.updateAutomationCompositionElementState(null, id, DeployState.UNDEPLOYED, null, null));
77     }
78
79     @Test
80     void updateAutomationCompositionHandlerTest() {
81         var uuid = UUID.randomUUID();
82         var partecipantId = CommonTestData.getParticipantId();
83         var definition = CommonTestData.getDefinition();
84
85         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
86         var acKey = ach.getAutomationCompositionMap().keySet().iterator().next();
87         var key = ach.getAutomationCompositionMap().get(acKey).getElements().keySet().iterator().next();
88         var value = ach.getAutomationCompositionMap().get(acKey).getElements().get(key);
89         value.setDeployState(DeployState.DEPLOYING);
90         value.setLockState(LockState.NONE);
91         ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, uuid, DeployState.DEPLOYED, null,
92                 "Deployed");
93         assertEquals(DeployState.DEPLOYED, value.getDeployState());
94
95         ach.getAutomationCompositionMap().values().iterator().next().getElements().putIfAbsent(key, value);
96         ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, key, DeployState.DEPLOYED, null,
97                 "Deployed");
98         assertEquals(DeployState.DEPLOYED, value.getDeployState());
99
100         ach.getAutomationCompositionMap().values().iterator().next().getElements().clear();
101         assertDoesNotThrow(() -> ach.updateAutomationCompositionElementState(CommonTestData.AC_ID_1, key, null,
102                 LockState.UNLOCKED, null));
103     }
104
105     @Test
106     void handleAutomationCompositionStateChangeTest() {
107         var uuid = UUID.randomUUID();
108         var partecipantId = CommonTestData.getParticipantId();
109         var definition = CommonTestData.getDefinition();
110         var stateChange = commonTestData.getStateChange(partecipantId, uuid, DeployOrder.NONE, LockOrder.UNLOCK);
111         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
112         assertDoesNotThrow(() -> ach
113                 .handleAutomationCompositionStateChange(mock(AutomationCompositionStateChange.class), List.of()));
114
115         ach.handleAutomationCompositionStateChange(stateChange, List.of());
116         var newPartecipantId = CommonTestData.getRndParticipantId();
117         stateChange.setAutomationCompositionId(UUID.randomUUID());
118         stateChange.setParticipantId(newPartecipantId);
119         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChange, List.of()));
120     }
121
122     @Test
123     void handleAutomationCompositionDeployTest() throws PfModelException {
124         var acd = new AutomationCompositionElementDefinition();
125         var definition = CommonTestData.getDefinition();
126         acd.setAcElementDefinitionId(definition);
127         acd.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
128         var updateMsg = new AutomationCompositionDeploy();
129         updateMsg.setAutomationCompositionId(UUID.randomUUID());
130         var uuid = UUID.randomUUID();
131         updateMsg.setMessageId(uuid);
132         var partecipantId = CommonTestData.getParticipantId();
133         updateMsg.setParticipantId(partecipantId);
134         updateMsg.setFirstStartPhase(true);
135         updateMsg.setStartPhase(0);
136         var acElementDefinitions = List.of(acd);
137         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
138         var listener = mock(AutomationCompositionElementListener.class);
139         ach.registerAutomationCompositionElementListener(listener);
140         ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions);
141         verify(listener, times(0)).deploy(any(), any(), anyMap());
142         updateMsg.setFirstStartPhase(false);
143         updateMsg.setStartPhase(1);
144         ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions);
145         verify(listener, times(0)).deploy(any(), any(), anyMap());
146
147         ach.getAutomationCompositionMap().clear();
148         updateMsg.setFirstStartPhase(true);
149         updateMsg.setStartPhase(0);
150         ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions);
151         verify(listener, times(0)).deploy(any(), any(), anyMap());
152
153         updateMsg.setAutomationCompositionId(UUID.randomUUID());
154         updateMsg.setParticipantUpdatesList(List.of(mock(ParticipantDeploy.class)));
155         ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions);
156         verify(listener, times(0)).deploy(any(), any(), anyMap());
157
158         updateMsg.setStartPhase(1);
159         var participantDeploy = new ParticipantDeploy();
160         participantDeploy.setParticipantId(partecipantId);
161         var element = new AcElementDeploy();
162         element.setDefinition(definition);
163         participantDeploy.setAcElementList(List.of(element));
164         updateMsg.setParticipantUpdatesList(List.of(participantDeploy));
165
166         updateMsg.setStartPhase(0);
167         ach.handleAutomationCompositionDeploy(updateMsg, acElementDefinitions);
168         verify(listener, times(1)).deploy(any(), any(), anyMap());
169     }
170
171     @Test
172     void acUndeployTest() throws PfModelException {
173         var uuid = UUID.randomUUID();
174         var partecipantId = CommonTestData.getParticipantId();
175         var definition = CommonTestData.getDefinition();
176
177         var stateChangeUndeploy =
178                 commonTestData.getStateChange(partecipantId, uuid, DeployOrder.UNDEPLOY, LockOrder.NONE);
179
180         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
181         stateChangeUndeploy
182                 .setAutomationCompositionId(ach.getAutomationCompositionMap().entrySet().iterator().next().getKey());
183         var listener = mock(AutomationCompositionElementListener.class);
184         ach.registerAutomationCompositionElementListener(listener);
185
186         var acd = new AutomationCompositionElementDefinition();
187         acd.setAcElementDefinitionId(definition);
188         acd.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
189         ach.handleAutomationCompositionStateChange(stateChangeUndeploy, List.of(acd));
190         verify(listener, times(1)).undeploy(any(), any());
191
192         stateChangeUndeploy.setAutomationCompositionId(UUID.randomUUID());
193         stateChangeUndeploy.setParticipantId(CommonTestData.getRndParticipantId());
194         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChangeUndeploy, List.of()));
195     }
196
197     @Test
198     void automationCompositionStateLock() throws PfModelException {
199         var uuid = UUID.randomUUID();
200         var partecipantId = CommonTestData.getParticipantId();
201         var definition = CommonTestData.getDefinition();
202
203         var stateChangeLock =
204                 commonTestData.getStateChange(partecipantId, uuid, DeployOrder.NONE, LockOrder.LOCK);
205
206         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
207         var listener = mock(AutomationCompositionElementListener.class);
208         ach.registerAutomationCompositionElementListener(listener);
209         stateChangeLock
210                 .setAutomationCompositionId(ach.getAutomationCompositionMap().entrySet().iterator().next().getKey());
211         var acd = new AutomationCompositionElementDefinition();
212         acd.setAcElementDefinitionId(definition);
213         acd.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
214         ach.handleAutomationCompositionStateChange(stateChangeLock, List.of(acd));
215         stateChangeLock.setAutomationCompositionId(UUID.randomUUID());
216         stateChangeLock.setParticipantId(CommonTestData.getRndParticipantId());
217         ach.handleAutomationCompositionStateChange(stateChangeLock, List.of());
218         verify(listener, times(1)).lock(any(), any());
219     }
220
221     @Test
222     void automationCompositionStateUnlock() throws PfModelException {
223         var uuid = UUID.randomUUID();
224         var partecipantId = CommonTestData.getParticipantId();
225         var definition = CommonTestData.getDefinition();
226
227         var stateChangeUnlock =
228                 commonTestData.getStateChange(partecipantId, uuid, DeployOrder.NONE, LockOrder.UNLOCK);
229
230         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
231         var listener = mock(AutomationCompositionElementListener.class);
232         ach.registerAutomationCompositionElementListener(listener);
233         stateChangeUnlock
234                 .setAutomationCompositionId(ach.getAutomationCompositionMap().entrySet().iterator().next().getKey());
235         var acd = new AutomationCompositionElementDefinition();
236         acd.setAcElementDefinitionId(definition);
237         acd.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
238         ach.handleAutomationCompositionStateChange(stateChangeUnlock, List.of(acd));
239         stateChangeUnlock.setAutomationCompositionId(UUID.randomUUID());
240         stateChangeUnlock.setParticipantId(CommonTestData.getRndParticipantId());
241         ach.handleAutomationCompositionStateChange(stateChangeUnlock, List.of());
242         verify(listener, times(1)).unlock(any(), any());
243     }
244
245
246     @Test
247     void testgetAutomationCompositionInfoList() {
248         var uuid = UUID.randomUUID();
249         var partecipantId = CommonTestData.getParticipantId();
250         var definition = CommonTestData.getDefinition();
251         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
252         var result = ach.getAutomationCompositionInfoList();
253         assertThat(result).hasSize(1);
254         assertThat(result.get(0).getElements()).hasSize(1);
255     }
256
257     @Test
258     void testsendAcElementInfo() {
259         var participantMessagePublisher = mock(ParticipantMessagePublisher.class);
260         var ach = new AutomationCompositionHandler(CommonTestData.getParticipantParameters(),
261                 participantMessagePublisher);
262         ach.getAutomationCompositionMap().putAll(commonTestData.getTestAutomationCompositionMap());
263         var key = ach.getAutomationCompositionMap().keySet().iterator().next();
264         var keyElement = ach.getAutomationCompositionMap().get(key).getElements().keySet().iterator().next();
265         ach.sendAcElementInfo(key, keyElement, "useState", "operationalState", Map.of("key", 1));
266         verify(participantMessagePublisher).sendParticipantStatus(any());
267     }
268
269     @Test
270     void testUndeployInstances() throws PfModelException {
271         var uuid = UUID.randomUUID();
272         var partecipantId = CommonTestData.getParticipantId();
273         var definition = CommonTestData.getDefinition();
274         var ach = commonTestData.setTestAutomationCompositionHandler(definition, uuid, partecipantId);
275         var listener = mock(AutomationCompositionElementListener.class);
276         ach.registerAutomationCompositionElementListener(listener);
277         ach.undeployInstances();
278         verify(listener).undeploy(any(), any());
279     }
280 }