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
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;
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;
42 class AutomationCompositionElementHandlerV2Test {
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(), Map.of(), Map.of());
48 private static final CompositionDto COMPOSITION = new CompositionDto(UUID.randomUUID(), Map.of(), Map.of());
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");
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!");
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");
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!");
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");
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!");
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");
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!");
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(),
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");
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!");
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");
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!");
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");
178 config.setPrimeSuccess(false);
179 acElementHandler.prime(COMPOSITION);
180 verify(intermediaryApi).updateCompositionState(
181 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.FAILED, "Prime failed!");
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");
195 config.setDeprimeSuccess(false);
196 acElementHandler.deprime(COMPOSITION);
197 verify(intermediaryApi).updateCompositionState(
198 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.FAILED, "Deprime failed!");
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(),
210 var instanceElementMigrated = new InstanceElementDto(
211 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
212 Map.of("key", "value"), new HashMap<>());
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");
219 config.setMigrateSuccess(false);
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!");
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);
235 var compositionElement = new CompositionElementDto(
236 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
238 var instanceElement = new InstanceElementDto(
239 UUID.randomUUID(), UUID.randomUUID(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
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(), Map.of(), new HashMap<>(), ElementState.NEW);
246 .migrate(compositionElement, compoElTargetAdd, instanceElement, inElMigratedAdd);
247 verify(intermediaryApi).updateAutomationCompositionElementState(
248 instanceElement.instanceId(), instanceElement.elementId(),
249 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
253 void testMigrateRemove() {
254 var config = CommonTestData.createSimConfig();
255 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
256 var simulatorService = new SimulatorService(intermediaryApi);
257 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
258 simulatorService.setConfig(config);
260 var compoElTargetRemove = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
261 Map.of(), Map.of(), ElementState.REMOVED);
262 var inElMigratedRemove = new InstanceElementDto(
263 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
264 Map.of("key", "value"), Map.of(), ElementState.REMOVED);
266 .migrate(COMPOSITION_ELEMENT, compoElTargetRemove, INSTANCE_ELEMENT, inElMigratedRemove);
267 verify(intermediaryApi).updateAutomationCompositionElementState(
268 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
269 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
270 verify(intermediaryApi).updateAutomationCompositionElementState(
271 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
272 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
276 void testMigratePrecheck() {
277 var config = CommonTestData.createSimConfig();
278 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
279 var simulatorService = new SimulatorService(intermediaryApi);
280 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
281 simulatorService.setConfig(config);
282 var compositionElementTarget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
284 var instanceElementMigrated = new InstanceElementDto(
285 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
286 Map.of("key", "value"), Map.of());
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.NO_ERROR, "Migration precheck completed");
294 config.setMigratePrecheck(false);
295 acElementHandler.migratePrecheck(COMPOSITION_ELEMENT, compositionElementTarget,
296 INSTANCE_ELEMENT, instanceElementMigrated);
297 verify(intermediaryApi).updateAutomationCompositionElementState(
298 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
299 DeployState.DEPLOYED, null,
300 StateChangeResult.FAILED, "Migration precheck failed");
305 var config = CommonTestData.createSimConfig();
306 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
307 var simulatorService = new SimulatorService(intermediaryApi);
308 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
309 simulatorService.setConfig(config);
310 acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
311 verify(intermediaryApi).updateAutomationCompositionElementState(
312 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
313 null, StateChangeResult.NO_ERROR, "Prepare completed");
315 config.setPrepare(false);
316 acElementHandler.prepare(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
317 verify(intermediaryApi).updateAutomationCompositionElementState(
318 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.UNDEPLOYED,
319 null, StateChangeResult.FAILED, "Prepare failed");
324 var config = CommonTestData.createSimConfig();
325 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
326 var simulatorService = new SimulatorService(intermediaryApi);
327 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
328 simulatorService.setConfig(config);
329 acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
330 verify(intermediaryApi).updateAutomationCompositionElementState(
331 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
332 null, StateChangeResult.NO_ERROR, "Review completed");
334 config.setReview(false);
335 acElementHandler.review(COMPOSITION_ELEMENT, INSTANCE_ELEMENT);
336 verify(intermediaryApi).updateAutomationCompositionElementState(
337 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(), DeployState.DEPLOYED,
338 null, StateChangeResult.FAILED, "Review failed");