6e7f287350561b0c65408004a03479bc97e443ff
[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.models.messages.dmaap.participant.ParticipantUpdate;
38 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
39 import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
51
52 @ExtendWith(SpringExtension.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
54 @TestPropertySource(locations = {"classpath:application_test.properties"})
55 class ParticipantDcaeTest {
56
57     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
58     private static final String TOPIC = "my-topic";
59     private static final Object lockit = new Object();
60
61     private static final String LOOP = "pmsh_loop";
62     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
63
64     private static ClientAndServer mockClampServer;
65     private static ClientAndServer mockConsulServer;
66
67     @Autowired
68     private ParticipantHandler participantHandler;
69
70     /**
71      * start Servers.
72      */
73     @BeforeAll
74     public void startServers() {
75
76         // Clamp
77         mockClampServer = ClientAndServer.startClientAndServer(8443);
78
79         mockClampServer.when(request().withMethod("GET").withPath("/restservices/clds/v2/loop/getstatus/" + LOOP))
80                 .respond(response().withBody(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED).toString())
81                         .withStatusCode(200));
82
83         mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/deploy/" + LOOP))
84                 .respond(response().withStatusCode(202));
85
86         mockClampServer.when(request().withMethod("PUT").withPath("/restservices/clds/v2/loop/undeploy/" + LOOP))
87                 .respond(response().withStatusCode(202));
88
89         // Consul
90         mockConsulServer = ClientAndServer.startClientAndServer(31321);
91
92         mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy"))
93                 .respond(response().withStatusCode(200));
94         ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg();
95
96         synchronized (lockit) {
97             ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler);
98             participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg);
99         }
100     }
101
102     /**
103      * stop Server.
104      */
105     @AfterAll
106     public static void stopServer() {
107         mockClampServer.stop();
108         mockClampServer = null;
109
110         mockConsulServer.stop();
111         mockConsulServer = null;
112     }
113
114     @Test
115     void testControlLoopStateChangeMessageListener() {
116         ControlLoopStateChange controlLoopStateChangeMsg =
117                 TestListenerUtils.createControlLoopStateChangeMsg(ControlLoopOrderedState.UNINITIALISED);
118         controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.PASSIVE);
119
120         ControlLoopStateChangeListener clStateChangeListener = new ControlLoopStateChangeListener(participantHandler);
121
122         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
123         assertEquals(ControlLoopOrderedState.PASSIVE, controlLoopStateChangeMsg.getOrderedState());
124
125         controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
126         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
127         assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState());
128
129         controlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
130         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, controlLoopStateChangeMsg);
131         assertEquals(ControlLoopOrderedState.RUNNING, controlLoopStateChangeMsg.getOrderedState());
132     }
133
134     @Test
135     void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
136         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
137         controlLoopUpdateMsg.getParticipantId().setName("DummyName");
138
139         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
140         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
141
142         // Verify the content in participantHandler
143         assertNotEquals(controlLoopUpdateMsg.getParticipantId().getName(),
144                 participantHandler.getParticipantId().getName());
145     }
146
147     @Test
148     void testControlLoopUpdateListenerPassive() throws CoderException {
149         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
150
151         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
152         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
153
154         // Verify the content in participantHandler
155         assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
156         assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
157     }
158
159     @Test
160     void testControlLoopUpdateListenerUninitialised() throws CoderException {
161         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
162
163         ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
164         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
165
166         // Verify the content in participantHandler
167         assertEquals(participantHandler.getParticipantId(), controlLoopUpdateMsg.getParticipantId());
168         assertEquals(1, participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList().size());
169     }
170
171     @Test
172     void testControlLoopUpdateListenerString() throws CoderException {
173         ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
174
175         assertThat(controlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
176         ControlLoopUpdate copyControlLoopUpdateMsg =
177                 TestListenerUtils.createCopyControlLoopUpdateMsg(controlLoopUpdateMsg);
178         assertThat(copyControlLoopUpdateMsg.toString()).contains("state=UNINITIALISED");
179         assertNotEquals(controlLoopUpdateMsg, copyControlLoopUpdateMsg);
180     }
181 }