diff options
Diffstat (limited to 'test/org/traccar')
-rw-r--r-- | test/org/traccar/protocol/Gps103ProtocolDecoderTest.java | 3 | ||||
-rw-r--r-- | test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java | 6 | ||||
-rw-r--r-- | test/org/traccar/reports/ReportUtilsTest.java | 234 | ||||
-rw-r--r-- | test/org/traccar/reports/TripsTest.java | 70 |
4 files changed, 240 insertions, 73 deletions
diff --git a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java index 345a89ad9..ee48ebb27 100644 --- a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Gps103ProtocolDecoderTest extends ProtocolTest { Gps103ProtocolDecoder decoder = new Gps103ProtocolDecoder(new Gps103Protocol()); + verifyNull(decoder, text( + "imei:868683027825532,OBD,170613203014,,,,0,0,0.00%,0,0.00%,0,0.00,,,,;")); + verifyAttributes(decoder, text( "imei:862106025092216,OBD,170612165656,196043,,145803.9,,000,0.0%,+,0.0%,00000,12.6,,,,;")); diff --git a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java index 1eb188421..89b994fbe 100644 --- a/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java +++ b/test/org/traccar/protocol/OwnTracksProtocolDecoderTest.java @@ -12,11 +12,13 @@ public class OwnTracksProtocolDecoderTest extends ProtocolTest { OwnTracksProtocolDecoder decoder = new OwnTracksProtocolDecoder(new OwnTracksProtocol()); verifyPosition(decoder, request(HttpMethod.POST, "/", - buffer(text("{\"lon\":2.29513,\"lat\":48.85833,\"tst\":1497349316,\"_type\":\"location\",\"tid\":\"JJ\"}")))); + buffer("{\"lon\":2.29513,\"lat\":48.85833,\"tst\":1497349316,\"_type\":\"location\",\"tid\":\"JJ\"}"))); verifyPosition(decoder, request(HttpMethod.POST, "/", - buffer(text("{\"cog\":271,\"lon\":2.29513,\"acc\":5,\"vel\":61,\"vac\":21,\"lat\":48.85833,\"tst\":1497349316,\"alt\":167,\"_type\":\"location\",\"tid\":\"JJ\",\"t\":\"u\",\"batt\":67}")))); + buffer("{\"cog\":271,\"lon\":2.29513,\"acc\":5,\"vel\":61,\"vac\":21,\"lat\":48.85833,\"tst\":1497349316,\"alt\":167,\"_type\":\"location\",\"tid\":\"JJ\",\"t\":\"u\",\"batt\":67}"))); + verifyPosition(decoder, request(HttpMethod.POST, "/", + buffer("{\"lat\":48.85,\"lon\":2.295,\"_type\":\"location\",\"tid\":\"JJ\",\"tst\":1497476456}"))); } } diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index 2f785c538..2fe05e688 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -1,10 +1,48 @@ package org.traccar.reports; +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 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.TimeZone; + import org.junit.Assert; import org.junit.Test; +import org.traccar.BaseTest; import org.traccar.model.Position; +import org.traccar.reports.model.BaseReport; +import org.traccar.reports.model.StopReport; +import org.traccar.reports.model.TripReport; +import org.traccar.reports.model.TripsConfig; + +public class ReportUtilsTest extends BaseTest { + + private Date date(String time) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return dateFormat.parse(time); + } -public class ReportUtilsTest { + private Position position(String time, double speed, double totalDistance) throws ParseException { + + Position position = new Position(); + + if (time != null) { + position.setTime(date(time)); + } + position.setValid(true); + position.setSpeed(speed); + position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); + + return position; + } @Test public void testCalculateDistance() { @@ -28,4 +66,198 @@ public class ReportUtilsTest { Assert.assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), "0.2"); } + @Test + public void testDetectTripsSimple() throws ParseException { + + Collection<Position> 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", 10, 0), + 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, true); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + TripReport itemTrip = (TripReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); + assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime()); + assertEquals(180000, itemTrip.getDuration()); + assertEquals(10, itemTrip.getAverageSpeed(), 0.01); + assertEquals(10, itemTrip.getMaxSpeed(), 0.01); + assertEquals(3000, itemTrip.getDistance(), 0.01); + + result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + StopReport itemStop = (StopReport) result.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()); + + } + + @Test + public void testDetectStopsOnly() throws ParseException { + + Collection<Position> 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + StopReport itemStop = (StopReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); + assertEquals(300000, itemStop.getDuration()); + + } + + @Test + public void testDetectStopsWithTripCut() throws ParseException { + + Collection<Position> 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + StopReport itemStop = (StopReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getEndTime()); + assertEquals(240000, itemStop.getDuration()); + + tripsConfig.setGreedyParking(true); + + result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + itemStop = (StopReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); + assertEquals(300000, itemStop.getDuration()); + + } + + @Test + public void testDetectStopsStartedFromTrip() throws ParseException { + + Collection<Position> 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + StopReport itemStop = (StopReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); + assertEquals(300000, itemStop.getDuration()); + + } + + @Test + public void testDetectStopsMoving() throws ParseException { + + Collection<Position> 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertTrue(result.isEmpty()); + + } + + @Test + public void testDetectTripAndStopByGap() throws ParseException { + + Collection<Position> 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), + position("2016-01-01 00:03:00.000", 5, 600), + position("2016-01-01 00:04:00.000", 3, 700), + 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)); + + TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, false, 900000); + + Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, true); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + TripReport itemTrip = (TripReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemTrip.getStartTime()); + assertEquals(date("2016-01-01 00:04:00.000"), itemTrip.getEndTime()); + assertEquals(240000, itemTrip.getDuration()); + assertEquals(6.75, itemTrip.getAverageSpeed(), 0.01); + assertEquals(7, itemTrip.getMaxSpeed(), 0.01); + assertEquals(600, itemTrip.getDistance(), 0.01); + + result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + StopReport itemStop = (StopReport) result.iterator().next(); + + assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:23:00.000"), itemStop.getEndTime()); + assertEquals(1140000, itemStop.getDuration()); + } + } diff --git a/test/org/traccar/reports/TripsTest.java b/test/org/traccar/reports/TripsTest.java deleted file mode 100644 index dea101cf2..000000000 --- a/test/org/traccar/reports/TripsTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.traccar.reports; - -import org.junit.Test; -import org.traccar.BaseTest; -import org.traccar.model.Position; -import org.traccar.reports.model.TripReport; - -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.TimeZone; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -public class TripsTest extends BaseTest { - - private Date date(String time) throws ParseException { - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return dateFormat.parse(time); - } - - private Position position(String time, double speed, double totalDistance) throws ParseException { - - Position position = new Position(); - - if (time != null) { - position.setTime(date(time)); - } - position.setValid(true); - position.setSpeed(speed); - position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); - - return position; - } - - @Test - public void testDetectTripsSimple() throws ParseException { - - Collection<Position> 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", 10, 0), - 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)); - - Collection<TripReport> result = Trips.detectTrips(0.01, 500, 300000, 300000, false, false, data); - - assertNotNull(result); - assertFalse(result.isEmpty()); - - TripReport item = result.iterator().next(); - - assertEquals(date("2016-01-01 00:02:00.000"), item.getStartTime()); - assertEquals(date("2016-01-01 00:05:00.000"), item.getEndTime()); - assertEquals(180000, item.getDuration()); - assertEquals(10, item.getAverageSpeed(), 0.01); - assertEquals(10, item.getMaxSpeed(), 0.01); - assertEquals(3000, item.getDistance(), 0.01); - - } - -} |