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.apps.uservice.test.adapt.restclient;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 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;
36 import org.glassfish.grizzly.http.server.HttpServer;
37 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
38 import org.glassfish.jersey.server.ResourceConfig;
39 import org.junit.AfterClass;
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;
47 import com.google.gson.Gson;
50 public class TestFile2REST {
51 private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
52 private static HttpServer server;
54 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57 private final PrintStream stdout = System.out;
58 private final PrintStream stderr = System.err;
61 public static void setUp() throws Exception {
62 final ResourceConfig rc = new ResourceConfig(TestRESTClientEndpoint.class);
63 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
65 while (!server.isStarted()) {
66 ThreadUtilities.sleep(50);
71 public static void tearDown() throws Exception {
76 public void testFileEventsPost() throws MessagingException, ApexException, IOException {
77 final Client client = ClientBuilder.newClient();
79 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPost.json"};
80 final ApexMain apexMain = new ApexMain(args);
82 // Wait for the required amount of events to be received or for 10 seconds
83 for (int i = 0; i < 100; i++) {
84 ThreadUtilities.sleep(100);
85 final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
86 .request("application/json").get();
88 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
89 final String responseString = response.readEntity(String.class);
91 @SuppressWarnings("unchecked")
92 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
93 if ((double) jsonMap.get("POST") == 100) {
102 public void testFileEventsPut() throws MessagingException, ApexException, IOException {
103 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPut.json"};
104 final ApexMain apexMain = new ApexMain(args);
106 final Client client = ClientBuilder.newClient();
108 // Wait for the required amount of events to be received or for 10 seconds
109 for (int i = 0; i < 100; i++) {
110 ThreadUtilities.sleep(100);
111 final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
112 .request("application/json").get();
114 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
115 final String responseString = response.readEntity(String.class);
117 @SuppressWarnings("unchecked")
118 final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
119 if ((double) jsonMap.get("PUT") == 100) {
128 public void testFileEventsNoURL() throws MessagingException, ApexException, IOException {
129 System.setOut(new PrintStream(outContent));
130 System.setErr(new PrintStream(errContent));
132 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
133 final ApexMain apexMain = new ApexMain(args);
135 ThreadUtilities.sleep(200);
138 final String outString = outContent.toString();
140 System.setOut(stdout);
141 System.setErr(stderr);
143 assertTrue(outString.contains(" no URL has been set for event sending on REST client"));
147 public void testFileEventsBadURL() throws MessagingException, ApexException, IOException {
148 System.setOut(new PrintStream(outContent));
149 System.setErr(new PrintStream(errContent));
151 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
152 final ApexMain apexMain = new ApexMain(args);
154 ThreadUtilities.sleep(200);
157 final String outString = outContent.toString();
159 System.setOut(stdout);
160 System.setErr(stderr);
162 assertTrue(outString.contains(
163 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
167 public void testFileEventsBadHTTPMethod() throws MessagingException, ApexException, IOException {
168 System.setOut(new PrintStream(outContent));
169 System.setErr(new PrintStream(errContent));
171 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadHTTPMethod.json"};
172 final ApexMain apexMain = new ApexMain(args);
174 ThreadUtilities.sleep(200);
177 final String outString = outContent.toString();
179 System.setOut(stdout);
180 System.setErr(stderr);
182 assertTrue(outString.contains(
183 "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supproted for event sending on REST client producer"));
187 public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
188 System.setOut(new PrintStream(outContent));
189 System.setErr(new PrintStream(errContent));
191 final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
192 final ApexMain apexMain = new ApexMain(args);
194 ThreadUtilities.sleep(500);
197 final String outString = outContent.toString();
199 System.setOut(stdout);
200 System.setErr(stderr);
202 assertTrue(outString.contains(
203 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\" using HTTP \"POST\" failed with status code 400"));