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