fb8a04602a72e86b9a9fec1dd569ff38dc045483
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Future;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.Spy;
48 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
49 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
50 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
51 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
52 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData;
53 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
54 import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils;
55 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
56 import org.onap.policy.clamp.models.acm.concepts.DeployState;
57 import org.onap.policy.clamp.models.acm.concepts.LockState;
58 import org.onap.policy.common.utils.coder.Coder;
59 import org.onap.policy.common.utils.coder.CoderException;
60 import org.onap.policy.common.utils.coder.StandardCoder;
61 import org.onap.policy.models.base.PfModelException;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
63 import org.springframework.test.context.junit.jupiter.SpringExtension;
64
65 @ExtendWith(SpringExtension.class)
66 class AutomationCompositionElementHandlerTest {
67
68     private static final Coder CODER = new StandardCoder();
69     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
70     private static final String KEY_NAME =
71             "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement";
72     private static List<ChartInfo> charts;
73     private static ToscaServiceTemplate toscaServiceTemplate;
74     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
75             "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
76     private final CommonTestData commonTestData = new CommonTestData();
77
78     @InjectMocks
79     @Spy
80     private AutomationCompositionElementHandler automationCompositionElementHandler =
81             new AutomationCompositionElementHandler();
82
83     @Mock
84     private ChartService chartService;
85
86     @Mock
87     private ParticipantIntermediaryApi participantIntermediaryApi;
88
89     @Mock
90     private ExecutorService executor;
91     @Mock
92     private Future<String> result;
93
94     @BeforeAll
95     static void init() throws CoderException {
96         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
97         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
98     }
99
100     @Test
101     void test_AutomationCompositionElementStateChange() throws ServiceException {
102         var automationCompositionElementId1 = UUID.randomUUID();
103         var automationCompositionElementId2 = UUID.randomUUID();
104
105         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
106         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
107
108         doNothing().when(chartService).uninstallChart(charts.get(0));
109
110         automationCompositionElementHandler.undeploy(commonTestData.getAutomationCompositionId(),
111                 automationCompositionElementId1);
112
113         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
114
115         assertDoesNotThrow(() -> automationCompositionElementHandler
116                 .undeploy(commonTestData.getAutomationCompositionId(), automationCompositionElementId1));
117     }
118
119     @Test
120     void test_AutomationCompositionElementUpdate()
121             throws PfModelException, IOException, ServiceException, ExecutionException, InterruptedException {
122         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
123         var element = CommonTestData.createAcElementDeploy();
124         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
125
126         doReturn(false).when(chartService).installChart(any());
127         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
128                 element, nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
129
130         doReturn(true).when(chartService).installChart(any());
131         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
132                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
133
134         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(element.getId());
135
136         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
137
138         var elementId2 = UUID.randomUUID();
139         element.setId(elementId2);
140         assertThrows(PfModelException.class,
141                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
142                         nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
143
144         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
145     }
146
147     @Test
148     void test_checkPodStatus() throws ExecutionException, InterruptedException {
149         var chartInfo = charts.get(0);
150         var automationCompositionId = UUID.randomUUID();
151         assertThrows(ServiceException.class, () -> automationCompositionElementHandler
152                 .checkPodStatus(automationCompositionId, UUID.randomUUID(), chartInfo, 1, 1));
153     }
154
155     @Test
156     void testUpdate() throws PfModelException {
157         var element = CommonTestData.createAcElementDeploy();
158         var automationCompositionId = commonTestData.getAutomationCompositionId();
159         assertDoesNotThrow(
160                 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
161     }
162
163     @Test
164     void testLock() throws PfModelException {
165         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
166     }
167
168     @Test
169     void testUnlock() throws PfModelException {
170         assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
171     }
172
173     @Test
174     void testDelete() throws PfModelException {
175         assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
176     }
177
178     @Test
179     void testPrime() throws PfModelException {
180         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
181     }
182
183     @Test
184     void testDeprime() throws PfModelException {
185         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
186     }
187
188     @Test
189     void testHandleRestartComposition() throws PfModelException {
190         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartComposition(UUID.randomUUID(),
191                 List.of(), AcTypeState.PRIMED));
192     }
193
194     @Test
195     void testHandleRestartInstanceDeploying()
196             throws PfModelException, InterruptedException, ServiceException, IOException {
197         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
198         var element = CommonTestData.createAcElementDeploy();
199         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
200
201         doReturn(true).when(chartService).installChart(any());
202         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
203                 commonTestData.getAutomationCompositionId(), element,
204                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
205                 LockState.NONE));
206
207         assertThat(automationCompositionElementHandler.getChartMap()).containsKey(element.getId());
208     }
209
210     @Test
211     void testHandleRestartInstanceDeployed()
212             throws PfModelException, InterruptedException, ServiceException, IOException {
213         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
214         var element = CommonTestData.createAcElementDeploy();
215         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
216
217         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
218                 commonTestData.getAutomationCompositionId(), element,
219                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
220                 LockState.LOCKED));
221
222         assertThat(automationCompositionElementHandler.getChartMap()).containsKey(element.getId());
223     }
224
225     @Test
226     void testHandleRestartInstanceUndeploying()
227             throws PfModelException, InterruptedException, ServiceException, IOException {
228         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
229         var element = CommonTestData.createAcElementDeploy();
230         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
231
232         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
233                 commonTestData.getAutomationCompositionId(), element,
234                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
235                 LockState.LOCKED));
236     }
237
238     @Test
239     void testMigrate() throws PfModelException {
240         var element = CommonTestData.createAcElementDeploy();
241         var automationCompositionId = commonTestData.getAutomationCompositionId();
242         assertDoesNotThrow(() -> automationCompositionElementHandler.migrate(automationCompositionId, element,
243                 UUID.randomUUID(), Map.of()));
244     }
245 }