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

Last change on this file was 2117, checked in by pharms, 8 years ago
File size: 23.2 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.*;
18
19import java.io.File;
20import java.io.IOException;
21import java.io.StringReader;
22import java.net.URL;
23import java.util.Collection;
24import java.util.Iterator;
25import java.util.LinkedList;
26import java.util.List;
27
28import javax.servlet.ServletException;
29import javax.servlet.http.HttpServlet;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32import javax.xml.bind.JAXBContext;
33import javax.xml.bind.JAXBElement;
34import javax.xml.bind.Unmarshaller;
35import javax.xml.namespace.QName;
36
37import org.eclipse.jetty.server.Server;
38import org.eclipse.jetty.servlet.ServletContextHandler;
39import org.eclipse.jetty.servlet.ServletHolder;
40import org.junit.Test;
41
42import dummyservice.ObjectFactory;
43import dummyservice.VerifyUserInMsgType;
44import dummyservice.VerifyUserOutMsgType;
45
46import de.ugoe.cs.autoquest.eventcore.Event;
47import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy;
48import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
49import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
50import dummyservice.DummyService;
51import dummyservice.DummyServicePortType;
52
53/**
54 * @author Patrick Harms
55 */
56public class HttpMonitoringProxyTest extends AbstractTC {
57
58    /**
59     *
60     */
61    private HttpMonitoringProxy proxy;
62
63    /**
64     * the jetty web server used for receiving messages copied by the proxy
65     */
66    private Server dummyMonitor;
67
68    /**
69     * the id generator used by the dummy
70     */
71    private IdGenerator idGenerator;
72   
73    /**
74     * the messages received by the dummy monitor
75     */
76    private List<String> messages;
77
78    /**
79     *
80     */
81    protected void setUpHook() throws Exception {
82        // setup a simple HTTP server
83        messages = new LinkedList<String>();
84       
85        idGenerator = new SimpleIdGenerator();
86        idGenerator.init();
87        idGenerator.start();
88       
89        dummyMonitor = new Server(MONITOR_PORT);
90        ServletContextHandler root =
91            new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS);
92
93        root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*");
94       
95        dummyMonitor.start();
96    }
97   
98    /**
99     *
100     */
101    protected void tearDownHook() throws Exception {
102        if (proxy != null) {
103            try {
104                proxy.stop();
105            }
106            finally {
107                proxy = null;
108            }
109        }
110       
111        if (dummyMonitor != null) {
112            try {
113                dummyMonitor.stop();
114            }
115            finally {
116                dummyMonitor = null;
117            }
118        }
119       
120        if (idGenerator != null) {
121            try {
122                idGenerator.stop();
123            }
124            finally {
125                idGenerator = null;
126            }
127        }
128        messages = null;
129    }
130   
131    /**
132     *
133     */
134    @Test(expected=java.lang.IllegalArgumentException.class)
135    public void test_InvalidInitialization_01() throws Exception {
136        proxy = new HttpMonitoringProxy(new String[] {  });
137    }
138   
139    /**
140     *
141     */
142    @Test
143    public void test_SimpleText_Local() throws Exception {
144        proxy = new HttpMonitoringProxy
145            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
146                            "local" });
147
148        proxy.init();
149        proxy.start();
150       
151        String message = "dummy message";
152        String expectedResponse = "response content";
153        String response = sendDummyMessage("POST", null, message, expectedResponse);
154       
155        assertEquals(expectedResponse, response);
156       
157        proxy.stop();
158        proxy = null;
159
160        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
161       
162        assertTrue(logFile.exists());
163       
164        HTTPLogParser parser = new HTTPLogParser();
165
166        parser.parseFile(logFile);
167
168        // check the sequences
169        Collection<List<Event>> sequences = parser.getSequences();
170
171        assertNotNull(sequences);
172
173        Iterator<List<Event>> iterator = sequences.iterator();
174        assertTrue(iterator.hasNext());
175
176        List<Event> sequence = iterator.next();
177        assertFalse(iterator.hasNext());
178
179        assertNotNull(sequence);
180        assertEquals(1, sequence.size());
181
182        assertEvent(sequence.get(0), "POST", null, message, expectedResponse);
183    }
184   
185    /**
186     *
187     */
188    @Test
189    public void test_SimpleText_Remote() throws Exception {
190        proxy = new HttpMonitoringProxy
191            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
192                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
193
194        proxy.init();
195        proxy.start();
196       
197        String message = "dummy message";
198        String expectedResponse = "response content";
199        String response = sendDummyMessage("POST", null, message, expectedResponse);
200       
201        assertEquals(expectedResponse, response);
202       
203        // give the proxy some time to send the copy of the message
204        while(messages.size() < 1) {
205            System.out.println("waiting for message to be send by the proxy");
206            Thread.sleep(1000);
207        }
208       
209        assertEquals(1, messages.size());
210       
211        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
212        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
213       
214        @SuppressWarnings("unchecked")
215        JAXBElement<HttpExchange> jaxObject =
216            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
217       
218        assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse);
219    }
220   
221    /**
222     *
223     */
224    @Test
225    public void test_XMLMessage_Local() throws Exception {
226        proxy = new HttpMonitoringProxy
227            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
228                            "local" });
229
230        proxy.init();
231        proxy.start();
232           
233        String message =
234            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
235            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
236            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
237            "</xsd:schema>";
238       
239        String expectedResponse =
240            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
241            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
242            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
243            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
244            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
245            "    <headers>" +
246            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
247            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
248            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
249            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
250            "      <header key=\"Content-Length\" value=\"13\"/>" +
251            "    </headers>" +
252            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
253            "             length=\"13\">" +
254            "      <data>dummy message</data>" +
255            "    </content>" +
256            "  </request>" +
257            "  <response status=\"200\">" +
258            "    <headers>" +
259            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
260            "      <header key=\"Content-Length\" value=\"16\"/>" +
261            "    </headers>" +
262            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
263            "      <data>response content</data>" +
264            "    </content>" +
265            "  </response>" +
266            "</httpEvent>";
267
268        String response = sendDummyMessage("POST", null, message, expectedResponse);
269       
270        assertEquals(expectedResponse, response);
271       
272        proxy.stop();
273        proxy = null;
274
275        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
276       
277        assertTrue(logFile.exists());
278       
279        HTTPLogParser parser = new HTTPLogParser();
280
281        parser.parseFile(logFile);
282
283        // check the sequences
284        Collection<List<Event>> sequences = parser.getSequences();
285
286        assertNotNull(sequences);
287
288        Iterator<List<Event>> iterator = sequences.iterator();
289        assertTrue(iterator.hasNext());
290
291        List<Event> sequence = iterator.next();
292        assertFalse(iterator.hasNext());
293
294        assertNotNull(sequence);
295        assertEquals(1, sequence.size());
296
297        assertEvent(sequence.get(0), "POST", null, message, expectedResponse);
298    }
299   
300    /**
301     *
302     */
303    @Test
304    public void test_XMLMessage_Remote() throws Exception {
305        proxy = new HttpMonitoringProxy
306            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
307                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
308
309        proxy.init();
310        proxy.start();
311           
312        String message =
313            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
314            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
315            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
316            "</xsd:schema>";
317       
318        String expectedResponse =
319            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
320            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
321            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
322            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
323            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
324            "    <headers>" +
325            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
326            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
327            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
328            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
329            "      <header key=\"Content-Length\" value=\"13\"/>" +
330            "    </headers>" +
331            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
332            "             length=\"13\">" +
333            "      <data>dummy message</data>" +
334            "    </content>" +
335            "  </request>" +
336            "  <response status=\"200\">" +
337            "    <headers>" +
338            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
339            "      <header key=\"Content-Length\" value=\"16\"/>" +
340            "    </headers>" +
341            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
342            "      <data>response content</data>" +
343            "    </content>" +
344            "  </response>" +
345            "</httpEvent>";
346
347        String response = sendDummyMessage("POST", null, message, expectedResponse);
348       
349        assertEquals(expectedResponse, response);
350       
351        // give the proxy some time to send the copy of the message
352        while(messages.size() < 1) {
353            System.out.println("waiting for message to be send by the proxy");
354            Thread.sleep(1000);
355        }
356       
357        assertEquals(1, messages.size());
358       
359        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
360        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
361       
362        @SuppressWarnings("unchecked")
363        JAXBElement<HttpExchange> jaxObject =
364            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
365       
366        assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse);
367    }
368   
369    /**
370     *
371     */
372    @Test
373    public void test_SOAP_Local() throws Exception {
374        proxy = new HttpMonitoringProxy
375            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
376                            "local" });
377
378        proxy.init();
379        proxy.start();
380
381        DummyService service = new DummyService
382            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
383             new QName("DummyService", "DummyService"));
384
385        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
386       
387        ObjectFactory factory = new ObjectFactory();
388        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
389        VerifyUserOutMsgType response = dummyService.verifyUser(request);
390
391        assertNotNull(response);
392       
393        proxy.stop();
394        proxy = null;
395
396        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
397       
398        assertTrue(logFile.exists());
399       
400        HTTPLogParser parser = new HTTPLogParser();
401
402        parser.parseFile(logFile);
403
404        // check the sequences
405        Collection<List<Event>> sequences = parser.getSequences();
406
407        assertNotNull(sequences);
408
409        Iterator<List<Event>> iterator = sequences.iterator();
410        assertTrue(iterator.hasNext());
411
412        List<Event> sequence = iterator.next();
413        assertFalse(iterator.hasNext());
414
415        assertNotNull(sequence);
416        assertEquals(2, sequence.size());
417
418        assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL
419        assertEvent(sequence.get(1), "POST", null, null, null); // send request
420    }
421   
422    /**
423     *
424     */
425    @SuppressWarnings("unchecked")
426    @Test
427    public void test_SOAP_Remote() throws Exception {
428        proxy = new HttpMonitoringProxy
429            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
430                            "localhost:" + MONITOR_PORT  });
431
432        proxy.init();
433        proxy.start();
434
435        DummyService service = new DummyService
436            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
437             new QName("DummyService", "DummyService"));
438
439        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
440       
441        ObjectFactory factory = new ObjectFactory();
442        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
443        VerifyUserOutMsgType response = dummyService.verifyUser(request);
444
445        assertNotNull(response);
446       
447        proxy.stop();
448        proxy = null;
449
450        // give the proxy some time to send the copy of the message
451        while(messages.size() < 1) {
452            System.out.println("waiting for message to be send by the proxy");
453            Thread.sleep(1000);
454        }
455       
456        assertEquals(2, messages.size());
457       
458        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
459        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
460       
461        System.out.println("message 0: " + messages.get(0));
462        System.out.println("message 1: " + messages.get(1));
463        JAXBElement<HttpExchange> jaxObject =
464            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
465       
466        assertExchange(jaxObject.getValue(), "GET", "wsdl", null, null);
467
468        jaxObject =
469            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(1)));
470           
471        assertExchange(jaxObject.getValue(), "POST", null, null, null);
472    }
473   
474    /**
475     *
476     */
477    @Test
478    public void test_LargeRequest_Local() throws Exception {
479        proxy = new HttpMonitoringProxy
480            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
481                            "local" });
482
483        proxy.init();
484        proxy.start();
485       
486        StringBuffer message = new StringBuffer();
487        StringBuffer expectedResponse = new StringBuffer();
488       
489        for (int i = 0; i < 1000; i++) {
490            message.append(" # " + i + " test request data");
491            expectedResponse.append(" # " + i + " test response data");
492        }
493       
494        String response =
495            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
496       
497        assertEquals(expectedResponse.toString(), response);
498       
499        proxy.stop();
500        proxy = null;
501
502        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
503       
504        assertTrue(logFile.exists());
505       
506        HTTPLogParser parser = new HTTPLogParser();
507
508        parser.parseFile(logFile);
509
510        // check the sequences
511        Collection<List<Event>> sequences = parser.getSequences();
512
513        assertNotNull(sequences);
514
515        Iterator<List<Event>> iterator = sequences.iterator();
516        assertTrue(iterator.hasNext());
517
518        List<Event> sequence = iterator.next();
519        assertFalse(iterator.hasNext());
520
521        assertNotNull(sequence);
522        assertEquals(1, sequence.size());
523
524        assertEvent(sequence.get(0), "POST", null, message.toString(), expectedResponse.toString());
525    }
526   
527    /**
528     *
529     */
530    @Test
531    public void test_LargeRequest_Remote() throws Exception {
532        proxy = new HttpMonitoringProxy
533            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
534                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
535
536        proxy.init();
537        proxy.start();
538       
539       
540        StringBuffer message = new StringBuffer();
541        StringBuffer expectedResponse = new StringBuffer();
542       
543        for (int i = 0; i < 1000; i++) {
544            message.append(" # " + i + " test request data");
545            expectedResponse.append(" # " + i + " test response data");
546        }
547       
548        String response =
549            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
550       
551        assertEquals(expectedResponse.toString(), response);
552       
553        // give the proxy some time to send the copy of the message
554        while(messages.size() < 1) {
555            System.out.println("waiting for message to be send by the proxy");
556            Thread.sleep(1000);
557        }
558       
559        assertEquals(1, messages.size());
560       
561        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
562        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
563       
564        @SuppressWarnings("unchecked")
565        JAXBElement<HttpExchange> jaxObject =
566            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
567       
568        assertExchange(jaxObject.getValue(), "POST", null,
569                       message.toString(), expectedResponse.toString());
570    }
571   
572    /**
573     *
574     */
575    @Test
576    public void test_Query_Local() throws Exception {
577        proxy = new HttpMonitoringProxy
578            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
579                            "local" });
580
581        proxy.init();
582        proxy.start();
583       
584        String message = "dummy message";
585        String expectedResponse = "response content";
586        String query = "key=value&key2=value2&key3=%3E%3CXXX";
587        String response = sendDummyMessage("POST", query, message, expectedResponse);
588       
589        assertEquals(expectedResponse, response);
590       
591        proxy.stop();
592        proxy = null;
593
594        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
595       
596        assertTrue(logFile.exists());
597       
598        HTTPLogParser parser = new HTTPLogParser();
599
600        parser.parseFile(logFile);
601
602        // check the sequences
603        Collection<List<Event>> sequences = parser.getSequences();
604
605        assertNotNull(sequences);
606
607        Iterator<List<Event>> iterator = sequences.iterator();
608        assertTrue(iterator.hasNext());
609
610        List<Event> sequence = iterator.next();
611        assertFalse(iterator.hasNext());
612
613        assertNotNull(sequence);
614        assertEquals(1, sequence.size());
615
616        assertEvent(sequence.get(0), "POST", query, message, expectedResponse);
617    }
618   
619    /**
620     *
621     */
622    @Test
623    public void test_Query_Remote() throws Exception {
624        proxy = new HttpMonitoringProxy
625            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
626                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
627
628        proxy.init();
629        proxy.start();
630       
631        String message = "dummy message";
632        String expectedResponse = "response content";
633        String query = "key=value&key2=value2&key3=%3E%3CXXX";
634        String response = sendDummyMessage("POST", query, message, expectedResponse);
635       
636        assertEquals(expectedResponse, response);
637       
638        // give the proxy some time to send the copy of the message
639        while(messages.size() < 1) {
640            System.out.println("waiting for message to be send by the proxy");
641            Thread.sleep(1000);
642        }
643       
644        assertEquals(1, messages.size());
645       
646        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
647        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
648       
649        @SuppressWarnings("unchecked")
650        JAXBElement<HttpExchange> jaxObject =
651            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
652       
653        assertExchange(jaxObject.getValue(), "POST", query, message, expectedResponse);
654    }
655
656    /**
657     * @author Patrick Harms
658     */
659    private class DummyMonitorServlet extends HttpServlet {
660
661        /**  */
662        private static final long serialVersionUID = 1L;
663       
664        /* (non-Javadoc)
665         * @see HttpServlet#service(HttpServletRequest, HttpServletResponse)
666         */
667        @Override
668        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
669            throws ServletException, IOException
670        {
671            messages.add(readToString(req.getReader()));
672           
673            resp.setStatus(HttpServletResponse.SC_OK);
674            System.out.println("send ok response");
675        }
676
677        /* (non-Javadoc)
678         * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
679         */
680        @Override
681        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
682            throws ServletException, IOException
683        {
684            resp.setHeader("X-AutoQUEST-OrderingId", Long.toString(idGenerator.getNextId()));
685        }
686       
687       
688    }
689
690}
Note: See TracBrowser for help on using the repository browser.