dec11fd4edb5bdf764d9247a7d82534badcc2a03
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-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.a1pms.handler;
22
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;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
37 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
39 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47
48 class AcElementHandlerTest {
49
50     private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
51
52     private final CommonTestData commonTestData = new CommonTestData();
53
54     private static ToscaServiceTemplate serviceTemplate;
55     private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
56             "org.onap.domain.database.A1PMSAutomationCompositionElement";
57
58     @BeforeAll
59     static void init() {
60         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
61     }
62
63     @BeforeEach
64     void startMocks() throws A1PolicyServiceException {
65         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
66         doNothing().when(acA1PmsClient).createService(any());
67     }
68
69     @Test
70     void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
71         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
72         var automationCompositionElementHandler =
73                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
74
75         var automationCompositionId = commonTestData.getAutomationCompositionId();
76         var element = commonTestData.getAutomationCompositionElement();
77         var automationCompositionElementId = element.getId();
78
79         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
80         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
81                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
82         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
83                 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
84
85         automationCompositionElementHandler.undeploy(automationCompositionId, automationCompositionElementId);
86         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
87                 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
88
89         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
90         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
91                 .undeploy(automationCompositionId, automationCompositionElementId));
92     }
93
94     @Test
95     void test_AutomationCompositionElementUpdate() throws A1PolicyServiceException {
96         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
97         var automationCompositionElementHandler =
98                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
99
100         var element = commonTestData.getAutomationCompositionElement();
101         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
102         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
103                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
104         verify(participantIntermediaryApi).updateAutomationCompositionElementState(
105                 commonTestData.getAutomationCompositionId(), element.getId(), DeployState.DEPLOYED, null,
106                 StateChangeResult.NO_ERROR, "Deployed");
107     }
108
109     @Test
110     void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
111         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
112         var automationCompositionElementHandler =
113                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
114
115         var element = commonTestData.getAutomationCompositionElement();
116         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
117
118         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
119         assertThrows(A1PolicyServiceException.class,
120                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
121                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
122     }
123
124     @Test
125     void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
126         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
127         var automationCompositionElementHandler =
128                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
129
130         var element = commonTestData.getAutomationCompositionElement();
131         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
132                 .deploy(commonTestData.getAutomationCompositionId(), element, Map.of()));
133     }
134
135     @Test
136     void testLock() throws PfModelException {
137         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
138         var automationCompositionElementHandler =
139                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
140
141         var automationCompositionId = UUID.randomUUID();
142         var elementId = UUID.randomUUID();
143         automationCompositionElementHandler.lock(automationCompositionId, elementId);
144
145         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
146                 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
147     }
148
149     @Test
150     void testUnlock() throws PfModelException {
151         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
152         var automationCompositionElementHandler =
153                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
154
155         var automationCompositionId = UUID.randomUUID();
156         var elementId = UUID.randomUUID();
157         automationCompositionElementHandler.unlock(automationCompositionId, elementId);
158
159         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
160                 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
161     }
162
163     @Test
164     void testUpdate() throws PfModelException {
165         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
166         var automationCompositionElementHandler =
167                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
168
169         var automationCompositionId = UUID.randomUUID();
170         var element = commonTestData.getAutomationCompositionElement();
171         automationCompositionElementHandler.update(automationCompositionId, element, Map.of());
172
173         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
174                 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
175     }
176
177     @Test
178     void testDelete() throws PfModelException {
179         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
180         var automationCompositionElementHandler =
181                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
182
183         var automationCompositionId = UUID.randomUUID();
184         var elementId = UUID.randomUUID();
185         automationCompositionElementHandler.delete(automationCompositionId, elementId);
186
187         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
188                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
189     }
190
191     @Test
192     void testPrime() throws PfModelException {
193         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
194         var automationCompositionElementHandler =
195                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
196
197         var compositionId = UUID.randomUUID();
198         automationCompositionElementHandler.prime(compositionId, List.of());
199
200         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
201                 StateChangeResult.NO_ERROR, "Primed");
202     }
203
204     @Test
205     void testDeprime() throws PfModelException {
206         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
207         var automationCompositionElementHandler =
208                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
209
210         var compositionId = UUID.randomUUID();
211         automationCompositionElementHandler.deprime(compositionId);
212
213         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
214                 StateChangeResult.NO_ERROR, "Deprimed");
215     }
216
217     @Test
218     void testHandleRestartComposition() throws PfModelException {
219         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
220         var automationCompositionElementHandler =
221                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
222
223         var compositionId = UUID.randomUUID();
224         automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);
225
226         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
227                 StateChangeResult.NO_ERROR, "Restarted");
228     }
229
230     @Test
231     void testHandleRestartInstanceDeploying() throws PfModelException {
232         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
233         var automationCompositionElementHandler =
234                 new AutomationCompositionElementHandler(participantIntermediaryApi, acA1PmsClient);
235
236         var automationCompositionId = UUID.randomUUID();
237         var element = commonTestData.getAutomationCompositionElement();
238         var automationCompositionElementId = element.getId();
239         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
240         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
241                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
242                 LockState.NONE);
243         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
244                 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
245     }
246
247     @Test
248     void testHandleRestartInstanceDeployed() throws PfModelException {
249         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
250         var automationCompositionElementHandler =
251                 new AutomationCompositionElementHandler(intermediaryApi, acA1PmsClient);
252
253         var automationCompositionId = UUID.randomUUID();
254         var element = commonTestData.getAutomationCompositionElement();
255         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
256         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
257                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
258                 LockState.LOCKED);
259         verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId, element.getId(),
260                 DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted");
261     }
262
263     @Test
264     void testHandleRestartInstanceUndeployed() throws PfModelException {
265         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
266         var automationCompositionElementHandler =
267                 new AutomationCompositionElementHandler(intermediaryApi, acA1PmsClient);
268
269         var automationCompositionId = UUID.randomUUID();
270         var element = commonTestData.getAutomationCompositionElement();
271         var automationCompositionElementId = element.getId();
272         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
273         automationCompositionElementHandler.handleRestartInstance(automationCompositionId, element,
274                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
275                 LockState.LOCKED);
276         verify(intermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
277                 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
278     }
279 }