diff options
Diffstat (limited to 'test/org/traccar')
-rw-r--r-- | test/org/traccar/events/MotionEventHandlerTest.java | 31 | ||||
-rw-r--r-- | test/org/traccar/events/OverspeedEventHandlerTest.java | 106 |
2 files changed, 132 insertions, 5 deletions
diff --git a/test/org/traccar/events/MotionEventHandlerTest.java b/test/org/traccar/events/MotionEventHandlerTest.java index c44f3f4eb..6b7b9daee 100644 --- a/test/org/traccar/events/MotionEventHandlerTest.java +++ b/test/org/traccar/events/MotionEventHandlerTest.java @@ -27,8 +27,9 @@ public class MotionEventHandlerTest extends BaseTest { } @Test - public void testMotionEventHandler() throws Exception { - TripsConfig tripsConfig = new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0); + public void testMotionWithPosition() throws Exception { + MotionEventHandler motionEventHandler = new MotionEventHandler( + new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0)); Position position = new Position(); position.setTime(date("2017-01-01 00:00:00")); @@ -43,11 +44,11 @@ public class MotionEventHandlerTest extends BaseTest { nextPosition.set(Position.KEY_MOTION, true); nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200); - Event event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig); + Event event = motionEventHandler.updateMotionState(deviceState, nextPosition); assertNull(event); nextPosition.set(Position.KEY_TOTAL_DISTANCE, 600); - event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig); + event = motionEventHandler.updateMotionState(deviceState, nextPosition); assertNotNull(event); assertEquals(Event.TYPE_DEVICE_MOVING, event.getType()); assertTrue(deviceState.getMotionState()); @@ -57,7 +58,27 @@ public class MotionEventHandlerTest extends BaseTest { deviceState.setMotionPosition(position); nextPosition.setTime(date("2017-01-01 00:06:00")); nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200); - event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig); + 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, false, 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()); diff --git a/test/org/traccar/events/OverspeedEventHandlerTest.java b/test/org/traccar/events/OverspeedEventHandlerTest.java new file mode 100644 index 000000000..48d7445ff --- /dev/null +++ b/test/org/traccar/events/OverspeedEventHandlerTest.java @@ -0,0 +1,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); + } + +} |