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