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.api.ParticipantIntermediaryApi;
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.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(properties = "participant.file=src/test/resources/parameters/TestParameters.json")
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 ParticipantIntermediaryApi participantIntermediaryApi;
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 =
111 new ControlLoopStateChangeListener(participantIntermediaryApi.getParticipantHandler());
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 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
128 participantControlLoopUpdateMsg.getParticipantId().setName("DummyName");
129 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
131 ControlLoopUpdateListener clUpdateListener =
132 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
133 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
135 // Verify the content in participantHandler
136 assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(),
137 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
141 void testControlLoopUpdateListenerPassive() throws CoderException {
142 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
143 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
145 ControlLoopUpdateListener clUpdateListener =
146 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
147 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
149 // Verify the content in participantHandler
150 assertEquals(participantIntermediaryApi.getParticipantHandler().getParticipantId(),
151 participantControlLoopUpdateMsg.getParticipantId());
152 assertEquals(1, participantIntermediaryApi.getParticipantHandler().getControlLoopHandler().getControlLoops()
153 .getControlLoopList().size());
157 void testControlLoopUpdateListenerUninitialised() throws CoderException {
158 ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
159 participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
161 ControlLoopUpdateListener clUpdateListener =
162 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
163 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
165 // Verify the content in participantHandler
166 assertEquals(participantIntermediaryApi.getParticipantHandler().getParticipantId(),
167 participantControlLoopUpdateMsg.getParticipantId());
168 assertEquals(1, participantIntermediaryApi.getParticipantHandler().getControlLoopHandler().getControlLoops()
169 .getControlLoopList().size());