f3d27a63c4e2cc405d03d4d3080c846fc98dd563
[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.Mockito.doNothing;
26 import static org.mockito.Mockito.doThrow;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.UUID;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
43 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
46 import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.TestUtils;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.models.base.PfModelException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
54
55
56 @ExtendWith(SpringExtension.class)
57 class ControlLoopElementHandlerTest {
58
59     private static final Coder CODER = new StandardCoder();
60     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
61     private static final String KEY_NAME = "org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement";
62     private static List<ChartInfo> charts;
63     private static ToscaServiceTemplate toscaServiceTemplate;
64
65
66     @InjectMocks
67     private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
68
69     @Mock
70     private ChartService chartService;
71
72     @Mock
73     private ParticipantIntermediaryApi participantIntermediaryApi;
74
75     @BeforeAll
76     static void init() throws CoderException {
77         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
78         toscaServiceTemplate = TestUtils.testControlLoopRead();
79     }
80
81
82     @Test
83     void test_ControlLoopElementStateChange() throws ServiceException {
84         UUID controlLoopElementId1 = UUID.randomUUID();
85         UUID controlLoopElementId2 = UUID.randomUUID();
86
87         controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0));
88         controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1));
89
90         doNothing().when(chartService).uninstallChart(charts.get(0));
91
92         controlLoopElementHandler.controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
93             ControlLoopOrderedState.UNINITIALISED);
94
95         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService)
96             .uninstallChart(charts.get(0));
97
98         assertDoesNotThrow(() -> controlLoopElementHandler
99             .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
100                 ControlLoopOrderedState.UNINITIALISED));
101
102         assertDoesNotThrow(() -> controlLoopElementHandler
103             .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
104                 ControlLoopOrderedState.RUNNING));
105
106     }
107
108     @Test
109     void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException {
110
111         UUID elementId1 = UUID.randomUUID();
112         ControlLoopElement element = new ControlLoopElement();
113         element.setId(elementId1);
114         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
115         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
116
117         controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
118
119         assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
120
121         doThrow(new ServiceException("Error installing the chart")).when(chartService)
122             .installChart(Mockito.any());
123
124         UUID elementId2 = UUID.randomUUID();
125         element.setId(elementId2);
126         controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
127
128         assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse();
129     }
130 }