14b505f66ad374e0d819131e32c38aa968d70d1f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.kubernetes.handler;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.UUID;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Future;
42 import org.junit.jupiter.api.BeforeAll;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.api.extension.ExtendWith;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.Mockito;
48 import org.mockito.Spy;
49 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
50 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
51 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
52 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
53 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData;
54 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
55 import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils;
56 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
57 import org.onap.policy.clamp.models.acm.concepts.DeployState;
58 import org.onap.policy.clamp.models.acm.concepts.LockState;
59 import org.onap.policy.common.utils.coder.Coder;
60 import org.onap.policy.common.utils.coder.CoderException;
61 import org.onap.policy.common.utils.coder.StandardCoder;
62 import org.onap.policy.models.base.PfModelException;
63 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
64 import org.springframework.test.context.junit.jupiter.SpringExtension;
65
66 @ExtendWith(SpringExtension.class)
67 class AutomationCompositionElementHandlerTest {
68
69     private static final Coder CODER = new StandardCoder();
70     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
71     private static final String KEY_NAME =
72             "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement";
73     private static List<ChartInfo> charts;
74     private static ToscaServiceTemplate toscaServiceTemplate;
75     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
76             "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
77     private final CommonTestData commonTestData = new CommonTestData();
78
79     @InjectMocks
80     @Spy
81     private AutomationCompositionElementHandler automationCompositionElementHandler =
82             new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class));
83
84     @Mock
85     private ChartService chartService;
86
87     @Mock
88     private ExecutorService executor;
89     @Mock
90     private Future<String> result;
91
92     @BeforeAll
93     static void init() throws CoderException {
94         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
95         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
96     }
97
98     @Test
99     void test_AutomationCompositionElementStateChange() throws ServiceException {
100         var automationCompositionElementId1 = UUID.randomUUID();
101         var automationCompositionElementId2 = UUID.randomUUID();
102
103         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
104         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
105
106         doNothing().when(chartService).uninstallChart(charts.get(0));
107
108         automationCompositionElementHandler.undeploy(commonTestData.getAutomationCompositionId(),
109                 automationCompositionElementId1);
110
111         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
112
113         assertDoesNotThrow(() -> automationCompositionElementHandler
114                 .undeploy(commonTestData.getAutomationCompositionId(), automationCompositionElementId1));
115     }
116
117     @Test
118     void test_AutomationCompositionElementUpdate()
119             throws PfModelException, IOException, ServiceException, ExecutionException, InterruptedException {
120         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
121         var element = CommonTestData.createAcElementDeploy();
122         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
123
124         doReturn(false).when(chartService).installChart(any());
125         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
126                 element, nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
127
128         doReturn(true).when(chartService).installChart(any());
129         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
130                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
131
132         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(element.getId());
133
134         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
135
136         var elementId2 = UUID.randomUUID();
137         element.setId(elementId2);
138         assertThrows(PfModelException.class,
139                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
140                         nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
141
142         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
143     }
144
145     @Test
146     void test_checkPodStatus() throws ExecutionException, InterruptedException {
147         var chartInfo = charts.get(0);
148         var automationCompositionId = UUID.randomUUID();
149         assertThrows(ServiceException.class, () -> automationCompositionElementHandler
150                 .checkPodStatus(automationCompositionId, UUID.randomUUID(), chartInfo, 1, 1));
151     }
152
153     @Test
154     void testUpdate() throws PfModelException {
155         var element = CommonTestData.createAcElementDeploy();
156         var automationCompositionId = commonTestData.getAutomationCompositionId();
157         assertDoesNotThrow(
158                 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
159     }
160
161     @Test
162     void testLock() throws PfModelException {
163         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
164     }
165
166     @Test
167     void testUnlock() throws PfModelException {
168         assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
169     }
170
171     @Test
172     void testDelete() throws PfModelException {
173         assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
174     }
175
176     @Test
177     void testPrime() throws PfModelException {
178         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
179     }
180
181     @Test
182     void testDeprime() throws PfModelException {
183         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
184     }
185
186     @Test
187     void testHandleRestartComposition() throws PfModelException {
188         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartComposition(UUID.randomUUID(),
189                 List.of(), AcTypeState.PRIMED));
190     }
191
192     @Test
193     void testHandleRestartInstanceDeploying()
194             throws PfModelException, InterruptedException, ServiceException, IOException {
195         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
196         var element = CommonTestData.createAcElementDeploy();
197         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
198
199         doReturn(true).when(chartService).installChart(any());
200         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
201                 commonTestData.getAutomationCompositionId(), element,
202                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
203                 LockState.NONE));
204
205         assertThat(automationCompositionElementHandler.getChartMap()).containsKey(element.getId());
206     }
207
208     @Test
209     void testHandleRestartInstanceDeployed()
210             throws PfModelException, InterruptedException, ServiceException, IOException {
211         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
212         var element = CommonTestData.createAcElementDeploy();
213         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
214
215         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
216                 commonTestData.getAutomationCompositionId(), element,
217                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
218                 LockState.LOCKED));
219
220         assertThat(automationCompositionElementHandler.getChartMap()).containsKey(element.getId());
221     }
222
223     @Test
224     void testHandleRestartInstanceUndeploying()
225             throws PfModelException, InterruptedException, ServiceException, IOException {
226         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
227         var element = CommonTestData.createAcElementDeploy();
228         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
229
230         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
231                 commonTestData.getAutomationCompositionId(), element,
232                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
233                 LockState.LOCKED));
234     }
235
236     @Test
237     void testMigrate() throws PfModelException {
238         var element = CommonTestData.createAcElementDeploy();
239         var automationCompositionId = commonTestData.getAutomationCompositionId();
240         assertDoesNotThrow(() -> automationCompositionElementHandler.migrate(automationCompositionId, element,
241                 UUID.randomUUID(), Map.of()));
242     }
243 }