[1376] | 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.util.Collection; |
---|
| 25 | import java.util.Iterator; |
---|
| 26 | import java.util.List; |
---|
| 27 | |
---|
| 28 | import javax.xml.bind.JAXBContext; |
---|
| 29 | import javax.xml.bind.Marshaller; |
---|
| 30 | |
---|
| 31 | import org.apache.http.HttpEntity; |
---|
| 32 | import org.apache.http.client.methods.HttpPost; |
---|
| 33 | import org.apache.http.entity.ContentType; |
---|
| 34 | import org.apache.http.entity.StringEntity; |
---|
| 35 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
| 36 | import org.junit.Test; |
---|
| 37 | |
---|
| 38 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 39 | import de.ugoe.cs.autoquest.http.HTTPTestUtils; |
---|
| 40 | import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; |
---|
| 41 | import de.ugoe.cs.autoquest.httpmonitor.exchange.ObjectFactory; |
---|
| 42 | import de.ugoe.cs.autoquest.httpmonitor.exchange.Session; |
---|
| 43 | import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; |
---|
| 44 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
| 45 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
| 47 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * @author Patrick Harms |
---|
| 51 | */ |
---|
| 52 | public class HttpMonitorTest extends AbstractTC { |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * |
---|
| 56 | */ |
---|
| 57 | private HttpMonitoringProxy proxy; |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * |
---|
| 61 | */ |
---|
| 62 | private HttpMonitor monitor; |
---|
| 63 | |
---|
| 64 | /* (non-Javadoc) |
---|
| 65 | * @see de.ugoe.cs.autoquest.httpmonitor.AbstractTC#setUpHook() |
---|
| 66 | */ |
---|
| 67 | @Override |
---|
| 68 | protected void setUpHook() throws Exception { |
---|
| 69 | // nothing to do |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * |
---|
| 74 | */ |
---|
| 75 | public void tearDownHook() throws Exception { |
---|
| 76 | if (proxy != null) { |
---|
| 77 | try { |
---|
| 78 | proxy.stop(); |
---|
| 79 | } |
---|
| 80 | finally { |
---|
| 81 | proxy = null; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | if (monitor != null) { |
---|
| 85 | try { |
---|
| 86 | monitor.stop(); |
---|
| 87 | } |
---|
| 88 | finally { |
---|
| 89 | monitor = null; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | @Test |
---|
| 95 | public void test_SimulatedSession_MonitorOnly() throws Exception { |
---|
| 96 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, PORT +"" }); |
---|
| 97 | |
---|
| 98 | monitor.init(); |
---|
| 99 | monitor.start(); |
---|
| 100 | |
---|
| 101 | Session simulatedSession = HTTPTestUtils.createRandomSession(50); |
---|
| 102 | sendExchanges(simulatedSession.getHttpExchange()); |
---|
| 103 | |
---|
| 104 | monitor.stop(); |
---|
| 105 | monitor = null; |
---|
| 106 | |
---|
| 107 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
| 108 | |
---|
| 109 | assertTrue(logFile.exists()); |
---|
| 110 | |
---|
| 111 | HTTPLogParser parser = new HTTPLogParser(); |
---|
| 112 | |
---|
| 113 | parser.parseFile(logFile); |
---|
| 114 | |
---|
| 115 | // check the sequences |
---|
| 116 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 117 | |
---|
| 118 | assertNotNull(sequences); |
---|
| 119 | |
---|
| 120 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 121 | assertTrue(iterator.hasNext()); |
---|
| 122 | |
---|
| 123 | List<Event> sequence = iterator.next(); |
---|
| 124 | assertFalse(iterator.hasNext()); |
---|
| 125 | |
---|
| 126 | assertNotNull(sequence); |
---|
| 127 | assertEquals(simulatedSession.getHttpExchange().size(), sequence.size()); |
---|
| 128 | |
---|
| 129 | System.out.println("{"); |
---|
| 130 | System.out.println(" {"); |
---|
| 131 | for (int j = 0; j < sequence.size(); j++) { |
---|
| 132 | System.out.print(" "); |
---|
| 133 | System.out.print(sequence.get(j)); |
---|
| 134 | System.out.println(","); |
---|
| 135 | |
---|
| 136 | assertNotNull(sequence.get(j)); |
---|
| 137 | assertNotNull(sequence.get(j).getType()); |
---|
| 138 | assertTrue(sequence.get(j).getType() instanceof HTTPEventType); |
---|
| 139 | |
---|
| 140 | assertNotNull(sequence.get(j).getTarget()); |
---|
| 141 | assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget); |
---|
| 142 | |
---|
| 143 | HTTPTestUtils.assertExchangeEquals |
---|
| 144 | (simulatedSession.getHttpExchange().get(j), |
---|
| 145 | ((HTTPEventType) sequence.get(j).getType()).getExchange()); |
---|
| 146 | |
---|
| 147 | assertEquals(HTTPUtils.toString(simulatedSession.getHttpExchange().get(j).getReceiver()), |
---|
| 148 | ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier()); |
---|
| 149 | } |
---|
| 150 | System.out.println(" }"); |
---|
| 151 | System.out.println("}"); |
---|
| 152 | System.out.println("\n\n"); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | @Test |
---|
| 156 | public void test_SimpleText_ProxyAndMonitor() throws Exception { |
---|
| 157 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, (PORT + 2) + "" }); |
---|
| 158 | |
---|
| 159 | monitor.init(); |
---|
| 160 | monitor.start(); |
---|
| 161 | |
---|
| 162 | proxy = new HttpMonitoringProxy |
---|
| 163 | (new String[] { LOG_FILE_DIR, PORT +"", |
---|
| 164 | "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); |
---|
| 165 | |
---|
| 166 | proxy.init(); |
---|
| 167 | proxy.start(); |
---|
| 168 | |
---|
| 169 | String message = "dummy message"; |
---|
| 170 | String expectedResponse = "response content"; |
---|
| 171 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
| 172 | |
---|
| 173 | assertEquals(expectedResponse, response); |
---|
| 174 | |
---|
| 175 | // the monitor needs some time to receive the exchange --> give it |
---|
| 176 | Thread.sleep(1000); |
---|
| 177 | |
---|
| 178 | monitor.stop(); |
---|
| 179 | monitor = null; |
---|
| 180 | |
---|
| 181 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
| 182 | |
---|
| 183 | assertTrue(logFile.exists()); |
---|
| 184 | |
---|
| 185 | HTTPLogParser parser = new HTTPLogParser(); |
---|
| 186 | |
---|
| 187 | parser.parseFile(logFile); |
---|
| 188 | |
---|
| 189 | // check the sequences |
---|
| 190 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 191 | |
---|
| 192 | assertNotNull(sequences); |
---|
| 193 | |
---|
| 194 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 195 | assertTrue(iterator.hasNext()); |
---|
| 196 | |
---|
| 197 | List<Event> sequence = iterator.next(); |
---|
| 198 | assertFalse(iterator.hasNext()); |
---|
| 199 | |
---|
| 200 | assertNotNull(sequence); |
---|
| 201 | assertEquals(1, sequence.size()); |
---|
| 202 | |
---|
| 203 | assertNotNull(sequence.get(0)); |
---|
| 204 | assertNotNull(sequence.get(0).getType()); |
---|
| 205 | assertTrue(sequence.get(0).getType() instanceof HTTPEventType); |
---|
| 206 | |
---|
| 207 | assertNotNull(sequence.get(0).getTarget()); |
---|
| 208 | assertTrue(sequence.get(0).getTarget() instanceof HTTPTarget); |
---|
| 209 | |
---|
| 210 | HttpExchange exchange = ((HTTPEventType) sequence.get(0).getType()).getExchange(); |
---|
| 211 | |
---|
| 212 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
| 213 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | @Test |
---|
| 217 | public void test_XMLMessage_ProxyAndMonitor() throws Exception { |
---|
| 218 | monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, (PORT + 2) + "" }); |
---|
| 219 | |
---|
| 220 | monitor.init(); |
---|
| 221 | monitor.start(); |
---|
| 222 | |
---|
| 223 | proxy = new HttpMonitoringProxy |
---|
| 224 | (new String[] { LOG_FILE_DIR, PORT +"", |
---|
| 225 | "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); |
---|
| 226 | |
---|
| 227 | proxy.init(); |
---|
| 228 | proxy.start(); |
---|
| 229 | |
---|
| 230 | String message = |
---|
| 231 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
| 232 | "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
| 233 | " <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" + |
---|
| 234 | "</xsd:schema>"; |
---|
| 235 | |
---|
| 236 | String expectedResponse = |
---|
| 237 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
| 238 | "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
| 239 | " <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" + |
---|
| 240 | " <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" + |
---|
| 241 | " <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" + |
---|
| 242 | " <headers>" + |
---|
| 243 | " <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" + |
---|
| 244 | " <header key=\"Connection\" value=\"keep-alive\"/>" + |
---|
| 245 | " <header key=\"Host\" value=\"localhost:19098\"/>" + |
---|
| 246 | " <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" + |
---|
| 247 | " <header key=\"Content-Length\" value=\"13\"/>" + |
---|
| 248 | " </headers>" + |
---|
| 249 | " <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" + |
---|
| 250 | " length=\"13\">" + |
---|
| 251 | " <data>dummy message</data>" + |
---|
| 252 | " </content>" + |
---|
| 253 | " </request>" + |
---|
| 254 | " <response status=\"200\">" + |
---|
| 255 | " <headers>" + |
---|
| 256 | " <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" + |
---|
| 257 | " <header key=\"Content-Length\" value=\"16\"/>" + |
---|
| 258 | " </headers>" + |
---|
| 259 | " <content encoding=\"ISO-8859-1\" length=\"16\">" + |
---|
| 260 | " <data>response content</data>" + |
---|
| 261 | " </content>" + |
---|
| 262 | " </response>" + |
---|
| 263 | "</httpEvent>"; |
---|
| 264 | |
---|
| 265 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
| 266 | |
---|
| 267 | assertEquals(expectedResponse, response); |
---|
| 268 | |
---|
| 269 | // the monitor needs some time to receive the exchange --> give it |
---|
| 270 | Thread.sleep(1000); |
---|
| 271 | |
---|
| 272 | monitor.stop(); |
---|
| 273 | monitor = 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 | assertNotNull(sequence.get(0)); |
---|
| 298 | assertNotNull(sequence.get(0).getType()); |
---|
| 299 | assertTrue(sequence.get(0).getType() instanceof HTTPEventType); |
---|
| 300 | |
---|
| 301 | assertNotNull(sequence.get(0).getTarget()); |
---|
| 302 | assertTrue(sequence.get(0).getTarget() instanceof HTTPTarget); |
---|
| 303 | |
---|
| 304 | HttpExchange exchange = ((HTTPEventType) sequence.get(0).getType()).getExchange(); |
---|
| 305 | |
---|
| 306 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
| 307 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | * |
---|
| 312 | */ |
---|
| 313 | private void sendExchanges(List<HttpExchange> exchanges) throws Exception { |
---|
| 314 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
| 315 | |
---|
| 316 | for (HttpExchange exchange : exchanges) { |
---|
| 317 | HttpPost httpRequest = new HttpPost("http://localhost:" + PORT + "/"); |
---|
| 318 | |
---|
| 319 | JAXBContext jaxbContext = |
---|
| 320 | JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
| 321 | Marshaller marshaller = jaxbContext.createMarshaller(); |
---|
| 322 | |
---|
| 323 | StringWriter out = new StringWriter(); |
---|
| 324 | marshaller.marshal(new ObjectFactory().createHttpExchange(exchange), out); |
---|
| 325 | |
---|
| 326 | HttpEntity entity = new StringEntity |
---|
| 327 | (out.toString(), ContentType.create("text/plain", "UTF-8")); |
---|
| 328 | ((HttpPost) httpRequest).setEntity(entity); |
---|
| 329 | |
---|
| 330 | try { |
---|
| 331 | httpclient.execute(httpRequest); |
---|
| 332 | } |
---|
| 333 | finally { |
---|
| 334 | httpRequest.releaseConnection(); |
---|
| 335 | out.close(); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | } |
---|