diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-08-11 23:37:16 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-11 23:37:16 +1200 |
commit | 30e8409375f9992a5248f7e2597691c0ee08188c (patch) | |
tree | ea51b7d111cfae849d23941ad82045139e87520b /test/org/traccar/events/OverspeedEventHandlerTest.java | |
parent | 3562e0d927d539a1e9730c48ed376ee84ebc2b25 (diff) | |
parent | 26db3ccc2ed5d43a0e791a29d5cc4afca192e4b7 (diff) | |
download | trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.tar.gz trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.tar.bz2 trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.zip |
Merge pull request #3431 from Abyss777/delayed_overspeed
Implement delayed overspeed events
Diffstat (limited to 'test/org/traccar/events/OverspeedEventHandlerTest.java')
-rw-r--r-- | test/org/traccar/events/OverspeedEventHandlerTest.java | 106 |
1 files changed, 106 insertions, 0 deletions
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); + } + +} |