diff options
Diffstat (limited to 'src/test/java')
4 files changed, 69 insertions, 47 deletions
diff --git a/src/test/java/org/traccar/handler/MotionHandlerTest.java b/src/test/java/org/traccar/handler/MotionHandlerTest.java index 2a7af23ba..10cdf6a90 100644 --- a/src/test/java/org/traccar/handler/MotionHandlerTest.java +++ b/src/test/java/org/traccar/handler/MotionHandlerTest.java @@ -1,10 +1,15 @@ package org.traccar.handler; import org.junit.jupiter.api.Test; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.model.Device; import org.traccar.model.Position; -import org.traccar.reports.common.TripsConfig; +import org.traccar.session.cache.CacheManager; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -13,10 +18,13 @@ public class MotionHandlerTest { @Test public void testCalculateMotion() { - TripsConfig tripsConfig = mock(TripsConfig.class); - when(tripsConfig.getSpeedThreshold()).thenReturn(0.01); + var cacheManager = mock(CacheManager.class); + when(cacheManager.getObject(eq(Device.class), anyLong())).thenReturn(mock(Device.class)); + var config = mock(Config.class); + when(config.getString(Keys.EVENT_MOTION_SPEED_THRESHOLD.getKey())).thenReturn("0.01"); + when(cacheManager.getConfig()).thenReturn(config); - MotionHandler motionHandler = new MotionHandler(tripsConfig); + MotionHandler motionHandler = new MotionHandler(cacheManager); Position position = motionHandler.handlePosition(new Position()); diff --git a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java index f2d858656..c61ae913d 100644 --- a/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/MotionEventHandlerTest.java @@ -36,7 +36,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testMotionWithPosition() throws ParseException { - TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false, false, 0.01); + TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false); MotionState state = new MotionState(); @@ -63,7 +63,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testMotionFluctuation() throws ParseException { - TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false, false, 0.01); + TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, false); MotionState state = new MotionState(); @@ -94,7 +94,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testStopWithPositionIgnition() throws ParseException { - TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, true, false, 0.01); + TripsConfig tripsConfig = new TripsConfig(500, 300000, 300000, 0, true); MotionState state = new MotionState(); state.setMotionStreak(true); diff --git a/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java index 67f214563..2f317d049 100644 --- a/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java @@ -15,6 +15,14 @@ public class HuabaoProtocolDecoderTest extends ProtocolTest { "7e010200204f07788ef67601824f4459344f544d314d4459774d4441314d444977626d5633553235536457786cba7e")); verifyAttribute(decoder, binary( + "7e0200006476806111898300710000000000100046005d3156065f7128000000000000230511165956660b01fe000001031a5d1a8101670831333231343332326902018b6a01166b01006c0f323034303830393230373533363735711438393434343738383030303030323131303030464b7e"), + Position.KEY_BATTERY, 3.95); + + verifyAttribute(decoder, binary( + "7e02000071768060874297002d0000000000208022015a30b006c869f8000000000000230505062034600b0004003930303235343939660b01cc0000000c40f89f27b067083133323134333232690201936a01116b01006c0f34363030383138353937303632343071143839383630343938313032313930353835373430607e"), + Position.KEY_EVENT, 4); + + verifyAttribute(decoder, binary( "7e0200002f017028775424038d000000000000000a0189dbeb04ca653a00000012014723040700074401040000000030011b31010ee1012dea020001dc7e"), Position.KEY_BATTERY_LEVEL, 45); diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java index 09e5b3e27..22d70c93a 100644 --- a/src/test/java/org/traccar/reports/ReportUtilsTest.java +++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java @@ -2,15 +2,16 @@ package org.traccar.reports; import org.apache.velocity.app.VelocityEngine; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; 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; @@ -24,6 +25,7 @@ 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.jupiter.api.Assertions.assertEquals; @@ -62,6 +64,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 +93,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); @@ -101,12 +115,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:15:00.000", 0, 3000), position("2016-01-01 00:25:00.000", 0, 3000)); - 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.detectTripsAndStops(device, data, false, 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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -156,12 +169,11 @@ public class ReportUtilsTest extends BaseTest { 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.detectTripsAndStops(device, data, false, TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -175,7 +187,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.detectTripsAndStops(device, data, false, TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -189,7 +201,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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -227,12 +239,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:19:00.000", 0, 7000), position("2016-01-01 00:29:00.000", 0, 7000)); - 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.detectTripsAndStops(device, data, false, TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -246,7 +257,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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -278,12 +289,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:04:00.000", 1, 0), position("2016-01-01 00:05:00.000", 0, 0)); - 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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -307,12 +317,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:04:00.000", 1, 0), position("2016-01-01 00:05:00.000", 2, 0)); - 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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -336,12 +345,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:22:00.000", 0, 0), position("2016-01-01 00:32:00.000", 0, 0)); - 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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -365,12 +373,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:04:00.000", 5, 0), position("2016-01-01 00:05:00.000", 5, 0)); - 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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(result); assertTrue(result.isEmpty()); @@ -390,12 +397,11 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:24:00.000", 5, 800), position("2016-01-01 00:25:00.000", 5, 900)); - 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.detectTripsAndStops(device, data, false, TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -409,7 +415,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.detectTripsAndStops(device, data, false, StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); |