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