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;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.core.Response;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
43 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.service.engine.main.ApexMain;
46 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
47 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
48 import org.onap.policy.common.gson.GsonMessageBodyHandler;
49 import org.onap.policy.common.utils.network.NetworkUtil;
50 import org.slf4j.ext.XLogger;
51 import org.slf4j.ext.XLoggerFactory;
54 * The Class TestFile2Rest.
56 public class TestFile2Rest {
57 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestFile2Rest.class);
59 private static final int PORT = 32801;
60 private static HttpServletServer server;
62 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
63 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
65 private final PrintStream stdout = System.out;
66 private final PrintStream stderr = System.err;
71 * @throws Exception the exception
74 public static void setUp() throws Exception {
75 server = HttpServletServerFactoryInstance.getServerFactory().build("TestFile2Rest", false, null, PORT,
76 "/TestFile2Rest", false, false);
78 server.addServletClass(null, TestRestClientEndpoint.class.getName());
79 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
83 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
84 throw new IllegalStateException("port " + PORT + " is still not in use");
91 * @throws Exception the exception
94 public static void tearDown() throws Exception {
101 * Clear relative file root environment variable.
104 public void clearRelativeFileRoot() {
105 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
109 * Test file events post.
111 * @throws MessagingException the messaging exception
112 * @throws ApexException the apex exception
113 * @throws IOException Signals that an I/O exception has occurred.
116 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
117 final Client client = ClientBuilder.newClient();
120 final String[] args = {
124 "target/examples/config/SampleDomain/File2RESTJsonEventPost.json"
127 final ApexMain apexMain = new ApexMain(args);
129 Response response = null;
131 // Wait for the required amount of events to be received or for 10 seconds
132 for (int i = 0; i < 100; i++) {
133 ThreadUtilities.sleep(100);
134 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
135 .request("application/json").get();
137 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
141 final String responseString = response.readEntity(String.class);
143 @SuppressWarnings("unchecked")
144 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
145 if ((double) jsonMap.get("POST") == 100) {
152 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
156 * Test file events put.
158 * @throws MessagingException the messaging exception
159 * @throws ApexException the apex exception
160 * @throws IOException Signals that an I/O exception has occurred.
163 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
165 final String[] args = {
169 "target/examples/config/SampleDomain/File2RESTJsonEventPut.json"
172 final ApexMain apexMain = new ApexMain(args);
174 final Client client = ClientBuilder.newClient();
176 Response response = null;
178 // Wait for the required amount of events to be received or for 10 seconds
179 for (int i = 0; i < 20; i++) {
180 ThreadUtilities.sleep(300);
181 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
182 .request("application/json").get();
184 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
188 final String responseString = response.readEntity(String.class);
190 @SuppressWarnings("unchecked")
191 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
192 if ((double) jsonMap.get("PUT") == 20) {
199 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
203 * Test file events no url.
205 * @throws MessagingException the messaging exception
206 * @throws ApexException the apex exception
207 * @throws IOException Signals that an I/O exception has occurred.
210 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
211 System.setOut(new PrintStream(outContent));
212 System.setErr(new PrintStream(errContent));
214 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
215 final ApexMain apexMain = new ApexMain(args);
217 ThreadUtilities.sleep(200);
220 final String outString = outContent.toString();
222 System.setOut(stdout);
223 System.setErr(stderr);
225 LOGGER.info("NoUrl-OUTSTRING=\n" + outString + "\nEnd-NoUrl");
226 assertTrue(outString.contains(" no URL has been set for event sending on RESTCLIENT"));
230 * Test file events bad url.
232 * @throws MessagingException the messaging exception
233 * @throws ApexException the apex exception
234 * @throws IOException Signals that an I/O exception has occurred.
237 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
238 System.setOut(new PrintStream(outContent));
239 System.setErr(new PrintStream(errContent));
241 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
242 final ApexMain apexMain = new ApexMain(args);
244 ThreadUtilities.sleep(2000);
247 final String outString = outContent.toString();
249 System.setOut(stdout);
250 System.setErr(stderr);
252 LOGGER.info("BadUrl-OUTSTRING=\n" + outString + "\nEnd-BadUrl");
253 assertTrue(outString.contains(
254 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
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 {
266 System.setOut(new PrintStream(outContent));
267 System.setErr(new PrintStream(errContent));
269 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json"};
270 final ApexMain apexMain = new ApexMain(args);
272 ThreadUtilities.sleep(200);
275 final String outString = outContent.toString();
277 System.setOut(stdout);
278 System.setErr(stderr);
280 LOGGER.info("BadHttpMethod-OUTSTRING=\n" + outString + "\nEnd-BadHttpMethod");
282 .contains("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
283 + "are supported for event sending on REST client producer"));
287 * Test file events bad response.
289 * @throws MessagingException the messaging exception
290 * @throws ApexException the apex exception
291 * @throws IOException Signals that an I/O exception has occurred.
294 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
295 System.setOut(new PrintStream(outContent));
296 System.setErr(new PrintStream(errContent));
298 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
299 final ApexMain apexMain = new ApexMain(args);
301 ThreadUtilities.sleep(2000);
304 final String outString = outContent.toString();
306 System.setOut(stdout);
307 System.setErr(stderr);
309 LOGGER.info("BadResponse-OUTSTRING=\n" + outString + "\nEnd-BadResponse");
310 assertTrue(outString.contains(
311 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
312 + " using HTTP \"POST\" failed with status code 400"));