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.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotEquals;
25 import static org.mockserver.model.HttpRequest.request;
26 import static org.mockserver.model.HttpResponse.response;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockserver.integration.ClientAndServer;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
36 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
46 import org.springframework.test.context.TestPropertySource;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
49 @ExtendWith(SpringExtension.class)
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51 @TestPropertySource(locations = {"classpath:application_test.properties"})
52 class PartecipantDcaeTest {
54 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
55 private static final String TOPIC = "my-topic";
57 private static final String LOOP = "pmsh_loop";
58 private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
60 private static ClientAndServer mockClampServer;
61 private static ClientAndServer mockConsulServer;
64 private ParticipantHandler participantHandler;
70 public static void startServers() {
73 mockClampServer = ClientAndServer.startClientAndServer(8443);
75 mockClampServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
76 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED).toString())
77 .withStatusCode(200));
79 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
80 .respond(response().withStatusCode(202));
82 mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
83 .respond(response().withStatusCode(202));
86 mockConsulServer = ClientAndServer.startClientAndServer(31321);
88 mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy"))
89 .respond(response().withStatusCode(200));
96 public static void stopServer() {
97 mockClampServer.stop();
98 mockClampServer = null;
100 mockConsulServer.stop();
101 mockConsulServer = null;
105 void testParticipantControlLoopStateChangeMessageListener() {
106 ParticipantControlLoopStateChange participantControlLoopStateChangeMsg =
107 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
108 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE);
110 ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
112 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
113 assertEquals(ControlLoopOrderedState.PASSIVE, participantControlLoopStateChangeMsg.getOrderedState());
115 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
116 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
117 assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
119 participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
120 clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
121 assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
125 void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
126 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
127 participantControlLoopUpdateMsg.getParticipantId().setName("DummyName");
128 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
130 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
131 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
133 // Verify the content in participantHandler
134 assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(),
135 participantHandler.getParticipantId().getName());
139 void testControlLoopUpdateListenerPassive() throws CoderException {
140 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
141 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
143 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
144 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
146 // Verify the content in participantHandler
147 assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId());
148 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
152 void testControlLoopUpdateListenerUninitialised() throws CoderException {
153 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
154 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
156 ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
157 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
159 // Verify the content in participantHandler
160 assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId());
161 assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());