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 com.google.gson.Gson;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.service.engine.main.ApexMain;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.onap.policy.common.utils.network.NetworkUtil;
38 import javax.ws.rs.client.Client;
39 import javax.ws.rs.client.ClientBuilder;
40 import javax.ws.rs.core.Response;
41 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44 import java.io.PrintStream;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
51 * The Class TestRestRequestor.
53 public class RestRequestorTest {
54 private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
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(
72 null, false, null, PORT, "/TestRESTRequestor", false, false);
74 server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
75 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
79 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 2000, 1L)) {
80 throw new IllegalStateException("port " + PORT + " is still not in use");
87 * @throws Exception the exception
90 public static void tearDown() throws Exception {
95 new File("src/test/resources/events/EventsOut.json").delete();
96 new File("src/test/resources/events/EventsOutMulti0.json").delete();
97 new File("src/test/resources/events/EventsOutMulti1.json").delete();
104 public void resetCounters() {
105 SupportRestRequestorEndpoint.resetCounters();
109 * Test rest requestor get.
111 * @throws MessagingException the messaging exception
112 * @throws ApexException the apex exception
113 * @throws IOException Signals that an I/O exception has occurred.
116 public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
117 final Client client = ClientBuilder.newClient();
119 final String[] args =
120 { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
121 final ApexMain apexMain = new ApexMain(args);
123 Response response = null;
125 // Wait for the required amount of events to be received or for 10 seconds
126 Double getsSoFar = 0.0;
127 for (int i = 0; i < 40; i++) {
128 ThreadUtilities.sleep(100);
130 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
131 .request("application/json").get();
133 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
137 final String responseString = response.readEntity(String.class);
139 @SuppressWarnings("unchecked")
140 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
141 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
143 if (getsSoFar >= 50.0) {
151 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
153 assertEquals(Double.valueOf(50.0), getsSoFar);
157 * Test rest requestor get empty.
159 * @throws MessagingException the messaging exception
160 * @throws ApexException the apex exception
161 * @throws IOException Signals that an I/O exception has occurred.
164 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
165 final Client client = ClientBuilder.newClient();
167 final String[] args =
168 { "src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json" };
169 final ApexMain apexMain = new ApexMain(args);
171 Response response = null;
173 // Wait for the required amount of events to be received or for 10 seconds
174 Double getsSoFar = 0.0;
175 for (int i = 0; i < 40; i++) {
176 ThreadUtilities.sleep(100);
178 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
179 .request("application/json").get();
181 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
185 final String responseString = response.readEntity(String.class);
187 @SuppressWarnings("unchecked")
188 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
189 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
191 if (getsSoFar >= 50.0) {
199 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
203 * Test REST requestor put.
205 * @throws MessagingException the messaging exception
206 * @throws ApexException the apex exception
207 * @throws IOException Signals that an I/O exception has occurred.
210 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
211 final Client client = ClientBuilder.newClient();
213 final String[] args =
214 { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
215 final ApexMain apexMain = new ApexMain(args);
217 // Wait for the required amount of events to be received or for 10 seconds
218 Double putsSoFar = 0.0;
220 Response response = null;
221 for (int i = 0; i < 40; i++) {
222 ThreadUtilities.sleep(100);
224 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
225 .request("application/json").get();
227 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
231 final String responseString = response.readEntity(String.class);
233 @SuppressWarnings("unchecked")
234 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
235 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
237 if (putsSoFar >= 50.0) {
245 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
246 assertEquals(Double.valueOf(50.0), putsSoFar);
250 * Test REST requestor post.
252 * @throws MessagingException the messaging exception
253 * @throws ApexException the apex exception
254 * @throws IOException Signals that an I/O exception has occurred.
257 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
258 final Client client = ClientBuilder.newClient();
260 final String[] args =
261 { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
262 final ApexMain apexMain = new ApexMain(args);
264 // Wait for the required amount of events to be received or for 10 seconds
265 Double postsSoFar = 0.0;
266 for (int i = 0; i < 40; i++) {
267 ThreadUtilities.sleep(100);
269 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
270 .request("application/json").get();
272 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
273 final String responseString = response.readEntity(String.class);
275 @SuppressWarnings("unchecked")
276 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
277 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
279 if (postsSoFar >= 50.0) {
287 assertEquals(Double.valueOf(50.0), postsSoFar);
291 * Test REST requestor delete.
293 * @throws MessagingException the messaging exception
294 * @throws ApexException the apex exception
295 * @throws IOException Signals that an I/O exception has occurred.
298 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
299 final Client client = ClientBuilder.newClient();
301 final String[] args =
302 { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
303 final ApexMain apexMain = new ApexMain(args);
305 // Wait for the required amount of events to be received or for 10 seconds
306 Double deletesSoFar = 0.0;
307 for (int i = 0; i < 40; i++) {
308 ThreadUtilities.sleep(100);
310 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
311 .request("application/json").get();
313 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
314 final String responseString = response.readEntity(String.class);
316 @SuppressWarnings("unchecked")
317 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
318 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
320 if (deletesSoFar >= 50.0) {
328 assertEquals(Double.valueOf(50.0), deletesSoFar);
332 * Test REST requestor multi inputs.
334 * @throws MessagingException the messaging exception
335 * @throws ApexException the apex exception
336 * @throws IOException Signals that an I/O exception has occurred.
339 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
340 final Client client = ClientBuilder.newClient();
342 final String[] args =
343 { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
344 final ApexMain apexMain = new ApexMain(args);
346 // Wait for the required amount of events to be received or for 10 seconds
347 Double getsSoFar = 0.0;
348 for (int i = 0; i < 40; i++) {
349 ThreadUtilities.sleep(100);
351 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
352 .request("application/json").get();
354 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
355 final String responseString = response.readEntity(String.class);
357 @SuppressWarnings("unchecked")
358 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
359 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
361 if (getsSoFar >= 8.0) {
369 assertEquals(Double.valueOf(8.0), getsSoFar);
371 ThreadUtilities.sleep(1000);
375 * Test REST requestor producer alone.
377 * @throws MessagingException the messaging exception
378 * @throws ApexException the apex exception
379 * @throws IOException Signals that an I/O exception has occurred.
382 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
383 System.setOut(new PrintStream(outContent));
384 System.setErr(new PrintStream(errContent));
386 final String[] args =
387 { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
389 final ApexMain apexMain = new ApexMain(args);
390 ThreadUtilities.sleep(200);
393 final String outString = outContent.toString();
395 System.setOut(stdout);
396 System.setErr(stderr);
398 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
399 + "must run in peered requestor mode with a REST Requestor consumer"));
403 * Test REST requestor consumer alone.
405 * @throws MessagingException the messaging exception
406 * @throws ApexException the apex exception
407 * @throws IOException Signals that an I/O exception has occurred.
410 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
411 System.setOut(new PrintStream(outContent));
412 System.setErr(new PrintStream(errContent));
414 final String[] args =
415 { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
417 final ApexMain apexMain = new ApexMain(args);
418 ThreadUtilities.sleep(200);
421 final String outString = outContent.toString();
423 System.setOut(stdout);
424 System.setErr(stderr);
426 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
427 + "does not exist or is not defined with the same peered mode"));