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

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