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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.participant.dcae.main.handler;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
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.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
41 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
42 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
43 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
44 import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils;
45 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
46 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
47 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
48 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
49 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.common.utils.coder.Coder;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.models.base.PfModelException;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
59 * Class to perform unit test of {@link ControlLoopElementHandler}.
62 @ExtendWith(SpringExtension.class)
63 class ControlLoopElementHandlerTest {
65 private static final String LOOP = "pmsh_loop";
66 private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
67 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
68 private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
70 public static final Coder CODER = new StandardCoder();
71 private static final Object lockit = new Object();
72 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
73 private static final String TOPIC = "my-topic";
74 private CommonTestData commonTestData = new CommonTestData();
77 private ParticipantHandler participantHandler;
80 * Initial ParticipantUpdate message.
83 public void sendParticipantUpdate() {
84 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
86 synchronized (lockit) {
87 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
88 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
93 void test_ControlLoopElementStateChange() {
94 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
95 ConsulDcaeHttpClient consulClient = mock(ConsulDcaeHttpClient.class);
96 ControlLoopElementHandler controlLoopElementHandler =
97 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
99 when(clampClient.getstatus(LOOP)).thenReturn(new Loop());
101 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
102 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
104 UUID controlLoopElementId = UUID.randomUUID();
105 controlLoopElementHandler.controlLoopElementStateChange(TestListenerUtils.getControlLoopId(),
106 controlLoopElementId, ControlLoopState.PASSIVE,
107 ControlLoopOrderedState.UNINITIALISED);
109 verify(clampClient).undeploy(LOOP);
110 controlLoopElementHandler.handleStatistics(controlLoopElementId);
111 assertThat(intermediaryApi.getControlLoopElement(controlLoopElementId)).isNull();
115 void testCreate_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
116 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
117 Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
118 when(clampClient.create(LOOP, TEMPLATE)).thenReturn(loopDeployed);
119 when(clampClient.deploy(LOOP)).thenReturn(true);
122 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
123 when(clampClient.getstatus(LOOP)).thenReturn(null, loopInstalled);
125 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
126 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
128 ControlLoopElementHandler controlLoopElementHandler =
129 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
131 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
132 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
134 ControlLoopElement element = new ControlLoopElement();
135 element.setId(UUID.randomUUID());
136 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
138 final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
139 controlLoopElementHandler.controlLoopElementUpdate(TestListenerUtils.getControlLoopId(), element,
140 controlLoopDefinition.getToscaTopologyTemplate().getNodeTemplates()
141 .get("org.onap.domain.pmsh.PMSH_DCAEMicroservice"));
143 verify(clampClient).create(LOOP, TEMPLATE);
144 verify(consulClient).deploy(any(String.class), any(String.class));
145 verify(clampClient).deploy(LOOP);
149 void test_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
150 ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
151 Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
153 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
154 when(clampClient.getstatus(LOOP)).thenReturn(loopDeployed, loopInstalled);
155 when(clampClient.deploy(LOOP)).thenReturn(true);
157 ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
158 when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
160 ControlLoopElementHandler controlLoopElementHandler =
161 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
163 ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
164 controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
166 ControlLoopElement element = new ControlLoopElement();
167 element.setId(UUID.randomUUID());
168 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
170 ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
171 controlLoopElementHandler.controlLoopElementUpdate(TestListenerUtils.getControlLoopId(), element,
172 controlLoopDefinition.getToscaTopologyTemplate().getNodeTemplates()
173 .get("org.onap.domain.pmsh.PMSH_DCAEMicroservice"));
175 verify(clampClient, times(0)).create(LOOP, TEMPLATE);
176 verify(consulClient).deploy(any(String.class), any(String.class));
177 verify(clampClient).deploy(LOOP);