46317729afef4540c0ef1fe8e4479f1d139d9e2c
[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.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;
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 ParticipantIntermediaryApi participantIntermediaryApi;
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 =
111                 new ControlLoopStateChangeListener(participantIntermediaryApi.getParticipantHandler());
112
113         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
114         assertEquals(ControlLoopOrderedState.PASSIVE, participantControlLoopStateChangeMsg.getOrderedState());
115
116         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
117         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
118         assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
119
120         participantControlLoopStateChangeMsg.setOrderedState(ControlLoopOrderedState.RUNNING);
121         clStateChangeListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopStateChangeMsg);
122         assertEquals(ControlLoopOrderedState.RUNNING, participantControlLoopStateChangeMsg.getOrderedState());
123     }
124
125     @Test
126     void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
127         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
128         participantControlLoopUpdateMsg.getParticipantId().setName("DummyName");
129         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
130
131         ControlLoopUpdateListener clUpdateListener =
132                 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
133         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
134
135         // Verify the content in participantHandler
136         assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(),
137                 participantIntermediaryApi.getParticipantHandler().getParticipantId().getName());
138     }
139
140     @Test
141     void testControlLoopUpdateListenerPassive() throws CoderException {
142         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
143         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
144
145         ControlLoopUpdateListener clUpdateListener =
146                 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
147         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
148
149         // Verify the content in participantHandler
150         assertEquals(participantIntermediaryApi.getParticipantHandler().getParticipantId(),
151                 participantControlLoopUpdateMsg.getParticipantId());
152         assertEquals(1, participantIntermediaryApi.getParticipantHandler().getControlLoopHandler().getControlLoops()
153                 .getControlLoopList().size());
154     }
155
156     @Test
157     void testControlLoopUpdateListenerUninitialised() throws CoderException {
158         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
159         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED);
160
161         ControlLoopUpdateListener clUpdateListener =
162                 new ControlLoopUpdateListener(participantIntermediaryApi.getParticipantHandler());
163         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
164
165         // Verify the content in participantHandler
166         assertEquals(participantIntermediaryApi.getParticipantHandler().getParticipantId(),
167                 participantControlLoopUpdateMsg.getParticipantId());
168         assertEquals(1, participantIntermediaryApi.getParticipantHandler().getControlLoopHandler().getControlLoops()
169                 .getControlLoopList().size());
170     }
171 }