aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/events/MotionEventHandlerTest.java
blob: 0543d86fe2f606f65326116d277e134229214505 (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
package org.traccar.events;

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

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;
import org.traccar.reports.model.TripsConfig;

public class MotionEventHandlerTest 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);
    }

    @Test
    public void testMotionWithPosition() throws Exception {
        MotionEventHandler motionEventHandler = new MotionEventHandler(
                new TripsConfig(500, 300 * 1000, 300 * 1000, 0));

        Position position = new Position();
        position.setTime(date("2017-01-01 00:00:00"));
        position.set(Position.KEY_MOTION, true);
        position.set(Position.KEY_TOTAL_DISTANCE, 0);
        DeviceState deviceState = new DeviceState();
        deviceState.setMotionState(false);
        deviceState.setMotionPosition(position);
        Position nextPosition = new Position();

        nextPosition.setTime(date("2017-01-01 00:02:00"));
        nextPosition.set(Position.KEY_MOTION, true);
        nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);

        Event event = motionEventHandler.updateMotionState(deviceState, nextPosition);
        assertNull(event);

        nextPosition.set(Position.KEY_TOTAL_DISTANCE, 600);
        event = motionEventHandler.updateMotionState(deviceState, nextPosition);        
        assertNotNull(event);
        assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
        assertTrue(deviceState.getMotionState());
        assertNull(deviceState.getMotionPosition());

        deviceState.setMotionState(false);
        deviceState.setMotionPosition(position);
        nextPosition.setTime(date("2017-01-01 00:06:00"));
        nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);
        event = motionEventHandler.updateMotionState(deviceState, nextPosition);
        assertNotNull(event);
        assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
        assertTrue(deviceState.getMotionState());
        assertNull(deviceState.getMotionPosition());
    }

    @Test
    public void testMotionWithStatus() throws Exception {
        MotionEventHandler motionEventHandler = new MotionEventHandler(
                new TripsConfig(500, 300 * 1000, 300 * 1000, 0));

        Position position = new Position();
        position.setTime(new Date(System.currentTimeMillis() - 360000));
        position.set(Position.KEY_MOTION, true);
        DeviceState deviceState = new DeviceState();
        deviceState.setMotionState(false);
        deviceState.setMotionPosition(position);

        Event event = motionEventHandler.updateMotionState(deviceState);

        assertNotNull(event);
        assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
        assertTrue(deviceState.getMotionState());
        assertNull(deviceState.getMotionPosition());
    }

}