0db6cfe137512ffcc3fb1ea77cbf9be2686b94d7
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.http.server.test;
22
23 import static org.junit.Assert.assertTrue;
24
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;
30 import java.net.URL;
31 import java.util.UUID;
32
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * HttpServletServer JUNIT tests
40  */
41 public class HttpServerTest {
42
43     /**
44      * Logger
45      */
46     private static Logger logger = LoggerFactory.getLogger(HttpServerTest.class);
47
48     @Test
49     public void testSingleServer() throws Exception {
50         logger.info("-- testSingleServer() --");
51
52         HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", false, true);
53         server.addServletPackage("/*", this.getClass().getPackage().getName());
54         server.waitedStart(5000);
55
56         assertTrue(HttpServletServer.factory.get(5678).isAlive());
57
58         String response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello");
59         assertTrue("hello".equals(response));
60
61         response = null;
62         try {
63             response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/swagger.json");
64         } catch (IOException e) {
65             // Expected
66         }
67         assertTrue(response == null);
68
69         assertTrue(HttpServletServer.factory.get(5678).isAlive());
70         assertTrue(HttpServletServer.factory.inventory().size() == 1);
71
72         HttpServletServer.factory.destroy(5678);
73         assertTrue(HttpServletServer.factory.inventory().size() == 0);
74     }
75
76     @Test
77     public void testMultipleServers() throws Exception {
78         logger.info("-- testMultipleServers() --");
79
80         HttpServletServer server1 = HttpServletServer.factory.build("echo-1", false,"localhost", 5688, "/", true, true);
81         server1.addServletPackage("/*", this.getClass().getPackage().getName());
82         server1.waitedStart(5000);
83
84         HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5689, "/", false, true);
85         server2.addServletPackage("/*", this.getClass().getPackage().getName());
86         server2.waitedStart(5000);
87
88         assertTrue(HttpServletServer.factory.get(5688).isAlive());
89         assertTrue(HttpServletServer.factory.get(5689).isAlive());
90
91         String response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/junit/echo/hello");
92         assertTrue("hello".equals(response));
93
94         response = http(HttpServletServer.factory.get(5688), "http://localhost:5688/swagger.json");
95         assertTrue(response != null);
96
97         response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/junit/echo/hello");
98         assertTrue("hello".equals(response));
99
100         response = null;
101         try {
102             response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/swagger.json");
103         } catch (IOException e) {
104             // Expected
105         }
106         assertTrue(response == null);
107
108         HttpServletServer.factory.destroy();
109         assertTrue(HttpServletServer.factory.inventory().size() == 0);
110     }
111
112     @Test
113     public void testMultiServicePackage() throws Exception {
114         logger.info("-- testMultiServicePackage() --");
115
116         String randomName = UUID.randomUUID().toString();
117
118         HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5668, "/", false, true);
119         server.addServletPackage("/*", this.getClass().getPackage().getName());
120         server.waitedStart(5000);
121
122         assertTrue(HttpServletServer.factory.get(5668).isAlive());
123
124         String response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/echo/hello");
125         assertTrue("hello".equals(response));
126
127         response = http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
128         assertTrue(response.contains(randomName));
129
130         HttpServletServer.factory.destroy();
131         assertTrue(HttpServletServer.factory.inventory().size() == 0);
132     }
133
134     @Test
135     public void testServiceClass() throws Exception {
136         logger.info("-- testServiceClass() --");
137         String randomName = UUID.randomUUID().toString();
138
139         HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5658, "/", false, true);
140         server.addServletClass("/*", RestEchoService.class.getCanonicalName());
141         server.waitedStart(5000);
142
143         assertTrue(HttpServletServer.factory.get(5658).isAlive());
144
145         String response = http(HttpServletServer.factory.get(5658), "http://localhost:5658/junit/echo/hello");
146         assertTrue("hello".equals(response));
147
148         HttpServletServer.factory.destroy();
149         assertTrue(HttpServletServer.factory.inventory().size() == 0);
150     }
151
152     @Test
153     public void testMultiServiceClass() throws Exception {
154         logger.info("-- testMultiServiceClass() --");
155
156         String randomName = UUID.randomUUID().toString();
157
158         HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5648, "/", false, true);
159         server.addServletClass("/*", RestEchoService.class.getCanonicalName());
160         server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
161         server.waitedStart(5000);
162
163         assertTrue(HttpServletServer.factory.get(5648).isAlive());
164
165         String response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/echo/hello");
166         assertTrue("hello".equals(response));
167
168         response = http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
169         assertTrue(response.contains(randomName));
170
171         HttpServletServer.factory.destroy();
172         assertTrue(HttpServletServer.factory.inventory().size() == 0);
173     }
174
175     /**
176      * performs an http request
177      * 
178      * @throws MalformedURLException
179      * @throws IOException
180      * @throws InterruptedException
181      */
182     protected String http(HttpServletServer server, String aUrl)
183             throws MalformedURLException, IOException, InterruptedException {
184         URL url = new URL(aUrl);
185         String response = null;
186         int numRetries = 1, maxNumberRetries = 5;
187         while (numRetries <= maxNumberRetries) {
188             try {
189                 response = response(url);
190                 break;
191             } catch (ConnectException e) {
192                 logger.warn("http server {} @ {} ({}) - cannot connect yet ..", server, aUrl, numRetries, e);
193                 numRetries++;
194                 Thread.sleep(10000L);
195             } catch (Exception e) {
196                 throw e;
197             }
198         }
199
200         return response;
201     }
202
203     /**
204      * gets http response
205      * 
206      * @param url url
207      * 
208      * @throws IOException
209      */
210     protected String response(URL url) throws IOException {
211         String response = "";
212         try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
213             String line;
214             while ((line = ioReader.readLine()) != null) {
215                 response += line;
216             }
217         }
218         return response;
219     }
220
221
222
223 }