6124060cbd15cf4920c17140e337dd77d9c9960b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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 import static org.mockito.Mockito.when;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Future;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.Spy;
48 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
49 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
50 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
51 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
52 import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData;
53 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService;
54 import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils;
55 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
56 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
57 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
58 import org.onap.policy.common.utils.coder.Coder;
59 import org.onap.policy.common.utils.coder.CoderException;
60 import org.onap.policy.common.utils.coder.StandardCoder;
61 import org.onap.policy.models.base.PfModelException;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
63 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
64 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
65 import org.springframework.test.context.junit.jupiter.SpringExtension;
66
67 @ExtendWith(SpringExtension.class)
68 class AutomationCompositionElementHandlerTest {
69
70     private static final Coder CODER = new StandardCoder();
71     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
72     private static final String KEY_NAME =
73         "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement";
74     private static List<ChartInfo> charts;
75     private static ToscaServiceTemplate toscaServiceTemplate;
76     private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT =
77         "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement";
78     private CommonTestData commonTestData = new CommonTestData();
79
80     @InjectMocks
81     @Spy
82     private AutomationCompositionElementHandler automationCompositionElementHandler =
83         new AutomationCompositionElementHandler();
84
85     @Mock
86     private ChartService chartService;
87
88     @Mock
89     private ParticipantIntermediaryApi participantIntermediaryApi;
90
91     @Mock
92     private ExecutorService executor;
93     @Mock
94     private Future<String> result;
95
96     @BeforeAll
97     static void init() throws CoderException {
98         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
99         toscaServiceTemplate = TestUtils.testAutomationCompositionRead();
100     }
101
102     @Test
103     void test_AutomationCompositionElementStateChange() throws ServiceException {
104         UUID automationCompositionElementId1 = UUID.randomUUID();
105         UUID automationCompositionElementId2 = UUID.randomUUID();
106
107         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0));
108         automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1));
109
110         doNothing().when(chartService).uninstallChart(charts.get(0));
111
112         automationCompositionElementHandler.automationCompositionElementStateChange(
113             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
114             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED);
115
116         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
117
118         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
119             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
120             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.PASSIVE));
121
122         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
123             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
124             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED));
125
126         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
127             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
128             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING));
129
130     }
131
132     @Test
133     void test_AutomationCompositionElementUpdate() throws PfModelException, IOException, ServiceException,
134         ExecutionException, InterruptedException {
135         doReturn(true).when(chartService).installChart(any());
136         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
137         UUID elementId1 = UUID.randomUUID();
138         AutomationCompositionElement element = new AutomationCompositionElement();
139         element.setId(elementId1);
140         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
141         element.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
142
143         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
144             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
145         automationCompositionElementHandler.automationCompositionElementUpdate(
146             commonTestData.getAutomationCompositionId(), element,
147             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT));
148
149         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
150
151         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
152
153         UUID elementId2 = UUID.randomUUID();
154         element.setId(elementId2);
155         automationCompositionElementHandler.automationCompositionElementUpdate(
156             commonTestData.getAutomationCompositionId(), element,
157             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT));
158
159         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
160     }
161
162     @Test
163     void test_handleStatistics() throws PfModelException {
164         UUID elementId1 = UUID.randomUUID();
165         automationCompositionElementHandler.getChartMap().put(elementId1, charts.get(0));
166         when(participantIntermediaryApi.getAutomationCompositionElement(elementId1))
167             .thenReturn(new AutomationCompositionElement());
168         assertDoesNotThrow(() -> automationCompositionElementHandler.handleStatistics(elementId1));
169     }
170
171     @Test
172     void test_checkPodStatus() throws ExecutionException, InterruptedException {
173         doReturn(result).when(executor).submit(any(Runnable.class), any());
174         doReturn("Done").when(result).get();
175         doReturn(true).when(result).isDone();
176         var chartInfo = charts.get(0);
177         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
178         AutomationCompositionElement element = new AutomationCompositionElement();
179         assertDoesNotThrow(
180             () -> automationCompositionElementHandler.checkPodStatus(controlLoopId, element.getId(), chartInfo,
181                 1, 1));
182     }
183 }