2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.http.server.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.net.ConnectException;
31 import java.net.MalformedURLException;
33 import java.util.UUID;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * HttpServletServer JUNIT tests.
43 public class HttpServerTest {
48 private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
51 public void testSingleServer() throws Exception {
52 logger.info("-- testSingleServer() --");
54 HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", false, true);
55 server.addServletPackage("/*", this.getClass().getPackage().getName());
56 server.addFilterClass("/*", TestFilter.class.getCanonicalName());
57 server.waitedStart(5000);
59 assertTrue(HttpServletServer.factory.get(5678).isAlive());
60 assertFalse(HttpServletServer.factory.get(5678).isAaf());
62 String response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello");
63 assertTrue("hello".equals(response));
67 response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/swagger.json");
68 } catch (IOException e) {
71 assertTrue(response == null);
73 response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello?block=true");
74 assertEquals("FILTERED", response);
76 assertTrue(HttpServletServer.factory.get(5678).isAlive());
77 assertTrue(HttpServletServer.factory.inventory().size() == 1);
79 server.setAafAuthentication("/*");
80 assertTrue(HttpServletServer.factory.get(5678).isAaf());
82 HttpServletServer.factory.destroy(5678);
83 assertTrue(HttpServletServer.factory.inventory().size() == 0);
87 public void testMultipleServers() throws Exception {
88 logger.info("-- testMultipleServers() --");
90 HttpServletServer server1 = HttpServletServer.factory.build("echo-1", false,"localhost", 5688, "/", true, true);
91 server1.addServletPackage("/*", this.getClass().getPackage().getName());
92 server1.waitedStart(5000);
94 HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5689, "/", false, true);
95 server2.addServletPackage("/*", this.getClass().getPackage().getName());
96 server2.waitedStart(5000);
98 assertTrue(HttpServletServer.factory.get(5688).isAlive());
99 assertTrue(HttpServletServer.factory.get(5689).isAlive());
101 String response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/junit/echo/hello");
102 assertTrue("hello".equals(response));
104 response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/swagger.json");
105 assertTrue(response != null);
107 response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/junit/echo/hello");
108 assertTrue("hello".equals(response));
112 response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/swagger.json");
113 } catch (IOException e) {
116 assertTrue(response == null);
118 HttpServletServer.factory.destroy();
119 assertTrue(HttpServletServer.factory.inventory().size() == 0);
123 public void testMultiServicePackage() throws Exception {
124 logger.info("-- testMultiServicePackage() --");
126 String randomName = UUID.randomUUID().toString();
128 HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5668, "/", false, true);
129 server.addServletPackage("/*", this.getClass().getPackage().getName());
130 server.waitedStart(5000);
132 assertTrue(HttpServletServer.factory.get(5668).isAlive());
134 String response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/echo/hello");
135 assertTrue("hello".equals(response));
137 response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
138 assertTrue(response.contains(randomName));
140 HttpServletServer.factory.destroy();
141 assertTrue(HttpServletServer.factory.inventory().size() == 0);
145 public void testServiceClass() throws Exception {
146 logger.info("-- testServiceClass() --");
147 String randomName = UUID.randomUUID().toString();
149 HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5658, "/", false, true);
150 server.addServletClass("/*", RestEchoService.class.getCanonicalName());
151 server.waitedStart(5000);
153 assertTrue(HttpServletServer.factory.get(5658).isAlive());
155 String response = http(HttpServletServer.factory.get(5658), "http://localhost:5658/junit/echo/hello");
156 assertTrue("hello".equals(response));
158 HttpServletServer.factory.destroy();
159 assertTrue(HttpServletServer.factory.inventory().size() == 0);
163 public void testMultiServiceClass() throws Exception {
164 logger.info("-- testMultiServiceClass() --");
166 String randomName = UUID.randomUUID().toString();
168 HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5648, "/", false, true);
169 server.addServletClass("/*", RestEchoService.class.getCanonicalName());
170 server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
171 server.waitedStart(5000);
173 assertTrue(HttpServletServer.factory.get(5648).isAlive());
175 String response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/echo/hello");
176 assertTrue("hello".equals(response));
178 response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
179 assertTrue(response.contains(randomName));
181 HttpServletServer.factory.destroy();
182 assertTrue(HttpServletServer.factory.inventory().size() == 0);
186 * performs an http request.
188 * @throws MalformedURLException make sure URL is good
189 * @throws IOException thrown is IO exception occurs
190 * @throws InterruptedException thrown if thread interrupted occurs
192 protected String http(HttpServletServer server, String urlString)
193 throws MalformedURLException, IOException, InterruptedException {
194 URL url = new URL(urlString);
195 String response = null;
197 int maxNumberRetries = 5;
198 while (numRetries <= maxNumberRetries) {
200 response = response(url);
202 } catch (ConnectException e) {
203 logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, urlString, numRetries, e);
205 Thread.sleep(10000L);
206 } catch (Exception e) {
215 * gets http response.
219 * @throws IOException if an I/O error occurs
221 protected String response(URL url) throws IOException {
222 String response = "";
223 try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
225 while ((line = ioReader.readLine()) != null) {