f8381ee7fdf76991f25fa38dbcbb9dd639687677
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doThrow;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.Spy;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
46 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
47 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
48 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
49 import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.TestUtils;
50 import org.onap.policy.common.utils.coder.Coder;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.models.base.PfModelException;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
57
58
59 @ExtendWith(SpringExtension.class)
60 class ControlLoopElementHandlerTest {
61
62     private static final Coder CODER = new StandardCoder();
63     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
64     private static final String KEY_NAME = "org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement";
65     private static List<ChartInfo> charts;
66     private static ToscaServiceTemplate toscaServiceTemplate;
67
68
69     @InjectMocks
70     @Spy
71     private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
72
73     @Mock
74     private ChartService chartService;
75
76     @Mock
77     private ParticipantIntermediaryApi participantIntermediaryApi;
78
79     @BeforeAll
80     static void init() throws CoderException {
81         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
82         toscaServiceTemplate = TestUtils.testControlLoopRead();
83     }
84
85
86     @Test
87     void test_ControlLoopElementStateChange() throws ServiceException {
88         UUID controlLoopElementId1 = UUID.randomUUID();
89         UUID controlLoopElementId2 = UUID.randomUUID();
90
91         controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0));
92         controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1));
93
94         doNothing().when(chartService).uninstallChart(charts.get(0));
95
96         controlLoopElementHandler.controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
97             ControlLoopOrderedState.UNINITIALISED);
98
99         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService)
100             .uninstallChart(charts.get(0));
101
102         assertDoesNotThrow(() -> controlLoopElementHandler
103             .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
104                 ControlLoopOrderedState.UNINITIALISED));
105
106         assertDoesNotThrow(() -> controlLoopElementHandler
107             .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
108                 ControlLoopOrderedState.RUNNING));
109
110     }
111
112     @Test
113     void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException {
114         doNothing().when(controlLoopElementHandler).checkPodStatus(any(), anyInt(), anyInt());
115         UUID elementId1 = UUID.randomUUID();
116         ControlLoopElement element = new ControlLoopElement();
117         element.setId(elementId1);
118         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
119         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
120
121         controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
122
123         assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
124
125         doThrow(new ServiceException("Error installing the chart")).when(chartService)
126             .installChart(Mockito.any());
127
128         UUID elementId2 = UUID.randomUUID();
129         element.setId(elementId2);
130         controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
131
132         assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse();
133     }
134 }