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