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;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
34 import java.util.concurrent.TimeUnit;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import javax.ws.rs.core.Response;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.apex.service.engine.main.ApexMain;
47 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
49 import org.onap.policy.common.gson.GsonMessageBodyHandler;
50 import org.onap.policy.common.utils.network.NetworkUtil;
53 * The Class TestRestRequestor.
55 public class RestRequestorTest {
56 private static final int PORT = 32801;
57 private static HttpServletServer server;
59 private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
60 private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
62 private final PrintStream stdout = System.out;
63 private final PrintStream stderr = System.err;
68 * @throws Exception the exception
71 public static void setUp() throws Exception {
72 server = HttpServletServerFactoryInstance.getServerFactory().build(null, false, null, PORT,
73 "/TestRESTRequestor", false, false);
75 server.addServletClass(null, SupportRestRequestorEndpoint.class.getName());
76 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
80 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
81 throw new IllegalStateException("port " + PORT + " is still not in use");
88 * @throws Exception the exception
91 public static void tearDown() throws Exception {
101 public void resetCounters() {
102 SupportRestRequestorEndpoint.resetCounters();
106 * Test rest requestor get.
108 * @throws MessagingException the messaging exception
109 * @throws Exception an exception
112 public void testRestRequestorGet() throws Exception {
113 final Client client = ClientBuilder.newClient();
115 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"};
116 final ApexMain apexMain = new ApexMain(args);
117 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
119 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
120 .until(() -> getStatsFromServer(client, "GET") >= 50.0);
123 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
129 * Test rest requestor get empty.
131 * @throws MessagingException the messaging exception
132 * @throws ApexException the apex exception
133 * @throws IOException Signals that an I/O exception has occurred.
136 public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException {
137 final Client client = ClientBuilder.newClient();
139 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"};
140 final ApexMain apexMain = new ApexMain(args);
141 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
143 Response response = null;
145 // Wait for the required amount of events to be received or for 10 seconds
146 double getsSoFar = 0.0;
147 for (int i = 0; i < 40; i++) {
148 response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
149 .request("application/json").get();
151 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
155 final String responseString = response.readEntity(String.class);
157 @SuppressWarnings("unchecked")
158 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
159 getsSoFar = Double.parseDouble(jsonMap.get("GET").toString());
161 if (getsSoFar >= 50.0) {
167 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
171 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
175 * Test REST requestor put.
177 * @throws MessagingException the messaging exception
178 * @throws ApexException the apex exception
179 * @throws IOException Signals that an I/O exception has occurred.
182 public void testRestRequestorPut() throws MessagingException, ApexException, IOException {
183 final Client client = ClientBuilder.newClient();
185 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"};
186 final ApexMain apexMain = new ApexMain(args);
187 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
189 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
190 .until(() -> getStatsFromServer(client, "PUT") >= 50.0);
193 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
199 * Test REST requestor post.
201 * @throws MessagingException the messaging exception
202 * @throws ApexException the apex exception
203 * @throws IOException Signals that an I/O exception has occurred.
206 public void testRestRequestorPost() throws MessagingException, ApexException, IOException {
207 final Client client = ClientBuilder.newClient();
209 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"};
210 final ApexMain apexMain = new ApexMain(args);
211 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
213 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
214 .until(() -> getStatsFromServer(client, "POST") >= 50.0);
217 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
223 * Test REST requestor delete.
225 * @throws MessagingException the messaging exception
226 * @throws ApexException the apex exception
227 * @throws IOException Signals that an I/O exception has occurred.
230 public void testRestRequestorDelete() throws MessagingException, ApexException, IOException {
231 final Client client = ClientBuilder.newClient();
233 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"};
234 final ApexMain apexMain = new ApexMain(args);
235 await().atMost(2, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
237 // Wait for the required amount of events to be received
238 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
239 .until(() -> getStatsFromServer(client, "DELETE") >= 50.0);
242 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
248 * Test REST requestor multi inputs.
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 testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException {
256 final Client client = ClientBuilder.newClient();
258 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"};
259 final ApexMain apexMain = new ApexMain(args);
260 await().atMost(10, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
262 await().pollInterval(300, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS)
263 .until(() -> getStatsFromServer(client, "GET") >= 8.0);
266 await().atMost(2, TimeUnit.SECONDS).until(() -> !apexMain.isAlive());
272 * Test REST requestor producer alone.
274 * @throws MessagingException the messaging exception
275 * @throws ApexException the apex exception
276 * @throws IOException Signals that an I/O exception has occurred.
279 public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException {
280 System.setOut(new PrintStream(outContent));
281 System.setErr(new PrintStream(errContent));
283 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"};
285 ApexMain apexMain = new ApexMain(args);
288 final String outString = outContent.toString();
290 System.setOut(stdout);
291 System.setErr(stderr);
293 assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) "
294 + "must run in peered requestor mode with a REST Requestor consumer"));
298 * Test REST requestor consumer alone.
300 * @throws MessagingException the messaging exception
301 * @throws ApexException the apex exception
302 * @throws IOException Signals that an I/O exception has occurred.
305 public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
306 System.setOut(new PrintStream(outContent));
307 System.setErr(new PrintStream(errContent));
309 final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"};
311 ApexMain apexMain = new ApexMain(args);
314 final String outString = outContent.toString();
316 System.setOut(stdout);
317 System.setErr(stderr);
319 assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR "
320 + "does not exist or is not defined with the same peered mode"));
323 private double getStatsFromServer(final Client client, final String statToGet) {
324 final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
325 .request("application/json").get();
327 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
328 final String responseString = response.readEntity(String.class);
330 @SuppressWarnings("unchecked")
331 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
332 return Double.parseDouble(jsonMap.get(statToGet).toString());