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