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