2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
28 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35 import javax.ws.rs.core.Response;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
41 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
42 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
43 import org.onap.policy.apex.service.engine.main.ApexMain;
44 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
45 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
46 import org.onap.policy.common.gson.GsonMessageBodyHandler;
47 import org.onap.policy.common.utils.network.NetworkUtil;
50 * The Class TestRestRequestor.
52 public class RestRequestorTest {
53 private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
54 private static final int PORT = 32801;
55 private static HttpServletServer server;
57 private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58 private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
60 private final PrintStream stdout = System.out;
61 private final PrintStream stderr = System.err;
66 * @throws Exception the exception
69 public static void setUp() throws Exception {
70 server = HttpServletServerFactoryInstance.getServerFactory().build(
71 null, false, null, PORT, "/TestRESTRequestor", false, false);
73 server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
74 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
78 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
79 throw new IllegalStateException("port " + PORT + " is still not in use");
86 * @throws Exception the exception
89 public static void tearDown() throws Exception {
94 new File("src/test/resources/events/EventsOut.json").delete();
95 new File("src/test/resources/events/EventsOutMulti0.json").delete();
96 new File("src/test/resources/events/EventsOutMulti1.json").delete();
103 public void resetCounters() {
104 SupportRestRequestorEndpoint.resetCounters();
108 * Test rest requestor get.
110 * @throws MessagingException the messaging exception
111 * @throws ApexException the apex exception
112 * @throws IOException Signals that an I/O exception has occurred.
115 public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
116 final Client client = ClientBuilder.newClient();
118 final String[] args =
119 { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
120 final ApexMain apexMain = new ApexMain(args);
122 Response response = null;
124 // Wait for the required amount of events to be received or for 10 seconds
125 Double getsSoFar = 0.0;
126 for (int i = 0; i < 40; i++) {
127 ThreadUtilities.sleep(100);
129 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
130 .request("application/json").get();
132 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
136 final String responseString = response.readEntity(String.class);
138 @SuppressWarnings("unchecked")
139 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
140 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
142 if (getsSoFar >= 50.0) {
150 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
152 assertEquals(Double.valueOf(50.0), getsSoFar);
156 * Test rest requestor get empty.
158 * @throws MessagingException the messaging exception
159 * @throws ApexException the apex exception
160 * @throws IOException Signals that an I/O exception has occurred.
163 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
164 final Client client = ClientBuilder.newClient();
166 final String[] args =
167 { "src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json" };
168 final ApexMain apexMain = new ApexMain(args);
170 Response response = null;
172 // Wait for the required amount of events to be received or for 10 seconds
173 Double getsSoFar = 0.0;
174 for (int i = 0; i < 40; i++) {
175 ThreadUtilities.sleep(100);
177 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
178 .request("application/json").get();
180 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
184 final String responseString = response.readEntity(String.class);
186 @SuppressWarnings("unchecked")
187 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
188 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
190 if (getsSoFar >= 50.0) {
198 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
202 * Test REST requestor put.
204 * @throws MessagingException the messaging exception
205 * @throws ApexException the apex exception
206 * @throws IOException Signals that an I/O exception has occurred.
209 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
210 final Client client = ClientBuilder.newClient();
212 final String[] args =
213 { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
214 final ApexMain apexMain = new ApexMain(args);
216 // Wait for the required amount of events to be received or for 10 seconds
217 Double putsSoFar = 0.0;
219 Response response = null;
220 for (int i = 0; i < 40; i++) {
221 ThreadUtilities.sleep(100);
223 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
224 .request("application/json").get();
226 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
230 final String responseString = response.readEntity(String.class);
232 @SuppressWarnings("unchecked")
233 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
234 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
236 if (putsSoFar >= 50.0) {
244 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
245 assertEquals(Double.valueOf(50.0), putsSoFar);
249 * Test REST requestor post.
251 * @throws MessagingException the messaging exception
252 * @throws ApexException the apex exception
253 * @throws IOException Signals that an I/O exception has occurred.
256 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
257 final Client client = ClientBuilder.newClient();
259 final String[] args =
260 { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
261 final ApexMain apexMain = new ApexMain(args);
263 // Wait for the required amount of events to be received or for 10 seconds
264 Double postsSoFar = 0.0;
265 for (int i = 0; i < 40; i++) {
266 ThreadUtilities.sleep(100);
268 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
269 .request("application/json").get();
271 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
272 final String responseString = response.readEntity(String.class);
274 @SuppressWarnings("unchecked")
275 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
276 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
278 if (postsSoFar >= 50.0) {
286 assertEquals(Double.valueOf(50.0), postsSoFar);
290 * Test REST requestor delete.
292 * @throws MessagingException the messaging exception
293 * @throws ApexException the apex exception
294 * @throws IOException Signals that an I/O exception has occurred.
297 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
298 final Client client = ClientBuilder.newClient();
300 final String[] args =
301 { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
302 final ApexMain apexMain = new ApexMain(args);
304 // Wait for the required amount of events to be received or for 10 seconds
305 Double deletesSoFar = 0.0;
306 for (int i = 0; i < 40; i++) {
307 ThreadUtilities.sleep(100);
309 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
310 .request("application/json").get();
312 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
313 final String responseString = response.readEntity(String.class);
315 @SuppressWarnings("unchecked")
316 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
317 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
319 if (deletesSoFar >= 50.0) {
327 assertEquals(Double.valueOf(50.0), deletesSoFar);
331 * Test REST requestor multi inputs.
333 * @throws MessagingException the messaging exception
334 * @throws ApexException the apex exception
335 * @throws IOException Signals that an I/O exception has occurred.
338 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
339 final Client client = ClientBuilder.newClient();
341 final String[] args =
342 { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
343 final ApexMain apexMain = new ApexMain(args);
345 // Wait for the required amount of events to be received or for 10 seconds
346 Double getsSoFar = 0.0;
347 for (int i = 0; i < 40; i++) {
348 ThreadUtilities.sleep(100);
350 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
351 .request("application/json").get();
353 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
354 final String responseString = response.readEntity(String.class);
356 @SuppressWarnings("unchecked")
357 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
358 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
360 if (getsSoFar >= 8.0) {
368 assertEquals(Double.valueOf(8.0), getsSoFar);
370 ThreadUtilities.sleep(1000);
374 * Test REST requestor producer alone.
376 * @throws MessagingException the messaging exception
377 * @throws ApexException the apex exception
378 * @throws IOException Signals that an I/O exception has occurred.
381 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
382 System.setOut(new PrintStream(outContent));
383 System.setErr(new PrintStream(errContent));
385 final String[] args =
386 { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
388 final ApexMain apexMain = new ApexMain(args);
389 ThreadUtilities.sleep(200);
392 final String outString = outContent.toString();
394 System.setOut(stdout);
395 System.setErr(stderr);
397 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
398 + "must run in peered requestor mode with a REST Requestor consumer"));
402 * Test REST requestor consumer alone.
404 * @throws MessagingException the messaging exception
405 * @throws ApexException the apex exception
406 * @throws IOException Signals that an I/O exception has occurred.
409 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
410 System.setOut(new PrintStream(outContent));
411 System.setErr(new PrintStream(errContent));
413 final String[] args =
414 { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
416 final ApexMain apexMain = new ApexMain(args);
417 ThreadUtilities.sleep(200);
420 final String outString = outContent.toString();
422 System.setOut(stdout);
423 System.setErr(stderr);
425 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
426 + "does not exist or is not defined with the same peered mode"));