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