ee0039c3f24e0d9aaa26e70bd360a012f16a316f
[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.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
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.UUID;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Future;
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.clamp.models.acm.concepts.AcElementDeploy;
54 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
55 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
56 import org.onap.policy.common.utils.coder.Coder;
57 import org.onap.policy.common.utils.coder.CoderException;
58 import org.onap.policy.common.utils.coder.StandardCoder;
59 import org.onap.policy.models.base.PfModelException;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
61 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
62 import org.springframework.test.context.junit.jupiter.SpringExtension;
63
64 @ExtendWith(SpringExtension.class)
65 class AutomationCompositionElementHandlerTest {
66
67     private static final Coder CODER = new StandardCoder();
68     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
69     private static final String KEY_NAME =
70         "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement";
71     private static List<ChartInfo> charts;
72     private static ToscaServiceTemplate toscaServiceTemplate;
73     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
74         "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
75     private final CommonTestData commonTestData = new CommonTestData();
76
77     @InjectMocks
78     @Spy
79     private AutomationCompositionElementHandler automationCompositionElementHandler =
80         new AutomationCompositionElementHandler();
81
82     @Mock
83     private ChartService chartService;
84
85     @Mock
86     private ParticipantIntermediaryApi participantIntermediaryApi;
87
88     @Mock
89     private ExecutorService executor;
90     @Mock
91     private Future<String> result;
92
93     @BeforeAll
94     static void init() throws CoderException {
95         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
96         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
97     }
98
99     @Test
100     void test_AutomationCompositionElementStateChange() throws ServiceException {
101         var automationCompositionElementId1 = UUID.randomUUID();
102         var automationCompositionElementId2 = UUID.randomUUID();
103
104         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
105         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
106
107         doNothing().when(chartService).uninstallChart(charts.get(0));
108
109         automationCompositionElementHandler.undeploy(
110             commonTestData.getAutomationCompositionId(), automationCompositionElementId1);
111
112         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
113
114         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(
115             commonTestData.getAutomationCompositionId(), automationCompositionElementId1));
116     }
117
118     @Test
119     void test_AutomationCompositionElementUpdate() throws PfModelException, IOException, ServiceException,
120         ExecutionException, InterruptedException {
121         doReturn(true).when(chartService).installChart(any());
122         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
123         var elementId1 = UUID.randomUUID();
124         var element = new AcElementDeploy();
125         element.setId(elementId1);
126         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
127         element.setOrderedState(DeployOrder.DEPLOY);
128
129         var nodeTemplatesMap =
130             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
131         automationCompositionElementHandler.deploy(
132             commonTestData.getAutomationCompositionId(), element,
133             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
134
135         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
136
137         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
138
139         var elementId2 = UUID.randomUUID();
140         element.setId(elementId2);
141         automationCompositionElementHandler.deploy(
142             commonTestData.getAutomationCompositionId(), element,
143             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
144
145         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
146     }
147
148     @Test
149     void test_checkPodStatus() throws ExecutionException, InterruptedException {
150         doReturn(result).when(executor).submit(any(Runnable.class), any());
151         doReturn("Done").when(result).get();
152         doReturn(true).when(result).isDone();
153         var chartInfo = charts.get(0);
154         var automationCompositionId = UUID.randomUUID();
155         var element = new AutomationCompositionElement();
156         assertDoesNotThrow(
157             () -> automationCompositionElementHandler.checkPodStatus(automationCompositionId,
158                     element.getId(), chartInfo, 1, 1));
159     }
160 }