aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/reports/ReportUtilsTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/traccar/reports/ReportUtilsTest.java')
-rw-r--r--src/test/java/org/traccar/reports/ReportUtilsTest.java155
1 files changed, 84 insertions, 71 deletions
diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java
index 4bf668064..0f14577fd 100644
--- a/src/test/java/org/traccar/reports/ReportUtilsTest.java
+++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java
@@ -1,16 +1,16 @@
package org.traccar.reports;
import org.apache.velocity.app.VelocityEngine;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.traccar.BaseTest;
import org.traccar.api.security.PermissionsService;
import org.traccar.config.Config;
+import org.traccar.config.Keys;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Position;
import org.traccar.reports.common.ReportUtils;
-import org.traccar.reports.common.TripsConfig;
import org.traccar.reports.model.StopReportItem;
import org.traccar.reports.model.TripReportItem;
import org.traccar.storage.Storage;
@@ -20,17 +20,18 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.TimeZone;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -38,10 +39,10 @@ public class ReportUtilsTest extends BaseTest {
private Storage storage;
- @Before
+ @BeforeEach
public void init() throws StorageException {
storage = mock(Storage.class);
- when(storage.getObject(any(), any())).thenReturn(mock(Device.class));
+ when(storage.getObject(eq(Device.class), any())).thenReturn(mock(Device.class));
}
private Date date(String time) throws ParseException {
@@ -62,6 +63,19 @@ public class ReportUtilsTest extends BaseTest {
return position;
}
+
+ private Device mockDevice(
+ double minimalTripDistance, long minimalTripDuration, long minimalParkingDuration,
+ long minimalNoDataDuration, boolean useIgnition) {
+ Device device = mock(Device.class);
+ when(device.getAttributes()).thenReturn(Map.of(
+ Keys.REPORT_TRIP_MINIMAL_TRIP_DISTANCE.getKey(), minimalTripDistance,
+ Keys.REPORT_TRIP_MINIMAL_TRIP_DURATION.getKey(), minimalTripDuration,
+ Keys.REPORT_TRIP_MINIMAL_PARKING_DURATION.getKey(), minimalParkingDuration,
+ Keys.REPORT_TRIP_MINIMAL_NO_DATA_DURATION.getKey(), minimalNoDataDuration,
+ Keys.REPORT_TRIP_USE_IGNITION.getKey(), useIgnition));
+ return device;
+ }
@Test
public void testCalculateDistance() {
@@ -78,8 +92,7 @@ public class ReportUtilsTest extends BaseTest {
@Test
public void testCalculateSpentFuel() {
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- mock(TripsConfig.class), mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
Position startPosition = new Position();
Position endPosition = new Position();
assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.0, 0.01);
@@ -98,15 +111,15 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:03:00.000", 10, 1000),
position("2016-01-01 00:04:00.000", 10, 2000),
position("2016-01-01 00:05:00.000", 0, 3000),
- position("2016-01-01 00:06:00.000", 0, 3000),
- position("2016-01-01 00:07:00.000", 0, 3000));
+ position("2016-01-01 00:15:00.000", 0, 3000),
+ position("2016-01-01 00:25:00.000", 0, 3000));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 180, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var trips = reportUtils.detectTripsAndStops(mock(Device.class), data, false, TripReportItem.class);
+ var trips = reportUtils.slowTripsAndStops(device, new Date(), new Date(), TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -120,7 +133,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- var stops = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var stops = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -136,8 +149,8 @@ public class ReportUtilsTest extends BaseTest {
itemStop = iterator.next();
assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime());
- assertEquals(date("2016-01-01 00:07:00.000"), itemStop.getEndTime());
- assertEquals(120000, itemStop.getDuration());
+ assertEquals(date("2016-01-01 00:25:00.000"), itemStop.getEndTime());
+ assertEquals(1200000, itemStop.getDuration());
}
@@ -151,17 +164,17 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:03:00.000", 10, 1000),
position("2016-01-01 00:04:00.000", 10, 2000),
position("2016-01-01 00:05:00.000", 0, 3000),
- position("2016-01-01 00:06:00.000", 0, 3000),
- position("2016-01-01 00:07:00.000", 0, 3000));
+ position("2016-01-01 00:15:00.000", 0, 3000),
+ position("2016-01-01 00:25:00.000", 0, 3000));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
data.get(5).set(Position.KEY_IGNITION, false);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, true, false, 0.01);
+ Device device = mockDevice(500, 300, 180, 900, true);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var trips = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, TripReportItem.class);
+ var trips = reportUtils.slowTripsAndStops(device, new Date(), new Date(), TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -175,7 +188,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- trips = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, TripReportItem.class);
+ trips = reportUtils.slowTripsAndStops(device, new Date(), new Date(), TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -189,7 +202,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- var stops = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var stops = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -205,8 +218,8 @@ public class ReportUtilsTest extends BaseTest {
itemStop = iterator.next();
assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime());
- assertEquals(date("2016-01-01 00:07:00.000"), itemStop.getEndTime());
- assertEquals(120000, itemStop.getDuration());
+ assertEquals(date("2016-01-01 00:25:00.000"), itemStop.getEndTime());
+ assertEquals(1200000, itemStop.getDuration());
}
@@ -224,15 +237,15 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:07:00.000", 0, 5000),
position("2016-01-01 00:08:00.000", 10, 6000),
position("2016-01-01 00:09:00.000", 0, 7000),
- position("2016-01-01 00:10:00.000", 0, 7000),
- position("2016-01-01 00:11:00.000", 0, 7000));
+ position("2016-01-01 00:19:00.000", 0, 7000),
+ position("2016-01-01 00:29:00.000", 0, 7000));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 180, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var trips = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, TripReportItem.class);
+ var trips = reportUtils.slowTripsAndStops(device, new Date(), new Date(), TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -246,7 +259,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(7000, itemTrip.getDistance(), 0.01);
- var stops = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var stops = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -262,28 +275,28 @@ public class ReportUtilsTest extends BaseTest {
itemStop = iterator.next();
assertEquals(date("2016-01-01 00:09:00.000"), itemStop.getStartTime());
- assertEquals(date("2016-01-01 00:11:00.000"), itemStop.getEndTime());
- assertEquals(120000, itemStop.getDuration());
+ assertEquals(date("2016-01-01 00:29:00.000"), itemStop.getEndTime());
+ assertEquals(1200000, itemStop.getDuration());
}
@Test
public void testDetectStopsOnly() throws Exception {
- Collection<Position> data = Arrays.asList(
+ var data = Arrays.asList(
position("2016-01-01 00:00:00.000", 0, 0),
position("2016-01-01 00:01:00.000", 0, 0),
position("2016-01-01 00:02:00.000", 1, 0),
position("2016-01-01 00:03:00.000", 0, 0),
position("2016-01-01 00:04:00.000", 1, 0),
position("2016-01-01 00:05:00.000", 0, 0));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 200, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var result = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var result = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -299,20 +312,20 @@ public class ReportUtilsTest extends BaseTest {
@Test
public void testDetectStopsWithTripCut() throws Exception {
- Collection<Position> data = Arrays.asList(
+ var data = Arrays.asList(
position("2016-01-01 00:00:00.000", 0, 0),
position("2016-01-01 00:01:00.000", 0, 0),
position("2016-01-01 00:02:00.000", 0, 0),
position("2016-01-01 00:03:00.000", 0, 0),
position("2016-01-01 00:04:00.000", 1, 0),
position("2016-01-01 00:05:00.000", 2, 0));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 200, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var result = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var result = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -328,20 +341,20 @@ public class ReportUtilsTest extends BaseTest {
@Test
public void testDetectStopsStartedFromTrip() throws Exception {
- Collection<Position> data = Arrays.asList(
+ var data = Arrays.asList(
position("2016-01-01 00:00:00.000", 2, 0),
position("2016-01-01 00:01:00.000", 1, 0),
position("2016-01-01 00:02:00.000", 0, 0),
- position("2016-01-01 00:03:00.000", 0, 0),
- position("2016-01-01 00:04:00.000", 0, 0),
- position("2016-01-01 00:05:00.000", 0, 0));
+ position("2016-01-01 00:12:00.000", 0, 0),
+ position("2016-01-01 00:22:00.000", 0, 0),
+ position("2016-01-01 00:32:00.000", 0, 0));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 200, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var result = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var result = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -349,28 +362,28 @@ public class ReportUtilsTest extends BaseTest {
StopReportItem itemStop = result.iterator().next();
assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getStartTime());
- assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime());
- assertEquals(180000, itemStop.getDuration());
+ assertEquals(date("2016-01-01 00:32:00.000"), itemStop.getEndTime());
+ assertEquals(1800000, itemStop.getDuration());
}
@Test
public void testDetectStopsMoving() throws Exception {
- Collection<Position> data = Arrays.asList(
+ var data = Arrays.asList(
position("2016-01-01 00:00:00.000", 5, 0),
position("2016-01-01 00:01:00.000", 5, 0),
position("2016-01-01 00:02:00.000", 3, 0),
position("2016-01-01 00:03:00.000", 5, 0),
position("2016-01-01 00:04:00.000", 5, 0),
position("2016-01-01 00:05:00.000", 5, 0));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 300, 200, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var result = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var result = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(result);
assertTrue(result.isEmpty());
@@ -380,7 +393,7 @@ public class ReportUtilsTest extends BaseTest {
@Test
public void testDetectTripAndStopByGap() throws Exception {
- Collection<Position> data = Arrays.asList(
+ var data = Arrays.asList(
position("2016-01-01 00:00:00.000", 7, 100),
position("2016-01-01 00:01:00.000", 7, 300),
position("2016-01-01 00:02:00.000", 5, 500),
@@ -389,13 +402,13 @@ public class ReportUtilsTest extends BaseTest {
position("2016-01-01 00:23:00.000", 2, 700),
position("2016-01-01 00:24:00.000", 5, 800),
position("2016-01-01 00:25:00.000", 5, 900));
+ when(storage.getObjects(eq(Position.class), any())).thenReturn(data);
- TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false, false, 0.01);
+ Device device = mockDevice(500, 200, 200, 900, false);
ReportUtils reportUtils = new ReportUtils(
- mock(Config.class), storage, mock(PermissionsService.class),
- tripsConfig, mock(VelocityEngine.class), null);
+ mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
- var trips = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, TripReportItem.class);
+ var trips = reportUtils.slowTripsAndStops(device, new Date(), new Date(), TripReportItem.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -409,7 +422,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(7, itemTrip.getMaxSpeed(), 0.01);
assertEquals(600, itemTrip.getDistance(), 0.01);
- var stops = reportUtils.detectTripsAndStops(mock(Device.class) ,data, false, StopReportItem.class);
+ var stops = reportUtils.slowTripsAndStops(device, new Date(), new Date(), StopReportItem.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());