2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020,2023 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
29 import com.google.gson.Gson;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
34 import java.util.concurrent.TimeUnit;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.core.Response;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.service.engine.main.ApexMain;
46 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
47 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
48 import org.onap.policy.common.gson.GsonMessageBodyHandler;
49 import org.onap.policy.common.utils.network.NetworkUtil;
52 * The Class TestRestRequestor.
54 public class RestRequestorTest {
55 private static final int PORT = 32801;
56 private static HttpServletServer server;
58 private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
59 private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
61 private final PrintStream stdout = System.out;
62 private final PrintStream stderr = System.err;
67 * @throws Exception the exception
70 public static void setUp() throws Exception {
71 server = HttpServletServerFactoryInstance.getServerFactory().build(null, false, null, PORT, false,
72 "/TestRESTRequestor", false, false);
74 server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
75 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
79 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
80 throw new IllegalStateException("port " + PORT + " is still not in use");
87 * @throws Exception the exception
90 public static void tearDown() throws Exception {
100 public void beforeTest() {
101 SupportRestRequestorEndpoint.resetCounters();
102 System.setOut(new PrintStream(outContent));
103 System.setErr(new PrintStream(errContent));
110 public void afterTest() {
111 System.setOut(stdout);
112 System.setErr(stderr);
116 * Test rest requestor get.
118 * @throws MessagingException the messaging exception
119 * @throws Exception an exception
122 public void testRestRequestorGet() throws Exception {
123 final Client client = ClientBuilder.newClient();
125 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"};
126 final ApexMain apexMain = new ApexMain(args);
127 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
129 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
130 .until(() -> getStatsFromServer(client, "GET") >= 50.0);
133 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
139 * Test rest requestor get empty.
141 * @throws MessagingException the messaging exception
142 * @throws ApexException the apex exception
143 * @throws IOException Signals that an I/O exception has occurred.
146 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
147 final Client client = ClientBuilder.newClient();
149 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"};
150 final ApexMain apexMain = new ApexMain(args);
151 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
153 Response response = null;
155 // Wait for the required amount of events to be received or for 10 seconds
156 double getsSoFar = 0.0;
157 for (int i = 0; i < 40; i++) {
158 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
159 .request("application/json").get();
161 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
165 final String responseString = response.readEntity(String.class);
167 @SuppressWarnings("unchecked")
168 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
169 getsSoFar = Double.parseDouble(jsonMap.get("GET").toString());
171 if (getsSoFar >= 50.0) {
177 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
181 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
185 * Test REST requestor put.
187 * @throws MessagingException the messaging exception
188 * @throws ApexException the apex exception
189 * @throws IOException Signals that an I/O exception has occurred.
192 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
193 final Client client = ClientBuilder.newClient();
195 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"};
196 final ApexMain apexMain = new ApexMain(args);
197 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
199 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
200 .until(() -> getStatsFromServer(client, "PUT") >= 50.0);
203 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
209 * Test REST requestor post.
211 * @throws MessagingException the messaging exception
212 * @throws ApexException the apex exception
213 * @throws IOException Signals that an I/O exception has occurred.
216 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
217 final Client client = ClientBuilder.newClient();
219 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"};
220 final ApexMain apexMain = new ApexMain(args);
221 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
223 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
224 .until(() -> getStatsFromServer(client, "POST") >= 50.0);
227 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
233 * Test REST requestor delete.
235 * @throws MessagingException the messaging exception
236 * @throws ApexException the apex exception
237 * @throws IOException Signals that an I/O exception has occurred.
240 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
241 final Client client = ClientBuilder.newClient();
243 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"};
244 final ApexMain apexMain = new ApexMain(args);
245 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
247 // Wait for the required amount of events to be received
248 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
249 .until(() -> getStatsFromServer(client, "DELETE") >= 50.0);
252 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
258 * Test REST requestor multi inputs.
260 * @throws MessagingException the messaging exception
261 * @throws ApexException the apex exception
262 * @throws IOException Signals that an I/O exception has occurred.
265 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
266 final Client client = ClientBuilder.newClient();
268 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"};
269 final ApexMain apexMain = new ApexMain(args);
270 await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
272 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
273 .until(() -> getStatsFromServer(client, "GET") >= 8.0);
276 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
282 * Test REST requestor producer alone.
284 * @throws MessagingException the messaging exception
285 * @throws ApexException the apex exception
286 * @throws IOException Signals that an I/O exception has occurred.
289 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
291 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
293 ApexMain apexMain = new ApexMain(args);
296 final String outString = outContent.toString();
298 assertThat(outString).contains("REST Requestor producer (RestRequestorProducer) "
299 + "must run in peered requestor mode with a REST Requestor consumer");
303 * Test REST requestor consumer alone.
305 * @throws MessagingException the messaging exception
306 * @throws ApexException the apex exception
307 * @throws IOException Signals that an I/O exception has occurred.
310 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
311 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
312 ApexMain apexMain = new ApexMain(args);
314 final String outString = outContent.toString();
315 assertThat(outString).contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
316 + "does not exist or is not defined with the same peered mode");
319 private double getStatsFromServer(final Client client, final String statToGet) {
320 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
321 .request("application/json").get();
323 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
324 final String responseString = response.readEntity(String.class);
326 @SuppressWarnings("unchecked")
327 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
328 return Double.parseDouble(jsonMap.get(statToGet).toString());