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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.kubernetes.handler;
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;
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;
56 @ExtendWith(SpringExtension.class)
57 class ControlLoopElementHandlerTest {
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;
67 private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
70 private ChartService chartService;
73 private ParticipantIntermediaryApi participantIntermediaryApi;
76 static void init() throws CoderException {
77 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
78 toscaServiceTemplate = TestUtils.testControlLoopRead();
83 void test_ControlLoopElementStateChange() throws ServiceException {
84 UUID controlLoopElementId1 = UUID.randomUUID();
85 UUID controlLoopElementId2 = UUID.randomUUID();
87 controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0));
88 controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1));
90 doNothing().when(chartService).uninstallChart(charts.get(0));
92 controlLoopElementHandler.controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
93 ControlLoopOrderedState.UNINITIALISED);
95 doThrow(new ServiceException("Error uninstalling the chart")).when(chartService)
96 .uninstallChart(charts.get(0));
98 assertDoesNotThrow(() -> controlLoopElementHandler
99 .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
100 ControlLoopOrderedState.UNINITIALISED));
102 assertDoesNotThrow(() -> controlLoopElementHandler
103 .controlLoopElementStateChange(controlLoopElementId1, ControlLoopState.PASSIVE,
104 ControlLoopOrderedState.RUNNING));
109 void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException {
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);
117 controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
119 assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1);
121 doThrow(new ServiceException("Error installing the chart")).when(chartService)
122 .installChart(Mockito.any());
124 UUID elementId2 = UUID.randomUUID();
125 element.setId(elementId2);
126 controlLoopElementHandler.controlLoopElementUpdate(element, toscaServiceTemplate);
128 assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse();