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