41b3f20016c67e53aa0c9d0895651c7cfcddc93c
[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.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ElementState;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
36 import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.clamp.models.acm.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42
43 class AutomationCompositionElementHandlerV3Test {
44
45     private static final CompositionElementDto COMPOSITION_ELEMENT =
46             new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of());
47     private static final InstanceElementDto INSTANCE_ELEMENT =
48             new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), Map.of(), Map.of());
49     private static final CompositionDto COMPOSITION = new CompositionDto(UUID.randomUUID(), Map.of(), Map.of());
50
51     @Test
52     void testDeploy() {
53         var config = CommonTestData.createSimConfig();
54         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
55         var simulatorService = new SimulatorService(intermediaryApi);
56         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
57         simulatorService.setConfig(config);
58         acElementHandler.deploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
59         verify(intermediaryApi).updateAutomationCompositionElementState(
60                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
61                 null, StateChangeResult.NO_ERROR, "Deployed");
62
63         config.setDeploySuccess(false);
64         acElementHandler.deploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
65         verify(intermediaryApi).updateAutomationCompositionElementState(
66                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
67                 null, StateChangeResult.FAILED, "Deploy failed!");
68     }
69
70     @Test
71     void testUndeploy() {
72         var config = CommonTestData.createSimConfig();
73         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
74         var simulatorService = new SimulatorService(intermediaryApi);
75         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
76         simulatorService.setConfig(config);
77         acElementHandler.undeploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
78         verify(intermediaryApi).updateAutomationCompositionElementState(
79                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
80                 null, StateChangeResult.NO_ERROR, "Undeployed");
81
82         config.setUndeploySuccess(false);
83         acElementHandler.undeploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
84         verify(intermediaryApi).updateAutomationCompositionElementState(
85                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
86                 null, StateChangeResult.FAILED, "Undeploy failed!");
87     }
88
89     @Test
90     void testLock() {
91         var config = CommonTestData.createSimConfig();
92         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93         var simulatorService = new SimulatorService(intermediaryApi);
94         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
95         simulatorService.setConfig(config);
96         acElementHandler.lock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
97         verify(intermediaryApi).updateAutomationCompositionElementState(
98                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.LOCKED,
99                 StateChangeResult.NO_ERROR, "Locked");
100
101         config.setLockSuccess(false);
102         acElementHandler.lock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
103         verify(intermediaryApi).updateAutomationCompositionElementState(
104                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.UNLOCKED,
105                 StateChangeResult.FAILED, "Lock failed!");
106     }
107
108     @Test
109     void testUnlock() {
110         var config = CommonTestData.createSimConfig();
111         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
112         var simulatorService = new SimulatorService(intermediaryApi);
113         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
114         simulatorService.setConfig(config);
115         acElementHandler.unlock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
116         verify(intermediaryApi).updateAutomationCompositionElementState(
117                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.UNLOCKED,
118                 StateChangeResult.NO_ERROR, "Unlocked");
119
120         config.setUnlockSuccess(false);
121         acElementHandler.unlock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
122         verify(intermediaryApi).updateAutomationCompositionElementState(
123                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.LOCKED,
124                 StateChangeResult.FAILED, "Unlock failed!");
125     }
126
127     @Test
128     void testUpdate() {
129         var config = CommonTestData.createSimConfig();
130         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
131         var simulatorService = new SimulatorService(intermediaryApi);
132         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
133         simulatorService.setConfig(config);
134         var instanceElementUpdated = new InstanceElementDto(
135                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
136                 Map.of("key", "value"), Map.of());
137         acElementHandler.update(COMPOSITION_ELEMENT, INSTANCE_ELEMENT, instanceElementUpdated);
138         verify(intermediaryApi).updateAutomationCompositionElementState(
139                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
140                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
141
142         config.setUpdateSuccess(false);
143         acElementHandler.update(COMPOSITION_ELEMENT, INSTANCE_ELEMENT, instanceElementUpdated);
144         verify(intermediaryApi).updateAutomationCompositionElementState(
145                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
146                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
147     }
148
149     @Test
150     void testDelete() {
151         var config = CommonTestData.createSimConfig();
152         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
153         var simulatorService = new SimulatorService(intermediaryApi);
154         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
155         simulatorService.setConfig(config);
156         acElementHandler.delete(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
157         verify(intermediaryApi).updateAutomationCompositionElementState(
158                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DELETED,
159                 null, StateChangeResult.NO_ERROR, "Deleted");
160
161         config.setDeleteSuccess(false);
162         acElementHandler.delete(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
163         verify(intermediaryApi).updateAutomationCompositionElementState(
164                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
165                 null, StateChangeResult.FAILED, "Delete failed!");
166     }
167
168     @Test
169     void testPrime() {
170         var config = CommonTestData.createSimConfig();
171         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
172         var simulatorService = new SimulatorService(intermediaryApi);
173         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
174         simulatorService.setConfig(config);
175         acElementHandler.prime(COMPOSITION);
176         verify(intermediaryApi).updateCompositionState(
177                 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
178
179         config.setPrimeSuccess(false);
180         acElementHandler.prime(COMPOSITION);
181         verify(intermediaryApi).updateCompositionState(
182                 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.FAILED, "Prime failed!");
183     }
184
185     @Test
186     void testDeprime() {
187         var config = CommonTestData.createSimConfig();
188         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
189         var simulatorService = new SimulatorService(intermediaryApi);
190         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
191         simulatorService.setConfig(config);
192         acElementHandler.deprime(COMPOSITION);
193         verify(intermediaryApi).updateCompositionState(
194                 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed");
195
196         config.setDeprimeSuccess(false);
197         acElementHandler.deprime(COMPOSITION);
198         verify(intermediaryApi).updateCompositionState(
199                 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.FAILED, "Deprime failed!");
200     }
201
202     @Test
203     void testMigrate() {
204         var config = CommonTestData.createSimConfig();
205         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
206         var simulatorService = new SimulatorService(intermediaryApi);
207         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
208         simulatorService.setConfig(config);
209         var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
210                 Map.of(), Map.of());
211         var instanceElementMigrated = new InstanceElementDto(
212                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
213                 Map.of("key", "value"), new HashMap<>());
214         acElementHandler
215                 .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated, 0);
216         verify(intermediaryApi).updateAutomationCompositionElementState(
217                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
218                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
219
220         config.setMigrateSuccess(false);
221         acElementHandler
222                 .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated, 0);
223         verify(intermediaryApi).updateAutomationCompositionElementState(
224                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
225                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
226     }
227
228     @Test
229     void testMigrateStage() {
230         var config = CommonTestData.createSimConfig();
231         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
232         var simulatorService = new SimulatorService(intermediaryApi);
233         var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
234         simulatorService.setConfig(config);
235         var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
236                 Map.of("stage", List.of(1, 2)), Map.of());
237         var instanceElementMigrated = new InstanceElementDto(INSTANCE_ELEMENT.instanceId(),
238                 INSTANCE_ELEMENT.elementId(), Map.of(), new HashMap<>());
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(), 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(instanceElement.instanceId(), instanceElement.elementId(),
262                 Map.of(), new HashMap<>(), 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                 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                 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 }