2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
28 import com.google.gson.Gson;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
33 import java.util.concurrent.TimeUnit;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.core.Response;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
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(null, false, null, PORT,
70 "/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 {
98 public void resetCounters() {
99 SupportRestRequestorEndpoint.resetCounters();
103 * Test rest requestor get.
105 * @throws MessagingException the messaging exception
106 * @throws Exception an exception
109 public void testRestRequestorGet() throws Exception {
110 final Client client = ClientBuilder.newClient();
112 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"};
113 final ApexMain apexMain = new ApexMain(args);
114 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
116 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
117 .until(() -> getStatsFromServer(client, "GET") >= 50.0);
120 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
126 * Test rest requestor get empty.
128 * @throws MessagingException the messaging exception
129 * @throws ApexException the apex exception
130 * @throws IOException Signals that an I/O exception has occurred.
133 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
134 final Client client = ClientBuilder.newClient();
136 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"};
137 final ApexMain apexMain = new ApexMain(args);
138 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
140 Response response = null;
142 // Wait for the required amount of events to be received or for 10 seconds
143 double getsSoFar = 0.0;
144 for (int i = 0; i < 40; i++) {
145 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
146 .request("application/json").get();
148 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
152 final String responseString = response.readEntity(String.class);
154 @SuppressWarnings("unchecked")
155 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
156 getsSoFar = Double.parseDouble(jsonMap.get("GET").toString());
158 if (getsSoFar >= 50.0) {
164 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
168 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
172 * Test REST requestor put.
174 * @throws MessagingException the messaging exception
175 * @throws ApexException the apex exception
176 * @throws IOException Signals that an I/O exception has occurred.
179 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
180 final Client client = ClientBuilder.newClient();
182 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"};
183 final ApexMain apexMain = new ApexMain(args);
184 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
186 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
187 .until(() -> getStatsFromServer(client, "PUT") >= 50.0);
190 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
196 * Test REST requestor post.
198 * @throws MessagingException the messaging exception
199 * @throws ApexException the apex exception
200 * @throws IOException Signals that an I/O exception has occurred.
203 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
204 final Client client = ClientBuilder.newClient();
206 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"};
207 final ApexMain apexMain = new ApexMain(args);
208 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
210 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
211 .until(() -> getStatsFromServer(client, "POST") >= 50.0);
214 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
220 * Test REST requestor delete.
222 * @throws MessagingException the messaging exception
223 * @throws ApexException the apex exception
224 * @throws IOException Signals that an I/O exception has occurred.
227 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
228 final Client client = ClientBuilder.newClient();
230 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"};
231 final ApexMain apexMain = new ApexMain(args);
232 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
234 // Wait for the required amount of events to be received
235 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
236 .until(() -> getStatsFromServer(client, "DELETE") >= 50.0);
239 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
245 * Test REST requestor multi inputs.
247 * @throws MessagingException the messaging exception
248 * @throws ApexException the apex exception
249 * @throws IOException Signals that an I/O exception has occurred.
252 public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
253 final Client client = ClientBuilder.newClient();
255 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"};
256 final ApexMain apexMain = new ApexMain(args);
257 await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
259 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
260 .until(() -> getStatsFromServer(client, "GET") >= 8.0);
263 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
269 * Test REST requestor producer alone.
271 * @throws MessagingException the messaging exception
272 * @throws ApexException the apex exception
273 * @throws IOException Signals that an I/O exception has occurred.
276 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
277 System.setOut(new PrintStream(outContent));
278 System.setErr(new PrintStream(errContent));
280 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
282 ApexMain apexMain = new ApexMain(args);
285 final String outString = outContent.toString();
287 System.setOut(stdout);
288 System.setErr(stderr);
290 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
291 + "must run in peered requestor mode with a REST Requestor consumer"));
295 * Test REST requestor consumer alone.
297 * @throws MessagingException the messaging exception
298 * @throws ApexException the apex exception
299 * @throws IOException Signals that an I/O exception has occurred.
302 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
303 System.setOut(new PrintStream(outContent));
304 System.setErr(new PrintStream(errContent));
306 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
308 ApexMain apexMain = new ApexMain(args);
311 final String outString = outContent.toString();
313 System.setOut(stdout);
314 System.setErr(stderr);
316 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
317 + "does not exist or is not defined with the same peered mode"));
320 private double getStatsFromServer(final Client client, final String statToGet) {
321 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
322 .request("application/json").get();
324 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
325 final String responseString = response.readEntity(String.class);
327 @SuppressWarnings("unchecked")
328 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
329 return Double.parseDouble(jsonMap.get(statToGet).toString());