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;
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;
41 class AutomationCompositionElementHandlerV2Test {
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());
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");
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!");
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");
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!");
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");
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!");
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");
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!");
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");
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!");
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");
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!");
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");
177 config.setPrimeSuccess(false);
178 acElementHandler.prime(COMPOSITION);
179 verify(intermediaryApi).updateCompositionState(
180 COMPOSITION.compositionId(), AcTypeState.COMMISSIONED, StateChangeResult.FAILED, "Prime failed!");
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");
194 config.setDeprimeSuccess(false);
195 acElementHandler.deprime(COMPOSITION);
196 verify(intermediaryApi).updateCompositionState(
197 COMPOSITION.compositionId(), AcTypeState.PRIMED, StateChangeResult.FAILED, "Deprime failed!");
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(),
209 var instanceElementMigrated = new InstanceElementDto(
210 INSTANCE_ELEMENT.instanceId(), INSTANCE_ELEMENT.elementId(),
211 null, Map.of("key", "value"), Map.of());
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");
218 config.setMigrateSuccess(false);
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!");
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);
234 var compositionElement = new CompositionElementDto(
235 UUID.randomUUID(), new ToscaConceptIdentifier(), Map.of(), Map.of(), ElementState.NOT_PRESENT);
237 var instanceElement = new InstanceElementDto(
238 UUID.randomUUID(), UUID.randomUUID(), null, Map.of(), Map.of(), ElementState.NOT_PRESENT);
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);
245 .migrate(compositionElement, compoElTargetAdd, instanceElement, inElMigratedAdd);
246 verify(intermediaryApi).updateAutomationCompositionElementState(
247 instanceElement.instanceId(), instanceElement.elementId(),
248 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
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);
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);
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");
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(),
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");
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");
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");
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");
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");
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");