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.ControlLoopUpdate;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
37 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
42 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
50 @ExtendWith(SpringExtension.class)
51 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
52 @TestPropertySource(locations = {"classpath:application_test.properties"})
53 class ParticipantDcaeTest {
55 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
56 private static final String TOPIC = "my-topic";
58 private static final String LOOP = "pmsh_loop";
59 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
61 private static ClientAndServer mockClampServer;
62 private static ClientAndServer mockConsulServer;
65 private ParticipantHandler participantHandler;
71 public static void startServers() {
74 mockClampServer = ClientAndServer.startClientAndServer(8443);
76 mockClampServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
77 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED).toString())
78 .withStatusCode(200));
80 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
81 .respond(response().withStatusCode(202));
83 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
84 .respond(response().withStatusCode(202));
87 mockConsulServer = ClientAndServer.startClientAndServer(31321);
89 mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy"))
90 .respond(response().withStatusCode(200));
97 public static void stopServer() {
98 mockClampServer.stop();
99 mockClampServer = null;
101 mockConsulServer.stop();
102 mockConsulServer = null;
106 void testParticipantControlLoopStateChangeMessageListener() {
107 ParticipantControlLoopStateChange participantControlLoopStateChangeMsg =
108 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
109 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE);
111 ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
113 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
114 assertEquals(ControlLoopOrderedState.PASSIVE, participantControlLoopStateChangeMsg.getOrderedState());
116 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
117 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
118 assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
120 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
121 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
122 assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
126 void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
127 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
128 controlLoopUpdateMsg.getParticipantId().setName("DummyName");
129 controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
131 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
132 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
134 // Verify the content in participantHandler
135 assertNotEquals(controlLoopUpdateMsg.getParticipantId().getName(),
136 participantHandler.getParticipantId().getName());
140 void testControlLoopUpdateListenerPassive() throws CoderException {
141 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
142 controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
144 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
145 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
147 // Verify the content in participantHandler
148 assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
149 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
153 void testControlLoopUpdateListenerUninitialised() throws CoderException {
154 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
155 controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
157 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
158 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
160 // Verify the content in participantHandler
161 assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
162 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
166 void testControlLoopUpdateListenerString() throws CoderException {
167 ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
168 controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
170 assertThat(controlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
171 ControlLoopUpdate copyControlLoopUpdateMsg =
172 TestListenerUtils.createCopyControlLoopUpdateMsg(controlLoopUpdateMsg);
173 assertThat(copyControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
174 assertNotEquals(controlLoopUpdateMsg, copyControlLoopUpdateMsg);