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.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;
35 import java.io.IOException;
36 import java.util.List;
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;
60 @ExtendWith(SpringExtension.class)
61 class AutomationCompositionElementHandlerTest {
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();
75 private AutomationCompositionElementHandler automationCompositionElementHandler =
76 new AutomationCompositionElementHandler(mock(ParticipantIntermediaryApi.class));
79 private ChartService chartService;
82 static void init() throws CoderException {
83 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
84 toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
88 void test_AutomationCompositionElementStateChange() throws ServiceException {
89 var automationCompositionElementId1 = UUID.randomUUID();
90 var automationCompositionElementId2 = UUID.randomUUID();
92 automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
93 automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
95 doNothing().when(chartService).uninstallChart(charts.get(0));
97 automationCompositionElementHandler.undeploy(commonTestData.getAutomationCompositionId(),
98 automationCompositionElementId1);
100 doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
102 assertDoesNotThrow(() -> automationCompositionElementHandler
103 .undeploy(commonTestData.getAutomationCompositionId(), automationCompositionElementId1));
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();
113 doReturn(false).when(chartService).installChart(any());
114 assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
115 element, nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
117 doReturn(true).when(chartService).installChart(any());
118 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
119 nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
121 assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(element.getId());
123 doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
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()));
131 assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
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));
144 var element = CommonTestData.createAcElementDeploy();
145 var automationCompositionId = commonTestData.getAutomationCompositionId();
147 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
152 assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
157 assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
162 assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
167 assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
172 assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
177 var element = CommonTestData.createAcElementDeploy();
178 var automationCompositionId = commonTestData.getAutomationCompositionId();
179 assertDoesNotThrow(() -> automationCompositionElementHandler.migrate(automationCompositionId, element,
180 UUID.randomUUID(), Map.of()));