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 int PORT = 32801;
54 private static HttpServletServer server;
56 private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57 private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59 private final PrintStream stdout = System.out;
60 private final PrintStream stderr = System.err;
65 * @throws Exception the exception
68 public static void setUp() throws Exception {
69 server = HttpServletServerFactoryInstance.getServerFactory().build(
70 null, false, null, PORT, "/TestRESTRequestor", false, false);
72 server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
73 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
77 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
78 throw new IllegalStateException("port " + PORT + " is still not in use");
85 * @throws Exception the exception
88 public static void tearDown() throws Exception {
93 new File("src/test/resources/events/EventsOut.json").delete();
94 new File("src/test/resources/events/EventsOutMulti0.json").delete();
95 new File("src/test/resources/events/EventsOutMulti1.json").delete();
102 public void resetCounters() {
103 SupportRestRequestorEndpoint.resetCounters();
107 * Test rest requestor get.
109 * @throws MessagingException the messaging exception
110 * @throws ApexException the apex exception
111 * @throws IOException Signals that an I/O exception has occurred.
114 public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
115 final Client client = ClientBuilder.newClient();
117 final String[] args =
118 { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
119 final ApexMain apexMain = new ApexMain(args);
121 Response response = null;
123 // Wait for the required amount of events to be received or for 10 seconds
124 Double getsSoFar = 0.0;
125 for (int i = 0; i < 40; i++) {
126 ThreadUtilities.sleep(100);
128 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
129 .request("application/json").get();
131 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
135 final String responseString = response.readEntity(String.class);
137 @SuppressWarnings("unchecked")
138 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
139 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
141 if (getsSoFar >= 50.0) {
149 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
151 assertEquals(Double.valueOf(50.0), getsSoFar);
155 * Test rest requestor get empty.
157 * @throws MessagingException the messaging exception
158 * @throws ApexException the apex exception
159 * @throws IOException Signals that an I/O exception has occurred.
162 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
163 final Client client = ClientBuilder.newClient();
165 final String[] args =
166 { "src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json" };
167 final ApexMain apexMain = new ApexMain(args);
169 Response response = null;
171 // Wait for the required amount of events to be received or for 10 seconds
172 Double getsSoFar = 0.0;
173 for (int i = 0; i < 40; i++) {
174 ThreadUtilities.sleep(100);
176 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
177 .request("application/json").get();
179 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
183 final String responseString = response.readEntity(String.class);
185 @SuppressWarnings("unchecked")
186 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
187 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
189 if (getsSoFar >= 50.0) {
197 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
201 * Test REST requestor put.
203 * @throws MessagingException the messaging exception
204 * @throws ApexException the apex exception
205 * @throws IOException Signals that an I/O exception has occurred.
208 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
209 final Client client = ClientBuilder.newClient();
211 final String[] args =
212 { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
213 final ApexMain apexMain = new ApexMain(args);
215 // Wait for the required amount of events to be received or for 10 seconds
216 Double putsSoFar = 0.0;
218 Response response = null;
219 for (int i = 0; i < 40; i++) {
220 ThreadUtilities.sleep(100);
222 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
223 .request("application/json").get();
225 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
229 final String responseString = response.readEntity(String.class);
231 @SuppressWarnings("unchecked")
232 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
233 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
235 if (putsSoFar >= 50.0) {
243 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
244 assertEquals(Double.valueOf(50.0), putsSoFar);
248 * Test REST requestor post.
250 * @throws MessagingException the messaging exception
251 * @throws ApexException the apex exception
252 * @throws IOException Signals that an I/O exception has occurred.
255 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
256 final Client client = ClientBuilder.newClient();
258 final String[] args =
259 { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
260 final ApexMain apexMain = new ApexMain(args);
262 // Wait for the required amount of events to be received or for 10 seconds
263 Double postsSoFar = 0.0;
264 for (int i = 0; i < 40; i++) {
265 ThreadUtilities.sleep(100);
267 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
268 .request("application/json").get();
270 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
271 final String responseString = response.readEntity(String.class);
273 @SuppressWarnings("unchecked")
274 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
275 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
277 if (postsSoFar >= 50.0) {
285 assertEquals(Double.valueOf(50.0), postsSoFar);
289 * Test REST requestor delete.
291 * @throws MessagingException the messaging exception
292 * @throws ApexException the apex exception
293 * @throws IOException Signals that an I/O exception has occurred.
296 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
297 final Client client = ClientBuilder.newClient();
299 final String[] args =
300 { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
301 final ApexMain apexMain = new ApexMain(args);
303 // Wait for the required amount of events to be received or for 10 seconds
304 Double deletesSoFar = 0.0;
305 for (int i = 0; i < 40; i++) {
306 ThreadUtilities.sleep(100);
308 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
309 .request("application/json").get();
311 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
312 final String responseString = response.readEntity(String.class);
314 @SuppressWarnings("unchecked")
315 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
316 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
318 if (deletesSoFar >= 50.0) {
326 assertEquals(Double.valueOf(50.0), deletesSoFar);
330 * Test REST requestor multi inputs.
332 * @throws MessagingException the messaging exception
333 * @throws ApexException the apex exception
334 * @throws IOException Signals that an I/O exception has occurred.
337 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
338 final Client client = ClientBuilder.newClient();
340 final String[] args =
341 { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
342 final ApexMain apexMain = new ApexMain(args);
344 // Wait for the required amount of events to be received or for 10 seconds
345 Double getsSoFar = 0.0;
346 for (int i = 0; i < 40; i++) {
347 ThreadUtilities.sleep(100);
349 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
350 .request("application/json").get();
352 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
353 final String responseString = response.readEntity(String.class);
355 @SuppressWarnings("unchecked")
356 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
357 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
359 if (getsSoFar >= 8.0) {
367 assertEquals(Double.valueOf(8.0), getsSoFar);
369 ThreadUtilities.sleep(1000);
373 * Test REST requestor producer alone.
375 * @throws MessagingException the messaging exception
376 * @throws ApexException the apex exception
377 * @throws IOException Signals that an I/O exception has occurred.
380 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
381 System.setOut(new PrintStream(outContent));
382 System.setErr(new PrintStream(errContent));
384 final String[] args =
385 { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
387 final ApexMain apexMain = new ApexMain(args);
388 ThreadUtilities.sleep(200);
391 final String outString = outContent.toString();
393 System.setOut(stdout);
394 System.setErr(stderr);
396 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
397 + "must run in peered requestor mode with a REST Requestor consumer"));
401 * Test REST requestor consumer alone.
403 * @throws MessagingException the messaging exception
404 * @throws ApexException the apex exception
405 * @throws IOException Signals that an I/O exception has occurred.
408 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
409 System.setOut(new PrintStream(outContent));
410 System.setErr(new PrintStream(errContent));
412 final String[] args =
413 { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
415 final ApexMain apexMain = new ApexMain(args);
416 ThreadUtilities.sleep(200);
419 final String outString = outContent.toString();
421 System.setOut(stdout);
422 System.setErr(stderr);
424 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
425 + "does not exist or is not defined with the same peered mode"));