2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import com.google.gson.Gson;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.core.Response;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
43 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
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,
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 resetCounters() {
101 SupportRestRequestorEndpoint.resetCounters();
105 * Test rest requestor get.
107 * @throws MessagingException the messaging exception
108 * @throws ApexException the apex exception
109 * @throws IOException Signals that an I/O exception has occurred.
112 public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
113 final Client client = ClientBuilder.newClient();
115 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"};
116 final ApexMain apexMain = new ApexMain(args);
118 Response response = null;
120 // Wait for the required amount of events to be received or for 10 seconds
121 Double getsSoFar = 0.0;
122 for (int i = 0; i < 40; i++) {
123 ThreadUtilities.sleep(100);
125 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
126 .request("application/json").get();
128 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
132 final String responseString = response.readEntity(String.class);
134 @SuppressWarnings("unchecked")
135 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
136 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
138 if (getsSoFar >= 50.0) {
146 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
148 assertEquals(Double.valueOf(50.0), getsSoFar);
152 * Test rest requestor get empty.
154 * @throws MessagingException the messaging exception
155 * @throws ApexException the apex exception
156 * @throws IOException Signals that an I/O exception has occurred.
159 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
160 final Client client = ClientBuilder.newClient();
162 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"};
163 final ApexMain apexMain = new ApexMain(args);
165 Response response = null;
167 // Wait for the required amount of events to be received or for 10 seconds
168 Double getsSoFar = 0.0;
169 for (int i = 0; i < 40; i++) {
170 ThreadUtilities.sleep(100);
172 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
173 .request("application/json").get();
175 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
179 final String responseString = response.readEntity(String.class);
181 @SuppressWarnings("unchecked")
182 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
183 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
185 if (getsSoFar >= 50.0) {
193 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
197 * Test REST requestor put.
199 * @throws MessagingException the messaging exception
200 * @throws ApexException the apex exception
201 * @throws IOException Signals that an I/O exception has occurred.
204 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
205 final Client client = ClientBuilder.newClient();
207 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"};
208 final ApexMain apexMain = new ApexMain(args);
210 // Wait for the required amount of events to be received or for 10 seconds
211 Double putsSoFar = 0.0;
213 Response response = null;
214 for (int i = 0; i < 40; i++) {
215 ThreadUtilities.sleep(100);
217 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
218 .request("application/json").get();
220 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
224 final String responseString = response.readEntity(String.class);
226 @SuppressWarnings("unchecked")
227 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
228 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
230 if (putsSoFar >= 50.0) {
238 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
239 assertEquals(Double.valueOf(50.0), putsSoFar);
243 * Test REST requestor post.
245 * @throws MessagingException the messaging exception
246 * @throws ApexException the apex exception
247 * @throws IOException Signals that an I/O exception has occurred.
250 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
251 final Client client = ClientBuilder.newClient();
253 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"};
254 final ApexMain apexMain = new ApexMain(args);
256 // Wait for the required amount of events to be received or for 10 seconds
257 Double postsSoFar = 0.0;
258 for (int i = 0; i < 40; i++) {
259 ThreadUtilities.sleep(100);
261 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
262 .request("application/json").get();
264 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
265 final String responseString = response.readEntity(String.class);
267 @SuppressWarnings("unchecked")
268 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
269 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
271 if (postsSoFar >= 50.0) {
279 assertEquals(Double.valueOf(50.0), postsSoFar);
283 * Test REST requestor delete.
285 * @throws MessagingException the messaging exception
286 * @throws ApexException the apex exception
287 * @throws IOException Signals that an I/O exception has occurred.
290 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
291 final Client client = ClientBuilder.newClient();
293 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"};
294 final ApexMain apexMain = new ApexMain(args);
296 // Wait for the required amount of events to be received or for 10 seconds
297 Double deletesSoFar = 0.0;
298 for (int i = 0; i < 40; i++) {
299 ThreadUtilities.sleep(100);
301 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
302 .request("application/json").get();
304 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
305 final String responseString = response.readEntity(String.class);
307 @SuppressWarnings("unchecked")
308 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
309 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
311 if (deletesSoFar >= 50.0) {
319 assertEquals(Double.valueOf(50.0), deletesSoFar);
323 * Test REST requestor multi inputs.
325 * @throws MessagingException the messaging exception
326 * @throws ApexException the apex exception
327 * @throws IOException Signals that an I/O exception has occurred.
330 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
331 final Client client = ClientBuilder.newClient();
333 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"};
334 final ApexMain apexMain = new ApexMain(args);
336 // Wait for the required amount of events to be received or for 10 seconds
337 Double getsSoFar = 0.0;
338 for (int i = 0; i < 40; i++) {
339 ThreadUtilities.sleep(100);
341 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
342 .request("application/json").get();
344 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
345 final String responseString = response.readEntity(String.class);
347 @SuppressWarnings("unchecked")
348 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
349 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
351 if (getsSoFar >= 8.0) {
359 assertEquals(Double.valueOf(8.0), getsSoFar);
363 * Test REST requestor producer alone.
365 * @throws MessagingException the messaging exception
366 * @throws ApexException the apex exception
367 * @throws IOException Signals that an I/O exception has occurred.
370 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
371 System.setOut(new PrintStream(outContent));
372 System.setErr(new PrintStream(errContent));
374 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
376 final ApexMain apexMain = new ApexMain(args);
377 ThreadUtilities.sleep(200);
380 final String outString = outContent.toString();
382 System.setOut(stdout);
383 System.setErr(stderr);
385 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
386 + "must run in peered requestor mode with a REST Requestor consumer"));
390 * Test REST requestor consumer alone.
392 * @throws MessagingException the messaging exception
393 * @throws ApexException the apex exception
394 * @throws IOException Signals that an I/O exception has occurred.
397 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
398 System.setOut(new PrintStream(outContent));
399 System.setErr(new PrintStream(errContent));
401 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
403 final ApexMain apexMain = new ApexMain(args);
404 ThreadUtilities.sleep(200);
407 final String outString = outContent.toString();
409 System.setOut(stdout);
410 System.setErr(stderr);
412 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
413 + "does not exist or is not defined with the same peered mode"));