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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.sim.main.handler;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
26 import java.util.HashMap;
27 import java.util.List;
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;
43 class AutomationCompositionElementHandlerV3Test {
45 private static final CompositionElementDto COMPOSITION_ELEMENT =
46 new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of());
47 private static final InstanceElementDto INSTANCE_ELEMENT =
48 new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), Map.of(), Map.of());
49 private static final CompositionDto COMPOSITION = new CompositionDto(UUID.randomUUID(), Map.of(), Map.of());
53 var config = CommonTestData.createSimConfig();
54 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
55 var simulatorService = new SimulatorService(intermediaryApi);
56 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
57 simulatorService.setConfig(config);
58 acElementHandler.deploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
59 verify(intermediaryApi).updateAutomationCompositionElementState(
60 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
61 null, StateChangeResult.NO_ERROR, "Deployed");
63 config.setDeploySuccess(false);
64 acElementHandler.deploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
65 verify(intermediaryApi).updateAutomationCompositionElementState(
66 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
67 null, StateChangeResult.FAILED, "Deploy failed!");
72 var config = CommonTestData.createSimConfig();
73 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
74 var simulatorService = new SimulatorService(intermediaryApi);
75 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
76 simulatorService.setConfig(config);
77 acElementHandler.undeploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
78 verify(intermediaryApi).updateAutomationCompositionElementState(
79 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
80 null, StateChangeResult.NO_ERROR, "Undeployed");
82 config.setUndeploySuccess(false);
83 acElementHandler.undeploy(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
84 verify(intermediaryApi).updateAutomationCompositionElementState(
85 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
86 null, StateChangeResult.FAILED, "Undeploy failed!");
91 var config = CommonTestData.createSimConfig();
92 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93 var simulatorService = new SimulatorService(intermediaryApi);
94 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
95 simulatorService.setConfig(config);
96 acElementHandler.lock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
97 verify(intermediaryApi).updateAutomationCompositionElementState(
98 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.LOCKED,
99 StateChangeResult.NO_ERROR, "Locked");
101 config.setLockSuccess(false);
102 acElementHandler.lock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
103 verify(intermediaryApi).updateAutomationCompositionElementState(
104 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.UNLOCKED,
105 StateChangeResult.FAILED, "Lock failed!");
110 var config = CommonTestData.createSimConfig();
111 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
112 var simulatorService = new SimulatorService(intermediaryApi);
113 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
114 simulatorService.setConfig(config);
115 acElementHandler.unlock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
116 verify(intermediaryApi).updateAutomationCompositionElementState(
117 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.UNLOCKED,
118 StateChangeResult.NO_ERROR, "Unlocked");
120 config.setUnlockSuccess(false);
121 acElementHandler.unlock(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
122 verify(intermediaryApi).updateAutomationCompositionElementState(
123 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), null, LockState.LOCKED,
124 StateChangeResult.FAILED, "Unlock failed!");
129 var config = CommonTestData.createSimConfig();
130 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
131 var simulatorService = new SimulatorService(intermediaryApi);
132 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
133 simulatorService.setConfig(config);
134 var instanceElementUpdated = new InstanceElementDto(
135 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
136 Map.of("key", "value"), Map.of());
137 acElementHandler.update(COMPOSITION_ELEMENT, INSTANCE_ELEMENT, instanceElementUpdated);
138 verify(intermediaryApi).updateAutomationCompositionElementState(
139 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
140 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
142 config.setUpdateSuccess(false);
143 acElementHandler.update(COMPOSITION_ELEMENT, INSTANCE_ELEMENT, instanceElementUpdated);
144 verify(intermediaryApi).updateAutomationCompositionElementState(
145 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
146 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
151 var config = CommonTestData.createSimConfig();
152 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
153 var simulatorService = new SimulatorService(intermediaryApi);
154 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
155 simulatorService.setConfig(config);
156 acElementHandler.delete(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
157 verify(intermediaryApi).updateAutomationCompositionElementState(
158 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DELETED,
159 null, StateChangeResult.NO_ERROR, "Deleted");
161 config.setDeleteSuccess(false);
162 acElementHandler.delete(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
163 verify(intermediaryApi).updateAutomationCompositionElementState(
164 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
165 null, StateChangeResult.FAILED, "Delete failed!");
170 var config = CommonTestData.createSimConfig();
171 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
172 var simulatorService = new SimulatorService(intermediaryApi);
173 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
174 simulatorService.setConfig(config);
175 acElementHandler.prime(COMPOSITION);
176 verify(intermediaryApi).updateCompositionState(
177 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
179 config.setPrimeSuccess(false);
180 acElementHandler.prime(COMPOSITION);
181 verify(intermediaryApi).updateCompositionState(
182 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.FAILED, "Prime failed!");
187 var config = CommonTestData.createSimConfig();
188 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
189 var simulatorService = new SimulatorService(intermediaryApi);
190 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
191 simulatorService.setConfig(config);
192 acElementHandler.deprime(COMPOSITION);
193 verify(intermediaryApi).updateCompositionState(
194 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, "Deprimed");
196 config.setDeprimeSuccess(false);
197 acElementHandler.deprime(COMPOSITION);
198 verify(intermediaryApi).updateCompositionState(
199 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.FAILED, "Deprime failed!");
204 var config = CommonTestData.createSimConfig();
205 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
206 var simulatorService = new SimulatorService(intermediaryApi);
207 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
208 simulatorService.setConfig(config);
209 var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
211 var instanceElementMigrated = new InstanceElementDto(
212 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
213 Map.of("key", "value"), new HashMap<>());
215 .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated, 0);
216 verify(intermediaryApi).updateAutomationCompositionElementState(
217 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
218 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
220 config.setMigrateSuccess(false);
222 .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated, 0);
223 verify(intermediaryApi).updateAutomationCompositionElementState(
224 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
225 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
229 void testMigrateStage() {
230 var config = CommonTestData.createSimConfig();
231 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
232 var simulatorService = new SimulatorService(intermediaryApi);
233 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
234 simulatorService.setConfig(config);
235 var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
236 Map.of("stage", List.of(1, 2)), Map.of());
237 var instanceElementMigrated = new InstanceElementDto(INSTANCE_ELEMENT.instanceId(),
238 INSTANCE_ELEMENT.elementId(), Map.of(), new HashMap<>());
240 .migrate(COMPOSITION_ELEMENT, compositionElementTarget, INSTANCE_ELEMENT, instanceElementMigrated, 1);
241 verify(intermediaryApi).updateAutomationCompositionElementStage(
242 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
243 StateChangeResult.NO_ERROR, 2, "stage 1 Migrated");
247 void testMigrateAdd() {
248 var config = CommonTestData.createSimConfig();
249 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
250 var simulatorService = new SimulatorService(intermediaryApi);
251 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
252 simulatorService.setConfig(config);
253 var compositionElement = new CompositionElementDto(
254 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
256 var instanceElement = new InstanceElementDto(
257 UUID.randomUUID(), UUID.randomUUID(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
259 var compoElTargetAdd = new CompositionElementDto(
260 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NEW);
261 var inElMigratedAdd = new InstanceElementDto(instanceElement.instanceId(), instanceElement.elementId(),
262 Map.of(), new HashMap<>(), ElementState.NEW);
264 .migrate(compositionElement, compoElTargetAdd, instanceElement, inElMigratedAdd, 0);
265 verify(intermediaryApi).updateAutomationCompositionElementState(
266 instanceElement.instanceId(), instanceElement.elementId(),
267 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
271 void testMigrateRemove() {
272 var config = CommonTestData.createSimConfig();
273 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
274 var simulatorService = new SimulatorService(intermediaryApi);
275 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
276 simulatorService.setConfig(config);
278 var compoElTargetRemove = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
279 Map.of(), Map.of(), ElementState.REMOVED);
280 var inElMigratedRemove = new InstanceElementDto(
281 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
282 Map.of("key", "value"), Map.of(), ElementState.REMOVED);
284 .migrate(COMPOSITION_ELEMENT, compoElTargetRemove, INSTANCE_ELEMENT, inElMigratedRemove, 0);
285 verify(intermediaryApi).updateAutomationCompositionElementState(
286 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
287 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
288 verify(intermediaryApi).updateAutomationCompositionElementState(
289 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
290 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
294 void testMigratePrecheck() {
295 var config = CommonTestData.createSimConfig();
296 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
297 var simulatorService = new SimulatorService(intermediaryApi);
298 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
299 simulatorService.setConfig(config);
300 var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
302 var instanceElementMigrated = new InstanceElementDto(
303 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
304 Map.of("key", "value"), Map.of());
305 acElementHandler.migratePrecheck(COMPOSITION_ELEMENT, compositionElementTarget,
306 INSTANCE_ELEMENT, instanceElementMigrated);
307 verify(intermediaryApi).updateAutomationCompositionElementState(
308 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
309 DeployState.DEPLOYED, null,
310 StateChangeResult.NO_ERROR, "Migration precheck completed");
312 config.setMigratePrecheck(false);
313 acElementHandler.migratePrecheck(COMPOSITION_ELEMENT, compositionElementTarget,
314 INSTANCE_ELEMENT, instanceElementMigrated);
315 verify(intermediaryApi).updateAutomationCompositionElementState(
316 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
317 DeployState.DEPLOYED, null,
318 StateChangeResult.FAILED, "Migration precheck failed");
323 var config = CommonTestData.createSimConfig();
324 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
325 var simulatorService = new SimulatorService(intermediaryApi);
326 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
327 simulatorService.setConfig(config);
328 acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
329 verify(intermediaryApi).updateAutomationCompositionElementState(
330 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
331 null, StateChangeResult.NO_ERROR, "Prepare completed");
333 config.setPrepare(false);
334 acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
335 verify(intermediaryApi).updateAutomationCompositionElementState(
336 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
337 null, StateChangeResult.FAILED, "Prepare failed");
342 var config = CommonTestData.createSimConfig();
343 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
344 var simulatorService = new SimulatorService(intermediaryApi);
345 var acElementHandler = new AutomationCompositionElementHandlerV3(intermediaryApi, simulatorService);
346 simulatorService.setConfig(config);
347 acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
348 verify(intermediaryApi).updateAutomationCompositionElementState(
349 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
350 null, StateChangeResult.NO_ERROR, "Review completed");
352 config.setReview(false);
353 acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
354 verify(intermediaryApi).updateAutomationCompositionElementState(
355 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
356 null, StateChangeResult.FAILED, "Review failed");