1e2ec2c33e2488c29a27833070620915da4a92dc
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-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.sim.main.handler;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25
26 import java.util.Map;
27 import java.util.UUID;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
35 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41
42 class AutomationCompositionElementHandlerV2Test {
43
44     @Test
45     void testDeploy() {
46         var config = new SimConfig();
47         config.setDeployTimerMs(1);
48         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
49         var simulatorService = new SimulatorService(intermediaryApi);
50         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
51         simulatorService.setConfig(config);
52         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
53                 Map.of(), Map.of());
54         var instanceId = UUID.randomUUID();
55         var elementId = UUID.randomUUID();
56         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
57         acElementHandler.deploy(compositionElement, instanceElement);
58         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
59                 null, StateChangeResult.NO_ERROR, "Deployed");
60
61         config.setDeploySuccess(false);
62         acElementHandler.deploy(compositionElement, instanceElement);
63         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
64                 null, StateChangeResult.FAILED, "Deploy failed!");
65     }
66
67     @Test
68     void testUndeploy() {
69         var config = new SimConfig();
70         config.setUndeployTimerMs(1);
71         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
72         var simulatorService = new SimulatorService(intermediaryApi);
73         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
74         simulatorService.setConfig(config);
75         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
76                 Map.of(), Map.of());
77         var instanceId = UUID.randomUUID();
78         var elementId = UUID.randomUUID();
79         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
80         acElementHandler.undeploy(compositionElement, instanceElement);
81         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
82                 null, StateChangeResult.NO_ERROR, "Undeployed");
83
84         config.setUndeploySuccess(false);
85         acElementHandler.undeploy(compositionElement, instanceElement);
86         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
87                 null, StateChangeResult.FAILED, "Undeploy failed!");
88     }
89
90     @Test
91     void testLock() {
92         var config = new SimConfig();
93         config.setLockTimerMs(1);
94         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
95         var simulatorService = new SimulatorService(intermediaryApi);
96         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
97         simulatorService.setConfig(config);
98         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
99                 Map.of(), Map.of());
100         var instanceId = UUID.randomUUID();
101         var elementId = UUID.randomUUID();
102         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
103         acElementHandler.lock(compositionElement, instanceElement);
104         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
105                 StateChangeResult.NO_ERROR, "Locked");
106
107         config.setLockSuccess(false);
108         acElementHandler.lock(compositionElement, instanceElement);
109         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
110                 StateChangeResult.FAILED, "Lock failed!");
111     }
112
113     @Test
114     void testUnlock() {
115         var config = new SimConfig();
116         config.setUnlockTimerMs(1);
117         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
118         var simulatorService = new SimulatorService(intermediaryApi);
119         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
120         simulatorService.setConfig(config);
121         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
122                 Map.of(), Map.of());
123         var instanceId = UUID.randomUUID();
124         var elementId = UUID.randomUUID();
125         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
126         acElementHandler.unlock(compositionElement, instanceElement);
127         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
128                 StateChangeResult.NO_ERROR, "Unlocked");
129
130         config.setUnlockSuccess(false);
131         acElementHandler.unlock(compositionElement, instanceElement);
132         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
133                 StateChangeResult.FAILED, "Unlock failed!");
134     }
135
136     @Test
137     void testUpdate() {
138         var config = new SimConfig();
139         config.setUpdateTimerMs(1);
140         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
141         var simulatorService = new SimulatorService(intermediaryApi);
142         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
143         simulatorService.setConfig(config);
144         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
145                 Map.of(), Map.of());
146         var instanceId = UUID.randomUUID();
147         var element = new AcElementDeploy();
148         element.setId(UUID.randomUUID());
149         var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
150         var instanceElementUpdated = new InstanceElementDto(instanceId, element.getId(), null,
151                 Map.of("key", "value"), Map.of());
152         acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated);
153         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
154                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
155
156         config.setUpdateSuccess(false);
157         acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated);
158         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
159                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
160     }
161
162     @Test
163     void testDelete() {
164         var config = new SimConfig();
165         config.setDeleteTimerMs(1);
166         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
167         var simulatorService = new SimulatorService(intermediaryApi);
168         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
169         simulatorService.setConfig(config);
170         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
171                 Map.of(), Map.of());
172         var instanceId = UUID.randomUUID();
173         var elementId = UUID.randomUUID();
174         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
175         acElementHandler.delete(compositionElement, instanceElement);
176         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED,
177                 null, StateChangeResult.NO_ERROR, "Deleted");
178
179         config.setDeleteSuccess(false);
180         acElementHandler.delete(compositionElement, instanceElement);
181         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
182                 null, StateChangeResult.FAILED, "Delete failed!");
183     }
184
185     @Test
186     void testPrime() {
187         var config = new SimConfig();
188         config.setPrimeTimerMs(1);
189         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
190         var simulatorService = new SimulatorService(intermediaryApi);
191         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
192         simulatorService.setConfig(config);
193         var compositionId = UUID.randomUUID();
194         var composition = new CompositionDto(compositionId, Map.of(), Map.of());
195         acElementHandler.prime(composition);
196         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
197                 "Primed");
198
199         config.setPrimeSuccess(false);
200         acElementHandler.prime(composition);
201         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
202                 StateChangeResult.FAILED, "Prime failed!");
203     }
204
205     @Test
206     void testDeprime() {
207         var config = new SimConfig();
208         config.setDeprimeTimerMs(1);
209         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
210         var simulatorService = new SimulatorService(intermediaryApi);
211         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
212         simulatorService.setConfig(config);
213         var compositionId = UUID.randomUUID();
214         var composition = new CompositionDto(compositionId, Map.of(), Map.of());
215         acElementHandler.deprime(composition);
216         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
217                 StateChangeResult.NO_ERROR, "Deprimed");
218
219         config.setDeprimeSuccess(false);
220         acElementHandler.deprime(composition);
221         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
222                 "Deprime failed!");
223     }
224
225     @Test
226     void testMigrate() throws PfModelException {
227         var config = new SimConfig();
228         config.setUpdateTimerMs(1);
229         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
230         var simulatorService = new SimulatorService(intermediaryApi);
231         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
232         simulatorService.setConfig(config);
233         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
234                 Map.of(), Map.of());
235         var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
236                 Map.of(), Map.of());
237         var instanceId = UUID.randomUUID();
238         var element = new AcElementDeploy();
239         element.setId(UUID.randomUUID());
240         var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
241         var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(),
242                 null, Map.of("key", "value"), Map.of());
243         acElementHandler
244                 .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated);
245         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
246                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
247
248         config.setMigrateSuccess(false);
249         acElementHandler
250                 .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated);
251         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
252                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
253     }
254
255     @Test
256     void testMigratePrecheck() {
257         var config = new SimConfig();
258         config.setUpdateTimerMs(1);
259         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
260         var simulatorService = new SimulatorService(intermediaryApi);
261         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
262         simulatorService.setConfig(config);
263         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
264                 Map.of(), Map.of());
265         var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
266                 Map.of(), Map.of());
267         var instanceId = UUID.randomUUID();
268         var element = new AcElementDeploy();
269         element.setId(UUID.randomUUID());
270         var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
271         var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(),
272                 null, Map.of("key", "value"), Map.of());
273         acElementHandler.migratePrecheck(compositionElement, compositionElementTraget,
274                 instanceElement, instanceElementMigrated);
275         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
276                 DeployState.DEPLOYED, null,
277                 StateChangeResult.NO_ERROR, "Migration precheck completed");
278
279         config.setMigratePrecheck(false);
280         acElementHandler.migratePrecheck(compositionElement, compositionElementTraget,
281                 instanceElement, instanceElementMigrated);
282         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
283                 DeployState.DEPLOYED, null,
284                 StateChangeResult.FAILED, "Migration precheck failed");
285     }
286
287     @Test
288     void testPrepare() {
289         var config = new SimConfig();
290         config.setDeployTimerMs(1);
291         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
292         var simulatorService = new SimulatorService(intermediaryApi);
293         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
294         simulatorService.setConfig(config);
295         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
296                 Map.of(), Map.of());
297         var instanceId = UUID.randomUUID();
298         var elementId = UUID.randomUUID();
299         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
300         acElementHandler.prepare(compositionElement, instanceElement);
301         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
302                 null, StateChangeResult.NO_ERROR, "Prepare completed");
303
304         config.setPrepare(false);
305         acElementHandler.prepare(compositionElement, instanceElement);
306         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
307                 null, StateChangeResult.FAILED, "Prepare failed");
308     }
309
310     @Test
311     void testReview() {
312         var config = new SimConfig();
313         config.setDeployTimerMs(1);
314         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
315         var simulatorService = new SimulatorService(intermediaryApi);
316         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
317         simulatorService.setConfig(config);
318         var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
319                 Map.of(), Map.of());
320         var instanceId = UUID.randomUUID();
321         var elementId = UUID.randomUUID();
322         var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
323         acElementHandler.review(compositionElement, instanceElement);
324         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
325                 null, StateChangeResult.NO_ERROR, "Review completed");
326
327         config.setReview(false);
328         acElementHandler.review(compositionElement, instanceElement);
329         verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
330                 null, StateChangeResult.FAILED, "Review failed");
331     }
332 }