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.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;
50 * The Class TestFile2Rest.
52 public class TestFile2Rest {
53 private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
54 private static HttpServer server;
56 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59 private final PrintStream stdout = System.out;
60 private final PrintStream stderr = System.err;
65 * @throws Exception the exception
68 public static void setUp() throws Exception {
69 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
70 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
72 while (!server.isStarted()) {
73 ThreadUtilities.sleep(50);
80 * @throws Exception the exception
83 public static void tearDown() throws Exception {
88 * Test file events post.
90 * @throws MessagingException the messaging exception
91 * @throws ApexException the apex exception
92 * @throws IOException Signals that an I/O exception has occurred.
95 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
96 final Client client = ClientBuilder.newClient();
99 { "src/test/resources/prodcons/File2RESTJsonEventPost.json" };
100 final ApexMain apexMain = new ApexMain(args);
102 // Wait for the required amount of events to be received or for 10 seconds
103 for (int i = 0; i < 100; i++) {
104 ThreadUtilities.sleep(100);
105 final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
106 .request("application/json").get();
108 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
109 final String responseString = response.readEntity(String.class);
111 @SuppressWarnings("unchecked")
112 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
113 if ((double) jsonMap.get("POST") == 100) {
122 * Test file events put.
124 * @throws MessagingException the messaging exception
125 * @throws ApexException the apex exception
126 * @throws IOException Signals that an I/O exception has occurred.
129 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
130 final String[] args =
131 { "src/test/resources/prodcons/File2RESTJsonEventPut.json" };
132 final ApexMain apexMain = new ApexMain(args);
134 final Client client = ClientBuilder.newClient();
136 // Wait for the required amount of events to be received or for 10 seconds
137 for (int i = 0; i < 100; i++) {
138 ThreadUtilities.sleep(100);
139 final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
140 .request("application/json").get();
142 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
143 final String responseString = response.readEntity(String.class);
145 @SuppressWarnings("unchecked")
146 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
147 if ((double) jsonMap.get("PUT") == 100) {
156 * Test file events no url.
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 testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
164 System.setOut(new PrintStream(outContent));
165 System.setErr(new PrintStream(errContent));
167 final String[] args =
168 { "src/test/resources/prodcons/File2RESTJsonEventNoURL.json" };
169 final ApexMain apexMain = new ApexMain(args);
171 ThreadUtilities.sleep(200);
174 final String outString = outContent.toString();
176 System.setOut(stdout);
177 System.setErr(stderr);
179 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
183 * Test file events bad 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 testFileEventsBadUrl() 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/File2RESTJsonEventBadURL.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 assertTrue(outString.contains(
207 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
211 * Test file events bad http method.
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 testFileEventsBadHttpMethod() 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/File2RESTJsonEventBadHTTPMethod.json" };
224 final ApexMain apexMain = new ApexMain(args);
226 ThreadUtilities.sleep(200);
229 final String outString = outContent.toString();
231 System.setOut(stdout);
232 System.setErr(stderr);
234 assertTrue(outString.contains(
235 "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
236 + "are supproted for event sending on REST client producer"));
240 * Test file events bad response.
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 testFileEventsBadResponse() 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/File2RESTJsonEventPostBadResponse.json" };
253 final ApexMain apexMain = new ApexMain(args);
255 ThreadUtilities.sleep(500);
258 final String outString = outContent.toString();
260 System.setOut(stdout);
261 System.setErr(stderr);
263 assertTrue(outString.contains(
264 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\" using HTTP \"POST\" failed with status code 400"));