2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.oransc.participant.dme.handler;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.oransc.participant.dme.exception.DmeException;
45 import org.oransc.participant.dme.restclient.AcDmeClient;
46 import org.oransc.participant.dme.utils.CommonTestData;
47 import org.oransc.participant.dme.utils.ToscaUtils;
49 class AcElementHandlerTest {
51 private final AcDmeClient acDmeClient = mock(AcDmeClient.class);
53 private final CommonTestData commonTestData = new CommonTestData();
55 private static ToscaServiceTemplate serviceTemplate;
56 private static final String DME_AUTOMATION_COMPOSITION_ELEMENT =
57 "onap.policy.clamp.ac.element.DMEAutomationCompositionElement";
61 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
65 void startMocks() throws DmeException, JsonProcessingException {
66 when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.TRUE);
67 doNothing().when(acDmeClient).createInfoType(any());
71 void test_automationCompositionElementStateChange() throws DmeException {
72 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
73 var automationCompositionElementHandler =
74 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
76 var automationCompositionId = commonTestData.getAutomationCompositionId();
77 var element = commonTestData.getAutomationCompositionElement();
78 var automationCompositionElementId = element.getId();
80 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
81 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
82 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
83 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
84 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
86 automationCompositionElementHandler.undeploy(automationCompositionId, automationCompositionElementId);
87 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
88 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
90 when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.FALSE);
91 assertThrows(DmeException.class, () -> automationCompositionElementHandler
92 .undeploy(automationCompositionId, automationCompositionElementId));
96 void test_AutomationCompositionElementUpdate() throws DmeException {
97 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
98 var automationCompositionElementHandler =
99 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
101 var element = commonTestData.getAutomationCompositionElement();
102 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
103 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
104 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
105 verify(participantIntermediaryApi).updateAutomationCompositionElementState(
106 commonTestData.getAutomationCompositionId(), element.getId(), DeployState.DEPLOYED, null,
107 StateChangeResult.NO_ERROR, "Deployed");
111 void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
112 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
113 var automationCompositionElementHandler =
114 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
116 var element = commonTestData.getAutomationCompositionElement();
117 when(acDmeClient.isDmeHealthy()).thenReturn(Boolean.FALSE);
119 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
120 assertThrows(DmeException.class,
121 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
122 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
126 void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
127 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
128 var automationCompositionElementHandler =
129 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
131 var element = commonTestData.getAutomationCompositionElement();
132 assertThrows(DmeException.class, () -> automationCompositionElementHandler
133 .deploy(commonTestData.getAutomationCompositionId(), element, Map.of()));
138 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
139 var automationCompositionElementHandler =
140 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
142 var automationCompositionId = UUID.randomUUID();
143 var elementId = UUID.randomUUID();
144 automationCompositionElementHandler.lock(automationCompositionId, elementId);
146 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
147 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
152 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
153 var automationCompositionElementHandler =
154 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
156 var automationCompositionId = UUID.randomUUID();
157 var elementId = UUID.randomUUID();
158 automationCompositionElementHandler.unlock(automationCompositionId, elementId);
160 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
161 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
166 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
167 var automationCompositionElementHandler =
168 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
170 var automationCompositionId = UUID.randomUUID();
171 var element = commonTestData.getAutomationCompositionElement();
172 automationCompositionElementHandler.update(automationCompositionId, element, Map.of());
174 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
175 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
180 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
181 var automationCompositionElementHandler =
182 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
184 var automationCompositionId = UUID.randomUUID();
185 var elementId = UUID.randomUUID();
186 automationCompositionElementHandler.delete(automationCompositionId, elementId);
188 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
189 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
194 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
195 var automationCompositionElementHandler =
196 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
198 var compositionId = UUID.randomUUID();
199 automationCompositionElementHandler.prime(compositionId, List.of());
201 verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
202 StateChangeResult.NO_ERROR, "Primed");
207 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
208 var automationCompositionElementHandler =
209 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
211 var compositionId = UUID.randomUUID();
212 automationCompositionElementHandler.deprime(compositionId);
214 verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
215 StateChangeResult.NO_ERROR, "Deprimed");
219 void testHandleRestartComposition() {
220 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
221 var automationCompositionElementHandler =
222 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
224 var compositionId = UUID.randomUUID();
225 automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);
227 verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
228 StateChangeResult.NO_ERROR, "Restarted");
232 void testHandleRestartInstanceDeploying() throws PfModelException {
233 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
234 var automationCompositionElementHandler =
235 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
237 var automationCompositionId = UUID.randomUUID();
238 var element = commonTestData.getAutomationCompositionElement();
239 var automationCompositionElementId = element.getId();
240 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
241 automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
242 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
244 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
245 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
249 void testHandleRestartInstanceDeployed() throws PfModelException {
250 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
251 var automationCompositionElementHandler =
252 new AutomationCompositionElementHandler(intermediaryApi, acDmeClient);
254 var automationCompositionId = UUID.randomUUID();
255 var element = commonTestData.getAutomationCompositionElement();
256 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
257 automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
258 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
260 verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId, element.getId(),
261 DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted");
265 void testHandleRestartInstanceUndeployed() throws PfModelException {
266 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
267 var automationCompositionElementHandler =
268 new AutomationCompositionElementHandler(intermediaryApi, acDmeClient);
270 var automationCompositionId = UUID.randomUUID();
271 var element = commonTestData.getAutomationCompositionElement();
272 var automationCompositionElementId = element.getId();
273 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
274 automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
275 nodeTemplatesMap.get(DME_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
277 verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
278 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
283 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
284 var automationCompositionElementHandler =
285 new AutomationCompositionElementHandler(participantIntermediaryApi, acDmeClient);
287 var automationCompositionId = UUID.randomUUID();
288 var element = commonTestData.getAutomationCompositionElement();
289 automationCompositionElementHandler.migrate(automationCompositionId, element, UUID.randomUUID(), Map.of());
291 verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
292 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");