1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
16 | |
---|
17 | import static org.junit.Assert.assertEquals; |
---|
18 | import static org.junit.Assert.assertFalse; |
---|
19 | import static org.junit.Assert.assertNotNull; |
---|
20 | import static org.junit.Assert.assertTrue; |
---|
21 | |
---|
22 | import java.io.File; |
---|
23 | import java.io.StringWriter; |
---|
24 | import java.net.URL; |
---|
25 | import java.util.Collection; |
---|
26 | import java.util.Iterator; |
---|
27 | import java.util.List; |
---|
28 | |
---|
29 | import javax.xml.bind.JAXBContext; |
---|
30 | import javax.xml.bind.Marshaller; |
---|
31 | import javax.xml.namespace.QName; |
---|
32 | |
---|
33 | import org.apache.http.HttpEntity; |
---|
34 | import org.apache.http.client.methods.HttpPost; |
---|
35 | import org.apache.http.entity.ContentType; |
---|
36 | import org.apache.http.entity.StringEntity; |
---|
37 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
38 | import org.junit.Test; |
---|
39 | |
---|
40 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
41 | import de.ugoe.cs.autoquest.http.HTTPTestUtils; |
---|
42 | import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; |
---|
43 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
44 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
45 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
46 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
47 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
48 | import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory; |
---|
49 | import de.ugoe.cs.autoquest.plugin.http.logdata.Session; |
---|
50 | import dummyservice.DummyService; |
---|
51 | import dummyservice.DummyServicePortType; |
---|
52 | import dummyservice.VerifyUserInMsgType; |
---|
53 | import dummyservice.VerifyUserOutMsgType; |
---|
54 | |
---|
55 | /** |
---|
56 | * @author Patrick Harms |
---|
57 | */ |
---|
58 | public class HttpMonitorTest extends AbstractTC { |
---|
59 | |
---|
60 | /** |
---|
61 | * |
---|
62 | */ |
---|
63 | private HttpMonitoringProxy proxy; |
---|
64 | |
---|
65 | /** |
---|
66 | * |
---|
67 | */ |
---|
68 | private HttpMonitor monitor; |
---|
69 | |
---|
70 | /* (non-Javadoc) |
---|
71 | * @see de.ugoe.cs.autoquest.httpmonitor.AbstractTC#setUpHook() |
---|
72 | */ |
---|
73 | @Override |
---|
74 | protected void setUpHook() throws Exception { |
---|
75 | // nothing to do |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * |
---|
80 | */ |
---|
81 | public void tearDownHook() throws Exception { |
---|
82 | if (proxy != null) { |
---|
83 | try { |
---|
84 | proxy.stop(); |
---|
85 | } |
---|
86 | finally { |
---|
87 | proxy = null; |
---|
88 | } |
---|
89 | } |
---|
90 | if (monitor != null) { |
---|
91 | try { |
---|
92 | monitor.stop(); |
---|
93 | } |
---|
94 | finally { |
---|
95 | monitor = null; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | @Test |
---|
101 | public void test_SimulatedSession_MonitorOnly() throws Exception { |
---|
102 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" }); |
---|
103 | |
---|
104 | monitor.init(); |
---|
105 | monitor.start(); |
---|
106 | |
---|
107 | Session simulatedSession = HTTPTestUtils.createRandomSession(50); |
---|
108 | sendExchanges(simulatedSession.getHttpExchange()); |
---|
109 | |
---|
110 | monitor.stop(); |
---|
111 | monitor = null; |
---|
112 | |
---|
113 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
114 | |
---|
115 | assertTrue(logFile.exists()); |
---|
116 | |
---|
117 | HTTPLogParser parser = new HTTPLogParser(); |
---|
118 | |
---|
119 | parser.parseFile(logFile); |
---|
120 | |
---|
121 | // check the sequences |
---|
122 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
123 | |
---|
124 | assertNotNull(sequences); |
---|
125 | |
---|
126 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
127 | assertTrue(iterator.hasNext()); |
---|
128 | |
---|
129 | List<Event> sequence = iterator.next(); |
---|
130 | assertFalse(iterator.hasNext()); |
---|
131 | |
---|
132 | assertNotNull(sequence); |
---|
133 | assertEquals(simulatedSession.getHttpExchange().size(), sequence.size()); |
---|
134 | |
---|
135 | System.out.println("{"); |
---|
136 | System.out.println(" {"); |
---|
137 | for (int j = 0; j < sequence.size(); j++) { |
---|
138 | System.out.print(" "); |
---|
139 | System.out.print(sequence.get(j)); |
---|
140 | System.out.println(","); |
---|
141 | |
---|
142 | assertNotNull(sequence.get(j)); |
---|
143 | assertNotNull(sequence.get(j).getType()); |
---|
144 | assertTrue(sequence.get(j).getType() instanceof HTTPEventType); |
---|
145 | |
---|
146 | assertNotNull(sequence.get(j).getTarget()); |
---|
147 | assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget); |
---|
148 | |
---|
149 | HTTPTestUtils.assertExchangeEquals |
---|
150 | (simulatedSession.getHttpExchange().get(j), |
---|
151 | ((HTTPEventType) sequence.get(j).getType()).getExchange()); |
---|
152 | |
---|
153 | assertEquals(HTTPUtils.toString(simulatedSession.getHttpExchange().get(j).getReceiver()), |
---|
154 | ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier()); |
---|
155 | } |
---|
156 | System.out.println(" }"); |
---|
157 | System.out.println("}"); |
---|
158 | System.out.println("\n\n"); |
---|
159 | } |
---|
160 | |
---|
161 | @Test |
---|
162 | public void test_SimpleText_ProxyAndMonitor() throws Exception { |
---|
163 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" }); |
---|
164 | |
---|
165 | monitor.init(); |
---|
166 | monitor.start(); |
---|
167 | |
---|
168 | proxy = new HttpMonitoringProxy |
---|
169 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", |
---|
170 | "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); |
---|
171 | |
---|
172 | proxy.init(); |
---|
173 | proxy.start(); |
---|
174 | |
---|
175 | String message = "dummy message"; |
---|
176 | String expectedResponse = "response content"; |
---|
177 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
178 | |
---|
179 | assertEquals(expectedResponse, response); |
---|
180 | |
---|
181 | // the monitor needs some time to receive the exchange --> give it |
---|
182 | Thread.sleep(1000); |
---|
183 | |
---|
184 | monitor.stop(); |
---|
185 | monitor = null; |
---|
186 | |
---|
187 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
188 | |
---|
189 | assertTrue(logFile.exists()); |
---|
190 | |
---|
191 | HTTPLogParser parser = new HTTPLogParser(); |
---|
192 | |
---|
193 | parser.parseFile(logFile); |
---|
194 | |
---|
195 | // check the sequences |
---|
196 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
197 | |
---|
198 | assertNotNull(sequences); |
---|
199 | |
---|
200 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
201 | assertTrue(iterator.hasNext()); |
---|
202 | |
---|
203 | List<Event> sequence = iterator.next(); |
---|
204 | assertFalse(iterator.hasNext()); |
---|
205 | |
---|
206 | assertNotNull(sequence); |
---|
207 | assertEquals(1, sequence.size()); |
---|
208 | |
---|
209 | assertEvent(sequence.get(0), "POST", message, response); |
---|
210 | } |
---|
211 | |
---|
212 | @Test |
---|
213 | public void test_XMLMessage_ProxyAndMonitor() throws Exception { |
---|
214 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" }); |
---|
215 | |
---|
216 | monitor.init(); |
---|
217 | monitor.start(); |
---|
218 | |
---|
219 | proxy = new HttpMonitoringProxy |
---|
220 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", |
---|
221 | "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); |
---|
222 | |
---|
223 | proxy.init(); |
---|
224 | proxy.start(); |
---|
225 | |
---|
226 | String message = |
---|
227 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
228 | "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
229 | " <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" + |
---|
230 | "</xsd:schema>"; |
---|
231 | |
---|
232 | String expectedResponse = |
---|
233 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
234 | "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
235 | " <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" + |
---|
236 | " <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" + |
---|
237 | " <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" + |
---|
238 | " <headers>" + |
---|
239 | " <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" + |
---|
240 | " <header key=\"Connection\" value=\"keep-alive\"/>" + |
---|
241 | " <header key=\"Host\" value=\"localhost:19098\"/>" + |
---|
242 | " <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" + |
---|
243 | " <header key=\"Content-Length\" value=\"13\"/>" + |
---|
244 | " </headers>" + |
---|
245 | " <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" + |
---|
246 | " length=\"13\">" + |
---|
247 | " <data>dummy message</data>" + |
---|
248 | " </content>" + |
---|
249 | " </request>" + |
---|
250 | " <response status=\"200\">" + |
---|
251 | " <headers>" + |
---|
252 | " <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" + |
---|
253 | " <header key=\"Content-Length\" value=\"16\"/>" + |
---|
254 | " </headers>" + |
---|
255 | " <content encoding=\"ISO-8859-1\" length=\"16\">" + |
---|
256 | " <data>response content</data>" + |
---|
257 | " </content>" + |
---|
258 | " </response>" + |
---|
259 | "</httpEvent>"; |
---|
260 | |
---|
261 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
262 | |
---|
263 | assertEquals(expectedResponse, response); |
---|
264 | |
---|
265 | // the monitor needs some time to receive the exchange --> give it |
---|
266 | Thread.sleep(1000); |
---|
267 | |
---|
268 | monitor.stop(); |
---|
269 | monitor = null; |
---|
270 | |
---|
271 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
272 | |
---|
273 | assertTrue(logFile.exists()); |
---|
274 | |
---|
275 | HTTPLogParser parser = new HTTPLogParser(); |
---|
276 | |
---|
277 | parser.parseFile(logFile); |
---|
278 | |
---|
279 | // check the sequences |
---|
280 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
281 | |
---|
282 | assertNotNull(sequences); |
---|
283 | |
---|
284 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
285 | assertTrue(iterator.hasNext()); |
---|
286 | |
---|
287 | List<Event> sequence = iterator.next(); |
---|
288 | assertFalse(iterator.hasNext()); |
---|
289 | |
---|
290 | assertNotNull(sequence); |
---|
291 | assertEquals(1, sequence.size()); |
---|
292 | |
---|
293 | assertEvent(sequence.get(0), "POST", message, response); |
---|
294 | } |
---|
295 | |
---|
296 | @Test |
---|
297 | public void test_SOAP_ProxyAndMonitor() throws Exception { |
---|
298 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" }); |
---|
299 | |
---|
300 | monitor.init(); |
---|
301 | monitor.start(); |
---|
302 | |
---|
303 | proxy = new HttpMonitoringProxy |
---|
304 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", |
---|
305 | "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); |
---|
306 | |
---|
307 | proxy.init(); |
---|
308 | proxy.start(); |
---|
309 | |
---|
310 | DummyService service = new DummyService |
---|
311 | (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"), |
---|
312 | new QName("DummyService", "DummyService")); |
---|
313 | |
---|
314 | DummyServicePortType dummyService = service.getDummyServiceSOAPPort(); |
---|
315 | |
---|
316 | dummyservice.ObjectFactory factory = new dummyservice.ObjectFactory(); |
---|
317 | VerifyUserInMsgType request = factory.createVerifyUserInMsgType(); |
---|
318 | VerifyUserOutMsgType response = dummyService.verifyUser(request); |
---|
319 | |
---|
320 | assertNotNull(response); |
---|
321 | |
---|
322 | // the monitor needs some time to receive the exchange --> give it |
---|
323 | Thread.sleep(1000); |
---|
324 | |
---|
325 | monitor.stop(); |
---|
326 | monitor = null; |
---|
327 | |
---|
328 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
329 | |
---|
330 | assertTrue(logFile.exists()); |
---|
331 | |
---|
332 | HTTPLogParser parser = new HTTPLogParser(); |
---|
333 | |
---|
334 | parser.parseFile(logFile); |
---|
335 | |
---|
336 | // check the sequences |
---|
337 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
338 | |
---|
339 | assertNotNull(sequences); |
---|
340 | |
---|
341 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
342 | assertTrue(iterator.hasNext()); |
---|
343 | |
---|
344 | List<Event> sequence = iterator.next(); |
---|
345 | assertFalse(iterator.hasNext()); |
---|
346 | |
---|
347 | assertNotNull(sequence); |
---|
348 | assertEquals(2, sequence.size()); |
---|
349 | |
---|
350 | assertEvent(sequence.get(0), "GET", null, null); // get WSDL |
---|
351 | assertEvent(sequence.get(1), "POST", null, null); // send request |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * |
---|
356 | */ |
---|
357 | private void sendExchanges(List<HttpExchange> exchanges) throws Exception { |
---|
358 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
359 | |
---|
360 | for (HttpExchange exchange : exchanges) { |
---|
361 | HttpPost httpRequest = new HttpPost("http://localhost:" + MONITOR_PORT + "/"); |
---|
362 | |
---|
363 | JAXBContext jaxbContext = |
---|
364 | JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
365 | Marshaller marshaller = jaxbContext.createMarshaller(); |
---|
366 | |
---|
367 | StringWriter out = new StringWriter(); |
---|
368 | marshaller.marshal(new ObjectFactory().createHttpExchange(exchange), out); |
---|
369 | |
---|
370 | HttpEntity entity = new StringEntity |
---|
371 | (out.toString(), ContentType.create("text/plain", "UTF-8")); |
---|
372 | ((HttpPost) httpRequest).setEntity(entity); |
---|
373 | |
---|
374 | try { |
---|
375 | httpclient.execute(httpRequest); |
---|
376 | } |
---|
377 | finally { |
---|
378 | httpRequest.releaseConnection(); |
---|
379 | out.close(); |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | } |
---|