diff options
Diffstat (limited to 'test/org/traccar/events')
5 files changed, 216 insertions, 17 deletions
diff --git a/test/org/traccar/events/AlertEventHandlerTest.java b/test/org/traccar/events/AlertEventHandlerTest.java index 77128f066..4e11398e1 100644 --- a/test/org/traccar/events/AlertEventHandlerTest.java +++ b/test/org/traccar/events/AlertEventHandlerTest.java @@ -3,7 +3,7 @@ package org.traccar.events; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.util.Collection; +import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; @@ -19,9 +19,9 @@ public class AlertEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - Collection<Event> events = alertEventHandler.analyzePosition(position); + Map<Event, Position> events = alertEventHandler.analyzePosition(position); assertNotNull(events); - Event event = (Event) events.toArray()[0]; + Event event = events.keySet().iterator().next(); assertEquals(Event.TYPE_ALARM, event.getType()); } diff --git a/test/org/traccar/events/CommandResultEventHandlerTest.java b/test/org/traccar/events/CommandResultEventHandlerTest.java index f028e86ee..602108d1a 100644 --- a/test/org/traccar/events/CommandResultEventHandlerTest.java +++ b/test/org/traccar/events/CommandResultEventHandlerTest.java @@ -3,7 +3,7 @@ package org.traccar.events; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.util.Collection; +import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; @@ -19,9 +19,9 @@ public class CommandResultEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_RESULT, "Test Result"); - Collection<Event> events = commandResultEventHandler.analyzePosition(position); + Map<Event, Position> events = commandResultEventHandler.analyzePosition(position); assertNotNull(events); - Event event = (Event) events.toArray()[0]; + Event event = events.keySet().iterator().next(); assertEquals(Event.TYPE_COMMAND_RESULT, event.getType()); } diff --git a/test/org/traccar/events/IgnitionEventHandlerTest.java b/test/org/traccar/events/IgnitionEventHandlerTest.java index d6c348c77..7c4ac21b9 100644 --- a/test/org/traccar/events/IgnitionEventHandlerTest.java +++ b/test/org/traccar/events/IgnitionEventHandlerTest.java @@ -2,7 +2,7 @@ package org.traccar.events; import static org.junit.Assert.assertEquals; -import java.util.Collection; +import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; @@ -19,7 +19,7 @@ public class IgnitionEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_IGNITION, true); position.setValid(true); - Collection<Event> events = ignitionEventHandler.analyzePosition(position); + Map<Event, Position> events = ignitionEventHandler.analyzePosition(position); assertEquals(events, null); } diff --git a/test/org/traccar/events/MotionEventHandlerTest.java b/test/org/traccar/events/MotionEventHandlerTest.java index f05ef54d5..3fc63adf0 100644 --- a/test/org/traccar/events/MotionEventHandlerTest.java +++ b/test/org/traccar/events/MotionEventHandlerTest.java @@ -1,29 +1,119 @@ 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 static org.junit.Assert.assertTrue; -import java.util.Collection; +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 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 testMotionEventHandler() throws Exception { - - MotionEventHandler motionEventHandler = new MotionEventHandler(); - + public void testMotionWithPosition() throws Exception { + MotionEventHandler motionEventHandler = new MotionEventHandler( + new TripsConfig(500, 300 * 1000, 300 * 1000, 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.setValid(true); - Collection<Event> events = motionEventHandler.analyzePosition(position); + 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 = (Event) events.toArray()[0]; + 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()); + } + + @Test + public void testMotionWithStatus() throws Exception { + MotionEventHandler motionEventHandler = new MotionEventHandler( + new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01)); + + 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); + + Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState); + + assertNotNull(events); + Event event = events.keySet().iterator().next(); + assertEquals(Event.TYPE_DEVICE_MOVING, event.getType()); + assertTrue(deviceState.getMotionState()); + assertNull(deviceState.getMotionPosition()); + } + + @Test + public void testStopWithPositionIgnition() throws Exception { + MotionEventHandler motionEventHandler = new MotionEventHandler( + new TripsConfig(500, 300 * 1000, 300 * 1000, 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()); } } diff --git a/test/org/traccar/events/OverspeedEventHandlerTest.java b/test/org/traccar/events/OverspeedEventHandlerTest.java new file mode 100644 index 000000000..d38367cd9 --- /dev/null +++ b/test/org/traccar/events/OverspeedEventHandlerTest.java @@ -0,0 +1,109 @@ +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.Map; +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); + + Map<Event, Position> events = overspeedEventHandler.updateOverspeedState(deviceState, position, 40); + assertNull(events); + assertFalse(deviceState.getOverspeedState()); + assertEquals(position, deviceState.getOverspeedPosition()); + + Position nextPosition = new Position(); + nextPosition.setTime(date("2017-01-01 00:00:10")); + nextPosition.setSpeed(55); + + events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40); + assertNull(events); + + nextPosition.setTime(date("2017-01-01 00:00:20")); + + events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40); + assertNotNull(events); + Event event = events.keySet().iterator().next(); + 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")); + events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40); + assertNull(events); + 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); + + events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40); + assertNull(events); + 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); + + Map<Event, Position> events = overspeedEventHandler.updateOverspeedState(deviceState, 40); + + assertNotNull(events); + Event event = events.keySet().iterator().next(); + 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); + } + +} |