aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/handler
diff options
context:
space:
mode:
Diffstat (limited to 'test/org/traccar/handler')
-rw-r--r--test/org/traccar/handler/ComputedAttributesTest.java71
-rw-r--r--test/org/traccar/handler/DistanceHandlerTest.java30
-rw-r--r--test/org/traccar/handler/FilterHandlerTest.java88
-rw-r--r--test/org/traccar/handler/MotionHandlerTest.java21
-rw-r--r--test/org/traccar/handler/events/AlertEventHandlerTest.java30
-rw-r--r--test/org/traccar/handler/events/CommandResultEventHandlerTest.java28
-rw-r--r--test/org/traccar/handler/events/IgnitionEventHandlerTest.java27
-rw-r--r--test/org/traccar/handler/events/MotionEventHandlerTest.java119
-rw-r--r--test/org/traccar/handler/events/OverspeedEventHandlerTest.java128
9 files changed, 542 insertions, 0 deletions
diff --git a/test/org/traccar/handler/ComputedAttributesTest.java b/test/org/traccar/handler/ComputedAttributesTest.java
new file mode 100644
index 000000000..a76d8169b
--- /dev/null
+++ b/test/org/traccar/handler/ComputedAttributesTest.java
@@ -0,0 +1,71 @@
+package org.traccar.handler;
+
+import java.util.Date;
+
+import org.junit.Test;
+import org.traccar.config.Config;
+import org.traccar.model.Attribute;
+import org.traccar.model.Position;
+
+import static org.junit.Assert.assertEquals;
+
+public class ComputedAttributesTest {
+
+ @Test
+ public void testComputedAttributes() {
+
+ ComputedAttributesHandler handler = new ComputedAttributesHandler(new Config(), null, null);
+
+ Date date = new Date();
+ Position position = new Position();
+ position.setTime(date);
+ position.setSpeed(42);
+ position.setValid(false);
+ position.set("adc1", 128);
+ position.set("booleanFlag", true);
+ position.set("adc2", 100);
+ position.set("bitFlag", 7);
+ position.set("event", 42);
+ position.set("result", "success");
+ Attribute attribute = new Attribute();
+
+ attribute.setExpression("adc1");
+ assertEquals(128, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("!booleanFlag");
+ assertEquals(false, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("adc2 * 2 + 50");
+ assertEquals(250, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("(bitFlag & 4) != 0");
+ assertEquals(true, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("if (event == 42) \"lowBattery\"");
+ assertEquals("lowBattery", handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("speed > 5 && valid");
+ assertEquals(false, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("fixTime");
+ assertEquals(date, handler.computeAttribute(attribute, position));
+
+ attribute.setExpression("math:pow(adc1, 2)");
+ assertEquals(16384.0, handler.computeAttribute(attribute, position));
+
+ // modification tests
+ attribute.setExpression("adc1 = 256");
+ handler.computeAttribute(attribute, position);
+ assertEquals(128, position.getInteger("adc1"));
+
+ attribute.setExpression("result = \"fail\"");
+ handler.computeAttribute(attribute, position);
+ assertEquals("success", position.getString("result"));
+
+ attribute.setExpression("fixTime = \"2017-10-18 10:00:01\"");
+ handler.computeAttribute(attribute, position);
+ assertEquals(date, position.getFixTime());
+
+ }
+
+}
diff --git a/test/org/traccar/handler/DistanceHandlerTest.java b/test/org/traccar/handler/DistanceHandlerTest.java
new file mode 100644
index 000000000..f7c6e42cd
--- /dev/null
+++ b/test/org/traccar/handler/DistanceHandlerTest.java
@@ -0,0 +1,30 @@
+package org.traccar.handler;
+
+import org.junit.Test;
+import org.traccar.config.Config;
+import org.traccar.model.Position;
+
+import static org.junit.Assert.assertEquals;
+
+public class DistanceHandlerTest {
+
+ @Test
+ public void testCalculateDistance() {
+
+ DistanceHandler distanceHandler = new DistanceHandler(new Config(), null);
+
+ Position position = distanceHandler.handlePosition(new Position());
+
+ assertEquals(0.0, position.getAttributes().get(Position.KEY_DISTANCE));
+ assertEquals(0.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE));
+
+ position.set(Position.KEY_DISTANCE, 100);
+
+ position = distanceHandler.handlePosition(position);
+
+ assertEquals(100.0, position.getAttributes().get(Position.KEY_DISTANCE));
+ assertEquals(100.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE));
+
+ }
+
+}
diff --git a/test/org/traccar/handler/FilterHandlerTest.java b/test/org/traccar/handler/FilterHandlerTest.java
new file mode 100644
index 000000000..ad8d244a6
--- /dev/null
+++ b/test/org/traccar/handler/FilterHandlerTest.java
@@ -0,0 +1,88 @@
+package org.traccar.handler;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.traccar.BaseTest;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+import org.traccar.model.Position;
+
+import java.util.Date;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class FilterHandlerTest extends BaseTest {
+
+ private FilterHandler passingHandler = new FilterHandler(new Config());
+ private FilterHandler filteringHandler;
+
+ @Before
+ public void before() {
+ Config config = new Config();
+ config.setString(Keys.FILTER_INVALID, String.valueOf(true));
+ config.setString(Keys.FILTER_ZERO, String.valueOf(true));
+ config.setString(Keys.FILTER_DUPLICATE, String.valueOf(true));
+ config.setString(Keys.FILTER_FUTURE, String.valueOf(5 * 60));
+ config.setString(Keys.FILTER_APPROXIMATE, String.valueOf(true));
+ config.setString(Keys.FILTER_STATIC, String.valueOf(true));
+ config.setString(Keys.FILTER_DISTANCE, String.valueOf(10));
+ config.setString(Keys.FILTER_MAX_SPEED, String.valueOf(500));
+ config.setString(Keys.FILTER_SKIP_LIMIT, String.valueOf(10));
+ config.setString(Keys.FILTER_SKIP_ATTRIBUTES_ENABLE, String.valueOf(true));
+ filteringHandler = new FilterHandler(config);
+ }
+
+ private Position createPosition(
+ long deviceId,
+ Date time,
+ boolean valid,
+ double latitude,
+ double longitude,
+ double altitude,
+ double speed,
+ double course) {
+
+ Position position = new Position();
+ position.setDeviceId(deviceId);
+ position.setTime(time);
+ position.setValid(valid);
+ position.setLatitude(latitude);
+ position.setLongitude(longitude);
+ position.setAltitude(altitude);
+ position.setSpeed(speed);
+ position.setCourse(course);
+ return position;
+ }
+
+ @Test
+ public void testFilter() {
+
+ Position position = createPosition(0, new Date(), true, 10, 10, 10, 10, 10);
+
+ assertNotNull(filteringHandler.handlePosition(position));
+ assertNotNull(passingHandler.handlePosition(position));
+
+ position = createPosition(0, new Date(Long.MAX_VALUE), true, 10, 10, 10, 10, 10);
+
+ assertNull(filteringHandler.handlePosition(position));
+ assertNotNull(passingHandler.handlePosition(position));
+
+ position = createPosition(0, new Date(), false, 10, 10, 10, 10, 10);
+
+ assertNull(filteringHandler.handlePosition(position));
+ assertNotNull(passingHandler.handlePosition(position));
+
+ }
+
+ @Test
+ public void testSkipAttributes() {
+
+ Position position = createPosition(0, new Date(), true, 10, 10, 10, 0, 10);
+ position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
+
+ assertNotNull(filteringHandler.handlePosition(position));
+
+ }
+
+}
diff --git a/test/org/traccar/handler/MotionHandlerTest.java b/test/org/traccar/handler/MotionHandlerTest.java
new file mode 100644
index 000000000..9e0859664
--- /dev/null
+++ b/test/org/traccar/handler/MotionHandlerTest.java
@@ -0,0 +1,21 @@
+package org.traccar.handler;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.traccar.model.Position;
+
+public class MotionHandlerTest {
+
+ @Test
+ public void testCalculateMotion() {
+
+ MotionHandler motionHandler = new MotionHandler(0.01);
+
+ Position position = motionHandler.handlePosition(new Position());
+
+ assertEquals(false, position.getAttributes().get(Position.KEY_MOTION));
+
+ }
+
+}
diff --git a/test/org/traccar/handler/events/AlertEventHandlerTest.java b/test/org/traccar/handler/events/AlertEventHandlerTest.java
new file mode 100644
index 000000000..3f0823245
--- /dev/null
+++ b/test/org/traccar/handler/events/AlertEventHandlerTest.java
@@ -0,0 +1,30 @@
+package org.traccar.handler.events;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.traccar.BaseTest;
+import org.traccar.TestIdentityManager;
+import org.traccar.config.Config;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public class AlertEventHandlerTest extends BaseTest {
+
+ @Test
+ public void testAlertEventHandler() {
+
+ AlertEventHandler alertEventHandler = new AlertEventHandler(new Config(), new TestIdentityManager());
+
+ Position position = new Position();
+ position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
+ Map<Event, Position> events = alertEventHandler.analyzePosition(position);
+ assertNotNull(events);
+ Event event = events.keySet().iterator().next();
+ assertEquals(Event.TYPE_ALARM, event.getType());
+ }
+
+}
diff --git a/test/org/traccar/handler/events/CommandResultEventHandlerTest.java b/test/org/traccar/handler/events/CommandResultEventHandlerTest.java
new file mode 100644
index 000000000..0ccf9f6b4
--- /dev/null
+++ b/test/org/traccar/handler/events/CommandResultEventHandlerTest.java
@@ -0,0 +1,28 @@
+package org.traccar.handler.events;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.traccar.BaseTest;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public class CommandResultEventHandlerTest extends BaseTest {
+
+ @Test
+ public void testCommandResultEventHandler() throws Exception {
+
+ CommandResultEventHandler commandResultEventHandler = new CommandResultEventHandler();
+
+ Position position = new Position();
+ position.set(Position.KEY_RESULT, "Test Result");
+ Map<Event, Position> events = commandResultEventHandler.analyzePosition(position);
+ assertNotNull(events);
+ Event event = events.keySet().iterator().next();
+ assertEquals(Event.TYPE_COMMAND_RESULT, event.getType());
+ }
+
+}
diff --git a/test/org/traccar/handler/events/IgnitionEventHandlerTest.java b/test/org/traccar/handler/events/IgnitionEventHandlerTest.java
new file mode 100644
index 000000000..dade20fb8
--- /dev/null
+++ b/test/org/traccar/handler/events/IgnitionEventHandlerTest.java
@@ -0,0 +1,27 @@
+package org.traccar.handler.events;
+
+import static org.junit.Assert.assertNull;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.traccar.BaseTest;
+import org.traccar.TestIdentityManager;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public class IgnitionEventHandlerTest extends BaseTest {
+
+ @Test
+ public void testIgnitionEventHandler() {
+
+ IgnitionEventHandler ignitionEventHandler = new IgnitionEventHandler(new TestIdentityManager());
+
+ Position position = new Position();
+ position.set(Position.KEY_IGNITION, true);
+ position.setValid(true);
+ Map<Event, Position> events = ignitionEventHandler.analyzePosition(position);
+ assertNull(events);
+ }
+
+}
diff --git a/test/org/traccar/handler/events/MotionEventHandlerTest.java b/test/org/traccar/handler/events/MotionEventHandlerTest.java
new file mode 100644
index 000000000..f57c16635
--- /dev/null
+++ b/test/org/traccar/handler/events/MotionEventHandlerTest.java
@@ -0,0 +1,119 @@
+package org.traccar.handler.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.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 testMotionWithPosition() throws Exception {
+ MotionEventHandler motionEventHandler = new MotionEventHandler(
+ null, null, 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.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());
+ }
+
+ @Test
+ public void testMotionWithStatus() throws Exception {
+ MotionEventHandler motionEventHandler = new MotionEventHandler(
+ null, null, 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(
+ null, null, 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/handler/events/OverspeedEventHandlerTest.java b/test/org/traccar/handler/events/OverspeedEventHandlerTest.java
new file mode 100644
index 000000000..515f37b5d
--- /dev/null
+++ b/test/org/traccar/handler/events/OverspeedEventHandlerTest.java
@@ -0,0 +1,128 @@
+package org.traccar.handler.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.config.Config;
+import org.traccar.config.Keys;
+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, long geofenceId) throws ParseException {
+ Config config = new Config();
+ config.setString(Keys.EVENT_OVERSPEED_NOT_REPEAT, String.valueOf(notRepeat));
+ 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);
+
+ 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, geofenceId);
+ assertNull(events);
+ assertFalse(deviceState.getOverspeedState());
+ assertEquals(position, deviceState.getOverspeedPosition());
+ assertEquals(geofenceId, deviceState.getOverspeedGeofenceId());
+
+ Position nextPosition = new Position();
+ nextPosition.setTime(date("2017-01-01 00:00:10"));
+ nextPosition.setSpeed(55);
+
+ events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
+ assertNull(events);
+
+ nextPosition.setTime(date("2017-01-01 00:00:20"));
+
+ events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
+ 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(geofenceId, event.getGeofenceId());
+
+ assertEquals(notRepeat, deviceState.getOverspeedState());
+ assertNull(deviceState.getOverspeedPosition());
+ assertEquals(0, deviceState.getOverspeedGeofenceId());
+
+ nextPosition.setTime(date("2017-01-01 00:00:30"));
+ events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
+ assertNull(events);
+ assertEquals(notRepeat, deviceState.getOverspeedState());
+
+ if (notRepeat) {
+ assertNull(deviceState.getOverspeedPosition());
+ assertEquals(0, deviceState.getOverspeedGeofenceId());
+ } else {
+ assertNotNull(deviceState.getOverspeedPosition());
+ assertEquals(geofenceId, deviceState.getOverspeedGeofenceId());
+ }
+
+ nextPosition.setTime(date("2017-01-01 00:00:40"));
+ nextPosition.setSpeed(30);
+
+ events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
+ assertNull(events);
+ assertFalse(deviceState.getOverspeedState());
+ assertNull(deviceState.getOverspeedPosition());
+ assertEquals(0, deviceState.getOverspeedGeofenceId());
+ }
+
+ private void testOverspeedWithStatus(boolean notRepeat) {
+ Config config = new Config();
+ config.setString(Keys.EVENT_OVERSPEED_NOT_REPEAT, String.valueOf(notRepeat));
+ 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);
+
+ 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, 0);
+ testOverspeedWithPosition(true, 0);
+
+ testOverspeedWithPosition(false, 1);
+ testOverspeedWithPosition(true, 1);
+
+ testOverspeedWithStatus(false);
+ testOverspeedWithStatus(true);
+ }
+
+}