f1357e435eb6ee20ed16c06dedc421bdededbe5c
[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.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.AutomationCompositionElement;
54 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
55 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
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.automationCompositionElementStateChange(
110             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
111             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED);
112
113         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0));
114
115         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
116             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
117             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.PASSIVE));
118
119         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
120             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
121             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED));
122
123         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
124             commonTestData.getAutomationCompositionId(), automationCompositionElementId1,
125             AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING));
126
127     }
128
129     @Test
130     void test_AutomationCompositionElementUpdate() throws PfModelException, IOException, ServiceException,
131         ExecutionException, InterruptedException {
132         doReturn(true).when(chartService).installChart(any());
133         doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
134         var elementId1 = UUID.randomUUID();
135         var element = new AutomationCompositionElement();
136         element.setId(elementId1);
137         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
138         element.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
139
140         var nodeTemplatesMap =
141             toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
142         automationCompositionElementHandler.automationCompositionElementUpdate(
143             commonTestData.getAutomationCompositionId(), element,
144             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
145
146         assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
147
148         doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any());
149
150         var elementId2 = UUID.randomUUID();
151         element.setId(elementId2);
152         automationCompositionElementHandler.automationCompositionElementUpdate(
153             commonTestData.getAutomationCompositionId(), element,
154             nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
155
156         assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse();
157     }
158
159     @Test
160     void test_checkPodStatus() throws ExecutionException, InterruptedException {
161         doReturn(result).when(executor).submit(any(Runnable.class), any());
162         doReturn("Done").when(result).get();
163         doReturn(true).when(result).isDone();
164         var chartInfo = charts.get(0);
165         var automationCompositionId = UUID.randomUUID();
166         var element = new AutomationCompositionElement();
167         assertDoesNotThrow(
168             () -> automationCompositionElementHandler.checkPodStatus(automationCompositionId,
169                     element.getId(), chartInfo, 1, 1));
170     }
171 }