aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/handler
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-09-21 17:53:50 -0700
committerAnton Tananaev <anton@traccar.org>2022-09-21 17:53:58 -0700
commit388799edf2adc9c3070a83e72f074e523629574f (patch)
tree35809a83791475cece4f053457cafddc9d226b71 /src/test/java/org/traccar/handler
parentd5b1385596f79da2514ea7cccf27e9bdeebcacae (diff)
downloadtrackermap-server-388799edf2adc9c3070a83e72f074e523629574f.tar.gz
trackermap-server-388799edf2adc9c3070a83e72f074e523629574f.tar.bz2
trackermap-server-388799edf2adc9c3070a83e72f074e523629574f.zip
Refactor motion handling
Diffstat (limited to 'src/test/java/org/traccar/handler')
-rw-r--r--src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java108
1 files changed, 47 insertions, 61 deletions
diff --git a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java
index 1f6212eec..0d4886429 100644
--- a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java
+++ b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java
@@ -10,89 +10,75 @@ import org.traccar.session.DeviceState;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Map;
import java.util.TimeZone;
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 static org.junit.Assert.assertTrue;
public class MotionEventHandlerTest extends BaseTest {
- private Date date(String time) throws ParseException {
+ private Position position(String time, boolean motion, double distance, Boolean ignition) throws ParseException {
+ Position position = new Position();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
- return dateFormat.parse(time);
+ position.setTime(dateFormat.parse(time));
+ position.set(Position.KEY_MOTION, motion);
+ position.set(Position.KEY_TOTAL_DISTANCE, distance);
+ position.set(Position.KEY_IGNITION, ignition);
+ return position;
+ }
+
+ private void verifyState(DeviceState deviceState, boolean state, long distance) {
+ assertEquals(state, deviceState.getMotionState());
+ assertEquals(distance, deviceState.getMotionDistance(), 0.1);
}
@Test
- public void testMotionWithPosition() throws Exception {
+ public void testMotionWithPosition() throws ParseException {
MotionEventHandler motionEventHandler = new MotionEventHandler(
- null, null, new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01));
+ null, null, new TripsConfig(500, 300000, 300000, 0, false, false, 0.01));
- 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);
-
- Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState, nextPosition);
- assertNull(events);
-
- nextPosition.set(Position.KEY_TOTAL_DISTANCE, 600);
- events = motionEventHandler.updateMotionState(deviceState, nextPosition);
- assertNotNull(events);
- Event event = events.keySet().iterator().next();
- 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);
- events = motionEventHandler.updateMotionState(deviceState, nextPosition);
- assertNotNull(event);
- event = events.keySet().iterator().next();
- assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
- assertTrue(deviceState.getMotionState());
- assertNull(deviceState.getMotionPosition());
+
+ Position position = position("2017-01-01 00:00:00", false, 0, null);
+ assertNull(motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION)));
+ verifyState(deviceState, false, 0);
+
+ position = position("2017-01-01 00:02:00", true, 100, null);
+ assertNull(motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION)));
+ verifyState(deviceState, true, 100);
+
+ position = position("2017-01-01 00:02:00", true, 700, null);
+ var events = motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
+ assertEquals(Event.TYPE_DEVICE_MOVING, events.keySet().iterator().next().getType());
+ verifyState(deviceState, true, 0);
+
+ position = position("2017-01-01 00:03:00", false, 700, null);
+ assertNull(motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION)));
+ verifyState(deviceState, false, 700);
+
+ position = position("2017-01-01 00:10:00", false, 700, null);
+ events = motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
+ assertEquals(Event.TYPE_DEVICE_STOPPED, events.keySet().iterator().next().getType());
+ verifyState(deviceState, false, 0);
}
@Test
- public void testStopWithPositionIgnition() throws Exception {
+ public void testStopWithPositionIgnition() throws ParseException {
MotionEventHandler motionEventHandler = new MotionEventHandler(
- null, null, new TripsConfig(500, 300 * 1000, 300 * 1000, 0, true, false, 0.01));
+ null, null, new TripsConfig(500, 300000, 300000, 0, true, false, 0.01));
- Position position = new Position();
- position.setTime(date("2017-01-01 00:00:00"));
- position.set(Position.KEY_MOTION, false);
- position.set(Position.KEY_IGNITION, true);
DeviceState deviceState = new DeviceState();
deviceState.setMotionState(true);
- deviceState.setMotionPosition(position);
-
- Position nextPosition = new Position();
- nextPosition.setTime(date("2017-01-01 00:02:00"));
- nextPosition.set(Position.KEY_MOTION, false);
- nextPosition.set(Position.KEY_IGNITION, false);
-
- Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState, nextPosition);
- assertNotNull(events);
- Event event = events.keySet().iterator().next();
- assertEquals(Event.TYPE_DEVICE_STOPPED, event.getType());
- assertFalse(deviceState.getMotionState());
- assertNull(deviceState.getMotionPosition());
+
+ Position position = position("2017-01-01 00:00:00", false, 100, true);
+ assertNull(motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION)));
+ verifyState(deviceState, false, 100);
+
+ position = position("2017-01-01 00:02:00", false, 100, false);
+ var events = motionEventHandler.updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
+ assertEquals(Event.TYPE_DEVICE_STOPPED, events.keySet().iterator().next().getType());
+ verifyState(deviceState, false, 0);
}
}