a96581e6c4851fcc67a19129cbdcac62458cace0
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.dcae.endtoend;
22
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;
27
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;
48
49 @ExtendWith(SpringExtension.class)
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51 @TestPropertySource(locations = {"classpath:application_test.properties"})
52 class PartecipantDcaeTest {
53
54     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
55     private static final String TOPIC = "my-topic";
56
57     private static final String LOOP = "pmsh_loop";
58     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
59
60     private static ClientAndServer mockClampServer;
61     private static ClientAndServer mockConsulServer;
62
63     @Autowired
64     private ParticipantHandler participantHandler;
65
66     /**
67      * start Servers.
68      */
69     @BeforeAll
70     public static void startServers() {
71
72         // Clamp
73         mockClampServer = ClientAndServer.startClientAndServer(8443);
74
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));
78
79         mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
80                 .respond(response().withStatusCode(202));
81
82         mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
83                 .respond(response().withStatusCode(202));
84
85         // Consul
86         mockConsulServer = ClientAndServer.startClientAndServer(31321);
87
88         mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy"))
89                 .respond(response().withStatusCode(200));
90     }
91
92     /**
93      * stop Server.
94      */
95     @AfterAll
96     public static void stopServer() {
97         mockClampServer.stop();
98         mockClampServer = null;
99
100         mockConsulServer.stop();
101         mockConsulServer = null;
102     }
103
104     @Test
105     void testParticipantControlLoopStateChangeMessageListener() {
106         ParticipantControlLoopStateChange participantControlLoopStateChangeMsg =
107                 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
108         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE);
109
110         ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
111
112         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
113         assertEquals(ControlLoopOrderedState.PASSIVE, participantControlLoopStateChangeMsg.getOrderedState());
114
115         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
116         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
117         assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
118
119         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
120         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
121         assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
122     }
123
124     @Test
125     void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
126         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
127         participantControlLoopUpdateMsg.getParticipantId().setName("DummyName");
128         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
129
130         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
131         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
132
133         // Verify the content in participantHandler
134         assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(),
135                 participantHandler.getParticipantId().getName());
136     }
137
138     @Test
139     void testControlLoopUpdateListenerPassive() throws CoderException {
140         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
141         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
142
143         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
144         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
145
146         // Verify the content in participantHandler
147         assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId());
148         assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
149     }
150
151     @Test
152     void testControlLoopUpdateListenerUninitialised() throws CoderException {
153         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
154         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
155
156         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
157         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
158
159         // Verify the content in participantHandler
160         assertEquals(participantHandler.getParticipantId(), participantControlLoopUpdateMsg.getParticipantId());
161         assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
162     }
163 }