2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.apps.uservice.test.adapt.restrequestor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.google.gson.Gson;
28 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.core.Response;
39 import org.glassfish.grizzly.http.server.HttpServer;
40 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
41 import org.glassfish.jersey.server.ResourceConfig;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
47 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.service.engine.main.ApexMain;
52 * The Class TestRestRequestor.
54 public class RestRequestorTest {
55 private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
56 private static HttpServer 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 final ResourceConfig rc = new ResourceConfig(RestRequestorEndpointTest.class);
72 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
74 while (!server.isStarted()) {
75 ThreadUtilities.sleep(50);
82 * @throws Exception the exception
85 public static void tearDown() throws Exception {
88 new File("src/test/resources/events/EventsOut.json").delete();
89 new File("src/test/resources/events/EventsOutMulti0.json").delete();
90 new File("src/test/resources/events/EventsOutMulti1.json").delete();
97 public void resetCounters() {
98 RestRequestorEndpointTest.resetCounters();
102 * Test rest requestor get.
104 * @throws MessagingException the messaging exception
105 * @throws ApexException the apex exception
106 * @throws IOException Signals that an I/O exception has occurred.
109 public void testRestRequestorGet() throws MessagingException, ApexException, IOException {
110 final Client client = ClientBuilder.newClient();
112 final String[] args =
113 { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
114 final ApexMain apexMain = new ApexMain(args);
116 Response response = null;
118 // Wait for the required amount of events to be received or for 10 seconds
119 Double getsSoFar = 0.0;
120 for (int i = 0; i < 40; i++) {
121 ThreadUtilities.sleep(100);
123 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
124 .request("application/json").get();
126 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
130 final String responseString = response.readEntity(String.class);
132 @SuppressWarnings("unchecked")
133 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
134 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
136 if (getsSoFar >= 50.0) {
144 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
146 assertEquals(Double.valueOf(50.0), getsSoFar);
150 * Test REST requestor put.
152 * @throws MessagingException the messaging exception
153 * @throws ApexException the apex exception
154 * @throws IOException Signals that an I/O exception has occurred.
157 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
158 final Client client = ClientBuilder.newClient();
160 final String[] args =
161 { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
162 final ApexMain apexMain = new ApexMain(args);
164 // Wait for the required amount of events to be received or for 10 seconds
165 Double putsSoFar = 0.0;
167 Response response = null;
168 for (int i = 0; i < 40; i++) {
169 ThreadUtilities.sleep(100);
171 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
172 .request("application/json").get();
174 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
178 final String responseString = response.readEntity(String.class);
180 @SuppressWarnings("unchecked")
181 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
182 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
184 if (putsSoFar >= 50.0) {
192 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
193 assertEquals(Double.valueOf(50.0), putsSoFar);
197 * Test REST requestor post.
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 testRestRequestorPost() throws MessagingException, ApexException, IOException {
205 final Client client = ClientBuilder.newClient();
207 final String[] args =
208 { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
209 final ApexMain apexMain = new ApexMain(args);
211 // Wait for the required amount of events to be received or for 10 seconds
212 Double postsSoFar = 0.0;
213 for (int i = 0; i < 40; i++) {
214 ThreadUtilities.sleep(100);
216 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
217 .request("application/json").get();
219 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
220 final String responseString = response.readEntity(String.class);
222 @SuppressWarnings("unchecked")
223 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
224 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
226 if (postsSoFar >= 50.0) {
234 assertEquals(Double.valueOf(50.0), postsSoFar);
238 * Test REST requestor delete.
240 * @throws MessagingException the messaging exception
241 * @throws ApexException the apex exception
242 * @throws IOException Signals that an I/O exception has occurred.
245 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
246 final Client client = ClientBuilder.newClient();
248 final String[] args =
249 { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
250 final ApexMain apexMain = new ApexMain(args);
252 // Wait for the required amount of events to be received or for 10 seconds
253 Double deletesSoFar = 0.0;
254 for (int i = 0; i < 40; i++) {
255 ThreadUtilities.sleep(100);
257 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
258 .request("application/json").get();
260 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
261 final String responseString = response.readEntity(String.class);
263 @SuppressWarnings("unchecked")
264 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
265 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
267 if (deletesSoFar >= 50.0) {
275 assertEquals(Double.valueOf(50.0), deletesSoFar);
279 * Test REST requestor multi inputs.
281 * @throws MessagingException the messaging exception
282 * @throws ApexException the apex exception
283 * @throws IOException Signals that an I/O exception has occurred.
286 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
287 final Client client = ClientBuilder.newClient();
289 final String[] args =
290 { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
291 final ApexMain apexMain = new ApexMain(args);
293 // Wait for the required amount of events to be received or for 10 seconds
294 Double getsSoFar = 0.0;
295 for (int i = 0; i < 40; i++) {
296 ThreadUtilities.sleep(100);
298 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
299 .request("application/json").get();
301 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
302 final String responseString = response.readEntity(String.class);
304 @SuppressWarnings("unchecked")
305 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
306 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
308 if (getsSoFar >= 8.0) {
316 assertEquals(Double.valueOf(8.0), getsSoFar);
318 ThreadUtilities.sleep(1000);
322 * Test REST requestor producer alone.
324 * @throws MessagingException the messaging exception
325 * @throws ApexException the apex exception
326 * @throws IOException Signals that an I/O exception has occurred.
329 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
330 System.setOut(new PrintStream(outContent));
331 System.setErr(new PrintStream(errContent));
333 final String[] args =
334 { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
336 final ApexMain apexMain = new ApexMain(args);
337 ThreadUtilities.sleep(200);
340 final String outString = outContent.toString();
342 System.setOut(stdout);
343 System.setErr(stderr);
345 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
346 + "must run in peered requestor mode with a REST Requestor consumer"));
350 * Test REST requestor consumer alone.
352 * @throws MessagingException the messaging exception
353 * @throws ApexException the apex exception
354 * @throws IOException Signals that an I/O exception has occurred.
357 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
358 System.setOut(new PrintStream(outContent));
359 System.setErr(new PrintStream(errContent));
361 final String[] args =
362 { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
364 final ApexMain apexMain = new ApexMain(args);
365 ThreadUtilities.sleep(200);
368 final String outString = outContent.toString();
370 System.setOut(stdout);
371 System.setErr(stderr);
373 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
374 + "does not exist or is not defined with the same peered mode"));