d7f09705b3f5875169d046d94cb1a35b82e60942
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.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.doThrow;
30 import static org.mockito.Mockito.when;
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 org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.Spy;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
46 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
47 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
48 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
49 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
50 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
51 import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.CommonTestData;
52 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
53 import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.TestUtils;
54 import org.onap.policy.common.utils.coder.Coder;
55 import org.onap.policy.common.utils.coder.CoderException;
56 import org.onap.policy.common.utils.coder.StandardCoder;
57 import org.onap.policy.models.base.PfModelException;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
61 import org.springframework.test.context.junit.jupiter.SpringExtension;
62
63
64 @ExtendWith(SpringExtension.class)
65 class ControlLoopElementHandlerTest {
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 = "org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement";
70     private static List<ChartInfo> charts;
71     private static ToscaServiceTemplate toscaServiceTemplate;
72     private static final String K8S_CONTROL_LOOP_ELEMENT =
73         "org.onap.domain.database.PMSH_K8SMicroserviceControlLoopElement";
74     private CommonTestData commonTestData = new CommonTestData();
75
76     @InjectMocks
77     @Spy
78     private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
79
80     @Mock
81     private ChartService chartService;
82
83     @Mock
84     private ParticipantIntermediaryApi participantIntermediaryApi;
85
86     @BeforeAll
87     static void init() throws CoderException {
88         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
89         toscaServiceTemplate = TestUtils.testControlLoopRead();
90     }
91
92
93     @Test
94     void test_ControlLoopElementStateChange() throws ServiceException {
95         UUID controlLoopElementId1 = UUID.randomUUID();
96         UUID controlLoopElementId2 = UUID.randomUUID();
97
98         controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0));
99         controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1));
100
101         doNothing().when(chartService).uninstallChart(charts.get(0));
102
103         controlLoopElementHandler.controlLoopElementStateChange(commonTestData.getControlLoopId(),
104             controlLoopElementId1, ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED);
105
106         doThrow(new ServiceException("Error uninstalling the chart")).when(chartService)
107             .uninstallChart(charts.get(0));
108
109         assertDoesNotThrow(() -> controlLoopElementHandler
110                 .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
111                     ControlLoopState.PASSIVE, ControlLoopOrderedState.PASSIVE));
112
113         assertDoesNotThrow(() -> controlLoopElementHandler
114             .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
115                 ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED));
116
117         assertDoesNotThrow(() -> controlLoopElementHandler
118             .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
119                 ControlLoopState.PASSIVE, ControlLoopOrderedState.RUNNING));
120
121     }
122
123     @Test
124     void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException {
125         doNothing().when(controlLoopElementHandler).checkPodStatus(any(), anyInt(), anyInt());
126         UUID elementId1 = UUID.randomUUID();
127         ControlLoopElement element = new ControlLoopElement();
128         element.setId(elementId1);
129         element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
130         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
131
132         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
133                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
134         controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
135             nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT));
136
137         assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
138
139         doThrow(new ServiceException("Error installing the chart")).when(chartService)
140             .installChart(Mockito.any());
141
142         UUID elementId2 = UUID.randomUUID();
143         element.setId(elementId2);
144         controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
145             nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT));
146
147         assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse();
148     }
149
150     @Test
151     void test_handleStatistics() throws PfModelException {
152         UUID elementId1 = UUID.randomUUID();
153         controlLoopElementHandler.getChartMap().put(elementId1, charts.get(0));
154         when(participantIntermediaryApi.getControlLoopElement(elementId1)).thenReturn(new ControlLoopElement());
155         assertDoesNotThrow(() -> controlLoopElementHandler.handleStatistics(elementId1));
156     }
157
158     @Test
159     void test_checkPodStatus() {
160         var chartInfo =  charts.get(0);
161         assertDoesNotThrow(() -> controlLoopElementHandler.checkPodStatus(chartInfo, 1, 1));
162     }
163 }