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