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