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;
51 public class TestRESTRequestor {
52 private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
53 private static HttpServer server;
55 private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56 private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58 private final PrintStream stdout = System.out;
59 private final PrintStream stderr = System.err;
62 public static void setUp() throws Exception {
63 final ResourceConfig rc = new ResourceConfig(TestRESTREequestorEndpoint.class);
64 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
66 while (!server.isStarted()) {
67 ThreadUtilities.sleep(50);
72 public static void tearDown() throws Exception {
75 new File("src/test/resources/events/EventsOut.json").delete();
76 new File("src/test/resources/events/EventsOutMulti0.json").delete();
77 new File("src/test/resources/events/EventsOutMulti1.json").delete();
81 public void resetCounters() {
82 TestRESTREequestorEndpoint.resetCounters();
86 public void testRESTRequestorGet() throws MessagingException, ApexException, IOException {
87 final Client client = ClientBuilder.newClient();
89 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
90 final ApexMain apexMain = new ApexMain(args);
92 // Wait for the required amount of events to be received or for 10 seconds
93 Double getsSoFar = 0.0;
94 for (int i = 0; i < 40; i++) {
95 ThreadUtilities.sleep(100);
97 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
98 .request("application/json").get();
100 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
101 final String responseString = response.readEntity(String.class);
103 @SuppressWarnings("unchecked")
104 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
105 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
107 if (getsSoFar >= 50.0) {
115 assertEquals(Double.valueOf(50.0), getsSoFar);
119 public void testRESTRequestorPut() throws MessagingException, ApexException, IOException {
120 final Client client = ClientBuilder.newClient();
122 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
123 final ApexMain apexMain = new ApexMain(args);
125 // Wait for the required amount of events to be received or for 10 seconds
126 Double putsSoFar = 0.0;
127 for (int i = 0; i < 40; i++) {
128 ThreadUtilities.sleep(100);
130 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
131 .request("application/json").get();
133 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
134 final String responseString = response.readEntity(String.class);
136 @SuppressWarnings("unchecked")
137 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
138 putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
140 if (putsSoFar >= 50.0) {
148 assertEquals(Double.valueOf(50.0), putsSoFar);
152 public void testRESTRequestorPost() throws MessagingException, ApexException, IOException {
153 final Client client = ClientBuilder.newClient();
155 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
156 final ApexMain apexMain = new ApexMain(args);
158 // Wait for the required amount of events to be received or for 10 seconds
159 Double postsSoFar = 0.0;
160 for (int i = 0; i < 40; i++) {
161 ThreadUtilities.sleep(100);
163 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
164 .request("application/json").get();
166 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
167 final String responseString = response.readEntity(String.class);
169 @SuppressWarnings("unchecked")
170 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
171 postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
173 if (postsSoFar >= 50.0) {
181 assertEquals(Double.valueOf(50.0), postsSoFar);
185 public void testRESTRequestorDelete() throws MessagingException, ApexException, IOException {
186 final Client client = ClientBuilder.newClient();
188 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
189 final ApexMain apexMain = new ApexMain(args);
191 // Wait for the required amount of events to be received or for 10 seconds
192 Double deletesSoFar = 0.0;
193 for (int i = 0; i < 40; i++) {
194 ThreadUtilities.sleep(100);
196 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
197 .request("application/json").get();
199 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
200 final String responseString = response.readEntity(String.class);
202 @SuppressWarnings("unchecked")
203 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
204 deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
206 if (deletesSoFar >= 50.0) {
214 assertEquals(Double.valueOf(50.0), deletesSoFar);
218 public void testRESTRequestorMultiInputs() throws MessagingException, ApexException, IOException {
219 final Client client = ClientBuilder.newClient();
221 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
222 final ApexMain apexMain = new ApexMain(args);
224 // Wait for the required amount of events to be received or for 10 seconds
225 Double getsSoFar = 0.0;
226 for (int i = 0; i < 40; i++) {
227 ThreadUtilities.sleep(100);
229 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
230 .request("application/json").get();
232 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
233 final String responseString = response.readEntity(String.class);
235 @SuppressWarnings("unchecked")
236 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
237 getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
239 if (getsSoFar >= 8.0) {
247 assertEquals(Double.valueOf(8.0), getsSoFar);
249 ThreadUtilities.sleep(1000);
253 public void testRESTRequestorProducerAlone() throws MessagingException, ApexException, IOException {
254 System.setOut(new PrintStream(outContent));
255 System.setErr(new PrintStream(errContent));
257 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
259 final ApexMain apexMain = new ApexMain(args);
260 ThreadUtilities.sleep(200);
263 final String outString = outContent.toString();
265 System.setOut(stdout);
266 System.setErr(stderr);
268 assertTrue(outString.contains(
269 "REST Requestor producer (RestRequestorProducer) must run in peered requestor mode with a REST Requestor consumer"));
273 public void testRESTRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
274 System.setOut(new PrintStream(outContent));
275 System.setErr(new PrintStream(errContent));
277 final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
279 final ApexMain apexMain = new ApexMain(args);
280 ThreadUtilities.sleep(200);
283 final String outString = outContent.toString();
285 System.setOut(stdout);
286 System.setErr(stderr);
288 assertTrue(outString.contains(
289 "event input for peered mode \"REQUESTOR\": peer \"RestRequestorProducer\" for event handler \"RestRequestorConsumer\" does not exist or is not defined as being synchronous"));