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.dcae.main.handler;
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;
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;
52 * Class to perform unit test of {@link ControlLoopElementHandler}.
55 @ExtendWith(SpringExtension.class)
56 class ControlLoopElementHandlerTest {
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";
63 public static final Coder CODER = new StandardCoder();
64 private CommonTestData commonTestData = new CommonTestData();
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());
73 when(clampClient.getstatus(eq(LOOP))).thenReturn(new Loop());
75 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
76 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
78 UUID controlLoopElementId = UUID.randomUUID();
79 controlLoopElementHandler.controlLoopElementStateChange(controlLoopElementId, ControlLoopState.PASSIVE,
80 ControlLoopOrderedState.UNINITIALISED);
82 verify(clampClient).undeploy(eq(LOOP));
83 controlLoopElementHandler.handleStatistics(controlLoopElementId);
84 assertThat(intermediaryApi.getControlLoopElement(controlLoopElementId)).isNull();
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);
95 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
96 when(clampClient.getstatus(eq(LOOP))).thenReturn(null, loopInstalled);
98 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
99 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
101 ControlLoopElementHandler controlLoopElementHandler =
102 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
104 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
105 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
107 ControlLoopElement element = new ControlLoopElement();
108 element.setId(UUID.randomUUID());
109 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
111 final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
112 controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
114 verify(clampClient).create(eq(LOOP), eq(TEMPLATE));
115 verify(consulClient).deploy(any(String.class), any(String.class));
116 verify(clampClient).deploy(eq(LOOP));
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);
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);
128 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
129 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
131 ControlLoopElementHandler controlLoopElementHandler =
132 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
134 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
135 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
137 ControlLoopElement element = new ControlLoopElement();
138 element.setId(UUID.randomUUID());
139 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
141 ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
142 controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
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));