source: trunk/autoquest-htmlmonitor-test/src/test/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorTest.java @ 864

Last change on this file since 864 was 864, checked in by pharms, 12 years ago
  • initial import.
File size: 13.1 KB
Line 
1package de.ugoe.cs.autoquest.htmlmonitor;
2
3import static org.junit.Assert.*;
4
5import java.io.BufferedReader;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.InputStreamReader;
9import java.util.ArrayList;
10import java.util.List;
11
12import org.apache.http.HttpEntity;
13import org.apache.http.HttpResponse;
14import org.apache.http.client.methods.HttpPost;
15import org.apache.http.entity.ContentType;
16import org.apache.http.entity.StringEntity;
17import org.apache.http.impl.client.DefaultHttpClient;
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21
22import de.ugoe.cs.util.console.TextConsole;
23
24/**
25 * <p>
26 * TODO comment
27 * </p>
28 *
29 * @author Patrick Harms
30 */
31public class HtmlMonitorTest {
32
33    /**
34     *
35     */
36    public static final TextConsole CONSOLE = new TextConsole();
37   
38    /**
39     *
40     */
41    private final static String LOG_FILE_DIR = "target/tmp/logfiles/";
42   
43    /**
44     *
45     */
46    private static final int PORT = 19098;
47
48    /**
49     *
50     */
51    private HtmlMonitor htmlMonitor;
52
53    /**
54     *
55     */
56    @Before
57    public void setUp() throws Exception {
58        htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
59        htmlMonitor.init();
60        htmlMonitor.start();
61    }
62
63    /**
64     *
65     */
66    @After
67    public void tearDown() throws Exception {
68        if (htmlMonitor != null) {
69            try {
70                htmlMonitor.stop();
71            }
72            finally {
73                htmlMonitor = null;
74            }
75        }
76       
77        deleteFiles(new File(LOG_FILE_DIR));
78    }
79
80    /**
81     *
82     */
83    @Test
84    public void testOneSimpleMessage() throws Exception {
85        String clientId = "123";
86       
87        String message =
88            "{" +
89            "  \"message\": {" +
90            "    \"clientInfos\": {" +
91            "      \"clientId\":\"" + clientId + "\"," +
92            "      \"userAgent\":\"Agent\"," +
93            "      \"title\":\"Title\"," +
94            "      \"url\":\"http://host/path\"" +
95            "    }," +
96            "    \"events\":" +
97            "    [ {" +
98            "        \"time\":\"12345\"," +
99            "        \"path\":\"/html[0]/body(id=gsr)\"," +
100            "        \"eventType\":\"onunload\"" +
101            "      }" +
102            "    ]" +
103            "  }" +
104            "}";
105
106        sendMessageAndAssertResponse(message);
107       
108        File logFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
109                                "htmlmonitor_" + clientId + ".log");
110       
111        assertTrue(logFile.exists());
112       
113        String[] logEntries = readLogEntries(logFile);
114        assertEquals(1, logEntries.length);
115       
116        assertLogMessage(logEntries[0], clientId, "12345", "Title", "http://host/path", "Agent",
117                         "onunload", "/html[0]/body(id=gsr)");
118    }
119
120    /**
121     *
122     */
123    @Test
124    public void testSeveralMessagesInOneSession() throws Exception {
125        String clientId = "123";
126       
127        String message =
128            "{" +
129            "  \"message\": {" +
130            "    \"clientInfos\": {" +
131            "      \"clientId\":\"" + clientId + "\"," +
132            "      \"userAgent\":\"Agent\"," +
133            "      \"title\":\"Title\"," +
134            "      \"url\":\"http://host/path\"" +
135            "    }," +
136            "    \"events\":" +
137            "    [" +
138            "      {" +
139            "        \"time\":\"1\"," +
140            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input1)\"," +
141            "        \"eventType\":\"onclick\"," +
142            "        \"coordinates\": [\"194\", \"7\"]" +
143            "      }," +
144            "      {" +
145            "        \"time\":\"2\"," +
146            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input2)\"," +
147            "        \"eventType\":\"ondblclick\"," +
148            "        \"coordinates\": [\"194\", \"7\"]" +
149            "      }," +
150            "      {" +
151            "        \"time\":\"3\"," +
152            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input3)\"," +
153            "        \"eventType\":\"onfocus\"" +
154            "      }," +
155            "      {" +
156            "        \"time\":\"4\"," +
157            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input4)\"," +
158            "        \"eventType\":\"onclick\"," +
159            "        \"coordinates\": [\"125\", \"14\"]" +
160            "      }," +
161            "      {" +
162            "        \"time\":\"5\"," +
163            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input5)\"," +
164            "        \"eventType\":\"onfocus\"" +
165            "      }," +
166            "      {" +
167            "        \"time\":\"6\"," +
168            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input6)\"," +
169            "        \"eventType\":\"onfocus\"" +
170            "      }," +
171            "      {" +
172            "        \"time\":\"7\"," +
173            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input7)\"," +
174            "        \"eventType\":\"onfocus\"" +
175            "      }," +
176            "      {" +
177            "        \"time\":\"8\"," +
178            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input8)\"," +
179            "        \"eventType\":\"onclick\"," +
180            "        \"coordinates\": [\"255\", \"4\"]" +
181            "      }," +
182            "      {" +
183            "        \"time\":\"9\"," +
184            "        \"path\":\"/html[0]/body(id=gsr)\"," +
185            "        \"eventType\":\"onscroll\"," +
186            "        \"scrollPosition\":\"165\"" +
187            "      }," +
188            "      {" +
189            "        \"time\":\"10\"," +
190            "        \"path\":\"/html[0]/body(id=gsr)/input(id=input10)\"," +
191            "        \"eventType\":\"onclick\"," +
192            "        \"coordinates\": [\"516\", \"154\"]" +
193            "      }" +
194            "    ]" +
195            "  }" +
196            "}";
197 
198        sendMessageAndAssertResponse(message);
199       
200        File logFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
201                                "htmlmonitor_" + clientId + ".log");
202       
203        assertTrue(logFile.exists());
204       
205        String[] logEntries = readLogEntries(logFile);
206        assertEquals(10, logEntries.length);
207       
208        assertLogMessage(logEntries[0], clientId, "1", "Title", "http://host/path", "Agent",
209                         "onclick", "/html[0]/body(id=gsr)/input(id=input1)", "194,7");
210        assertLogMessage(logEntries[1], clientId, "2", "Title", "http://host/path", "Agent",
211                         "ondblclick", "/html[0]/body(id=gsr)/input(id=input2)", "194,7");
212        assertLogMessage(logEntries[2], clientId, "3", "Title", "http://host/path", "Agent",
213                         "onfocus", "/html[0]/body(id=gsr)/input(id=input3)");
214        assertLogMessage(logEntries[3], clientId, "4", "Title", "http://host/path", "Agent",
215                         "onclick", "/html[0]/body(id=gsr)/input(id=input4)", "125,14");
216        assertLogMessage(logEntries[4], clientId, "5", "Title", "http://host/path", "Agent",
217                         "onfocus", "/html[0]/body(id=gsr)/input(id=input5)");
218        assertLogMessage(logEntries[5], clientId, "6", "Title", "http://host/path", "Agent",
219                         "onfocus", "/html[0]/body(id=gsr)/input(id=input6)");
220        assertLogMessage(logEntries[6], clientId, "7", "Title", "http://host/path", "Agent",
221                         "onfocus", "/html[0]/body(id=gsr)/input(id=input7)");
222        assertLogMessage(logEntries[7], clientId, "8", "Title", "http://host/path", "Agent",
223                         "onclick", "/html[0]/body(id=gsr)/input(id=input8)", "255,4");
224        assertLogMessage(logEntries[8], clientId, "9", "Title", "http://host/path", "Agent",
225                         "onscroll", "/html[0]/body(id=gsr)", "165");
226        assertLogMessage(logEntries[9], clientId, "10", "Title", "http://host/path", "Agent",
227                         "onclick", "/html[0]/body(id=gsr)/input(id=input10)", "516,154");
228    }
229
230    /**
231     *
232     */
233    @Test
234    public void testSeveralSessions() throws Exception {
235        String clientId = "123";
236       
237        String message =
238            "{" +
239            "  \"message\": {" +
240            "    \"clientInfos\": {" +
241            "      \"clientId\":\"" + clientId + "\"," +
242            "      \"userAgent\":\"Agent\"," +
243            "      \"title\":\"Title\"," +
244            "      \"url\":\"http://host/path\"" +
245            "    }," +
246            "    \"events\":" +
247            "    [ {" +
248            "        \"time\":\"12345\"," +
249            "        \"path\":\"/html[0]/body(id=gsr)\"," +
250            "        \"eventType\":\"onunload\"" +
251            "      }" +
252            "    ]" +
253            "  }" +
254            "}";
255
256        sendMessageAndAssertResponse(message);
257       
258        int numberOfSessions = 10;
259        for (int i = 0; i < numberOfSessions; i++) {
260            htmlMonitor.stop();
261            htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) });
262            htmlMonitor.init();
263            htmlMonitor.start();
264            sendMessageAndAssertResponse(message);
265        }
266       
267        // assert 9 already rotated log files
268        for (int i = 0; i < (numberOfSessions - 1); i++) {
269            File logFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
270                                    "htmlmonitor_" + clientId + "_00" + i + ".log");
271       
272            assertTrue(logFile.exists());
273       
274            String[] logEntries = readLogEntries(logFile);
275            assertEquals(1, logEntries.length);
276       
277            assertLogMessage(logEntries[0], clientId, "12345", "Title", "http://host/path", "Agent",
278                             "onunload", "/html[0]/body(id=gsr)");
279        }
280
281        // and now the last but current one
282        File logFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator +
283                                "htmlmonitor_" + clientId + ".log");
284   
285        assertTrue(logFile.exists());
286   
287        String[] logEntries = readLogEntries(logFile);
288        assertEquals(1, logEntries.length);
289   
290        assertLogMessage(logEntries[0], clientId, "12345", "Title", "http://host/path", "Agent",
291                         "onunload", "/html[0]/body(id=gsr)");
292    }
293
294    /**
295     * <p>
296     * TODO: comment
297     * </p>
298     *
299     * @param message
300     */
301    private void sendMessageAndAssertResponse(String message) throws Exception {
302        DefaultHttpClient httpclient = new DefaultHttpClient();
303        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
304        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
305        httpPost.setEntity(entity);
306       
307        try {
308            HttpResponse response = httpclient.execute(httpPost);
309           
310            // the monitor always returns 200 without any additional information. The client must
311            // never get more or less information. This is especially important for preventing
312            // hackers from finding out more
313            assertEquals(200, response.getStatusLine().getStatusCode());
314            assertTrue
315                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0));
316        }
317        finally {
318            httpPost.releaseConnection();
319        }
320    }
321
322    /**
323     * <p>
324     * TODO: comment
325     * </p>
326     *
327     * @param logFile
328     * @return
329     */
330    private String[] readLogEntries(File logFile) throws Exception {
331        List<String> logEntries = new ArrayList<String>();
332        BufferedReader reader = null;
333       
334        try {
335            reader = new BufferedReader
336                (new InputStreamReader(new FileInputStream(logFile)));
337       
338            String line = null;
339            while ((line = reader.readLine()) != null) {
340                logEntries.add(line);
341            }
342        }
343        finally {
344            if (reader != null) {
345                reader.close();
346            }
347        }
348       
349        return logEntries.toArray(new String[logEntries.size()]);
350    }
351
352    /**
353     *
354     */
355    private void assertLogMessage(String    logMessage,
356                                  String... assertedMessageContent)
357    {
358        StringBuffer assertedMessage = new StringBuffer();
359       
360        for (int i = 0; i < assertedMessageContent.length; i++) {
361            if (i > 0) {
362                assertedMessage.append(' ');
363            }
364           
365            assertedMessage.append('"');
366            assertedMessage.append(assertedMessageContent[i]);
367            assertedMessage.append('"');
368        }
369       
370        assertEquals(assertedMessage.toString(), logMessage);
371    }
372
373    /**
374     * <p>
375     * TODO: comment
376     * </p>
377     *
378     * @param file
379     */
380    private void deleteFiles(File file) {
381        if (file.exists()) {
382            if (file.isDirectory()) {
383                for (File child : file.listFiles()) {
384                    deleteFiles(child);
385                }
386            }
387           
388            try {
389                file.delete();
390            }
391            catch (Exception e) {
392                // ignore and delete as much as possible
393            }
394        }
395    }
396
397}
Note: See TracBrowser for help on using the repository browser.