1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*-
* ============LICENSE_START=======================================================
* policy-endpoints
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.policy.drools.http.server.test;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.UUID;
import org.junit.Test;
import org.openecomp.policy.drools.http.server.HttpServletServer;
/**
* HttpServletServer JUNIT tests
*/
public class HttpServerTest {
@Test
public void testSingleServer() throws Exception {
System.out.println("-- testSingleServer() --");
HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", false, true);
server.addServletPackage("/*", this.getClass().getPackage().getName());
server.waitedStart(5000);
assertTrue(HttpServletServer.factory.get(5678).isAlive());
String echo = "hello";
URL url = new URL("http://localhost:5678/junit/echo/" + echo);
String response = response(url);
System.out.println("Received .. " + response);
assertTrue(response.equals(echo));
String responseSwagger = null;
try {
URL urlSwagger = new URL("http://localhost:5678/swagger.json" + echo);
responseSwagger = response(urlSwagger);
} catch(IOException ioe) {
// Expected
}
assertTrue(responseSwagger == null);
HttpServletServer.factory.destroy();
assertTrue(HttpServletServer.factory.inventory().size() == 0);
}
@Test
public void testMultipleServers() throws Exception {
System.out.println("-- testMultipleServers() --");
HttpServletServer server1 = HttpServletServer.factory.build("echo-1", "localhost", 5678, "/", true, true);
server1.addServletPackage("/*", this.getClass().getPackage().getName());
server1.waitedStart(5000);
HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5679, "/", false, true);
server2.addServletPackage("/*", this.getClass().getPackage().getName());
server2.waitedStart(5000);
assertTrue(HttpServletServer.factory.get(5678).isAlive());
assertTrue(HttpServletServer.factory.get(5679).isAlive());
String echo = "hello";
URL url1 = new URL("http://localhost:5678/junit/echo/" + echo);
String response1 = response(url1);
System.out.println("Received .. " + response1);
assertTrue(response1.equals(echo));
URL urlSwagger = new URL("http://localhost:5678/swagger.json");
String responseSwagger = response(urlSwagger);
System.out.println("Received .. " + responseSwagger);
assertTrue(responseSwagger != null);
URL url2 = new URL("http://localhost:5679/junit/echo/" + echo);
String response2 = response(url2);
System.out.println("Received .. " + response2);
assertTrue(response2.equals(echo));
String responseSwagger2 = null;
try {
URL urlSwagger2 = new URL("http://localhost:5679/swagger.json");
responseSwagger2 = response(urlSwagger2);
} catch(IOException ioe) {
// Expected
}
System.out.println("Received .. " + responseSwagger2);
assertTrue(responseSwagger2 == null);
HttpServletServer.factory.destroy();
assertTrue(HttpServletServer.factory.inventory().size() == 0);
}
@Test
public void testMultiServicePackage() throws Exception {
System.out.println("-- testMultiServicePackage() --");
String randomName = UUID.randomUUID().toString();
HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
server.addServletPackage("/*", this.getClass().getPackage().getName());
server.waitedStart(5000);
assertTrue(HttpServletServer.factory.get(5678).isAlive());
String echo = "hello";
URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
String responseService1 = response(urlService1);
System.out.println("Received .. " + responseService1);
assertTrue(responseService1.equals(echo));
URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers");
String responseService2 = response(urlService2);
System.out.println("Received .. " + responseService2);
assertTrue(responseService2.contains(randomName));
HttpServletServer.factory.destroy();
assertTrue(HttpServletServer.factory.inventory().size() == 0);
}
@Test
public void testServiceClass() throws Exception {
System.out.println("-- testServiceClass() --");
String randomName = UUID.randomUUID().toString();
HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
server.addServletClass("/*", RestEchoService.class.getCanonicalName());
server.waitedStart(5000);
assertTrue(HttpServletServer.factory.get(5678).isAlive());
String echo = "hello";
URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
String responseService1 = response(urlService1);
System.out.println("Received .. " + responseService1);
assertTrue(responseService1.equals(echo));
HttpServletServer.factory.destroy();
assertTrue(HttpServletServer.factory.inventory().size() == 0);
}
@Test
public void testMultiServiceClass() throws Exception {
System.out.println("-- testMultiServiceClass() --");
String randomName = UUID.randomUUID().toString();
HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
server.addServletClass("/*", RestEchoService.class.getCanonicalName());
server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
server.waitedStart(5000);
assertTrue(HttpServletServer.factory.get(5678).isAlive());
String echo = "hello";
URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
String responseService1 = response(urlService1);
System.out.println("Received .. " + responseService1);
assertTrue(responseService1.equals(echo));
URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers");
String responseService2 = response(urlService2);
System.out.println("Received .. " + responseService2);
assertTrue(responseService2.contains(randomName));
HttpServletServer.factory.destroy();
assertTrue(HttpServletServer.factory.inventory().size() == 0);
}
/**
* @param url
* @throws IOException
*/
protected String response(URL url) throws IOException {
BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()));
String response = "";
String line;
while ((line = ioReader.readLine()) != null) {
response += line;
}
System.out.println("R is " + response);
ioReader.close();
return response;
}
}
|