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