ec55c178fddb9642ee715c3d8208d6769bb67375
[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 java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import javax.ws.rs.core.Response;
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 handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
61                 mock(PolicyPapHttpClient.class));
62         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
63         handler.setIntermediaryApi(intermediaryApi);
64
65         handler.undeploy(AC_ID, automationCompositionElementId);
66         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
67                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
68     }
69
70     private AcElementDeploy getTestingAcElement() {
71         var element = new AcElementDeploy();
72         element.setDefinition(DEFINITION);
73         element.setId(automationCompositionElementId);
74         element.setOrderedState(DeployOrder.DEPLOY);
75         var template = new ToscaServiceTemplate();
76         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
77         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
78         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
79         element.setToscaServiceTemplateFragment(template);
80         return element;
81     }
82
83     @Test
84     void testDeploy() throws PfModelException {
85         // Mock success scenario for policy creation and deployment
86         var api = mock(PolicyApiHttpClient.class);
87         doReturn(Response.ok().build()).when(api).createPolicyType(any());
88         doReturn(Response.ok().build()).when(api).createPolicy(any());
89
90         var pap = mock(PolicyPapHttpClient.class);
91         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
92
93         var handler = new AutomationCompositionElementHandler(api, pap);
94         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
95         handler.setIntermediaryApi(intermediaryApi);
96
97         handler.deploy(AC_ID, getTestingAcElement(), Map.of());
98         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
99                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
100
101         handler.undeploy(AC_ID, automationCompositionElementId);
102         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
103                 DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
104     }
105
106     @Test
107     void testDeployNoPolicy() throws PfModelException {
108         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
109                 mock(PolicyPapHttpClient.class));
110         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
111         handler.setIntermediaryApi(intermediaryApi);
112
113         var acElement = getTestingAcElement();
114         acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
115         handler.deploy(AC_ID, acElement, Map.of());
116         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
117                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
118     }
119
120     @Test
121     void testApiException() throws PfModelException {
122         var api = mock(PolicyApiHttpClient.class);
123         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
124         doReturn(Response.ok().build()).when(api).createPolicy(any());
125
126         var pap = mock(PolicyPapHttpClient.class);
127         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
128
129         var handler = new AutomationCompositionElementHandler(api, pap);
130         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
131         handler.setIntermediaryApi(intermediaryApi);
132         var element = getTestingAcElement();
133
134         // Mock failure in policy type creation
135         handler.deploy(AC_ID, element, Map.of());
136         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
137                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
138                 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
139     }
140
141     @Test
142     void testDeployPapException() throws PfModelException {
143         var api = mock(PolicyApiHttpClient.class);
144         doReturn(Response.ok().build()).when(api).createPolicyType(any());
145         doReturn(Response.ok().build()).when(api).createPolicy(any());
146
147         var pap = mock(PolicyPapHttpClient.class);
148         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
149
150         var handler = new AutomationCompositionElementHandler(api, pap);
151         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
152         handler.setIntermediaryApi(intermediaryApi);
153         var element = getTestingAcElement();
154         assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
155                 .hasMessageMatching("Deploy of Policy failed.");
156     }
157
158     @Test
159     void testUpdate() throws Exception {
160         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
161                 mock(PolicyPapHttpClient.class));
162         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
163         handler.setIntermediaryApi(intermediaryApi);
164
165         var acElement = getTestingAcElement();
166         acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
167         handler.update(AC_ID, acElement, Map.of());
168         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
169                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
170     }
171
172     @Test
173     void testLock() throws Exception {
174         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
175                 mock(PolicyPapHttpClient.class));
176         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
177         handler.setIntermediaryApi(intermediaryApi);
178
179         handler.lock(AC_ID, automationCompositionElementId);
180         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
181                 LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
182     }
183
184     @Test
185     void testUnlock() throws Exception {
186         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
187                 mock(PolicyPapHttpClient.class));
188         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
189         handler.setIntermediaryApi(intermediaryApi);
190
191         handler.unlock(AC_ID, automationCompositionElementId);
192         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
193                 LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
194     }
195
196     @Test
197     void testDelete() throws Exception {
198         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
199                 mock(PolicyPapHttpClient.class));
200         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
201         handler.setIntermediaryApi(intermediaryApi);
202
203         handler.delete(AC_ID, automationCompositionElementId);
204         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
205                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
206     }
207
208     @Test
209     void testPrime() throws Exception {
210         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
211                 mock(PolicyPapHttpClient.class));
212         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
213         handler.setIntermediaryApi(intermediaryApi);
214
215         handler.prime(AC_ID, List.of());
216         verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
217     }
218
219     @Test
220     void testDeprime() throws Exception {
221         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
222                 mock(PolicyPapHttpClient.class));
223         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
224         handler.setIntermediaryApi(intermediaryApi);
225
226         handler.deprime(AC_ID);
227         verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
228                 "Deprimed");
229     }
230 }