e89a826965a27043f8197df85a399c3e989659ab
[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.HashMap;
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 AutomationCompositionElementHandlerV2Test {
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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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 AutomationCompositionElementHandlerV2(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"), new HashMap<>());
213         acElementHandler
214             .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated);
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);
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 testMigrateAdd() {
229         var config = CommonTestData.createSimConfig();
230         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
231         var simulatorService = new SimulatorService(intermediaryApi);
232         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
233         simulatorService.setConfig(config);
234
235         var compositionElement = new CompositionElementDto(
236                 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
237
238         var instanceElement = new InstanceElementDto(
239                 UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of(), ElementState.NOT_PRESENT);
240
241         var compoElTargetAdd = new CompositionElementDto(
242                 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NEW);
243         var inElMigratedAdd = new InstanceElementDto(
244                 instanceElement.instanceId(), instanceElement.elementId(), null,
245                 Map.of(), new HashMap<>(), ElementState.NEW);
246         acElementHandler
247                 .migrate(compositionElement, compoElTargetAdd, instanceElement, inElMigratedAdd);
248         verify(intermediaryApi).updateAutomationCompositionElementState(
249                 instanceElement.instanceId(), instanceElement.elementId(),
250                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
251     }
252
253     @Test
254     void testMigrateRemove() {
255         var config = CommonTestData.createSimConfig();
256         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
257         var simulatorService = new SimulatorService(intermediaryApi);
258         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
259         simulatorService.setConfig(config);
260
261         var compoElTargetRemove = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
262                 Map.of(), Map.of(), ElementState.REMOVED);
263         var inElMigratedRemove = new InstanceElementDto(
264                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
265                 null, Map.of("key", "value"), Map.of(), ElementState.REMOVED);
266         acElementHandler
267                 .migrate(COMPOSITION_ELEMENT, compoElTargetRemove, INSTANCE_ELEMENT, inElMigratedRemove);
268         verify(intermediaryApi).updateAutomationCompositionElementState(
269                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
270                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
271         verify(intermediaryApi).updateAutomationCompositionElementState(
272                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
273                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
274     }
275
276     @Test
277     void testMigratePrecheck() {
278         var config = CommonTestData.createSimConfig();
279         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
280         var simulatorService = new SimulatorService(intermediaryApi);
281         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
282         simulatorService.setConfig(config);
283         var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
284                 Map.of(), Map.of());
285         var instanceElementMigrated = new InstanceElementDto(
286                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
287                 null, Map.of("key", "value"), Map.of());
288         acElementHandler.migratePrecheck(COMPOSITION_ELEMENT, compositionElementTarget,
289                 INSTANCE_ELEMENT, instanceElementMigrated);
290         verify(intermediaryApi).updateAutomationCompositionElementState(
291                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
292                 DeployState.DEPLOYED, null,
293                 StateChangeResult.NO_ERROR, "Migration precheck completed");
294
295         config.setMigratePrecheck(false);
296         acElementHandler.migratePrecheck(COMPOSITION_ELEMENT, compositionElementTarget,
297                 INSTANCE_ELEMENT, instanceElementMigrated);
298         verify(intermediaryApi).updateAutomationCompositionElementState(
299                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
300                 DeployState.DEPLOYED, null,
301                 StateChangeResult.FAILED, "Migration precheck failed");
302     }
303
304     @Test
305     void testPrepare() {
306         var config = CommonTestData.createSimConfig();
307         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
308         var simulatorService = new SimulatorService(intermediaryApi);
309         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
310         simulatorService.setConfig(config);
311         acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
312         verify(intermediaryApi).updateAutomationCompositionElementState(
313                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
314                 null, StateChangeResult.NO_ERROR, "Prepare completed");
315
316         config.setPrepare(false);
317         acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
318         verify(intermediaryApi).updateAutomationCompositionElementState(
319                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
320                 null, StateChangeResult.FAILED, "Prepare failed");
321     }
322
323     @Test
324     void testReview() {
325         var config = CommonTestData.createSimConfig();
326         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
327         var simulatorService = new SimulatorService(intermediaryApi);
328         var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
329         simulatorService.setConfig(config);
330         acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
331         verify(intermediaryApi).updateAutomationCompositionElementState(
332                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
333                 null, StateChangeResult.NO_ERROR, "Review completed");
334
335         config.setReview(false);
336         acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
337         verify(intermediaryApi).updateAutomationCompositionElementState(
338                 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
339                 null, StateChangeResult.FAILED, "Review failed");
340     }
341 }