source: trunk/autoquest-generic-event-monitor-test/src/test/java/de/ugoe/cs/autoquest/genericeventmonitor/GenericEventMonitorServerTest.java @ 2164

Last change on this file since 2164 was 2164, checked in by pharms, 7 years ago
  • changes for first VR oriented usability evaluation
  • Property svn:mime-type set to text/plain
File size: 20.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.genericeventmonitor;
16
17import static org.junit.Assert.*;
18
19import java.util.HashMap;
20import java.util.Map;
21
22import org.apache.http.HttpEntity;
23import org.apache.http.HttpResponse;
24import org.apache.http.client.methods.HttpPost;
25import org.apache.http.entity.ContentType;
26import org.apache.http.entity.StringEntity;
27import org.apache.http.impl.client.DefaultHttpClient;
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31
32import de.ugoe.cs.autoquest.genericeventmonitor.ClientInfos;
33import de.ugoe.cs.autoquest.genericeventmonitor.GenericEvent;
34import de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorMessageListener;
35import de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorServer;
36import de.ugoe.cs.autoquest.genericeventmonitor.GenericEventTarget;
37import de.ugoe.cs.util.console.TextConsole;
38
39/**
40 *
41 * @author Patrick Harms
42 */
43public class GenericEventMonitorServerTest implements GenericEventMonitorMessageListener {
44   
45    /**
46     *
47     */
48    public static final TextConsole CONSOLE = new TextConsole();
49   
50    /**
51     *
52     */
53    private static final int PORT = 19098;
54
55    /**
56     *
57     */
58    private GenericEvent[] events;
59
60    /**
61     *
62     */
63    private GenericEventTarget[] eventTargetStructures;
64
65    /**
66     *
67     */
68    private String eventHandlingError;
69
70    /**
71     *
72     */
73    private GenericEventMonitorServer server;
74
75    /**
76     *
77     */
78    @Before
79    public void setUp() throws Exception {
80        server = new GenericEventMonitorServer(PORT, this);
81        server.init();
82        server.start();
83    }
84   
85    /**
86     *
87     */
88    @After
89    public void tearDown() throws Exception {
90        events = null;
91        eventHandlingError = null;
92
93        if (server != null) {
94            try {
95                server.stop();
96            }
97            finally {
98                server = null;
99            }
100        }
101    }
102   
103
104    /**
105     *
106     */
107    @Test
108    public void testSimpleMessage() throws Exception {
109        String clientId = "123";
110        String appId = "456";
111       
112        String message =
113            "{" +
114            "  \"message\": {" +
115            "    \"clientInfos\": {" +
116            "      \"clientId\":\"" + clientId + "\"," +
117            "      \"appId\":\"" + appId + "\"," +
118            "    }," +
119            "    \"targetStructure\": [{" +
120            "      \"targetId\":\"target1\"," +
121            "      \"param1\":\"value1\"," +
122            "      \"param3\":\"value3\"," +
123            "      \"param2\":\"value2\"," +
124            "      \"children\":" +
125            "      [ {" +
126            "          \"targetId\":\"target3\"," +
127            "          \"index\":\"0\"," +
128            "        }," +
129            "        {" +
130            "          \"targetId\":\"target2\"," +
131            "          \"htmlId\":\"gsr\"," +
132            "        }" +
133            "      ]" +
134            "    }]," +
135            "    \"events\":" +
136            "    [ {" +
137            "        \"time\":\"12345\"," +
138            "        \"targetId\":\"target2\"," +
139            "        \"type\":\"gaze\"," +
140            "        \"xcoordinate\": \"194\"," +
141            "        \"ycoordinate\": \"12\"" +
142            "      }" +
143            "    ]" +
144            "  }" +
145            "}";
146       
147        sendMessageAndAssertResponse(message);
148       
149        if (eventHandlingError != null) {
150            fail(eventHandlingError);
151        }
152       
153        // check the target structure
154        GenericEventTarget parent = null;
155       
156        assertEquals(1, eventTargetStructures.length);
157        GenericEventTarget target = eventTargetStructures[0];
158       
159        assertTarget(target, parent, 2, new String[] {"param1", "value1"},
160                     new String[] {"param2", "value2"},
161                     new String[] {"param3", "value3"});
162       
163        parent = target;
164        target = parent.getChildren().get(0);
165        assertTarget(target, parent, 0, new String[] {"index", "0"});
166        target = parent.getChildren().get(1);
167        assertTarget(target, parent, 0, new String[] {"htmlId", "gsr"});
168
169        // check the event
170        assertNotNull(events);
171        assertEquals(1, events.length);
172       
173        assertEvent(0, 12345, target, "gaze", new String[] {"xcoordinate", "194"},
174                    new String[] {"ycoordinate", "12"});
175       
176    }
177
178    /**
179     *
180     */
181    @Test
182    public void testComplexMessage() throws Exception {
183        String clientId = "123";
184        String appId = "456";
185       
186        String message =
187            "{" +
188            "  \"message\": {" +
189            "    \"clientInfos\": {" +
190            "      \"clientId\":\"" + clientId + "\"," +
191            "      \"appId\":\"" + appId + "\"," +
192            "    }," +
193            "    \"targetStructure\": [{" +
194            "      \"targetId\":\"html\"," +
195            "      \"index\":\"0\"," +
196            "      \"children\":" +
197            "      [ {" +
198            "          \"targetId\":\"head\"," +
199            "          \"index\":\"0\"," +
200            "        }," +
201            "        {" +
202            "          \"tagName\":\"body\"," +
203            "          \"targetId\":\"gsr\"," +
204            "          \"children\":" +
205            "          [ {" +
206            "              \"tagName\":\"input_button\"," +
207            "              \"targetId\":\"input1\"," +
208            "            }," +
209            "            {" +
210            "              \"tagName\":\"input_button\"," +
211            "              \"targetId\":\"input2\"," +
212            "            }," +
213            "            {" +
214            "              \"tagName\":\"input_button\"," +
215            "              \"targetId\":\"input3\"," +
216            "            }," +
217            "            {" +
218            "              \"tagName\":\"input_button\"," +
219            "              \"targetId\":\"input4\"," +
220            "            }," +
221            "            {" +
222            "              \"tagName\":\"input_button\"," +
223            "              \"targetId\":\"input5\"," +
224            "            }," +
225            "            {" +
226            "              \"tagName\":\"input_button\"," +
227            "              \"targetId\":\"input6\"," +
228            "            }," +
229            "            {" +
230            "              \"tagName\":\"input_button\"," +
231            "              \"targetId\":\"input7\"," +
232            "            }," +
233            "            {" +
234            "              \"tagName\":\"input_button\"," +
235            "              \"targetId\":\"input8\"," +
236            "            }," +
237            "            {" +
238            "              \"tagName\":\"input_button\"," +
239            "              \"targetId\":\"input9\"," +
240            "            }," +
241            "            {" +
242            "              \"tagName\":\"input_button\"," +
243            "              \"targetId\":\"input10\"," +
244            "            }," +
245            "          ]" +
246            "        }" +
247            "      ]" +
248            "    }]," +
249            "    \"events\":" +
250            "    [ {" +
251            "        \"time\":\"1\"," +
252            "        \"targetId\":\"input1\"," +
253            "        \"type\":\"onclick\"," +
254            "        \"coordinates\": \"194\"" +
255            "      }," +
256            "      {" +
257            "        \"time\":\"2\"," +
258            "        \"targetId\":\"input2\"," +
259            "        \"type\":\"ondblclick\"," +
260            "        \"coordinates\": \"194\"" +
261            "      }," +
262            "      {" +
263            "        \"time\":\"3\"," +
264            "        \"targetId\":\"input3\"," +
265            "        \"type\":\"onfocus\"" +
266            "      }," +
267            "      {" +
268            "        \"time\":\"4\"," +
269            "        \"targetId\":\"input4\"," +
270            "        \"type\":\"onclick\"," +
271            "        \"coordinates\": \"255\"" +
272            "      }," +
273            "      {" +
274            "        \"time\":\"5\"," +
275            "        \"targetId\":\"input5\"," +
276            "        \"type\":\"onfocus\"" +
277            "      }," +
278            "      {" +
279            "        \"time\":\"6\"," +
280            "        \"targetId\":\"input6\"," +
281            "        \"type\":\"onfocus\"" +
282            "      }," +
283            "      {" +
284            "        \"time\":\"7\"," +
285            "        \"targetId\":\"input7\"," +
286            "        \"type\":\"onfocus\"" +
287            "      }," +
288            "      {" +
289            "        \"time\":\"8\"," +
290            "        \"targetId\":\"input8\"," +
291            "        \"type\":\"onclick\"," +
292            "        \"coordinates\": \"255\"" +
293            "      }," +
294            "      {" +
295            "        \"time\":\"9\"," +
296            "        \"targetId\":\"input9\"," +
297            "        \"type\":\"onscroll\"," +
298            "        \"scrollPosition\": \"194\"" +
299            "      }," +
300            "      {" +
301            "        \"time\":\"10\"," +
302            "        \"targetId\":\"input10\"," +
303            "        \"type\":\"onclick\"," +
304            "        \"coordinates\": \"194\"" +
305            "      }" +
306            "    ]" +
307            "  }" +
308            "}";
309 
310        sendMessageAndAssertResponse(message);
311       
312        if (eventHandlingError != null) {
313            fail(eventHandlingError);
314        }
315       
316        // check the target structure
317        GenericEventTarget parent = null;
318       
319        assertEquals(1, eventTargetStructures.length);
320        GenericEventTarget target = eventTargetStructures[0];
321       
322        assertTarget(target, parent, 2, new String[] {"index", "0"});
323
324        parent = target;
325        target = parent.getChildren().get(0);
326        assertTarget(target, parent, 0, new String[] {"index", "0"});
327        target = parent.getChildren().get(1);
328        assertTarget(target, parent, 10, new String[] {"tagName", "body"});
329       
330        parent = target;
331        for (GenericEventTarget child : parent.getChildren()) {
332            assertTarget(child, parent, 0, new String[] {"tagName", "input_button"});
333        }
334
335        // check the events
336        assertNotNull(events);
337        assertEquals(10, events.length);
338
339        for (int i = 0; i < events.length; i++) {
340            String eventType;
341            if ((i == 1)) {
342                eventType = "ondblclick";
343            }
344            else if ((i == 2) || ((4 <= i) && (i <= 6))) {
345                eventType = "onfocus";
346            }
347            else if ((i == 8)) {
348                eventType = "onscroll";
349            }
350            else {
351                eventType = "onclick";
352            }
353           
354            String[] parameter = null;
355            if (i <= 1) {
356                parameter = new String[] { "coordinates", "194" };
357            }
358            else if (i == 3) {
359                parameter = new String[] { "coordinates", "255" };
360            }
361            else if (i == 7) {
362                parameter = new String[] { "coordinates", "255" };
363            }
364            else if (i == 8) {
365                parameter = new String[] { "scrollPosition", "194" };
366            }
367            else if (i == 9) {
368                parameter = new String[] { "coordinates", "194" };
369            }
370           
371            if (parameter != null) {
372                 assertEvent(i, i + 1, target.getChildren().get(i), eventType, parameter);
373            }
374            else {
375                assertEvent(i, i + 1, target.getChildren().get(i), eventType);
376            }
377           
378        }
379    }
380   
381    /**
382     *
383     */
384    @Test
385    public void testInvalidMessages() throws Exception {
386        String message = "}";
387       
388        sendMessageAndAssertResponse(message);
389           
390        if (eventHandlingError != null) {
391            fail(eventHandlingError);
392        }
393
394        assertNull(events);
395
396        message = "blaublbidalslkdjflqkerowercalksdjfdlkkjdjfk";
397       
398        sendMessageAndAssertResponse(message);
399           
400        if (eventHandlingError != null) {
401            fail(eventHandlingError);
402        }
403
404        assertNull(events);
405       
406        // the following message doesn't work because of the missing scroll position in the event
407        message =
408            "{" +
409            "  \"message\": {" +
410            "    \"clientInfos\": {" +
411            "      \"clientId\":\"123\"," +
412            "      \"userAgent\":\"Agent\"," +
413            "      \"title\":\"Title\"," +
414            "      \"url\":\"http://host/path\"" +
415            "    }," +
416            "    \"guiModel\": {" +
417            "      \"tagName\":\"html\"," +
418            "      \"index\":\"0\"," +
419            "      \"children\":" +
420            "      [ {" +
421            "          \"tagName\":\"head\"," +
422            "          \"index\":\"0\"," +
423            "        }," +
424            "        {" +
425            "          \"tagName\":\"body\"," +
426            "          \"htmlId\":\"gsr\"," +
427            "        }" +
428            "      ]" +
429            "    }," +
430            "    \"events\":" +
431            "    [ {" +
432            "        \"time\":\"9\"," +
433            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
434            "        \"type\":\"onscroll\"," +
435            "      }" +
436            "    ]" +
437            "  }" +
438            "}";
439       
440        sendMessageAndAssertResponse(message);
441       
442        if (eventHandlingError != null) {
443            fail(eventHandlingError);
444        }
445
446        assertNull(events);
447       
448        // the following message doesn't work because of the missing client id
449        message =
450            "{" +
451            "  \"message\": {" +
452            "    \"clientInfos\": {" +
453            "      \"userAgent\":\"Agent\"," +
454            "      \"title\":\"Title\"," +
455            "      \"url\":\"http://host/path\"" +
456            "    }," +
457            "    \"guiModel\": {" +
458            "      \"tagName\":\"html\"," +
459            "      \"index\":\"0\"," +
460            "      \"children\":" +
461            "      [ {" +
462            "          \"tagName\":\"head\"," +
463            "          \"index\":\"0\"," +
464            "        }," +
465            "        {" +
466            "          \"tagName\":\"body\"," +
467            "          \"htmlId\":\"gsr\"," +
468            "        }" +
469            "      ]" +
470            "    }," +
471            "    \"events\":" +
472            "    [ {" +
473            "        \"time\":\"12345\"," +
474            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
475            "        \"type\":\"onunload\"" +
476            "      }" +
477            "    ]" +
478            "  }" +
479            "}";
480       
481        sendMessageAndAssertResponse(message);
482       
483        if (eventHandlingError != null) {
484            fail(eventHandlingError);
485        }
486
487        assertNull(events);
488       
489        // the following message doesn't work because of the invalid time stamp
490        message =
491            "{" +
492            "  \"message\": {" +
493            "    \"clientInfos\": {" +
494            "      \"clientId\":\"123\"," +
495            "      \"userAgent\":\"Agent\"," +
496            "      \"title\":\"Title\"," +
497            "      \"url\":\"http://host/path\"" +
498            "    }," +
499            "    \"guiModel\": {" +
500            "      \"tagName\":\"html\"," +
501            "      \"index\":\"0\"," +
502            "      \"children\":" +
503            "      [ {" +
504            "          \"tagName\":\"head\"," +
505            "          \"index\":\"0\"," +
506            "        }," +
507            "        {" +
508            "          \"tagName\":\"body\"," +
509            "          \"htmlId\":\"gsr\"," +
510            "        }" +
511            "      ]" +
512            "    }," +
513            "    \"events\":" +
514            "    [ {" +
515            "        \"time\":\"blub\"," +
516            "        \"path\":\"/html[0]/body(htmlId=gsr)\"," +
517            "        \"type\":\"onunload\"" +
518            "      }" +
519            "    ]" +
520            "  }" +
521            "}";
522       
523        sendMessageAndAssertResponse(message);
524       
525        if (eventHandlingError != null) {
526            fail(eventHandlingError);
527        }
528
529        assertNull(events);
530       
531    }
532   
533    /* (non-Javadoc)
534     * @see GenericEventMonitorMessageListener#handleEvents(ClientInfos, GenericEvent[])
535     */
536    @Override
537    public void handleEvents(ClientInfos clientInfos, GenericEvent[] events) {
538        if (clientInfos == null) {
539            eventHandlingError = "client infos were null";
540        }
541        else if (events == null) {
542            eventHandlingError = "events were null";
543        }
544        else {
545            Map<String, GenericEventTarget> roots = new HashMap<>();
546           
547            for (GenericEvent event : events) {
548                if (!clientInfos.equals(event.getClientInfos())) {
549                    eventHandlingError = "one of the events did not have a correct client info";
550                }
551               
552                GenericEventTarget parent = event.getTarget();
553               
554                while (parent.getParent() != null) {
555                    parent = parent.getParent();
556                }
557               
558                roots.put(parent.getId(), parent);
559            }
560           
561            this.events = events;
562            this.eventTargetStructures =
563                roots.values().toArray(new GenericEventTarget[roots.size()]);
564        }
565    }
566
567    /**
568     *
569     */
570    private void sendMessageAndAssertResponse(String message) throws Exception {
571        DefaultHttpClient httpclient = new DefaultHttpClient();
572        HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/");
573        //HttpPost httpPost = new HttpPost("http://localhost:8090/");
574        HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON);
575        httpPost.setEntity(entity);
576       
577        try {
578            HttpResponse response = httpclient.execute(httpPost);
579           
580            // the monitor always returns 200 without any additional information. The client must
581            // never get more or less information. This is especially important for preventing
582            // hackers from finding out more
583            assertEquals(200, response.getStatusLine().getStatusCode());
584            assertTrue
585                ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) ||
586                 ((response.getEntity().getContentLength() == 1) &&
587                  (response.getEntity().getContent().read() == ' ')));
588        }
589        finally {
590            httpPost.releaseConnection();
591        }
592    }
593
594    /**
595     *
596     */
597    private void assertTarget(GenericEventTarget target,
598                              GenericEventTarget parent,
599                              int                noOfChildren,
600                              String[] ...       parameters)
601    {
602        if (parent != null) {
603            assertNotNull(target.getParent());
604            assertEquals(parent.getId(), target.getParent().getId());
605        }
606        else {
607            assertNull(target.getParent());
608        }
609       
610        assertNotNull(target.getId());
611       
612        if (noOfChildren > 0) {
613            assertNotNull(target.getChildren());
614            assertEquals(noOfChildren, target.getChildren().size());
615        }
616        else {
617            assertNull(target.getChildren());
618        }
619       
620        assertEquals(parameters.length, target.getParameters().size());
621       
622        for (String[] parameter : parameters) {
623            assertEquals(parameter[1], target.getParameters().get(parameter[0]));
624        }
625    }
626
627    /**
628     *
629     */
630    private void assertEvent(int                index,
631                             long               timestamp,
632                             GenericEventTarget target,
633                             String             eventType,
634                             String[] ...       parameters)
635    {
636        assertEquals("event " + index, new Long(timestamp), events[index].getTime());
637        assertEquals("event " + index, target, events[index].getTarget());
638        assertEquals("event " + index, eventType, events[index].getEventType());
639       
640       
641        assertEquals("event " + index, parameters.length, events[index].getParameters().size());
642       
643        for (String[] parameter : parameters) {
644            assertEquals
645                ("event " + index, parameter[1], events[index].getParameters().get(parameter[0]));
646        }
647    }
648
649}
Note: See TracBrowser for help on using the repository browser.