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