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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.participant.kubernetes.handler;
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 import static org.mockito.Mockito.when;
34 import java.io.IOException;
35 import java.util.List;
37 import java.util.UUID;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Future;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.Spy;
48 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
49 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
50 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
51 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
52 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
53 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
54 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
55 import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.CommonTestData;
56 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
57 import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.TestUtils;
58 import org.onap.policy.common.utils.coder.Coder;
59 import org.onap.policy.common.utils.coder.CoderException;
60 import org.onap.policy.common.utils.coder.StandardCoder;
61 import org.onap.policy.models.base.PfModelException;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
63 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
64 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
65 import org.springframework.test.context.junit.jupiter.SpringExtension;
68 @ExtendWith(SpringExtension.class)
69 class ControlLoopElementHandlerTest {
71 private static final Coder CODER = new StandardCoder();
72 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
73 private static final String KEY_NAME = "org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement";
74 private static List<ChartInfo> charts;
75 private static ToscaServiceTemplate toscaServiceTemplate;
76 private static final String K8S_CONTROL_LOOP_ELEMENT =
77 "org.onap.domain.database.PMSH_K8SMicroserviceControlLoopElement";
78 private CommonTestData commonTestData = new CommonTestData();
82 private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
85 private ChartService chartService;
88 private ParticipantIntermediaryApi participantIntermediaryApi;
91 private ExecutorService executor;
94 private Future<String> result;
97 static void init() throws CoderException {
98 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
99 toscaServiceTemplate = TestUtils.testControlLoopRead();
104 void test_ControlLoopElementStateChange() throws ServiceException {
105 UUID controlLoopElementId1 = UUID.randomUUID();
106 UUID controlLoopElementId2 = UUID.randomUUID();
108 controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0));
109 controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1));
111 doNothing().when(chartService).uninstallChart(charts.get(0));
113 controlLoopElementHandler.controlLoopElementStateChange(commonTestData.getControlLoopId(),
114 controlLoopElementId1, ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED);
116 doThrow(new ServiceException("Error uninstalling the chart")).when(chartService)
117 .uninstallChart(charts.get(0));
119 assertDoesNotThrow(() -> controlLoopElementHandler
120 .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
121 ControlLoopState.PASSIVE, ControlLoopOrderedState.PASSIVE));
123 assertDoesNotThrow(() -> controlLoopElementHandler
124 .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
125 ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED));
127 assertDoesNotThrow(() -> controlLoopElementHandler
128 .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1,
129 ControlLoopState.PASSIVE, ControlLoopOrderedState.RUNNING));
134 void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException,
135 ExecutionException, InterruptedException {
136 doNothing().when(controlLoopElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt());
137 UUID elementId1 = UUID.randomUUID();
138 ControlLoopElement element = new ControlLoopElement();
139 element.setId(elementId1);
140 element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1"));
141 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
143 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
144 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
145 controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
146 nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT));
148 assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
150 doThrow(new ServiceException("Error installing the chart")).when(chartService)
151 .installChart(Mockito.any());
153 UUID elementId2 = UUID.randomUUID();
154 element.setId(elementId2);
155 controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
156 nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT));
158 assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse();
162 void test_handleStatistics() throws PfModelException {
163 UUID elementId1 = UUID.randomUUID();
164 controlLoopElementHandler.getChartMap().put(elementId1, charts.get(0));
165 when(participantIntermediaryApi.getControlLoopElement(elementId1)).thenReturn(new ControlLoopElement());
166 assertDoesNotThrow(() -> controlLoopElementHandler.handleStatistics(elementId1));
170 void test_checkPodStatus() throws ExecutionException, InterruptedException {
171 doReturn(result).when(executor).submit(any(Runnable.class), any());
172 doReturn("Done").when(result).get();
173 doReturn(true).when(result).isDone();
174 var chartInfo = charts.get(0);
175 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
176 ControlLoopElement element = new ControlLoopElement();
177 assertDoesNotThrow(() -> controlLoopElementHandler.checkPodStatus(controlLoopId, element.getId(), chartInfo,