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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.kubernetes.handler;
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;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
37 import java.io.IOException;
38 import java.util.List;
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;
58 class AutomationCompositionElementHandlerTest {
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();
71 static void init() throws CoderException {
72 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
73 toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
77 void test_AutomationCompositionElementStateChange() throws ServiceException, PfModelException {
78 var chartService = Mockito.mock(ChartService.class);
79 var automationCompositionElementHandler =
80 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
82 doNothing().when(chartService).uninstallChart(charts.get(0));
84 ObjectMapper objectMapper = new ObjectMapper();
85 Map<String, Object> inPropertiesMap = objectMapper.convertValue(charts.get(0), new TypeReference<>() {});
87 automationCompositionElementHandler.undeploy(commonTestData.createCompositionElementDto(),
88 commonTestData.createInstanceElementDto(Map.of("chart", inPropertiesMap)));
90 doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
92 assertDoesNotThrow(() -> automationCompositionElementHandler
93 .undeploy(commonTestData.createCompositionElementDto(),
94 commonTestData.createInstanceElementDto(inPropertiesMap)));
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));
104 doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt(),
106 var nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
107 var instanceElementDto = commonTestData.createInstanceElementDto(nodeTemplatesMap
108 .get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
109 var compositionElementDto = commonTestData.createCompositionElementDto();
111 doReturn(false).when(chartService).installChart(any());
112 assertThrows(PfModelException.class, () -> automationCompositionElementHandler.deploy(compositionElementDto,
113 instanceElementDto));
115 doReturn(true).when(chartService).installChart(any());
116 automationCompositionElementHandler.deploy(compositionElementDto, instanceElementDto);
118 doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
120 assertThrows(PfModelException.class,
121 () -> automationCompositionElementHandler.deploy(compositionElementDto, instanceElementDto));
126 void test_checkPodStatus() {
127 var chartService = Mockito.mock(ChartService.class);
128 var automationCompositionElementHandler =
129 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
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())));
140 var chartService = Mockito.mock(ChartService.class);
141 var automationCompositionElementHandler =
142 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
144 () -> automationCompositionElementHandler.update(commonTestData.createCompositionElementDto(),
145 commonTestData.createInstanceElementDto(Map.of()),
146 commonTestData.createInstanceElementDto(Map.of())));
151 var chartService = Mockito.mock(ChartService.class);
152 var automationCompositionElementHandler =
153 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
155 assertDoesNotThrow(() -> automationCompositionElementHandler.lock(commonTestData.createCompositionElementDto(),
156 commonTestData.createInstanceElementDto(Map.of())));
161 var chartService = Mockito.mock(ChartService.class);
162 var automationCompositionElementHandler =
163 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
165 assertDoesNotThrow(() -> automationCompositionElementHandler
166 .unlock(commonTestData.createCompositionElementDto(),
167 commonTestData.createInstanceElementDto(Map.of())));
172 var chartService = Mockito.mock(ChartService.class);
173 var automationCompositionElementHandler =
174 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
176 assertDoesNotThrow(() -> automationCompositionElementHandler
177 .delete(commonTestData.createCompositionElementDto(),
178 commonTestData.createInstanceElementDto(Map.of())));
183 var chartService = Mockito.mock(ChartService.class);
184 var automationCompositionElementHandler =
185 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
187 assertDoesNotThrow(() -> automationCompositionElementHandler.prime(new CompositionDto(UUID.randomUUID(),
188 Map.of(), Map.of())));
193 var chartService = Mockito.mock(ChartService.class);
194 var automationCompositionElementHandler =
195 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
197 assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(new CompositionDto(UUID.randomUUID(),
198 Map.of(), Map.of())));
203 var chartService = Mockito.mock(ChartService.class);
204 var automationCompositionElementHandler =
205 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class), chartService);
207 assertDoesNotThrow(() -> automationCompositionElementHandler
208 .migrate(commonTestData.createCompositionElementDto(),
209 commonTestData.createCompositionElementDto(), commonTestData.createInstanceElementDto(Map.of()),
210 commonTestData.createInstanceElementDto(Map.of())));