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