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

Last change on this file since 1390 was 1390, checked in by pharms, 10 years ago
  • added test cases for SOAP message exchange
File size: 17.8 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.exchange.HttpExchange;
48import de.ugoe.cs.autoquest.httpmonitor.exchange.Method;
49import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy;
50import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
51import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
52import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
53import dummyservice.DummyService;
54import dummyservice.DummyServicePortType;
55
56/**
57 * @author Patrick Harms
58 */
59public class HttpMonitoringProxyTest extends AbstractTC {
60
61    /**
62     *
63     */
64    private HttpMonitoringProxy proxy;
65
66    /**
67     * the jetty web server used for receiving messages copied by the proxy
68     */
69    private Server dummyMonitor;
70   
71    /**
72     * the messages received by the dummy monitor
73     */
74    private List<String> messages;
75
76    /**
77     *
78     */
79    protected void setUpHook() throws Exception {
80        // setup a simple HTTP server
81        messages = new LinkedList<String>();
82       
83        dummyMonitor = new Server(MONITOR_PORT);
84        ServletContextHandler root =
85            new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS);
86
87        root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*");
88       
89        dummyMonitor.start();
90    }
91   
92    /**
93     *
94     */
95    protected void tearDownHook() throws Exception {
96        if (proxy != null) {
97            try {
98                proxy.stop();
99            }
100            finally {
101                proxy = null;
102            }
103        }
104        if (dummyMonitor != null) {
105            try {
106                dummyMonitor.stop();
107            }
108            finally {
109                dummyMonitor = null;
110            }
111        }
112        messages = null;
113    }
114   
115    @Test(expected=java.lang.IllegalArgumentException.class)
116    public void test_InvalidInitialization_01() throws Exception {
117        proxy = new HttpMonitoringProxy(new String[] {  });
118    }
119   
120    @Test
121    public void test_SimpleText_Local() throws Exception {
122        proxy = new HttpMonitoringProxy
123            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
124                            "local" });
125
126        proxy.init();
127        proxy.start();
128       
129        String message = "dummy message";
130        String expectedResponse = "response content";
131        String response = sendDummyMessage("POST", message, expectedResponse);
132       
133        assertEquals(expectedResponse, response);
134       
135        proxy.stop();
136        proxy = null;
137
138        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
139       
140        assertTrue(logFile.exists());
141       
142        HTTPLogParser parser = new HTTPLogParser();
143
144        parser.parseFile(logFile);
145
146        // check the sequences
147        Collection<List<Event>> sequences = parser.getSequences();
148
149        assertNotNull(sequences);
150
151        Iterator<List<Event>> iterator = sequences.iterator();
152        assertTrue(iterator.hasNext());
153
154        List<Event> sequence = iterator.next();
155        assertFalse(iterator.hasNext());
156
157        assertNotNull(sequence);
158        assertEquals(1, sequence.size());
159
160        assertEvent(sequence.get(0), "POST", message, expectedResponse);
161    }
162
163    @Test
164    public void test_SimpleText_Remote() throws Exception {
165        proxy = new HttpMonitoringProxy
166            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
167                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
168
169        proxy.init();
170        proxy.start();
171       
172        String message = "dummy message";
173        String expectedResponse = "response content";
174        String response = sendDummyMessage("POST", message, expectedResponse);
175       
176        assertEquals(expectedResponse, response);
177       
178        // give the proxy some time to send the copy of the message
179        while(messages.size() < 1) {
180            Thread.sleep(1000);
181        }
182       
183        assertEquals(1, messages.size());
184       
185        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
186        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
187       
188        @SuppressWarnings("unchecked")
189        JAXBElement<HttpExchange> jaxObject =
190            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
191       
192        HttpExchange exchange = jaxObject.getValue();
193       
194        assertEquals(message, exchange.getRequest().getContent().getData());
195        assertEquals(response, exchange.getResponse().getContent().getData());
196    }
197   
198    @Test
199    public void test_XMLMessage_Local() throws Exception {
200        proxy = new HttpMonitoringProxy
201            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
202                            "local" });
203
204        proxy.init();
205        proxy.start();
206           
207        String message =
208            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
209            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
210            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
211            "</xsd:schema>";
212       
213        String expectedResponse =
214            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
215            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
216            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
217            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
218            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
219            "    <headers>" +
220            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
221            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
222            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
223            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
224            "      <header key=\"Content-Length\" value=\"13\"/>" +
225            "    </headers>" +
226            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
227            "             length=\"13\">" +
228            "      <data>dummy message</data>" +
229            "    </content>" +
230            "  </request>" +
231            "  <response status=\"200\">" +
232            "    <headers>" +
233            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
234            "      <header key=\"Content-Length\" value=\"16\"/>" +
235            "    </headers>" +
236            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
237            "      <data>response content</data>" +
238            "    </content>" +
239            "  </response>" +
240            "</httpEvent>";
241
242        String response = sendDummyMessage("POST", message, expectedResponse);
243       
244        assertEquals(expectedResponse, response);
245       
246        proxy.stop();
247        proxy = null;
248
249        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
250       
251        assertTrue(logFile.exists());
252       
253        HTTPLogParser parser = new HTTPLogParser();
254
255        parser.parseFile(logFile);
256
257        // check the sequences
258        Collection<List<Event>> sequences = parser.getSequences();
259
260        assertNotNull(sequences);
261
262        Iterator<List<Event>> iterator = sequences.iterator();
263        assertTrue(iterator.hasNext());
264
265        List<Event> sequence = iterator.next();
266        assertFalse(iterator.hasNext());
267
268        assertNotNull(sequence);
269        assertEquals(1, sequence.size());
270
271        assertEvent(sequence.get(0), "POST", message, expectedResponse);
272    }
273   
274    @Test
275    public void test_XMLMessage_Remote() throws Exception {
276        proxy = new HttpMonitoringProxy
277            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
278                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
279
280        proxy.init();
281        proxy.start();
282           
283        String message =
284            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
285            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
286            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
287            "</xsd:schema>";
288       
289        String expectedResponse =
290            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
291            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
292            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
293            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
294            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
295            "    <headers>" +
296            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
297            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
298            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
299            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
300            "      <header key=\"Content-Length\" value=\"13\"/>" +
301            "    </headers>" +
302            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
303            "             length=\"13\">" +
304            "      <data>dummy message</data>" +
305            "    </content>" +
306            "  </request>" +
307            "  <response status=\"200\">" +
308            "    <headers>" +
309            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
310            "      <header key=\"Content-Length\" value=\"16\"/>" +
311            "    </headers>" +
312            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
313            "      <data>response content</data>" +
314            "    </content>" +
315            "  </response>" +
316            "</httpEvent>";
317
318        String response = sendDummyMessage("POST", message, expectedResponse);
319       
320        assertEquals(expectedResponse, response);
321       
322        // give the proxy some time to send the copy of the message
323        while(messages.size() < 1) {
324            Thread.sleep(1000);
325        }
326       
327        assertEquals(1, messages.size());
328       
329        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
330        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
331       
332        @SuppressWarnings("unchecked")
333        JAXBElement<HttpExchange> jaxObject =
334            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
335       
336        HttpExchange exchange = jaxObject.getValue();
337       
338        assertEquals(message, exchange.getRequest().getContent().getData());
339        assertEquals(response, exchange.getResponse().getContent().getData());
340    }
341   
342    @Test
343    public void test_SOAP_Local() throws Exception {
344        proxy = new HttpMonitoringProxy
345            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
346                            "local" });
347
348        proxy.init();
349        proxy.start();
350
351        DummyService service = new DummyService
352            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
353             new QName("DummyService", "DummyService"));
354
355        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
356       
357        ObjectFactory factory = new ObjectFactory();
358        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
359        VerifyUserOutMsgType response = dummyService.verifyUser(request);
360
361        assertNotNull(response);
362       
363        proxy.stop();
364        proxy = null;
365
366        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
367       
368        assertTrue(logFile.exists());
369       
370        HTTPLogParser parser = new HTTPLogParser();
371
372        parser.parseFile(logFile);
373
374        // check the sequences
375        Collection<List<Event>> sequences = parser.getSequences();
376
377        assertNotNull(sequences);
378
379        Iterator<List<Event>> iterator = sequences.iterator();
380        assertTrue(iterator.hasNext());
381
382        List<Event> sequence = iterator.next();
383        assertFalse(iterator.hasNext());
384
385        assertNotNull(sequence);
386        assertEquals(2, sequence.size());
387
388        assertEvent(sequence.get(0), "GET", null, null); // get WSDL
389        assertEvent(sequence.get(1), "POST", null, null); // send request
390    }
391   
392    @SuppressWarnings("unchecked")
393    @Test
394    public void test_SOAP_Remote() throws Exception {
395        proxy = new HttpMonitoringProxy
396            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
397                            "localhost:" + MONITOR_PORT  });
398
399        proxy.init();
400        proxy.start();
401
402        DummyService service = new DummyService
403            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
404             new QName("DummyService", "DummyService"));
405
406        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
407       
408        ObjectFactory factory = new ObjectFactory();
409        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
410        VerifyUserOutMsgType response = dummyService.verifyUser(request);
411
412        assertNotNull(response);
413       
414        proxy.stop();
415        proxy = null;
416
417        // give the proxy some time to send the copy of the message
418        while(messages.size() < 1) {
419            Thread.sleep(1000);
420        }
421       
422        assertEquals(2, messages.size());
423       
424        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
425        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
426       
427        JAXBElement<HttpExchange> jaxObject =
428            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
429       
430        HttpExchange exchange = jaxObject.getValue();
431       
432        assertEquals(Method.GET, exchange.getRequest().getMethod());
433
434        assertNull(exchange.getRequest().getContent());
435        assertNotNull(exchange.getResponse().getContent().getData());
436
437        jaxObject =
438            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(1)));
439           
440        exchange = jaxObject.getValue();
441           
442        assertEquals(Method.POST, exchange.getRequest().getMethod());
443
444        assertNotNull(exchange.getRequest().getContent().getData());
445        assertNotNull(exchange.getResponse().getContent().getData());
446    }
447   
448    /**
449     *
450     */
451    private void assertEvent(Event event, String method, String message, String response) {
452        assertNotNull(event);
453        assertNotNull(event.getType());
454        assertNotNull(event.getTarget());
455       
456        assertTrue(event.getType() instanceof HTTPEventType);
457        assertTrue(event.getTarget() instanceof HTTPTarget);
458       
459        HttpExchange exchange = ((HTTPEventType) event.getType()).getExchange();
460       
461        assertEquals(Method.fromValue(method), exchange.getRequest().getMethod());
462       
463        if (message != null) {
464            assertEquals(message, exchange.getRequest().getContent().getData());
465        }
466        else if (exchange.getRequest().getContent() != null) {
467            System.err.println(exchange.getRequest().getContent().getData());
468        }
469       
470        if (response != null) {
471            assertEquals(response, exchange.getResponse().getContent().getData());
472        }
473        else if (exchange.getResponse().getContent() != null) {
474            System.err.println(exchange.getResponse().getContent().getData());
475        }
476    }
477
478    /**
479     * @author Patrick Harms
480     */
481    private class DummyMonitorServlet extends HttpServlet {
482
483        /**  */
484        private static final long serialVersionUID = 1L;
485       
486        /* (non-Javadoc)
487         * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
488         */
489        @Override
490        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
491            throws ServletException, IOException
492        {
493            messages.add(readToString(req.getReader()));
494           
495            resp.setStatus(HttpServletResponse.SC_OK);
496        }
497       
498       
499    }
500
501}
Note: See TracBrowser for help on using the repository browser.