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.endtoend;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
26 import static org.mockserver.model.HttpRequest.request;
27 import static org.mockserver.model.HttpResponse.response;
29 import org.junit.jupiter.api.AfterAll;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockserver.integration.ClientAndServer;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
38 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
39 import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
52 @ExtendWith(SpringExtension.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @TestPropertySource(locations = {"classpath:application_test.properties"})
55 class ParticipantDcaeTest {
57 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
58 private static final String TOPIC = "my-topic";
59 private static final Object lockit = new Object();
61 private static final String LOOP = "pmsh_loop";
62 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
64 private static ClientAndServer mockClampServer;
65 private static ClientAndServer mockConsulServer;
68 private ParticipantHandler participantHandler;
74 public void startServers() {
77 mockClampServer = ClientAndServer.startClientAndServer(8443);
79 mockClampServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
80 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED).toString())
81 .withStatusCode(200));
83 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
84 .respond(response().withStatusCode(202));
86 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
87 .respond(response().withStatusCode(202));
90 mockConsulServer = ClientAndServer.startClientAndServer(31321);
92 mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy"))
93 .respond(response().withStatusCode(200));
94 ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
96 synchronized (lockit) {
97 ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
98 participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
106 public static void stopServer() {
107 mockClampServer.stop();
108 mockClampServer = null;
110 mockConsulServer.stop();
111 mockConsulServer = null;
115 void testControlLoopStateChangeMessageListener() {
116 ControlLoopStateChange controlLoopStateChangeMsg =
117 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
118 controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE);
120 ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
122 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
123 assertEquals(ControlLoopOrderedState.PASSIVE, controlLoopStateChangeMsg.getOrderedState());
125 controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
126 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
127 assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState());
129 controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
130 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
131 assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState());
135 void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
136 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
137 controlLoopUpdateMsg.getParticipantId().setName("DummyName");
139 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
140 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
142 // Verify the content in participantHandler
143 assertNotEquals(controlLoopUpdateMsg.getParticipantId().getName(),
144 participantHandler.getParticipantId().getName());
148 void testControlLoopUpdateListenerPassive() throws CoderException {
149 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
151 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
152 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
154 // Verify the content in participantHandler
155 assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
156 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
160 void testControlLoopUpdateListenerUninitialised() throws CoderException {
161 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
163 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
164 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
166 // Verify the content in participantHandler
167 assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
168 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
172 void testControlLoopUpdateListenerString() throws CoderException {
173 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
175 assertThat(controlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
176 ControlLoopUpdate copyControlLoopUpdateMsg =
177 TestListenerUtils.createCopyControlLoopUpdateMsg(controlLoopUpdateMsg);
178 assertThat(copyControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
179 assertNotEquals(controlLoopUpdateMsg, copyControlLoopUpdateMsg);