source: trunk/autoquest-htmlmonitor-test/src/test/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorServerTest.java @ 922

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
File size: 13.3 KB
Line 
1package de.ugoe.cs.autoquest.htmlmonitor;
2
3import static org.junit.Assert.*;
4
5import org.apache.http.HttpEntity;
6import org.apache.http.HttpResponse;
7import org.apache.http.client.methods.HttpPost;
8import org.apache.http.entity.ContentType;
9import org.apache.http.entity.StringEntity;
10import org.apache.http.impl.client.DefaultHttpClient;
11import org.junit.After;
12import org.junit.Before;
13import org.junit.Test;
14
15import de.ugoe.cs.util.console.TextConsole;
16
17/**
18 * <p>
19 * TODO comment
20 * </p>
21 *
22 * @author Patrick Harms
23 */
24public class HtmlMonitorServerTest implements HtmlMonitorMessageListener {
25   
26    /**
27     *
28     */
29    public static final TextConsole CONSOLE = new TextConsole();
30   
31    /**
32     *
33     */
34    private static final int PORT = 19098;
35
36    /**
37     *
38     */
39    private HtmlEvent[] events;
40
41    /**
42     *
43     */
44    private String eventHandlingError;
45
46    /**
47     *
48     */
49    private HtmlMonitorServer server;
50
51    /**
52     *
53     */
54    @Before
55    public void setUp() throws Exception {
56        server = new HtmlMonitorServer(PORT, this);
57        server.init();
58        server.start();
59    }
60   
61    /**
62     *
63     */
64    @After
65    public void tearDown() throws Exception {
66        events = null;
67        eventHandlingError = null;
68
69        if (server != null) {
70            try {
71                server.stop();
72            }
73            finally {
74                server = null;
75            }
76        }
77    }
78   
79
80    /**
81     *
82     */
83    @Test
84    public void testSimpleMessage() throws Exception {
85        String message =
86            "{" +
87            "  \"message\": {" +
88            "    \"clientInfos\": {" +
89            "      \"clientId\":\"123\"," +
90            "      \"userAgent\":\"Agent\"," +
91            "      \"title\":\"Title\"," +
92            "      \"url\":\"http://host/path\"" +
93            "    }," +
94            "    \"events\":" +
95            "    [ {" +
96            "        \"time\":\"12345\"," +
97            "        \"path\":\"/html[0]/body(id=gsr)\"," +
98            "        \"eventType\":\"onunload\"" +
99            "      }" +
100            "    ]" +
101            "  }" +
102            "}";
103       
104        sendMessageAndAssertResponse(message);
105       
106        if (eventHandlingError != null) {
107            fail(eventHandlingError);
108        }
109       
110        assertNotNull(events);
111        assertEquals(1, events.length);
112       
113        assertEquals(new Long(12345), events[0].getTime());
114        assertEquals("/html[0]/body(id=gsr)", events[0].getPath());
115        assertEquals("onunload", events[0].getEventType());
116       
117        assertNull(events[0].getKey());
118        assertNull(events[0].getScrollPosition());
119        assertNull(events[0].getCoordinates());
120       
121        assertEquals("Title", events[0].getClientInfos().getTitle());
122        assertEquals("Agent", events[0].getClientInfos().getUserAgent());
123        assertEquals("http://host/path", events[0].getClientInfos().getUrl().toString());
124        assertEquals("123", events[0].getClientInfos().getClientId());
125    }
126
127    /**
128     *
129     */
130    @Test
131    public void testComplexMessage() throws Exception {
132        String message =
133            "{" +
134            "  \"message\": {" +
135            "    \"clientInfos\": {" +
136            "      \"clientId\":\"123\"," +
137            "      \"userAgent\":\"Agent\"," +
138            "      \"title\":\"Title\"," +
139            "      \"url\":\"http://host/path\"" +
140            "    }," +
141            "    \"events\":" +
142            "    [" +
143            "      {" +
144            "        \"time\":\"1\"," +
145            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input1)\"," +
146            "        \"eventType\":\"onclick\"," +
147            "        \"coordinates\": [\"194\", \"7\"]" +
148            "      }," +
149            "      {" +
150            "        \"time\":\"2\"," +
151            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input2)\"," +
152            "        \"eventType\":\"ondblclick\"," +
153            "        \"coordinates\": [\"194\", \"7\"]" +
154            "      }," +
155            "      {" +
156            "        \"time\":\"3\"," +
157            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input3)\"," +
158            "        \"eventType\":\"onfocus\"" +
159            "      }," +
160            "      {" +
161            "        \"time\":\"4\"," +
162            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input4)\"," +
163            "        \"eventType\":\"onclick\"," +
164            "        \"coordinates\": [\"125\", \"14\"]" +
165            "      }," +
166            "      {" +
167            "        \"time\":\"5\"," +
168            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input5)\"," +
169            "        \"eventType\":\"onfocus\"" +
170            "      }," +
171            "      {" +
172            "        \"time\":\"6\"," +
173            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input6)\"," +
174            "        \"eventType\":\"onfocus\"" +
175            "      }," +
176            "      {" +
177            "        \"time\":\"7\"," +
178            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input7)\"," +
179            "        \"eventType\":\"onfocus\"" +
180            "      }," +
181            "      {" +
182            "        \"time\":\"8\"," +
183            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input8)\"," +
184            "        \"eventType\":\"onclick\"," +
185            "        \"coordinates\": [\"255\", \"4\"]" +
186            "      }," +
187            "      {" +
188            "        \"time\":\"9\"," +
189            "        \"path\":\"/html[0]/body(id=gsr)\"," +
190            "        \"eventType\":\"onscroll\"," +
191            "        \"scrollPosition\":\"165\"" +
192            "      }," +
193            "      {" +
194            "        \"time\":\"10\"," +
195            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input10)\"," +
196            "        \"eventType\":\"onclick\"," +
197            "        \"coordinates\": [\"516\", \"154\"]" +
198            "      }" +
199            "    ]" +
200            "  }" +
201            "}";
202       
203        sendMessageAndAssertResponse(message);
204       
205        if (eventHandlingError != null) {
206            fail(eventHandlingError);
207        }
208       
209        assertNotNull(events);
210        assertEquals(10, events.length);
211
212        for (int i = 0; i < events.length; i++) {
213            assertEquals("event " + i, new Long(i + 1), events[i].getTime());
214           
215            if (i == 8) {
216                assertEquals("event " + i, "/html[0]/body(id=gsr)", events[i].getPath());
217            }
218            else {
219                assertEquals("event " + i, "/html[0]/body(id=gsr)/input(id=input" + (i + 1) + ")",
220                             events[i].getPath());
221            }
222           
223            if ((i == 1)) {
224                assertEquals("event " + i, "ondblclick", events[i].getEventType());
225            }
226            else if ((i == 2) || ((4 <= i) && (i <= 6))) {
227                assertEquals("event " + i, "onfocus", events[i].getEventType());
228            }
229            else if ((i == 8)) {
230                assertEquals("event " + i, "onscroll", events[i].getEventType());
231            }
232            else {
233                assertEquals("event " + i, "onclick", events[i].getEventType());
234            }
235           
236            assertNull("event " + i, events[i].getKey());
237           
238            if ((i == 8)) {
239                assertNotNull("event " + i, events[i].getScrollPosition());
240                assertEquals("event " + i, new Integer(165), events[i].getScrollPosition());
241            }
242            else {
243                assertNull("event " + i, events[i].getScrollPosition());
244            }
245           
246            if ((i == 2) || ((4 <= i) && (i <= 6)) || (i == 8)) {
247                assertNull("event " + i, events[i].getCoordinates());
248            }
249            else {
250                assertNotNull("event " + i, events[i].getCoordinates());
251                assertEquals("event " + i, 2, events[i].getCoordinates().length);
252            }
253           
254            assertEquals("event " + i, "Title", events[i].getClientInfos().getTitle());
255            assertEquals("event " + i, "Agent", events[i].getClientInfos().getUserAgent());
256            assertEquals("event " + i, "http://host/path",
257                         events[i].getClientInfos().getUrl().toString());
258            assertEquals("event " + i, "123", events[i].getClientInfos().getClientId());
259        }
260    }
261   
262    /**
263     *
264     */
265    @Test
266    public void testInvalidMessages() throws Exception {
267        String message = "}";
268       
269        sendMessageAndAssertResponse(message);
270           
271        if (eventHandlingError != null) {
272            fail(eventHandlingError);
273        }
274
275        assertNull(events);
276
277        message = "blaublbidalslkdjflqkerowercalksdjfdlkkjdjfk";
278       
279        sendMessageAndAssertResponse(message);
280           
281        if (eventHandlingError != null) {
282            fail(eventHandlingError);
283        }
284
285        assertNull(events);
286       
287        // the following message doesn't work because of the missing scroll position in the event
288        message =
289            "{" +
290            "  \"message\": {" +
291            "    \"clientInfos\": {" +
292            "      \"clientId\":\"123\"," +
293            "      \"userAgent\":\"Agent\"," +
294            "      \"title\":\"Title\"," +
295            "      \"url\":\"http://host/path\"" +
296            "    }," +
297            "    \"events\":" +
298            "    [ {" +
299            "        \"time\":\"12345\"," +
300            "        \"path\":\"/html[0]/body(id=gsr)\"," +
301            "        \"eventType\":\"onscroll\"" +
302            "      }" +
303            "    ]" +
304            "  }" +
305            "}";
306       
307        sendMessageAndAssertResponse(message);
308       
309        if (eventHandlingError != null) {
310            fail(eventHandlingError);
311        }
312
313        assertNull(events);
314       
315        // the following message doesn't work because of the missing client id
316        message =
317            "{" +
318            "  \"message\": {" +
319            "    \"clientInfos\": {" +
320            "      \"userAgent\":\"Agent\"," +
321            "      \"title\":\"Title\"," +
322            "      \"url\":\"http://host/path\"" +
323            "    }," +
324            "    \"events\":" +
325            "    [ {" +
326            "        \"time\":\"12345\"," +
327            "        \"path\":\"/html[0]/body(id=gsr)\"," +
328            "        \"eventType\":\"onunload\"" +
329            "      }" +
330            "    ]" +
331            "  }" +
332            "}";
333       
334        sendMessageAndAssertResponse(message);
335       
336        if (eventHandlingError != null) {
337            fail(eventHandlingError);
338        }
339
340        assertNull(events);
341       
342        // the following message doesn't work because of the invalid time stamp
343        message =
344            "{" +
345            "  \"message\": {" +
346            "    \"clientInfos\": {" +
347            "      \"clientId\":\"123\"," +
348            "      \"userAgent\":\"Agent\"," +
349            "      \"title\":\"Title\"," +
350            "      \"url\":\"http://host/path\"" +
351            "    }," +
352            "    \"events\":" +
353            "    [ {" +
354            "        \"time\":\"blub\"," +
355            "        \"path\":\"/html[0]/body(id=gsr)\"," +
356            "        \"eventType\":\"onunload\"" +
357            "      }" +
358            "    ]" +
359            "  }" +
360            "}";
361       
362        sendMessageAndAssertResponse(message);
363       
364        if (eventHandlingError != null) {
365            fail(eventHandlingError);
366        }
367
368        assertNull(events);
369       
370    }
371   
372    /* (non-Javadoc)
373     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitoringListener#handleEvents(de.ugoe.cs.autoquest.htmlmonitor.HtmlClientInfos, de.ugoe.cs.autoquest.htmlmonitor.HtmlEvent[])
374     */
375    @Override
376    public void handleMessage(HtmlClientInfos clientInfos, HtmlEvent[] events) {
377        if (clientInfos == null) {
378            eventHandlingError = "client infos were null";
379        }
380        else if (events == null) {
381            eventHandlingError = "events were null";
382        }
383        else {
384            for (HtmlEvent event : events) {
385                if (!clientInfos.equals(event.getClientInfos())) {
386                    eventHandlingError = "one of the events did not have a correct client info";
387                }
388            }
389           
390            this.events = events;
391        }
392    }
393
394    /**
395     * <p>
396     * TODO: comment
397     * </p>
398     *
399     * @param message
400     */
401    private void sendMessageAndAssertResponse(String message) throws Exception {
402        DefaultHttpClient httpclient = new DefaultHttpClient();
403        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
404        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
405        httpPost.setEntity(entity);
406       
407        try {
408            HttpResponse response = httpclient.execute(httpPost);
409           
410            // the monitor always returns 200 without any additional information. The client must
411            // never get more or less information. This is especially important for preventing
412            // hackers from finding out more
413            assertEquals(200, response.getStatusLine().getStatusCode());
414            assertTrue
415                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0));
416        }
417        finally {
418            httpPost.releaseConnection();
419        }
420    }
421
422}
Note: See TracBrowser for help on using the repository browser.