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