d211ab0d7131ccf3f047ad02e49b6b85b8d66a8e
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.policy.main.handler;
22
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;
28
29 import jakarta.ws.rs.core.Response;
30 import java.util.List;
31 import java.util.Map;
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;
49
50 class AutomationCompositionElementHandlerTest {
51
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);
57
58     @Test
59     void testHandlerUndeployNoPolicy() throws PfModelException {
60         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
61         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
62                 mock(PolicyPapHttpClient.class), intermediaryApi);
63
64         handler.undeploy(AC_ID, automationCompositionElementId);
65         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
66                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
67     }
68
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);
79         return element;
80     }
81
82     @Test
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());
88
89         var pap = mock(PolicyPapHttpClient.class);
90         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
91
92         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
94
95         handler.deploy(AC_ID, getTestingAcElement(), Map.of());
96         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
97                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
98
99         handler.undeploy(AC_ID, automationCompositionElementId);
100         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
101                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
102     }
103
104     @Test
105     void testDeployNoPolicy() throws PfModelException {
106         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
107         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
108                 mock(PolicyPapHttpClient.class), intermediaryApi);
109
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");
115     }
116
117     @Test
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());
122
123         var pap = mock(PolicyPapHttpClient.class);
124         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
125
126         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
127         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
128
129         var element = getTestingAcElement();
130
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.");
136     }
137
138     @Test
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());
143
144         var pap = mock(PolicyPapHttpClient.class);
145         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
146
147         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
148         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
149
150         var element = getTestingAcElement();
151         assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
152                 .hasMessageMatching("Deploy of Policy failed.");
153     }
154
155     @Test
156     void testUpdate() throws Exception {
157         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
158         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
159                 mock(PolicyPapHttpClient.class), intermediaryApi);
160
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");
166     }
167
168     @Test
169     void testLock() throws Exception {
170         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
171         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
172                 mock(PolicyPapHttpClient.class), intermediaryApi);
173
174         handler.lock(AC_ID, automationCompositionElementId);
175         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
176                 LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
177     }
178
179     @Test
180     void testUnlock() throws Exception {
181         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
182         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
183                 mock(PolicyPapHttpClient.class), intermediaryApi);
184
185         handler.unlock(AC_ID, automationCompositionElementId);
186         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
187                 LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
188     }
189
190     @Test
191     void testDelete() throws Exception {
192         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
193         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
194                 mock(PolicyPapHttpClient.class), intermediaryApi);
195
196         handler.delete(AC_ID, automationCompositionElementId);
197         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
198                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
199     }
200
201     @Test
202     void testPrime() throws Exception {
203         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
204         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
205                 mock(PolicyPapHttpClient.class), intermediaryApi);
206
207         handler.prime(AC_ID, List.of());
208         verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
209     }
210
211     @Test
212     void testDeprime() throws Exception {
213         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
214         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
215                 mock(PolicyPapHttpClient.class), intermediaryApi);
216
217         handler.deprime(AC_ID);
218         verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
219                 "Deprimed");
220     }
221
222     @Test
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);
228
229         var compositionId = UUID.randomUUID();
230         automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);
231
232         verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
233                 StateChangeResult.NO_ERROR, "Restarted");
234     }
235
236     @Test
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());
242
243         var pap = mock(PolicyPapHttpClient.class);
244         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
245
246         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
247         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
248         var element = getTestingAcElement();
249
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");
253
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");
257     }
258
259     @Test
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");
269     }
270 }