aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java
blob: 97d9295510496f222fd62da239b02bad6aff860a (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
package org.traccar.handler.events;

import org.junit.jupiter.api.Test;
import org.traccar.BaseTest;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.session.state.OverspeedProcessor;
import org.traccar.session.state.OverspeedState;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class OverspeedEventHandlerTest  extends BaseTest {

    private Position position(String time, double speed) throws ParseException {
        Position position = new Position();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        position.setTime(dateFormat.parse(time));
        position.setSpeed(speed);
        return position;
    }

    private void verifyState(OverspeedState overspeedState, boolean state, long geofenceId) {
        assertEquals(state, overspeedState.getOverspeedState());
        assertEquals(geofenceId, overspeedState.getOverspeedGeofenceId());
    }

    private void testOverspeedWithPosition(long geofenceId) throws ParseException {
        OverspeedState state = new OverspeedState();

        OverspeedProcessor.updateState(state, position("2017-01-01 00:00:00", 50), 40, 1, 15000, geofenceId);
        assertNull(state.getEvent());
        verifyState(state, true, geofenceId);

        OverspeedProcessor.updateState(state, position("2017-01-01 00:00:10", 55), 40, 1, 15000, geofenceId);
        assertNull(state.getEvent());

        OverspeedProcessor.updateState(state, position("2017-01-01 00:00:20", 55), 40, 1, 15000, geofenceId);
        assertNotNull(state.getEvent());
        assertEquals(Event.TYPE_DEVICE_OVERSPEED, state.getEvent().getType());
        assertEquals(55, state.getEvent().getDouble("speed"), 0.1);
        assertEquals(40, state.getEvent().getDouble("speedLimit"), 0.1);
        assertEquals(geofenceId, state.getEvent().getGeofenceId());
        verifyState(state, true, 0);

        OverspeedProcessor.updateState(state, position("2017-01-01 00:00:30", 55), 40, 1, 15000, geofenceId);
        assertNull(state.getEvent());
        verifyState(state, true, 0);

        OverspeedProcessor.updateState(state, position("2017-01-01 00:00:30", 30), 40, 1, 15000, geofenceId);
        assertNull(state.getEvent());
        verifyState(state, false, 0);
    }

    @Test
    public void testOverspeedEventHandler() throws Exception {
        testOverspeedWithPosition(0);
        testOverspeedWithPosition(1);
    }

}