2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.google.gson.Gson;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 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.glassfish.grizzly.http.server.HttpServer;
39 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
40 import org.glassfish.jersey.server.ResourceConfig;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
46 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
47 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
48 import org.onap.policy.apex.service.engine.main.ApexMain;
49 import org.slf4j.ext.XLogger;
50 import org.slf4j.ext.XLoggerFactory;
53 * The Class TestFile2Rest.
55 public class TestFile2Rest {
56 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestFile2Rest.class);
58 private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
59 private static HttpServer server;
61 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
62 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
64 private final PrintStream stdout = System.out;
65 private final PrintStream stderr = System.err;
70 * @throws Exception the exception
73 public static void setUp() throws Exception {
74 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
75 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
77 while (!server.isStarted()) {
78 ThreadUtilities.sleep(50);
85 * @throws Exception the exception
88 public static void tearDown() throws Exception {
93 * Clear relative file root environment variable.
96 public void clearRelativeFileRoot() {
97 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
101 * Test file events post.
103 * @throws MessagingException the messaging exception
104 * @throws ApexException the apex exception
105 * @throws IOException Signals that an I/O exception has occurred.
108 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
109 final Client client = ClientBuilder.newClient();
111 final String[] args =
112 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2RESTJsonEventPost.json" };
113 final ApexMain apexMain = new ApexMain(args);
115 Response response = null;
117 // Wait for the required amount of events to be received or for 10 seconds
118 for (int i = 0; i < 100; i++) {
119 ThreadUtilities.sleep(100);
120 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
121 .request("application/json").get();
123 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
127 final String responseString = response.readEntity(String.class);
129 @SuppressWarnings("unchecked")
130 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
131 if ((double) jsonMap.get("POST") == 100) {
138 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
142 * Test file events put.
144 * @throws MessagingException the messaging exception
145 * @throws ApexException the apex exception
146 * @throws IOException Signals that an I/O exception has occurred.
149 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
150 final String[] args =
151 { "-rfr", "target", "-c", "target/examples/config/SampleDomain/File2RESTJsonEventPut.json" };
152 final ApexMain apexMain = new ApexMain(args);
154 final Client client = ClientBuilder.newClient();
156 Response response = null;
158 // Wait for the required amount of events to be received or for 10 seconds
159 for (int i = 0; i < 20; i++) {
160 ThreadUtilities.sleep(300);
161 response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
162 .request("application/json").get();
164 if (Response.Status.OK.getStatusCode() != response.getStatus()) {
168 final String responseString = response.readEntity(String.class);
170 @SuppressWarnings("unchecked")
171 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
172 if ((double) jsonMap.get("PUT") == 20) {
179 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
183 * Test file events no url.
185 * @throws MessagingException the messaging exception
186 * @throws ApexException the apex exception
187 * @throws IOException Signals that an I/O exception has occurred.
190 public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
191 System.setOut(new PrintStream(outContent));
192 System.setErr(new PrintStream(errContent));
194 final String[] args =
195 { "src/test/resources/prodcons/File2RESTJsonEventNoURL.json" };
196 final ApexMain apexMain = new ApexMain(args);
198 ThreadUtilities.sleep(200);
201 final String outString = outContent.toString();
203 System.setOut(stdout);
204 System.setErr(stderr);
206 LOGGER.info("NoUrl-OUTSTRING=\n" + outString + "\nEnd-NoUrl");
207 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
211 * Test file events bad url.
213 * @throws MessagingException the messaging exception
214 * @throws ApexException the apex exception
215 * @throws IOException Signals that an I/O exception has occurred.
218 public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
219 System.setOut(new PrintStream(outContent));
220 System.setErr(new PrintStream(errContent));
222 final String[] args =
223 { "src/test/resources/prodcons/File2RESTJsonEventBadURL.json" };
224 final ApexMain apexMain = new ApexMain(args);
226 ThreadUtilities.sleep(2000);
229 final String outString = outContent.toString();
231 System.setOut(stdout);
232 System.setErr(stderr);
234 LOGGER.info("BadUrl-OUTSTRING=\n" + outString + "\nEnd-BadUrl");
235 assertTrue(outString.contains(
236 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
240 * Test file events bad http method.
242 * @throws MessagingException the messaging exception
243 * @throws ApexException the apex exception
244 * @throws IOException Signals that an I/O exception has occurred.
247 public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
248 System.setOut(new PrintStream(outContent));
249 System.setErr(new PrintStream(errContent));
251 final String[] args =
252 { "src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json" };
253 final ApexMain apexMain = new ApexMain(args);
255 ThreadUtilities.sleep(200);
258 final String outString = outContent.toString();
260 System.setOut(stdout);
261 System.setErr(stderr);
263 LOGGER.info("BadHttpMethod-OUTSTRING=\n" + outString + "\nEnd-BadHttpMethod");
264 assertTrue(outString.contains(
265 "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
266 + "are supproted for event sending on REST client producer"));
270 * Test file events bad response.
272 * @throws MessagingException the messaging exception
273 * @throws ApexException the apex exception
274 * @throws IOException Signals that an I/O exception has occurred.
277 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
278 System.setOut(new PrintStream(outContent));
279 System.setErr(new PrintStream(errContent));
281 final String[] args =
282 { "src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json" };
283 final ApexMain apexMain = new ApexMain(args);
285 ThreadUtilities.sleep(2000);
288 final String outString = outContent.toString();
290 System.setOut(stdout);
291 System.setErr(stderr);
293 LOGGER.info("BadResponse-OUTSTRING=\n" + outString + "\nEnd-BadResponse");
294 assertTrue(outString.contains(
295 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\""
296 + " using HTTP \"POST\" failed with status code 400"));