// Copyright 2012 Georg-August-Universität Göttingen, Germany // // 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. package de.ugoe.cs.autoquest.httpmonitor; import static org.junit.Assert.*; import java.io.File; import java.io.IOException; import java.io.StringReader; import java.net.URL; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.junit.Test; import dummyservice.ObjectFactory; import dummyservice.VerifyUserInMsgType; import dummyservice.VerifyUserOutMsgType; import de.ugoe.cs.autoquest.eventcore.Event; import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; import dummyservice.DummyService; import dummyservice.DummyServicePortType; /** * @author Patrick Harms */ public class HttpMonitoringProxyTest extends AbstractTC { /** * */ private HttpMonitoringProxy proxy; /** * the jetty web server used for receiving messages copied by the proxy */ private Server dummyMonitor; /** * the messages received by the dummy monitor */ private List messages; /** * */ protected void setUpHook() throws Exception { // setup a simple HTTP server messages = new LinkedList(); dummyMonitor = new Server(MONITOR_PORT); ServletContextHandler root = new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS); root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*"); dummyMonitor.start(); } /** * */ protected void tearDownHook() throws Exception { if (proxy != null) { try { proxy.stop(); } finally { proxy = null; } } if (dummyMonitor != null) { try { dummyMonitor.stop(); } finally { dummyMonitor = null; } } messages = null; } /** * */ @Test(expected=java.lang.IllegalArgumentException.class) public void test_InvalidInitialization_01() throws Exception { proxy = new HttpMonitoringProxy(new String[] { }); } /** * */ @Test public void test_SimpleText_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "local" }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String response = sendDummyMessage("POST", null, message, expectedResponse); assertEquals(expectedResponse, response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", null, message, expectedResponse); } /** * */ @Test public void test_SimpleText_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String response = sendDummyMessage("POST", null, message, expectedResponse); assertEquals(expectedResponse, response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { System.out.println("waiting for message to be send by the proxy"); Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse); } /** * */ @Test public void test_XMLMessage_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "local" }); proxy.init(); proxy.start(); String message = "" + "" + " " + ""; String expectedResponse = "" + "" + " 127.0.0.1127.0.0.142688" + " 127.0.0.1127.0.0.119098" + " " + " " + "
" + "
" + "
" + "
" + "
" + " " + " " + " dummy message" + " " + " " + " " + " " + "
" + "
" + " " + " " + " response content" + " " + " " + ""; String response = sendDummyMessage("POST", null, message, expectedResponse); assertEquals(expectedResponse, response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", null, message, expectedResponse); } /** * */ @Test public void test_XMLMessage_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); proxy.init(); proxy.start(); String message = "" + "" + " " + ""; String expectedResponse = "" + "" + " 127.0.0.1127.0.0.142688" + " 127.0.0.1127.0.0.119098" + " " + " " + "
" + "
" + "
" + "
" + "
" + " " + " " + " dummy message" + " " + " " + " " + " " + "
" + "
" + " " + " " + " response content" + " " + " " + ""; String response = sendDummyMessage("POST", null, message, expectedResponse); assertEquals(expectedResponse, response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { System.out.println("waiting for message to be send by the proxy"); Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse); } /** * */ @Test public void test_SOAP_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "local" }); proxy.init(); proxy.start(); DummyService service = new DummyService (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"), new QName("DummyService", "DummyService")); DummyServicePortType dummyService = service.getDummyServiceSOAPPort(); ObjectFactory factory = new ObjectFactory(); VerifyUserInMsgType request = factory.createVerifyUserInMsgType(); VerifyUserOutMsgType response = dummyService.verifyUser(request); assertNotNull(response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(2, sequence.size()); assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL assertEvent(sequence.get(1), "POST", null, null, null); // send request } /** * */ @SuppressWarnings("unchecked") @Test public void test_SOAP_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); proxy.init(); proxy.start(); DummyService service = new DummyService (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"), new QName("DummyService", "DummyService")); DummyServicePortType dummyService = service.getDummyServiceSOAPPort(); ObjectFactory factory = new ObjectFactory(); VerifyUserInMsgType request = factory.createVerifyUserInMsgType(); VerifyUserOutMsgType response = dummyService.verifyUser(request); assertNotNull(response); proxy.stop(); proxy = null; // give the proxy some time to send the copy of the message while(messages.size() < 1) { System.out.println("waiting for message to be send by the proxy"); Thread.sleep(1000); } assertEquals(2, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); assertExchange(jaxObject.getValue(), "GET", "wsdl", null, null); jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(1))); assertExchange(jaxObject.getValue(), "POST", null, null, null); } /** * */ @Test public void test_LargeRequest_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "local" }); proxy.init(); proxy.start(); StringBuffer message = new StringBuffer(); StringBuffer expectedResponse = new StringBuffer(); for (int i = 0; i < 1000; i++) { message.append(" # " + i + " test request data"); expectedResponse.append(" # " + i + " test response data"); } String response = sendDummyMessage("POST", null, message.toString(), expectedResponse.toString()); assertEquals(expectedResponse.toString(), response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", null, message.toString(), expectedResponse.toString()); } /** * */ @Test public void test_LargeRequest_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); proxy.init(); proxy.start(); StringBuffer message = new StringBuffer(); StringBuffer expectedResponse = new StringBuffer(); for (int i = 0; i < 1000; i++) { message.append(" # " + i + " test request data"); expectedResponse.append(" # " + i + " test response data"); } String response = sendDummyMessage("POST", null, message.toString(), expectedResponse.toString()); assertEquals(expectedResponse.toString(), response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { System.out.println("waiting for message to be send by the proxy"); Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); assertExchange(jaxObject.getValue(), "POST", null, message.toString(), expectedResponse.toString()); } /** * */ @Test public void test_Query_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "local" }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String query = "key=value&key2=value2&key3=%3E%3CXXX"; String response = sendDummyMessage("POST", query, message, expectedResponse); assertEquals(expectedResponse, response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", query, message, expectedResponse); } /** * */ @Test public void test_Query_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String query = "key=value&key2=value2&key3=%3E%3CXXX"; String response = sendDummyMessage("POST", query, message, expectedResponse); assertEquals(expectedResponse, response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { System.out.println("waiting for message to be send by the proxy"); Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); assertExchange(jaxObject.getValue(), "POST", query, message, expectedResponse); } /** * @author Patrick Harms */ private class DummyMonitorServlet extends HttpServlet { /** */ private static final long serialVersionUID = 1L; /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { messages.add(readToString(req.getReader())); resp.setStatus(HttpServletResponse.SC_OK); System.out.println("send ok response"); } } }