diff options
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java | 66 | ||||
-rw-r--r-- | src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java | 56 |
2 files changed, 55 insertions, 67 deletions
diff --git a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java index 0d4886429..22afbfa52 100644 --- a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java @@ -5,7 +5,8 @@ import org.traccar.BaseTest; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.reports.common.TripsConfig; -import org.traccar.session.DeviceState; +import org.traccar.session.state.MotionProcessor; +import org.traccar.session.state.MotionState; import java.text.DateFormat; import java.text.ParseException; @@ -28,57 +29,52 @@ public class MotionEventHandlerTest extends BaseTest { return position; } - private void verifyState(DeviceState deviceState, boolean state, long distance) { - assertEquals(state, deviceState.getMotionState()); - assertEquals(distance, deviceState.getMotionDistance(), 0.1); + private void verifyState(MotionState motionState, boolean state, long distance) { + assertEquals(state, motionState.getMotionState()); + assertEquals(distance, motionState.getMotionDistance(), 0.1); } @Test public void testMotionWithPosition() throws ParseException { - MotionEventHandler motionEventHandler = new MotionEventHandler( - null, null, new TripsConfig(500, 300000, 300000, 0, false, false, 0.01)); + TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false, false, 0.01); - DeviceState deviceState = new DeviceState(); + MotionState state = new MotionState(); - 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); + MotionProcessor.updateState(state, position("2017-01-01 00:00:00", false, 0, null), false, tripsConfig); + assertNull(state.getEvent()); + verifyState(state, 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); + MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 100, null), true, tripsConfig); + assertNull(state.getEvent()); + verifyState(state, 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); + MotionProcessor.updateState(state, position("2017-01-01 00:02:00", true, 700, null), true, tripsConfig); + assertEquals(Event.TYPE_DEVICE_MOVING, state.getEvent().getType()); + verifyState(state, 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); + MotionProcessor.updateState(state, position("2017-01-01 00:03:00", false, 700, null), false, tripsConfig); + assertNull(state.getEvent()); + verifyState(state, 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); + MotionProcessor.updateState(state, position("2017-01-01 00:10:00", false, 700, null), false, tripsConfig); + assertEquals(Event.TYPE_DEVICE_STOPPED, state.getEvent().getType()); + verifyState(state, false, 0); } @Test public void testStopWithPositionIgnition() throws ParseException { - MotionEventHandler motionEventHandler = new MotionEventHandler( - null, null, new TripsConfig(500, 300000, 300000, 0, true, false, 0.01)); + TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, true, false, 0.01); - DeviceState deviceState = new DeviceState(); - deviceState.setMotionState(true); + MotionState state = new MotionState(); + state.setMotionState(true); - 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); + MotionProcessor.updateState(state, position("2017-01-01 00:00:00", false, 100, true), false, tripsConfig); + assertNull(state.getEvent()); + verifyState(state, 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); + MotionProcessor.updateState(state, position("2017-01-01 00:02:00", false, 100, false), false, tripsConfig); + assertEquals(Event.TYPE_DEVICE_STOPPED, state.getEvent().getType()); + verifyState(state, false, 0); } } diff --git a/src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java b/src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java index bbddbae72..ee18ee052 100644 --- a/src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/OverspeedEventHandlerTest.java @@ -2,11 +2,10 @@ package org.traccar.handler.events; import org.junit.Test; import org.traccar.BaseTest; -import org.traccar.config.Config; -import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.session.DeviceState; +import org.traccar.session.state.OverspeedProcessor; +import org.traccar.session.state.OverspeedState; import java.text.DateFormat; import java.text.ParseException; @@ -28,43 +27,36 @@ public class OverspeedEventHandlerTest extends BaseTest { return position; } - private void verifyState(DeviceState deviceState, boolean state, long geofenceId) { - assertEquals(state, deviceState.getOverspeedState()); - assertEquals(geofenceId, deviceState.getOverspeedGeofenceId()); + private void verifyState(OverspeedState overspeedState, boolean state, long geofenceId) { + assertEquals(state, overspeedState.getOverspeedState()); + assertEquals(geofenceId, overspeedState.getOverspeedGeofenceId()); } private void testOverspeedWithPosition(long geofenceId) throws ParseException { - Config config = new Config(); - config.setString(Keys.EVENT_OVERSPEED_MINIMAL_DURATION, String.valueOf(15)); - config.setString(Keys.EVENT_OVERSPEED_PREFER_LOWEST, String.valueOf(false)); - OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(config, null, null); + OverspeedState state = new OverspeedState(); - DeviceState deviceState = new DeviceState(); + OverspeedProcessor.updateState(state, position("2017-01-01 00:00:00", 50), 40, 15000, geofenceId); + assertNull(state.getEvent()); + verifyState(state, true, geofenceId); - Position position = position("2017-01-01 00:00:00", 50); - assertNull(overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId)); - verifyState(deviceState, true, geofenceId); + OverspeedProcessor.updateState(state, position("2017-01-01 00:00:10", 55), 40, 15000, geofenceId); + assertNull(state.getEvent()); - position = position("2017-01-01 00:00:10", 55); - assertNull(overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId)); + OverspeedProcessor.updateState(state, position("2017-01-01 00:00:20", 55), 40, 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); - position = position("2017-01-01 00:00:20", 55); - var events = overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId); - assertNotNull(events); - Event event = events.keySet().iterator().next(); - assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType()); - assertEquals(55, event.getDouble("speed"), 0.1); - assertEquals(40, event.getDouble("speedLimit"), 0.1); - assertEquals(geofenceId, event.getGeofenceId()); - verifyState(deviceState, true, 0); + OverspeedProcessor.updateState(state, position("2017-01-01 00:00:30", 55), 40, 15000, geofenceId); + assertNull(state.getEvent()); + verifyState(state, true, 0); - position = position("2017-01-01 00:00:30", 55); - assertNull(overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId)); - verifyState(deviceState, true, 0); - - position = position("2017-01-01 00:00:30", 30); - assertNull(overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId)); - verifyState(deviceState, false, 0); + OverspeedProcessor.updateState(state, position("2017-01-01 00:00:30", 30), 40, 15000, geofenceId); + assertNull(state.getEvent()); + verifyState(state, false, 0); } @Test |