2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-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.onap.policy.clamp.acm.participant.policy.main.handler;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
29 import jakarta.ws.rs.core.Response;
30 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
36 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
37 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
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.clamp.models.acm.messages.rest.instantiation.DeployOrder;
43 import org.onap.policy.models.base.PfModelException;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
50 class AutomationCompositionElementHandlerTest {
52 private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
53 private static final String ID_VERSION = "1.0.1";
54 private static final UUID automationCompositionElementId = UUID.randomUUID();
55 public static final UUID AC_ID = UUID.randomUUID();
56 private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
59 void testHandlerUndeployNoPolicy() throws PfModelException {
60 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
61 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
62 mock(PolicyPapHttpClient.class), intermediaryApi);
64 handler.undeploy(AC_ID, automationCompositionElementId);
65 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
66 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
69 private AcElementDeploy getTestingAcElement() {
70 var element = new AcElementDeploy();
71 element.setDefinition(DEFINITION);
72 element.setId(automationCompositionElementId);
73 element.setOrderedState(DeployOrder.DEPLOY);
74 var template = new ToscaServiceTemplate();
75 template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
76 template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
77 template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
78 element.setToscaServiceTemplateFragment(template);
83 void testDeploy() throws PfModelException {
84 // Mock success scenario for policy creation and deployment
85 var api = mock(PolicyApiHttpClient.class);
86 doReturn(Response.ok().build()).when(api).createPolicyType(any());
87 doReturn(Response.ok().build()).when(api).createPolicy(any());
89 var pap = mock(PolicyPapHttpClient.class);
90 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
92 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
95 handler.deploy(AC_ID, getTestingAcElement(), Map.of());
96 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
97 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
99 handler.undeploy(AC_ID, automationCompositionElementId);
100 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
101 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
105 void testDeployNoPolicy() throws PfModelException {
106 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
107 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
108 mock(PolicyPapHttpClient.class), intermediaryApi);
110 var acElement = getTestingAcElement();
111 acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
112 handler.deploy(AC_ID, acElement, Map.of());
113 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
114 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
118 void testApiException() throws PfModelException {
119 var api = mock(PolicyApiHttpClient.class);
120 doReturn(Response.serverError().build()).when(api).createPolicyType(any());
121 doReturn(Response.ok().build()).when(api).createPolicy(any());
123 var pap = mock(PolicyPapHttpClient.class);
124 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
126 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
127 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
129 var element = getTestingAcElement();
131 // Mock failure in policy type creation
132 handler.deploy(AC_ID, element, Map.of());
133 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
134 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
135 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
139 void testDeployPapException() throws PfModelException {
140 var api = mock(PolicyApiHttpClient.class);
141 doReturn(Response.ok().build()).when(api).createPolicyType(any());
142 doReturn(Response.ok().build()).when(api).createPolicy(any());
144 var pap = mock(PolicyPapHttpClient.class);
145 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
147 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
148 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
150 var element = getTestingAcElement();
151 assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
152 .hasMessageMatching("Deploy of Policy failed.");
156 void testUpdate() throws Exception {
157 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
158 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
159 mock(PolicyPapHttpClient.class), intermediaryApi);
161 var acElement = getTestingAcElement();
162 acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
163 handler.update(AC_ID, acElement, Map.of());
164 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
165 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
169 void testLock() throws Exception {
170 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
171 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
172 mock(PolicyPapHttpClient.class), intermediaryApi);
174 handler.lock(AC_ID, automationCompositionElementId);
175 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
176 LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
180 void testUnlock() throws Exception {
181 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
182 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
183 mock(PolicyPapHttpClient.class), intermediaryApi);
185 handler.unlock(AC_ID, automationCompositionElementId);
186 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
187 LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
191 void testDelete() throws Exception {
192 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
193 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
194 mock(PolicyPapHttpClient.class), intermediaryApi);
196 handler.delete(AC_ID, automationCompositionElementId);
197 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
198 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
202 void testPrime() throws Exception {
203 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
204 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
205 mock(PolicyPapHttpClient.class), intermediaryApi);
207 handler.prime(AC_ID, List.of());
208 verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
212 void testDeprime() throws Exception {
213 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
214 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
215 mock(PolicyPapHttpClient.class), intermediaryApi);
217 handler.deprime(AC_ID);
218 verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
223 void testHandleRestartComposition() throws PfModelException {
224 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
225 var automationCompositionElementHandler =
226 new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
227 mock(PolicyPapHttpClient.class), intermediaryApi);
229 var compositionId = UUID.randomUUID();
230 automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);
232 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
233 StateChangeResult.NO_ERROR, "Restarted");
237 void testHandleRestartInstanceDeploying() throws PfModelException {
238 // Mock success scenario for policy creation and deployment
239 var api = mock(PolicyApiHttpClient.class);
240 doReturn(Response.ok().build()).when(api).createPolicyType(any());
241 doReturn(Response.ok().build()).when(api).createPolicy(any());
243 var pap = mock(PolicyPapHttpClient.class);
244 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
246 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
247 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
248 var element = getTestingAcElement();
250 handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.DEPLOYING, LockState.NONE);
251 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
252 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
254 handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.UNDEPLOYING, LockState.LOCKED);
255 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
256 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
260 void testHandleRestartInstanceDeployed() throws PfModelException {
261 var api = mock(PolicyApiHttpClient.class);
262 var pap = mock(PolicyPapHttpClient.class);
263 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
264 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
265 var element = getTestingAcElement();
266 handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKED);
267 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
268 DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted");
272 void testMigrate() throws Exception {
273 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
274 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
275 mock(PolicyPapHttpClient.class), intermediaryApi);
277 var acElement = getTestingAcElement();
278 acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
279 handler.migrate(AC_ID, acElement, UUID.randomUUID(), Map.of());
280 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
281 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");