260ef99184428e4f69f5783b7610a7a990d06c24
[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.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.spy;
33
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.UUID;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
45 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
46 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
47 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
48 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
49 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData;
50 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
51 import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils;
52 import org.onap.policy.common.utils.coder.Coder;
53 import org.onap.policy.common.utils.coder.CoderException;
54 import org.onap.policy.common.utils.coder.StandardCoder;
55 import org.onap.policy.models.base.PfModelException;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
57
58 class AutomationCompositionElementHandlerTest {
59
60     private static final Coder CODER = new StandardCoder();
61     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
62     private static List<ChartInfo> charts;
63     private static ToscaServiceTemplate toscaServiceTemplate;
64     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
65             "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
66     private final CommonTestData commonTestData = new CommonTestData();
67
68
69
70     @BeforeAll
71     static void init() throws CoderException {
72         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
73         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
74     }
75
76     @Test
77     void test_AutomationCompositionElementStateChange() throws ServiceException, PfModelException {
78         var chartService = Mockito.mock(ChartService.class);
79         var automationCompositionElementHandler =
80                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
81
82         doNothing().when(chartService).uninstallChart(charts.get(0));
83
84         ObjectMapper objectMapper = new ObjectMapper();
85         Map<String, Object> inPropertiesMap = objectMapper.convertValue(charts.get(0), new TypeReference<>() {});
86
87         automationCompositionElementHandler.undeploy(commonTestData.createCompositionElementDto(),
88                 commonTestData.createInstanceElementDto(Map.of("chart", inPropertiesMap)));
89
90         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
91
92         assertDoesNotThrow(() -> automationCompositionElementHandler
93                 .undeploy(commonTestData.createCompositionElementDto(),
94                         commonTestData.createInstanceElementDto(inPropertiesMap)));
95     }
96
97     @Test
98     void test_AutomationCompositionElementUpdate()
99             throws PfModelException, IOException, ServiceException, InterruptedException {
100         var chartService = Mockito.mock(ChartService.class);
101         var automationCompositionElementHandler =
102                 spy(new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService));
103
104         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt(),
105                 any());
106         var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
107         var instanceElementDto = commonTestData.createInstanceElementDto(nodeTemplatesMap
108                 .get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
109         var compositionElementDto = commonTestData.createCompositionElementDto();
110
111         doReturn(false).when(chartService).installChart(any());
112         assertThrows(PfModelException.class, () -> automationCompositionElementHandler.deploy(compositionElementDto,
113                 instanceElementDto));
114
115         doReturn(true).when(chartService).installChart(any());
116         automationCompositionElementHandler.deploy(compositionElementDto, instanceElementDto);
117
118         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
119
120         assertThrows(PfModelException.class,
121                 () -> automationCompositionElementHandler.deploy(compositionElementDto, instanceElementDto));
122
123     }
124
125     @Test
126     void test_checkPodStatus() {
127         var chartService = Mockito.mock(ChartService.class);
128         var automationCompositionElementHandler =
129                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
130
131         var chartInfo = charts.get(0);
132         var automationCompositionId = UUID.randomUUID();
133         assertThrows(PfModelException.class, () -> automationCompositionElementHandler
134                 .checkPodStatus(automationCompositionId, UUID.randomUUID(), chartInfo, 1, 1,
135                         commonTestData.createInstanceElementDto(Map.of())));
136     }
137
138     @Test
139     void testUpdate() {
140         var chartService = Mockito.mock(ChartService.class);
141         var automationCompositionElementHandler =
142                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
143         assertDoesNotThrow(
144                 () -> automationCompositionElementHandler.update(commonTestData.createCompositionElementDto(),
145                         commonTestData.createInstanceElementDto(Map.of()),
146                         commonTestData.createInstanceElementDto(Map.of())));
147     }
148
149     @Test
150     void testLock() {
151         var chartService = Mockito.mock(ChartService.class);
152         var automationCompositionElementHandler =
153                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
154
155         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(commonTestData.createCompositionElementDto(),
156                 commonTestData.createInstanceElementDto(Map.of())));
157     }
158
159     @Test
160     void testUnlock() {
161         var chartService = Mockito.mock(ChartService.class);
162         var automationCompositionElementHandler =
163                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
164
165         assertDoesNotThrow(() -> automationCompositionElementHandler
166                 .unlock(commonTestData.createCompositionElementDto(),
167                         commonTestData.createInstanceElementDto(Map.of())));
168     }
169
170     @Test
171     void testDelete() {
172         var chartService = Mockito.mock(ChartService.class);
173         var automationCompositionElementHandler =
174                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
175
176         assertDoesNotThrow(() -> automationCompositionElementHandler
177                 .delete(commonTestData.createCompositionElementDto(),
178                 commonTestData.createInstanceElementDto(Map.of())));
179     }
180
181     @Test
182     void testPrime() {
183         var chartService = Mockito.mock(ChartService.class);
184         var automationCompositionElementHandler =
185                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
186
187         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(new CompositionDto(UUID.randomUUID(),
188                 Map.of(), Map.of())));
189     }
190
191     @Test
192     void testDeprime() {
193         var chartService = Mockito.mock(ChartService.class);
194         var automationCompositionElementHandler =
195                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
196
197         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(new CompositionDto(UUID.randomUUID(),
198                 Map.of(), Map.of())));
199     }
200
201     @Test
202     void testMigrate() {
203         var chartService = Mockito.mock(ChartService.class);
204         var automationCompositionElementHandler =
205                 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
206
207         assertDoesNotThrow(() -> automationCompositionElementHandler
208                 .migrate(commonTestData.createCompositionElementDto(),
209                 commonTestData.createCompositionElementDto(), commonTestData.createInstanceElementDto(Map.of()),
210                 commonTestData.createInstanceElementDto(Map.of())));
211     }
212 }