26dcb05ffdab37c6a029b57ee2c1dc23266005ce
[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 org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.Spy;
46 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
47 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
48 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
49 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
50 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData;
51 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
52 import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.CoderException;
55 import org.onap.policy.common.utils.coder.StandardCoder;
56 import org.onap.policy.models.base.PfModelException;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
58 import org.springframework.test.context.junit.jupiter.SpringExtension;
59
60 @ExtendWith(SpringExtension.class)
61 class AutomationCompositionElementHandlerTest {
62
63     private static final Coder CODER = new StandardCoder();
64     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
65     private static final String KEY_NAME =
66             "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement";
67     private static List<ChartInfo> charts;
68     private static ToscaServiceTemplate toscaServiceTemplate;
69     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
70             "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
71     private final CommonTestData commonTestData = new CommonTestData();
72
73     @InjectMocks
74     @Spy
75     private AutomationCompositionElementHandler automationCompositionElementHandler =
76             new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class));
77
78     @Mock
79     private ChartService chartService;
80
81     @BeforeAll
82     static void init() throws CoderException {
83         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
84         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
85     }
86
87     @Test
88     void test_AutomationCompositionElementStateChange() throws ServiceException {
89         var automationCompositionElementId1 = UUID.randomUUID();
90         var automationCompositionElementId2 = UUID.randomUUID();
91
92         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
93         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
94
95         doNothing().when(chartService).uninstallChart(charts.get(0));
96
97         automationCompositionElementHandler.undeploy(commonTestData.getAutomationCompositionId(),
98                 automationCompositionElementId1);
99
100         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
101
102         assertDoesNotThrow(() -> automationCompositionElementHandler
103                 .undeploy(commonTestData.getAutomationCompositionId(), automationCompositionElementId1));
104     }
105
106     @Test
107     void test_AutomationCompositionElementUpdate()
108             throws PfModelException, IOException, ServiceException, InterruptedException {
109         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
110         var element = CommonTestData.createAcElementDeploy();
111         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
112
113         doReturn(false).when(chartService).installChart(any());
114         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
115                 element, nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
116
117         doReturn(true).when(chartService).installChart(any());
118         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
119                 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
120
121         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(element.getId());
122
123         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
124
125         var elementId2 = UUID.randomUUID();
126         element.setId(elementId2);
127         assertThrows(PfModelException.class,
128                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
129                         nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
130
131         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
132     }
133
134     @Test
135     void test_checkPodStatus() {
136         var chartInfo = charts.get(0);
137         var automationCompositionId = UUID.randomUUID();
138         assertThrows(ServiceException.class, () -> automationCompositionElementHandler
139                 .checkPodStatus(automationCompositionId, UUID.randomUUID(), chartInfo, 1, 1));
140     }
141
142     @Test
143     void testUpdate() {
144         var element = CommonTestData.createAcElementDeploy();
145         var automationCompositionId = commonTestData.getAutomationCompositionId();
146         assertDoesNotThrow(
147                 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
148     }
149
150     @Test
151     void testLock() {
152         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
153     }
154
155     @Test
156     void testUnlock() {
157         assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
158     }
159
160     @Test
161     void testDelete() {
162         assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
163     }
164
165     @Test
166     void testPrime() {
167         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
168     }
169
170     @Test
171     void testDeprime() {
172         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
173     }
174
175     @Test
176     void testMigrate() {
177         var element = CommonTestData.createAcElementDeploy();
178         var automationCompositionId = commonTestData.getAutomationCompositionId();
179         assertDoesNotThrow(() -> automationCompositionElementHandler.migrate(automationCompositionId, element,
180                 UUID.randomUUID(), Map.of()));
181     }
182 }