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.testsuites.integration.uservice.adapt.restclient;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import com.google.gson.Gson;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.core.Response;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
40 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
41 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
42 import org.onap.policy.apex.service.engine.main.ApexMain;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
44 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
45 import org.onap.policy.common.gson.GsonMessageBodyHandler;
46 import org.onap.policy.common.utils.network.NetworkUtil;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
51 * The Class TestFile2Rest.
53 public class TestFile2Rest {
54 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestFile2Rest.class);
56 private static final int PORT = 32801;
57 private static HttpServletServer server;
59 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
60 private final 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("TestFile2Rest", false, null, PORT,
73 "/TestFile2Rest", false, false);
75 server.addServletClass(null, TestRestClientEndpoint.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 {
98 * Clear relative file root environment variable.
101 public void clearRelativeFileRoot() {
102 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
106 * Test file events post.
108 * @throws MessagingException the messaging exception
109 * @throws ApexException the apex exception
110 * @throws IOException Signals that an I/O exception has occurred.
113 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
114 final Client client = ClientBuilder.newClient();
117 final String[] args = {
121 "target/examples/config/SampleDomain/File2RESTJsonEventPost.json"
124 final ApexMain apexMain = new ApexMain(args);
126 Response response = null;
128 // Wait for the required amount of events to be received or for 10 seconds
129 for (int i = 0; i < 100; i++) {
130 ThreadUtilities.sleep(100);
131 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
132 .request("application/json").get();
134 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
138 final String responseString = response.readEntity(String.class);
140 @SuppressWarnings("unchecked")
141 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
142 if ((double) jsonMap.get("POST") == 100) {
149 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
153 * Test file events put.
155 * @throws MessagingException the messaging exception
156 * @throws ApexException the apex exception
157 * @throws IOException Signals that an I/O exception has occurred.
160 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
162 final String[] args = {
166 "target/examples/config/SampleDomain/File2RESTJsonEventPut.json"
169 final ApexMain apexMain = new ApexMain(args);
171 final Client client = ClientBuilder.newClient();
173 Response response = null;
175 // Wait for the required amount of events to be received or for 10 seconds
176 for (int i = 0; i < 20; i++) {
177 ThreadUtilities.sleep(300);
178 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
179 .request("application/json").get();
181 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
185 final String responseString = response.readEntity(String.class);
187 @SuppressWarnings("unchecked")
188 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
189 if ((double) jsonMap.get("PUT") == 20) {
196 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
200 * Test file events no url.
202 * @throws MessagingException the messaging exception
203 * @throws ApexException the apex exception
204 * @throws IOException Signals that an I/O exception has occurred.
207 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
208 System.setOut(new PrintStream(outContent));
209 System.setErr(new PrintStream(errContent));
211 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
212 final ApexMain apexMain = new ApexMain(args);
214 ThreadUtilities.sleep(200);
217 final String outString = outContent.toString();
219 System.setOut(stdout);
220 System.setErr(stderr);
222 LOGGER.info("NoUrl-OUTSTRING=\n" + outString + "\nEnd-NoUrl");
223 assertTrue(outString.contains(" no URL has been set for event sending on RESTCLIENT"));
227 * Test file events bad url.
229 * @throws MessagingException the messaging exception
230 * @throws ApexException the apex exception
231 * @throws IOException Signals that an I/O exception has occurred.
234 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
235 System.setOut(new PrintStream(outContent));
236 System.setErr(new PrintStream(errContent));
238 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
239 final ApexMain apexMain = new ApexMain(args);
241 ThreadUtilities.sleep(2000);
244 final String outString = outContent.toString();
246 System.setOut(stdout);
247 System.setErr(stderr);
249 LOGGER.info("BadUrl-OUTSTRING=\n" + outString + "\nEnd-BadUrl");
250 assertTrue(outString.contains(
251 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
255 * Test file events bad http method.
257 * @throws MessagingException the messaging exception
258 * @throws ApexException the apex exception
259 * @throws IOException Signals that an I/O exception has occurred.
262 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
263 System.setOut(new PrintStream(outContent));
264 System.setErr(new PrintStream(errContent));
266 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json"};
267 final ApexMain apexMain = new ApexMain(args);
269 ThreadUtilities.sleep(200);
272 final String outString = outContent.toString();
274 System.setOut(stdout);
275 System.setErr(stderr);
277 LOGGER.info("BadHttpMethod-OUTSTRING=\n" + outString + "\nEnd-BadHttpMethod");
279 .contains("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
280 + "are supported for event sending on REST client producer"));
284 * Test file events bad response.
286 * @throws MessagingException the messaging exception
287 * @throws ApexException the apex exception
288 * @throws IOException Signals that an I/O exception has occurred.
291 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
292 System.setOut(new PrintStream(outContent));
293 System.setErr(new PrintStream(errContent));
295 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
296 final ApexMain apexMain = new ApexMain(args);
298 ThreadUtilities.sleep(2000);
301 final String outString = outContent.toString();
303 System.setOut(stdout);
304 System.setErr(stderr);
306 LOGGER.info("BadResponse-OUTSTRING=\n" + outString + "\nEnd-BadResponse");
307 assertTrue(outString.contains(
308 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
309 + " using HTTP \"POST\" failed with status code 400"));