40f2f5f7b1624e0464f9c5e11d436c48322de57d
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.UUID;
33 import org.json.JSONException;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
39 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
40 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
41 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
42 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.springframework.test.context.junit.jupiter.SpringExtension;
50
51 /**
52  * Class to perform unit test of {@link ControlLoopElementHandler}.
53  *
54  */
55 @ExtendWith(SpringExtension.class)
56 class ControlLoopElementHandlerTest {
57
58     private static final String LOOP = "pmsh_loop";
59     private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
60     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
61     private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
62
63     public static final Coder CODER = new StandardCoder();
64     private CommonTestData commonTestData = new CommonTestData();
65
66     @Test
67     void test_ControlLoopElementStateChange() {
68         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
69         ConsulDcaeHttpClient consulClient = mock(ConsulDcaeHttpClient.class);
70         ControlLoopElementHandler controlLoopElementHandler =
71                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
72
73         when(clampClient.getstatus(eq(LOOP))).thenReturn(new Loop());
74
75         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
76         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
77
78         UUID controlLoopElementId = UUID.randomUUID();
79         controlLoopElementHandler.controlLoopElementStateChange(controlLoopElementId, ControlLoopState.PASSIVE,
80                 ControlLoopOrderedState.UNINITIALISED);
81
82         verify(clampClient).undeploy(eq(LOOP));
83         controlLoopElementHandler.handleStatistics(controlLoopElementId);
84         assertThat(intermediaryApi.getControlLoopElement(controlLoopElementId)).isNull();
85     }
86
87     @Test
88     void testCreate_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
89         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
90         Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
91         when(clampClient.create(eq(LOOP), eq(TEMPLATE))).thenReturn(loopDeployed);
92         when(clampClient.deploy(eq(LOOP))).thenReturn(true);
93
94         Loop loopInstalled =
95                 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
96         when(clampClient.getstatus(eq(LOOP))).thenReturn(null, loopInstalled);
97
98         ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
99         when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
100
101         ControlLoopElementHandler controlLoopElementHandler =
102                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
103
104         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
105         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
106
107         ControlLoopElement element = new ControlLoopElement();
108         element.setId(UUID.randomUUID());
109         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
110
111         final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
112         controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
113
114         verify(clampClient).create(eq(LOOP), eq(TEMPLATE));
115         verify(consulClient).deploy(any(String.class), any(String.class));
116         verify(clampClient).deploy(eq(LOOP));
117     }
118
119     @Test
120     void test_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
121         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
122         Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
123         Loop loopInstalled =
124                 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
125         when(clampClient.getstatus(eq(LOOP))).thenReturn(loopDeployed, loopInstalled);
126         when(clampClient.deploy(eq(LOOP))).thenReturn(true);
127
128         ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
129         when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
130
131         ControlLoopElementHandler controlLoopElementHandler =
132                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
133
134         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
135         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
136
137         ControlLoopElement element = new ControlLoopElement();
138         element.setId(UUID.randomUUID());
139         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
140
141         ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
142         controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
143
144         verify(clampClient, times(0)).create(eq(LOOP), eq(TEMPLATE));
145         verify(consulClient).deploy(any(String.class), any(String.class));
146         verify(clampClient).deploy(eq(LOOP));
147     }
148 }