aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/events/OverspeedEventHandlerTest.java
blob: 48d7445ff0f49c6299e6dddef452e7d33bdd6776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.traccar.events;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.junit.Test;
import org.traccar.BaseTest;
import org.traccar.model.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Position;

public class OverspeedEventHandlerTest  extends BaseTest {

    private Date date(String time) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.parse(time);
    }

    private void testOverspeedWithPosition(boolean notRepeat) throws Exception {
        OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);

        Position position = new Position();
        position.setTime(date("2017-01-01 00:00:00"));
        position.setSpeed(50);
        DeviceState deviceState = new DeviceState();
        deviceState.setOverspeedState(false);

        Event event = overspeedEventHandler.updateOverspeedState(deviceState, position, 40);
        assertNull(event);
        assertFalse(deviceState.getOverspeedState());
        assertEquals(position, deviceState.getOverspeedPosition());

        Position nextPosition = new Position();
        nextPosition.setTime(date("2017-01-01 00:00:10"));
        nextPosition.setSpeed(55);

        event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
        assertNull(event);

        nextPosition.setTime(date("2017-01-01 00:00:20"));

        event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
        assertNotNull(event);
        assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
        assertEquals(50, event.getDouble("speed"), 0.1);
        assertEquals(40, event.getDouble(OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT), 0.1);

        assertEquals(notRepeat, deviceState.getOverspeedState());
        assertNull(deviceState.getOverspeedPosition());

        nextPosition.setTime(date("2017-01-01 00:00:30"));
        event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
        assertNull(event);
        assertEquals(notRepeat, deviceState.getOverspeedState());

        if (notRepeat) {
            assertNull(deviceState.getOverspeedPosition());
        } else {
            assertNotNull(deviceState.getOverspeedPosition());
        }

        nextPosition.setTime(date("2017-01-01 00:00:40"));
        nextPosition.setSpeed(30);

        event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
        assertNull(event);
        assertFalse(deviceState.getOverspeedState());
        assertNull(deviceState.getOverspeedPosition());
    }

    private void testOverspeedWithStatus(boolean notRepeat) throws Exception {
        OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);

        Position position = new Position();
        position.setTime(new Date(System.currentTimeMillis() - 30000));
        position.setSpeed(50);
        DeviceState deviceState = new DeviceState();
        deviceState.setOverspeedState(false);
        deviceState.setOverspeedPosition(position);

        Event event = overspeedEventHandler.updateOverspeedState(deviceState, 40);

        assertNotNull(event);
        assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
        assertEquals(notRepeat, deviceState.getOverspeedState());
    }

    @Test
    public void testOverspeedEventHandler() throws Exception {
        testOverspeedWithPosition(false);
        testOverspeedWithPosition(true);

        testOverspeedWithStatus(false);
        testOverspeedWithStatus(true);
    }

}