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.InstanceElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.acm.participant.sim.model.SimConfig;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
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.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 class AutomationCompositionElementHandlerV2Test {
46 var config = new SimConfig();
47 config.setDeployTimerMs(1);
48 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
49 var simulatorService = new SimulatorService(intermediaryApi);
50 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
51 simulatorService.setConfig(config);
52 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
54 var instanceId = UUID.randomUUID();
55 var elementId = UUID.randomUUID();
56 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
57 acElementHandler.deploy(compositionElement, instanceElement);
58 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
59 null, StateChangeResult.NO_ERROR, "Deployed");
61 config.setDeploySuccess(false);
62 acElementHandler.deploy(compositionElement, instanceElement);
63 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
64 null, StateChangeResult.FAILED, "Deploy failed!");
69 var config = new SimConfig();
70 config.setUndeployTimerMs(1);
71 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
72 var simulatorService = new SimulatorService(intermediaryApi);
73 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
74 simulatorService.setConfig(config);
75 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
77 var instanceId = UUID.randomUUID();
78 var elementId = UUID.randomUUID();
79 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
80 acElementHandler.undeploy(compositionElement, instanceElement);
81 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
82 null, StateChangeResult.NO_ERROR, "Undeployed");
84 config.setUndeploySuccess(false);
85 acElementHandler.undeploy(compositionElement, instanceElement);
86 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
87 null, StateChangeResult.FAILED, "Undeploy failed!");
92 var config = new SimConfig();
93 config.setLockTimerMs(1);
94 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
95 var simulatorService = new SimulatorService(intermediaryApi);
96 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
97 simulatorService.setConfig(config);
98 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
100 var instanceId = UUID.randomUUID();
101 var elementId = UUID.randomUUID();
102 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
103 acElementHandler.lock(compositionElement, instanceElement);
104 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
105 StateChangeResult.NO_ERROR, "Locked");
107 config.setLockSuccess(false);
108 acElementHandler.lock(compositionElement, instanceElement);
109 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
110 StateChangeResult.FAILED, "Lock failed!");
115 var config = new SimConfig();
116 config.setUnlockTimerMs(1);
117 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
118 var simulatorService = new SimulatorService(intermediaryApi);
119 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
120 simulatorService.setConfig(config);
121 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
123 var instanceId = UUID.randomUUID();
124 var elementId = UUID.randomUUID();
125 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
126 acElementHandler.unlock(compositionElement, instanceElement);
127 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
128 StateChangeResult.NO_ERROR, "Unlocked");
130 config.setUnlockSuccess(false);
131 acElementHandler.unlock(compositionElement, instanceElement);
132 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
133 StateChangeResult.FAILED, "Unlock failed!");
138 var config = new SimConfig();
139 config.setUpdateTimerMs(1);
140 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
141 var simulatorService = new SimulatorService(intermediaryApi);
142 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
143 simulatorService.setConfig(config);
144 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
146 var instanceId = UUID.randomUUID();
147 var element = new AcElementDeploy();
148 element.setId(UUID.randomUUID());
149 var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
150 var instanceElementUpdated = new InstanceElementDto(instanceId, element.getId(), null,
151 Map.of("key", "value"), Map.of());
152 acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated);
153 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
154 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
156 config.setUpdateSuccess(false);
157 acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated);
158 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
159 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
164 var config = new SimConfig();
165 config.setDeleteTimerMs(1);
166 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
167 var simulatorService = new SimulatorService(intermediaryApi);
168 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
169 simulatorService.setConfig(config);
170 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
172 var instanceId = UUID.randomUUID();
173 var elementId = UUID.randomUUID();
174 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
175 acElementHandler.delete(compositionElement, instanceElement);
176 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED,
177 null, StateChangeResult.NO_ERROR, "Deleted");
179 config.setDeleteSuccess(false);
180 acElementHandler.delete(compositionElement, instanceElement);
181 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
182 null, StateChangeResult.FAILED, "Delete failed!");
187 var config = new SimConfig();
188 config.setPrimeTimerMs(1);
189 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
190 var simulatorService = new SimulatorService(intermediaryApi);
191 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
192 simulatorService.setConfig(config);
193 var compositionId = UUID.randomUUID();
194 var composition = new CompositionDto(compositionId, Map.of(), Map.of());
195 acElementHandler.prime(composition);
196 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
199 config.setPrimeSuccess(false);
200 acElementHandler.prime(composition);
201 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
202 StateChangeResult.FAILED, "Prime failed!");
207 var config = new SimConfig();
208 config.setDeprimeTimerMs(1);
209 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
210 var simulatorService = new SimulatorService(intermediaryApi);
211 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
212 simulatorService.setConfig(config);
213 var compositionId = UUID.randomUUID();
214 var composition = new CompositionDto(compositionId, Map.of(), Map.of());
215 acElementHandler.deprime(composition);
216 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
217 StateChangeResult.NO_ERROR, "Deprimed");
219 config.setDeprimeSuccess(false);
220 acElementHandler.deprime(composition);
221 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
226 void testMigrate() throws PfModelException {
227 var config = new SimConfig();
228 config.setUpdateTimerMs(1);
229 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
230 var simulatorService = new SimulatorService(intermediaryApi);
231 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
232 simulatorService.setConfig(config);
233 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
235 var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
237 var instanceId = UUID.randomUUID();
238 var element = new AcElementDeploy();
239 element.setId(UUID.randomUUID());
240 var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
241 var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(),
242 null, Map.of("key", "value"), Map.of());
244 .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated);
245 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
246 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
248 config.setMigrateSuccess(false);
250 .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated);
251 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
252 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
256 void testMigratePrecheck() {
257 var config = new SimConfig();
258 config.setUpdateTimerMs(1);
259 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
260 var simulatorService = new SimulatorService(intermediaryApi);
261 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
262 simulatorService.setConfig(config);
263 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
265 var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
267 var instanceId = UUID.randomUUID();
268 var element = new AcElementDeploy();
269 element.setId(UUID.randomUUID());
270 var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of());
271 var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(),
272 null, Map.of("key", "value"), Map.of());
273 acElementHandler.migratePrecheck(compositionElement, compositionElementTraget,
274 instanceElement, instanceElementMigrated);
275 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
276 DeployState.DEPLOYED, null,
277 StateChangeResult.NO_ERROR, "Migration precheck completed");
279 config.setMigratePrecheck(false);
280 acElementHandler.migratePrecheck(compositionElement, compositionElementTraget,
281 instanceElement, instanceElementMigrated);
282 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
283 DeployState.DEPLOYED, null,
284 StateChangeResult.FAILED, "Migration precheck failed");
289 var config = new SimConfig();
290 config.setDeployTimerMs(1);
291 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
292 var simulatorService = new SimulatorService(intermediaryApi);
293 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
294 simulatorService.setConfig(config);
295 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
297 var instanceId = UUID.randomUUID();
298 var elementId = UUID.randomUUID();
299 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
300 acElementHandler.prepare(compositionElement, instanceElement);
301 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
302 null, StateChangeResult.NO_ERROR, "Prepare completed");
304 config.setPrepare(false);
305 acElementHandler.prepare(compositionElement, instanceElement);
306 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED,
307 null, StateChangeResult.FAILED, "Prepare failed");
312 var config = new SimConfig();
313 config.setDeployTimerMs(1);
314 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
315 var simulatorService = new SimulatorService(intermediaryApi);
316 var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService);
317 simulatorService.setConfig(config);
318 var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(),
320 var instanceId = UUID.randomUUID();
321 var elementId = UUID.randomUUID();
322 var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of());
323 acElementHandler.review(compositionElement, instanceElement);
324 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
325 null, StateChangeResult.NO_ERROR, "Review completed");
327 config.setReview(false);
328 acElementHandler.review(compositionElement, instanceElement);
329 verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED,
330 null, StateChangeResult.FAILED, "Review failed");