2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
30 import com.google.gson.Gson;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
35 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;
39 import org.junit.After;
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.core.infrastructure.threading.ThreadUtilities;
46 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
47 import org.onap.policy.apex.service.engine.main.ApexMain;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
49 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
50 import org.onap.policy.common.gson.GsonMessageBodyHandler;
51 import org.onap.policy.common.utils.network.NetworkUtil;
52 import org.slf4j.ext.XLogger;
53 import org.slf4j.ext.XLoggerFactory;
56 * The Class TestFile2Rest.
58 public class TestFile2Rest {
59 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestFile2Rest.class);
61 private static final int PORT = 32801;
62 private static HttpServletServer server;
64 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
65 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
67 private final PrintStream stdout = System.out;
68 private final PrintStream stderr = System.err;
69 private ApexMain apexMain;
74 * @throws Exception the exception
77 public static void setUp() throws Exception {
78 server = HttpServletServerFactoryInstance.getServerFactory().build("TestFile2Rest", false, null, PORT,
79 "/TestFile2Rest", false, false);
81 server.addServletClass(null, TestRestClientEndpoint.class.getName());
82 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
86 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
87 throw new IllegalStateException("port " + PORT + " is still not in use");
94 * @throws Exception the exception
97 public static void tearDown() throws Exception {
107 public void beforeTest() {
108 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
109 System.setOut(new PrintStream(outContent));
110 System.setErr(new PrintStream(errContent));
115 * @throws ApexException the exception.
118 public void afterTest() throws ApexException {
119 if (null != apexMain) {
122 System.setOut(stdout);
123 System.setErr(stderr);
127 * Test file events post.
129 * @throws MessagingException the messaging exception
130 * @throws ApexException the apex exception
131 * @throws IOException Signals that an I/O exception has occurred.
134 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
135 final Client client = ClientBuilder.newClient();
138 final String[] args = {
142 "target/examples/config/SampleDomain/File2RESTJsonEventPost.json"
145 final ApexMain apexMain = new ApexMain(args);
147 Response response = null;
149 // Wait for the required amount of events to be received or for 10 seconds
150 for (int i = 0; i < 100; i++) {
151 ThreadUtilities.sleep(100);
152 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
153 .request("application/json").get();
155 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
159 final String responseString = response.readEntity(String.class);
161 @SuppressWarnings("unchecked")
162 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
163 if ((double) jsonMap.get("POST") == 100) {
170 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
174 * Test file events put.
176 * @throws MessagingException the messaging exception
177 * @throws ApexException the apex exception
178 * @throws IOException Signals that an I/O exception has occurred.
181 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
183 final String[] args = {
187 "target/examples/config/SampleDomain/File2RESTJsonEventPut.json"
190 final ApexMain apexMain = new ApexMain(args);
192 final Client client = ClientBuilder.newClient();
194 Response response = null;
196 // Wait for the required amount of events to be received or for 10 seconds
197 for (int i = 0; i < 20; i++) {
198 ThreadUtilities.sleep(300);
199 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
200 .request("application/json").get();
202 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
206 final String responseString = response.readEntity(String.class);
208 @SuppressWarnings("unchecked")
209 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
210 if ((double) jsonMap.get("PUT") == 20) {
217 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
221 * Test file events no url.
223 * @throws MessagingException the messaging exception
224 * @throws ApexException the apex exception
225 * @throws IOException Signals that an I/O exception has occurred.
228 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
230 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
231 apexMain = new ApexMain(args);
232 final String outString = outContent.toString();
233 LOGGER.info("NoUrl-OUTSTRING=\n {} \nEnd-NoUrl", outString);
234 assertThat(outString).contains(" no URL has been set for event sending on RESTCLIENT");
238 * Test file events bad url.
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 testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
247 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
248 apexMain = new ApexMain(args);
249 await().atMost(5, TimeUnit.SECONDS)
250 .until(() -> outContent.toString()
251 .contains("send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" "
252 + "using HTTP \"POST\" failed with status code 404"));
253 assertTrue(apexMain.isAlive());
254 LOGGER.info("BadUrl-OUTSTRING=\n {} \nEnd-BadUrl", outContent.toString());
258 * Test file events bad http method.
260 * @throws MessagingException the messaging exception
261 * @throws ApexException the apex exception
262 * @throws IOException Signals that an I/O exception has occurred.
265 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
267 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json"};
268 apexMain = new ApexMain(args);
269 final String outString = outContent.toString();
270 LOGGER.info("BadHttpMethod-OUTSTRING=\n {} \nEnd-BadHttpMethod", outString);
271 assertThat(outString)
272 .contains("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
273 + "are supported for event sending on REST client producer");
277 * Test file events bad response.
279 * @throws MessagingException the messaging exception
280 * @throws ApexException the apex exception
281 * @throws IOException Signals that an I/O exception has occurred.
284 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
286 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
287 apexMain = new ApexMain(args);
289 await().atMost(5, TimeUnit.SECONDS)
290 .until(() -> outContent.toString().contains(
291 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
292 + " using HTTP \"POST\" failed with status code 400"));
293 assertTrue(apexMain.isAlive());
294 LOGGER.info("BadResponse-OUTSTRING=\n {} \nEnd-BadResponse", outContent.toString());