2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.assertTrue;
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.net.ConnectException;
29 import java.net.MalformedURLException;
31 import java.util.UUID;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
36 import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * HttpServletServer JUNIT tests
43 public class HttpServerTest {
45 HttpServletServerFactory httpServletServerFactory = IndexedHttpServletServerFactory.getInstance();
50 private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
53 public void testSingleServer() throws Exception {
54 logger.info("-- testSingleServer() --");
56 HttpServletServer server = httpServletServerFactory.build("echo", "localhost", 5678, "/", false, true);
57 server.addServletPackage("/*", this.getClass().getPackage().getName());
58 server.waitedStart(5000);
60 assertTrue(httpServletServerFactory.get(5678).isAlive());
62 String response = http(httpServletServerFactory.get(5678), "http://localhost:5678/junit/echo/hello");
63 assertTrue("hello".equals(response));
67 response = http(httpServletServerFactory.get(5678), "http://localhost:5678/swagger.json");
68 } catch (IOException e) {
71 assertTrue(response == null);
73 assertTrue(httpServletServerFactory.get(5678).isAlive());
74 assertTrue(httpServletServerFactory.inventory().size() == 1);
76 httpServletServerFactory.destroy(5678);
77 assertTrue(httpServletServerFactory.inventory().size() == 0);
81 public void testMultipleServers() throws Exception {
82 logger.info("-- testMultipleServers() --");
84 HttpServletServer server1 = httpServletServerFactory.build("echo-1", "localhost", 5688, "/", true, true);
85 server1.addServletPackage("/*", this.getClass().getPackage().getName());
86 server1.waitedStart(5000);
88 HttpServletServer server2 = httpServletServerFactory.build("echo-2", "localhost", 5689, "/", false, true);
89 server2.addServletPackage("/*", this.getClass().getPackage().getName());
90 server2.waitedStart(5000);
92 assertTrue(httpServletServerFactory.get(5688).isAlive());
93 assertTrue(httpServletServerFactory.get(5689).isAlive());
95 String response = http(httpServletServerFactory.get(5688), "http://localhost:5688/junit/echo/hello");
96 assertTrue("hello".equals(response));
98 response = http(httpServletServerFactory.get(5688), "http://localhost:5688/swagger.json");
99 assertTrue(response != null);
101 response = http(httpServletServerFactory.get(5689), "http://localhost:5689/junit/echo/hello");
102 assertTrue("hello".equals(response));
106 response = http(httpServletServerFactory.get(5689), "http://localhost:5689/swagger.json");
107 } catch (IOException e) {
110 assertTrue(response == null);
112 httpServletServerFactory.destroy();
113 assertTrue(httpServletServerFactory.inventory().size() == 0);
117 public void testMultiServicePackage() throws Exception {
118 logger.info("-- testMultiServicePackage() --");
120 String randomName = UUID.randomUUID().toString();
122 HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5668, "/", false, true);
123 server.addServletPackage("/*", this.getClass().getPackage().getName());
124 server.waitedStart(5000);
126 assertTrue(httpServletServerFactory.get(5668).isAlive());
128 String response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/echo/hello");
129 assertTrue("hello".equals(response));
131 response = http(httpServletServerFactory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
132 assertTrue(response.contains(randomName));
134 httpServletServerFactory.destroy();
135 assertTrue(httpServletServerFactory.inventory().size() == 0);
139 public void testServiceClass() throws Exception {
140 logger.info("-- testServiceClass() --");
141 String randomName = UUID.randomUUID().toString();
143 HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5658, "/", false, true);
144 server.addServletClass("/*", RestEchoService.class.getCanonicalName());
145 server.waitedStart(5000);
147 assertTrue(httpServletServerFactory.get(5658).isAlive());
149 String response = http(httpServletServerFactory.get(5658), "http://localhost:5658/junit/echo/hello");
150 assertTrue("hello".equals(response));
152 httpServletServerFactory.destroy();
153 assertTrue(httpServletServerFactory.inventory().size() == 0);
157 public void testMultiServiceClass() throws Exception {
158 logger.info("-- testMultiServiceClass() --");
160 String randomName = UUID.randomUUID().toString();
162 HttpServletServer server = httpServletServerFactory.build(randomName, "localhost", 5648, "/", false, true);
163 server.addServletClass("/*", RestEchoService.class.getCanonicalName());
164 server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
165 server.waitedStart(5000);
167 assertTrue(httpServletServerFactory.get(5648).isAlive());
169 String response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/echo/hello");
170 assertTrue("hello".equals(response));
172 response = http(httpServletServerFactory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
173 assertTrue(response.contains(randomName));
175 httpServletServerFactory.destroy();
176 assertTrue(httpServletServerFactory.inventory().size() == 0);
180 * performs an http request
182 * @throws MalformedURLException
183 * @throws IOException
184 * @throws InterruptedException
186 protected String http(HttpServletServer server, String aUrl)
187 throws MalformedURLException, IOException, InterruptedException {
188 URL url = new URL(aUrl);
189 String response = null;
190 int numRetries = 1, maxNumberRetries = 5;
191 while (numRetries <= maxNumberRetries) {
193 response = response(url);
195 } catch (ConnectException e) {
196 logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, aUrl, numRetries, e);
198 Thread.sleep(10000L);
199 } catch (Exception e) {
212 * @throws IOException
214 protected String response(URL url) throws IOException {
215 String response = "";
216 try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
218 while ((line = ioReader.readLine()) != null) {