source: trunk/autoquest-httpmonitor-test/src/test/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorTest.java

Last change on this file was 1991, checked in by pharms, 9 years ago
  • added ordering id for requests and responses
File size: 18.1 KB
Line 
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
15package de.ugoe.cs.autoquest.httpmonitor;
16
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertTrue;
21
22import java.io.File;
23import java.io.StringWriter;
24import java.net.URL;
25import java.util.Collection;
26import java.util.Iterator;
27import java.util.List;
28
29import javax.xml.bind.JAXBContext;
30import javax.xml.bind.Marshaller;
31import javax.xml.namespace.QName;
32
33import org.apache.http.HttpEntity;
34import org.apache.http.HttpResponse;
35import org.apache.http.client.methods.HttpGet;
36import org.apache.http.client.methods.HttpPost;
37import org.apache.http.entity.ContentType;
38import org.apache.http.entity.StringEntity;
39import org.apache.http.impl.client.DefaultHttpClient;
40import org.junit.Test;
41
42import de.ugoe.cs.autoquest.eventcore.Event;
43import de.ugoe.cs.autoquest.http.HTTPTestUtils;
44import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy;
45import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
46import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
47import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
48import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
49import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
50import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory;
51import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
52import dummyservice.DummyService;
53import dummyservice.DummyServicePortType;
54import dummyservice.VerifyUserInMsgType;
55import dummyservice.VerifyUserOutMsgType;
56
57/**
58 * @author Patrick Harms
59 */
60public class HttpMonitorTest extends AbstractTC {
61   
62    /**
63     *
64     */
65    private HttpMonitoringProxy proxy;
66
67    /**
68     *
69     */
70    private HttpMonitor monitor;
71
72    /* (non-Javadoc)
73     * @see de.ugoe.cs.autoquest.httpmonitor.AbstractTC#setUpHook()
74     */
75    @Override
76    protected void setUpHook() throws Exception {
77        // nothing to do
78    }
79
80    /**
81     *
82     */
83    public void tearDownHook() throws Exception {
84        if (proxy != null) {
85            try {
86                proxy.stop();
87            }
88            finally {
89                proxy = null;
90            }
91        }
92        if (monitor != null) {
93            try {
94                monitor.stop();
95            }
96            finally {
97                monitor = null;
98            }
99        }
100    }
101   
102    /**
103     *
104     */
105    @Test
106    public void test_RetrievalOfId_MonitorOnly() throws Exception {
107        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
108
109        monitor.init();
110        monitor.start();
111       
112        long id = getId();
113        assertTrue(id > 0);
114       
115        for (int i = 0; i < 300; i++) {
116            long prevId = id;
117            id = getId();
118            assertTrue(id > prevId);
119        }
120       
121        monitor.stop();
122        monitor = null;
123    }
124   
125    /**
126     *
127     */
128    @Test
129    public void test_SimulatedSession_MonitorOnly() throws Exception {
130        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
131
132        monitor.init();
133        monitor.start();
134       
135        Session simulatedSession = HTTPTestUtils.createRandomSession(50);
136        sendExchanges(simulatedSession.getHttpExchange());
137       
138        monitor.stop();
139        monitor = null;
140
141        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
142       
143        assertTrue(logFile.exists());
144       
145        HTTPLogParser parser = new HTTPLogParser();
146
147        parser.parseFile(logFile);
148
149        // check the sequences
150        Collection<List<Event>> sequences = parser.getSequences();
151
152        assertNotNull(sequences);
153
154        Iterator<List<Event>> iterator = sequences.iterator();
155        assertTrue(iterator.hasNext());
156
157        List<Event> sequence = iterator.next();
158        assertFalse(iterator.hasNext());
159
160        assertNotNull(sequence);
161        assertEquals(simulatedSession.getHttpExchange().size(), sequence.size());
162
163        System.out.println("{");
164        System.out.println("  {");
165        long prevId = 0;
166       
167        for (int j = 0; j < sequence.size(); j++) {
168            System.out.print("    ");
169            System.out.print(sequence.get(j));
170            System.out.println(",");
171
172            assertNotNull(sequence.get(j));
173            assertNotNull(sequence.get(j).getType());
174            assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
175
176            assertNotNull(sequence.get(j).getTarget());
177            assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
178
179            HTTPEventType eventType = (HTTPEventType) sequence.get(j).getType();
180           
181            HTTPTestUtils.assertExchangeEquals
182                (simulatedSession.getHttpExchange().get(j), eventType.getExchange());
183
184            assertEquals(HTTPUtils.toString(simulatedSession.getHttpExchange().get(j).getReceiver()),
185                         ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
186           
187            assertNotNull(eventType.getExchange().getRequest().getOrderingId());
188            assertTrue(prevId < eventType.getExchange().getRequest().getOrderingId());
189            prevId = eventType.getExchange().getRequest().getOrderingId();
190           
191            assertNotNull(eventType.getExchange().getResponse().getOrderingId());
192            assertTrue(prevId < eventType.getExchange().getResponse().getOrderingId());
193            prevId = eventType.getExchange().getResponse().getOrderingId();
194        }
195        System.out.println("  }");
196        System.out.println("}");
197        System.out.println("\n\n");
198    }
199   
200    /**
201     *
202     */
203    @Test
204    public void test_SimpleText_ProxyAndMonitor() throws Exception {
205        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
206
207        monitor.init();
208        monitor.start();
209
210        proxy = new HttpMonitoringProxy
211            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
212                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
213
214        proxy.init();
215        proxy.start();
216       
217        String message = "dummy message";
218        String expectedResponse = "response content";
219        String response = sendDummyMessage("POST", null, message, expectedResponse);
220       
221        assertEquals(expectedResponse, response);
222       
223        // the monitor needs some time to receive the exchange --> give it
224        Thread.sleep(1000);
225       
226        monitor.stop();
227        monitor = null;
228
229        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
230       
231        assertTrue(logFile.exists());
232       
233        HTTPLogParser parser = new HTTPLogParser();
234
235        parser.parseFile(logFile);
236
237        // check the sequences
238        Collection<List<Event>> sequences = parser.getSequences();
239
240        assertNotNull(sequences);
241
242        Iterator<List<Event>> iterator = sequences.iterator();
243        assertTrue(iterator.hasNext());
244
245        List<Event> sequence = iterator.next();
246        assertFalse(iterator.hasNext());
247
248        assertNotNull(sequence);
249        assertEquals(1, sequence.size());
250
251        assertEvent(sequence.get(0), "POST", null, message, response);
252    }
253   
254    /**
255     *
256     */
257    @Test
258    public void test_XMLMessage_ProxyAndMonitor() throws Exception {
259        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
260
261        monitor.init();
262        monitor.start();
263
264        proxy = new HttpMonitoringProxy
265            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
266                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
267
268        proxy.init();
269        proxy.start();
270       
271        String message =
272            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
273            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
274            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
275            "</xsd:schema>";
276   
277        String expectedResponse =
278            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
279            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
280            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
281            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
282            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
283            "    <headers>" +
284            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
285            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
286            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
287            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
288            "      <header key=\"Content-Length\" value=\"13\"/>" +
289            "    </headers>" +
290            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
291            "             length=\"13\">" +
292            "      <data>dummy message</data>" +
293            "    </content>" +
294            "  </request>" +
295            "  <response status=\"200\">" +
296            "    <headers>" +
297            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
298            "      <header key=\"Content-Length\" value=\"16\"/>" +
299            "    </headers>" +
300            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
301            "      <data>response content</data>" +
302            "    </content>" +
303            "  </response>" +
304            "</httpEvent>";
305
306        String response = sendDummyMessage("POST", null, message, expectedResponse);
307       
308        assertEquals(expectedResponse, response);
309       
310        // the monitor needs some time to receive the exchange --> give it
311        Thread.sleep(1000);
312       
313        monitor.stop();
314        monitor = null;
315
316        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
317       
318        assertTrue(logFile.exists());
319       
320        HTTPLogParser parser = new HTTPLogParser();
321
322        parser.parseFile(logFile);
323
324        // check the sequences
325        Collection<List<Event>> sequences = parser.getSequences();
326
327        assertNotNull(sequences);
328
329        Iterator<List<Event>> iterator = sequences.iterator();
330        assertTrue(iterator.hasNext());
331
332        List<Event> sequence = iterator.next();
333        assertFalse(iterator.hasNext());
334
335        assertNotNull(sequence);
336        assertEquals(1, sequence.size());
337
338        assertEvent(sequence.get(0), "POST", null, message, response);
339    }
340   
341    /**
342     *
343     */
344    @Test
345    public void test_SOAP_ProxyAndMonitor() throws Exception {
346        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
347
348        monitor.init();
349        monitor.start();
350
351        proxy = new HttpMonitoringProxy
352            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
353                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
354
355        proxy.init();
356        proxy.start();
357
358        DummyService service = new DummyService
359            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
360             new QName("DummyService", "DummyService"));
361
362        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
363       
364        dummyservice.ObjectFactory factory = new dummyservice.ObjectFactory();
365        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
366        VerifyUserOutMsgType response = dummyService.verifyUser(request);
367
368        assertNotNull(response);
369       
370        // the monitor needs some time to receive the exchange --> give it
371        Thread.sleep(1000);
372       
373        monitor.stop();
374        monitor = null;
375
376        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
377       
378        assertTrue(logFile.exists());
379       
380        HTTPLogParser parser = new HTTPLogParser();
381
382        parser.parseFile(logFile);
383
384        // check the sequences
385        Collection<List<Event>> sequences = parser.getSequences();
386
387        assertNotNull(sequences);
388
389        Iterator<List<Event>> iterator = sequences.iterator();
390        assertTrue(iterator.hasNext());
391
392        List<Event> sequence = iterator.next();
393        assertFalse(iterator.hasNext());
394
395        assertNotNull(sequence);
396        assertEquals(2, sequence.size());
397
398        assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL
399        assertEvent(sequence.get(1), "POST", null, null, null); // send request
400    }
401   
402    /**
403     *
404     */
405    @Test
406    public void test_LargeRequest_ProxyAndMonitor() throws Exception {
407        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
408
409        monitor.init();
410        monitor.start();
411
412        proxy = new HttpMonitoringProxy
413            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
414                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
415
416        proxy.init();
417        proxy.start();
418       
419        StringBuffer message = new StringBuffer();
420        StringBuffer expectedResponse = new StringBuffer();
421       
422        for (int i = 0; i < 1000; i++) {
423            message.append(" # " + i + " test request data");
424            expectedResponse.append(" # " + i + " test response data");
425        }
426       
427        String response =
428            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
429       
430        assertEquals(expectedResponse.toString(), response);
431       
432        // the monitor needs some time to receive the exchange --> give it
433        Thread.sleep(1000);
434       
435        monitor.stop();
436        monitor = null;
437
438        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
439       
440        assertTrue(logFile.exists());
441       
442        HTTPLogParser parser = new HTTPLogParser();
443
444        parser.parseFile(logFile);
445
446        // check the sequences
447        Collection<List<Event>> sequences = parser.getSequences();
448
449        assertNotNull(sequences);
450
451        Iterator<List<Event>> iterator = sequences.iterator();
452        assertTrue(iterator.hasNext());
453
454        List<Event> sequence = iterator.next();
455        assertFalse(iterator.hasNext());
456
457        assertNotNull(sequence);
458        assertEquals(1, sequence.size());
459
460        assertEvent(sequence.get(0), "POST", null, message.toString(), response);
461    }
462   
463    /**
464     *
465     */
466    @Test
467    public void test_Query_ProxyAndMonitor() throws Exception {
468        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
469
470        monitor.init();
471        monitor.start();
472
473        proxy = new HttpMonitoringProxy
474            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
475                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
476
477        proxy.init();
478        proxy.start();
479       
480        String message = "dummy message";
481        String query = "key=value&key2=value2&key3=%3E%3CXXX";
482        String expectedResponse = "response content";
483        String response = sendDummyMessage("POST", query, message, expectedResponse);
484       
485        assertEquals(expectedResponse, response);
486       
487        // the monitor needs some time to receive the exchange --> give it
488        Thread.sleep(1000);
489       
490        monitor.stop();
491        monitor = null;
492
493        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
494       
495        assertTrue(logFile.exists());
496       
497        HTTPLogParser parser = new HTTPLogParser();
498
499        parser.parseFile(logFile);
500
501        // check the sequences
502        Collection<List<Event>> sequences = parser.getSequences();
503
504        assertNotNull(sequences);
505
506        Iterator<List<Event>> iterator = sequences.iterator();
507        assertTrue(iterator.hasNext());
508
509        List<Event> sequence = iterator.next();
510        assertFalse(iterator.hasNext());
511
512        assertNotNull(sequence);
513        assertEquals(1, sequence.size());
514
515        assertEvent(sequence.get(0), "POST", query, message, response);
516    }
517
518    /**
519     *
520     */
521    private void sendExchanges(List<HttpExchange> exchanges) throws Exception {
522        DefaultHttpClient httpclient = new DefaultHttpClient();
523       
524        for (HttpExchange exchange : exchanges) {
525            HttpPost httpRequest = new HttpPost("http://localhost:" + MONITOR_PORT + "/");
526           
527            JAXBContext jaxbContext =
528                    JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
529            Marshaller marshaller = jaxbContext.createMarshaller();
530
531            StringWriter out = new StringWriter();
532            marshaller.marshal(new ObjectFactory().createHttpExchange(exchange), out);
533
534            HttpEntity entity = new StringEntity
535                (out.toString(), ContentType.create("text/plain", "UTF-8"));
536            ((HttpPost) httpRequest).setEntity(entity);
537       
538            try {
539                httpclient.execute(httpRequest);
540            }
541            finally {
542                httpRequest.releaseConnection();
543                out.close();
544            }
545        }
546    }
547
548    /**
549     *
550     */
551    private long getId() throws Exception {
552        DefaultHttpClient httpclient = new DefaultHttpClient();
553       
554        HttpGet httpRequest = new HttpGet("http://localhost:" + MONITOR_PORT + "/");
555           
556        try {
557            HttpResponse response = httpclient.execute(httpRequest);
558            return Long.parseLong(response.getFirstHeader("X-AutoQUEST-OrderingId").getValue());
559        }
560        finally {
561            httpRequest.releaseConnection();
562        }
563    }
564
565}
Note: See TracBrowser for help on using the repository browser.