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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.UUID;
32 import org.json.JSONException;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
39 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
40 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
41 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
51 * Class to perform unit test of {@link ControlLoopElementHandler}.
54 @ExtendWith(SpringExtension.class)
55 class ControlLoopElementHandlerTest {
57 private static final String LOOP = "pmsh_loop";
58 private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
59 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
60 private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
62 public static final Coder CODER = new StandardCoder();
63 private CommonTestData commonTestData = new CommonTestData();
66 void test_ControlLoopElementStateChange() {
67 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
68 ConsulDcaeHttpClient consulClient = mock(ConsulDcaeHttpClient.class);
69 ControlLoopElementHandler controlLoopElementHandler =
70 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
72 when(clampClient.getstatus(eq(LOOP))).thenReturn(new Loop());
74 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
75 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
77 controlLoopElementHandler.controlLoopElementStateChange(UUID.randomUUID(), ControlLoopState.PASSIVE,
78 ControlLoopOrderedState.UNINITIALISED);
80 verify(clampClient).undeploy(eq(LOOP));
84 void testCreate_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
85 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
86 Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
87 when(clampClient.create(eq(LOOP), eq(TEMPLATE))).thenReturn(loopDeployed);
88 when(clampClient.deploy(eq(LOOP))).thenReturn(true);
91 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
92 when(clampClient.getstatus(eq(LOOP))).thenReturn(null, loopInstalled);
94 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
95 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
97 ControlLoopElementHandler controlLoopElementHandler =
98 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
100 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
101 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
103 ControlLoopElement element = new ControlLoopElement();
104 element.setId(UUID.randomUUID());
105 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
107 final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
108 controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
110 verify(clampClient).create(eq(LOOP), eq(TEMPLATE));
111 verify(consulClient).deploy(any(String.class), any(String.class));
112 verify(clampClient).deploy(eq(LOOP));
116 void test_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
117 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
118 Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
120 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
121 when(clampClient.getstatus(eq(LOOP))).thenReturn(loopDeployed, loopInstalled);
122 when(clampClient.deploy(eq(LOOP))).thenReturn(true);
124 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
125 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
127 ControlLoopElementHandler controlLoopElementHandler =
128 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
130 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
131 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
133 ControlLoopElement element = new ControlLoopElement();
134 element.setId(UUID.randomUUID());
135 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
137 ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
138 controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
140 verify(clampClient, times(0)).create(eq(LOOP), eq(TEMPLATE));
141 verify(consulClient).deploy(any(String.class), any(String.class));
142 verify(clampClient).deploy(eq(LOOP));