From bf8e921d339982ff21b63549e7dd490c5dc982b8 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 8 May 2024 18:00:45 -0400 Subject: [PATCH 1/2] test(ManageLegTraversal): Replace time string parsing with timestamps --- .../triptracker/TrackingLocation.java | 5 ++-- .../api/ManageLegTraversalTest.java | 25 ++++++------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java index e161a88aa..88a7a394d 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java @@ -1,6 +1,5 @@ package org.opentripplanner.middleware.triptracker; -import java.time.ZonedDateTime; import java.util.Date; /** @@ -39,8 +38,8 @@ public TrackingLocation(Double lat, Double lon, Date timestamp) { } /** Used in testing **/ - public TrackingLocation(String dateTime, double lat, double lon) { - this.timestamp = new Date(ZonedDateTime.parse(dateTime).toInstant().toEpochMilli()); + public TrackingLocation(long millis, double lat, double lon) { + this.timestamp = new Date(millis); this.lat = lat; this.lon = lon; } diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java index 30f4e9526..c0c908d17 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java @@ -23,10 +23,7 @@ import org.opentripplanner.middleware.utils.JsonUtils; import java.io.IOException; -import java.time.Instant; -import java.time.ZoneId; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; @@ -68,7 +65,7 @@ public static void setUp() throws IOException { @ParameterizedTest @MethodSource("createTrace") - void canTrackTrip(String time, double lat, double lon, TripStatus expected, String message) { + void canTrackTrip(long time, double lat, double lon, TripStatus expected, String message) { TrackedJourney trackedJourney = new TrackedJourney(); TrackingLocation trackingLocation = new TrackingLocation(time, lat, lon); trackedJourney.locations = List.of(trackingLocation); @@ -95,49 +92,49 @@ private static Stream createTrace() { ); return Stream.of( Arguments.of( - getDateTimeAsString(startTime, before.cumulativeTime - before.timeInSegment), + startTime.getTime() + 1000 * (long) Math.floor(before.cumulativeTime - before.timeInSegment), current.start.lat, current.start.lon, TripStatus.AHEAD_OF_SCHEDULE, "For the current location and time the traveler is ahead of schedule." ), Arguments.of( - getDateTimeAsString(startTime, current.cumulativeTime - current.timeInSegment), + startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime - current.timeInSegment), current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time the traveler is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, after.cumulativeTime), + startTime.getTime() + 1000 * (long) Math.floor(after.cumulativeTime), current.start.lat, current.start.lon, TripStatus.BEHIND_SCHEDULE, "For the current location and time the traveler is behind schedule." ), Arguments.of( - getDateTimeAsString(startTime, (current.cumulativeTime - current.timeInSegment) - 10), + startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime - current.timeInSegment) - 10000, current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time (with a slight deviation) the traveler is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, current.cumulativeTime), + startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime), current.start.lat + 0.00001, current.start.lon + 0.00001, TripStatus.ON_SCHEDULE, "The current location, with a slight deviation, is on schedule." ), Arguments.of( - getDateTimeAsString(startTime, 0), + startTime.getTime(), notOnTripCoordinates.lat, notOnTripCoordinates.lon, TripStatus.DEVIATED, "Arbitrary lat/lon values which aren't on the trip leg." ), Arguments.of( - getDateTimeAsString(busStopToJusticeCenterItinerary.endTime, 1), + busStopToJusticeCenterItinerary.endTime.getTime() + 1000, deviatedCoordinates.lat, deviatedCoordinates.lon, TripStatus.DEVIATED, @@ -422,12 +419,6 @@ private static List createSegmentsForLeg() { return interpolatePoints(busStopToJusticeCenterItinerary.legs.get(0)); } - private static String getDateTimeAsString(Date date, double offset) { - Instant dateTime = date.toInstant().plusSeconds((long) offset); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.systemDefault());; - return formatter.format(dateTime); - } - private int getNumberOfExcludedPoints(List legPositions, Leg leg) { int excluded = 0; for (Position position : legPositions) { From 52f62445d28e0e10e455c3b2a0d0fe30d91e429b Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 8 May 2024 18:12:10 -0400 Subject: [PATCH 2/2] test(ManageLegTraversal): Use Instant in test cases. --- .../triptracker/TrackingLocation.java | 7 +++---- .../api/ManageLegTraversalTest.java | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java index 88a7a394d..c31340863 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TrackingLocation.java @@ -1,5 +1,6 @@ package org.opentripplanner.middleware.triptracker; +import java.time.Instant; import java.util.Date; /** @@ -38,9 +39,7 @@ public TrackingLocation(Double lat, Double lon, Date timestamp) { } /** Used in testing **/ - public TrackingLocation(long millis, double lat, double lon) { - this.timestamp = new Date(millis); - this.lat = lat; - this.lon = lon; + public TrackingLocation(Instant instant, double lat, double lon) { + this(lat, lon, new Date(instant.toEpochMilli())); } } diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java index c0c908d17..e9d27a2d7 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/ManageLegTraversalTest.java @@ -23,6 +23,7 @@ import org.opentripplanner.middleware.utils.JsonUtils; import java.io.IOException; +import java.time.Instant; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Date; @@ -65,9 +66,9 @@ public static void setUp() throws IOException { @ParameterizedTest @MethodSource("createTrace") - void canTrackTrip(long time, double lat, double lon, TripStatus expected, String message) { + void canTrackTrip(Instant instant, double lat, double lon, TripStatus expected, String message) { TrackedJourney trackedJourney = new TrackedJourney(); - TrackingLocation trackingLocation = new TrackingLocation(time, lat, lon); + TrackingLocation trackingLocation = new TrackingLocation(instant, lat, lon); trackedJourney.locations = List.of(trackingLocation); TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, busStopToJusticeCenterItinerary); TripStatus tripStatus = TripStatus.getTripStatus(travelerPosition); @@ -90,51 +91,53 @@ private static Stream createTrace() { 1000, calculateBearing(current.start, after.start) ); + Instant startInstant = startTime.toInstant(); + long currentSegmentStartOffsetSecs = (long) Math.floor(current.cumulativeTime - current.timeInSegment); return Stream.of( Arguments.of( - startTime.getTime() + 1000 * (long) Math.floor(before.cumulativeTime - before.timeInSegment), + startInstant.plusSeconds((long) Math.floor(before.cumulativeTime - before.timeInSegment)), current.start.lat, current.start.lon, TripStatus.AHEAD_OF_SCHEDULE, "For the current location and time the traveler is ahead of schedule." ), Arguments.of( - startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime - current.timeInSegment), + startInstant.plusSeconds(currentSegmentStartOffsetSecs), current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time the traveler is on schedule." ), Arguments.of( - startTime.getTime() + 1000 * (long) Math.floor(after.cumulativeTime), + startInstant.plusSeconds((long) Math.floor(after.cumulativeTime)), current.start.lat, current.start.lon, TripStatus.BEHIND_SCHEDULE, "For the current location and time the traveler is behind schedule." ), Arguments.of( - startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime - current.timeInSegment) - 10000, + startInstant.plusSeconds(currentSegmentStartOffsetSecs - 10), current.start.lat, current.start.lon, TripStatus.ON_SCHEDULE, "For the current location and time (with a slight deviation) the traveler is on schedule." ), Arguments.of( - startTime.getTime() + 1000 * (long) Math.floor(current.cumulativeTime), + startInstant.plusSeconds((long) Math.floor(current.cumulativeTime)), current.start.lat + 0.00001, current.start.lon + 0.00001, TripStatus.ON_SCHEDULE, "The current location, with a slight deviation, is on schedule." ), Arguments.of( - startTime.getTime(), + startInstant, notOnTripCoordinates.lat, notOnTripCoordinates.lon, TripStatus.DEVIATED, "Arbitrary lat/lon values which aren't on the trip leg." ), Arguments.of( - busStopToJusticeCenterItinerary.endTime.getTime() + 1000, + busStopToJusticeCenterItinerary.endTime.toInstant().plusSeconds(1), deviatedCoordinates.lat, deviatedCoordinates.lon, TripStatus.DEVIATED,