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