diff --git a/src/ext-test/java/org/opentripplanner/ext/siri/SiriRealtimeTestEnvironment.java b/src/ext-test/java/org/opentripplanner/ext/siri/SiriRealtimeTestEnvironment.java index 3cb7bd05632..6a2472135fb 100644 --- a/src/ext-test/java/org/opentripplanner/ext/siri/SiriRealtimeTestEnvironment.java +++ b/src/ext-test/java/org/opentripplanner/ext/siri/SiriRealtimeTestEnvironment.java @@ -1,7 +1,12 @@ package org.opentripplanner.ext.siri; import java.time.Duration; +import java.time.LocalDate; import java.util.List; +import org.opentripplanner.transit.model.framework.FeedScopedId; +import org.opentripplanner.transit.model.network.TripPattern; +import org.opentripplanner.transit.model.timetable.Trip; +import org.opentripplanner.transit.model.timetable.TripTimes; import org.opentripplanner.updater.TimetableSnapshotSourceParameters; import org.opentripplanner.updater.spi.UpdateResult; import org.opentripplanner.updater.trip.AbstractRealtimeTestEnvironment; @@ -9,12 +14,43 @@ public class SiriRealtimeTestEnvironment extends AbstractRealtimeTestEnvironment { + private static final TimetableSnapshotSourceParameters PARAMETERS = new TimetableSnapshotSourceParameters( + Duration.ZERO, + false + ); private final SiriTimetableSnapshotSource source; public SiriRealtimeTestEnvironment() { super(); - var parameters = new TimetableSnapshotSourceParameters(Duration.ZERO, false); - source = new SiriTimetableSnapshotSource(parameters, transitModel); + source = new SiriTimetableSnapshotSource(PARAMETERS, transitModel); + } + + public EntityResolver getEntityResolver() { + return new EntityResolver(getTransitService(), getFeedId()); + } + + public TripPattern getPatternForTrip(FeedScopedId tripId) { + return getPatternForTrip(tripId, serviceDate); + } + + public TripPattern getPatternForTrip(FeedScopedId tripId, LocalDate serviceDate) { + var transitService = getTransitService(); + var trip = transitService.getTripOnServiceDateById(tripId); + return transitService.getPatternForTrip(trip.getTrip(), serviceDate); + } + + /** + * Find the current TripTimes for a trip id on the default serviceDate + */ + public TripTimes getTripTimesForTrip(Trip trip) { + return getTripTimesForTrip(trip.getId(), serviceDate); + } + + /** + * Find the current TripTimes for a trip id on the default serviceDate + */ + public TripTimes getTripTimesForTrip(String id) { + return getTripTimesForTrip(id(id), serviceDate); } public UpdateResult applyEstimatedTimetable(List updates) { diff --git a/src/test/java/org/opentripplanner/updater/trip/AbstractRealtimeTestEnvironment.java b/src/test/java/org/opentripplanner/updater/trip/AbstractRealtimeTestEnvironment.java index 9047f5e15ef..62407d6b89d 100644 --- a/src/test/java/org/opentripplanner/updater/trip/AbstractRealtimeTestEnvironment.java +++ b/src/test/java/org/opentripplanner/updater/trip/AbstractRealtimeTestEnvironment.java @@ -6,7 +6,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import org.opentripplanner.DateTimeHelper; -import org.opentripplanner.ext.siri.EntityResolver; import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore; import org.opentripplanner.model.StopTime; import org.opentripplanner.model.calendar.CalendarServiceData; @@ -27,7 +26,7 @@ import org.opentripplanner.transit.service.TransitModel; import org.opentripplanner.transit.service.TransitService; -public class AbstractRealtimeTestEnvironment { +abstract public class AbstractRealtimeTestEnvironment { protected static final FeedScopedId CAL_ID = TransitModelForTest.id("CAL_1"); private final TransitModelForTest testModel = TransitModelForTest.of(); @@ -95,34 +94,6 @@ public TransitService getTransitService() { return new DefaultTransitService(transitModel); } - public EntityResolver getEntityResolver() { - return new EntityResolver(getTransitService(), getFeedId()); - } - - public TripPattern getPatternForTrip(FeedScopedId tripId) { - return getPatternForTrip(tripId, serviceDate); - } - - public TripPattern getPatternForTrip(FeedScopedId tripId, LocalDate serviceDate) { - var transitService = getTransitService(); - var trip = transitService.getTripOnServiceDateById(tripId); - return transitService.getPatternForTrip(trip.getTrip(), serviceDate); - } - - /** - * Find the current TripTimes for a trip id on the default serviceDate - */ - public TripTimes getTripTimesForTrip(Trip trip) { - return getTripTimesForTrip(trip.getId(), serviceDate); - } - - /** - * Find the current TripTimes for a trip id on the default serviceDate - */ - public TripTimes getTripTimesForTrip(String id) { - return getTripTimesForTrip(id(id), serviceDate); - } - /** * Find the current TripTimes for a trip id on a serviceDate */ diff --git a/src/test/java/org/opentripplanner/updater/trip/GtfsRealtimeTestEnvironment.java b/src/test/java/org/opentripplanner/updater/trip/GtfsRealtimeTestEnvironment.java index 71b2d99fe5d..837abfb98ae 100644 --- a/src/test/java/org/opentripplanner/updater/trip/GtfsRealtimeTestEnvironment.java +++ b/src/test/java/org/opentripplanner/updater/trip/GtfsRealtimeTestEnvironment.java @@ -3,6 +3,8 @@ import com.google.transit.realtime.GtfsRealtime; import java.time.Duration; import java.util.List; +import org.opentripplanner.transit.model.network.TripPattern; +import org.opentripplanner.transit.model.timetable.Trip; import org.opentripplanner.updater.TimetableSnapshotSourceParameters; import org.opentripplanner.updater.spi.UpdateResult; @@ -29,4 +31,8 @@ public UpdateResult applyTripUpdates(List updates) { getFeedId() ); } + + public TripPattern getPatternForTrip(Trip trip) { + return transitModel.getTransitModelIndex().getPatternForTrip().get(trip); + } } diff --git a/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/DelayedTest.java b/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/DelayedTest.java index c44b99d5e6d..5d80ae2eb03 100644 --- a/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/DelayedTest.java +++ b/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/DelayedTest.java @@ -3,57 +3,83 @@ import static com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; import org.junit.jupiter.api.Test; import org.opentripplanner.model.Timetable; import org.opentripplanner.model.TimetableSnapshot; -import org.opentripplanner.transit.model.network.TripPattern; import org.opentripplanner.transit.model.timetable.RealTimeState; import org.opentripplanner.updater.trip.GtfsRealtimeTestEnvironment; import org.opentripplanner.updater.trip.TripUpdateBuilder; +/** + * Delays should be applied to the first trip but should leave the second trip untouched. + */ public class DelayedTest { + private static final int DELAY = 1; + private static final int STOP_SEQUENCE = 1; + @Test public void delayed() { var env = new GtfsRealtimeTestEnvironment(); - final TripPattern pattern = env.transitModel - .getTransitModelIndex() - .getPatternForTrip() - .get(env.trip1); - final int tripIndex = pattern.getScheduledTimetable().getTripIndex(env.trip1.getId()); - //final int tripIndex2 = pattern.getScheduledTimetable().getTripIndex(env.trip2.getId()); - - var tripUpdateBuilder = new TripUpdateBuilder( + var tripUpdate = new TripUpdateBuilder( env.trip1.getId().getId(), env.serviceDate, SCHEDULED, env.timeZone - ); - - int stopSequence = 1; - int delay = 1; - tripUpdateBuilder.addDelayedStopTime(stopSequence, delay); - - var tripUpdate = tripUpdateBuilder.build(); + ) + .addDelayedStopTime(STOP_SEQUENCE, DELAY) + .build(); var result = env.applyTripUpdates(tripUpdate); assertEquals(1, result.successful()); - final TimetableSnapshot snapshot = env.source.getTimetableSnapshot(); - final Timetable forToday = snapshot.resolve(pattern, env.serviceDate); - final Timetable schedule = snapshot.resolve(pattern, null); - assertNotSame(forToday, schedule); - assertNotSame(forToday.getTripTimes(tripIndex), schedule.getTripTimes(tripIndex)); - //assertSame(forToday.getTripTimes(tripIndex2), schedule.getTripTimes(tripIndex2)); - assertEquals(1, forToday.getTripTimes(tripIndex).getArrivalDelay(1)); - assertEquals(1, forToday.getTripTimes(tripIndex).getDepartureDelay(1)); - - assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex).getRealTimeState()); - assertEquals(RealTimeState.UPDATED, forToday.getTripTimes(tripIndex).getRealTimeState()); - //assertEquals(RealTimeState.SCHEDULED, schedule.getTripTimes(tripIndex2).getRealTimeState()); - //assertEquals(RealTimeState.SCHEDULED, forToday.getTripTimes(tripIndex2).getRealTimeState()); + // trip1 should be modified + { + var pattern1 = env.getPatternForTrip(env.trip1); + final int trip1Index = pattern1.getScheduledTimetable().getTripIndex(env.trip1.getId()); + + final TimetableSnapshot snapshot = env.source.getTimetableSnapshot(); + final Timetable trip1Realtime = snapshot.resolve(pattern1, env.serviceDate); + final Timetable trip1Scheduled = snapshot.resolve(pattern1, null); + + assertNotSame(trip1Realtime, trip1Scheduled); + assertNotSame( + trip1Realtime.getTripTimes(trip1Index), + trip1Scheduled.getTripTimes(trip1Index) + ); + assertEquals(1, trip1Realtime.getTripTimes(trip1Index).getArrivalDelay(STOP_SEQUENCE)); + assertEquals(1, trip1Realtime.getTripTimes(trip1Index).getDepartureDelay(STOP_SEQUENCE)); + + assertEquals( + RealTimeState.SCHEDULED, + trip1Scheduled.getTripTimes(trip1Index).getRealTimeState() + ); + assertEquals( + RealTimeState.UPDATED, + trip1Realtime.getTripTimes(trip1Index).getRealTimeState() + ); + } + + // trip2 should keep the scheduled information + { + var pattern = env.getPatternForTrip(env.trip2); + final int tripIndex = pattern.getScheduledTimetable().getTripIndex(env.trip2.getId()); + + final TimetableSnapshot snapshot = env.source.getTimetableSnapshot(); + final Timetable realtime = snapshot.resolve(pattern, env.serviceDate); + final Timetable scheduled = snapshot.resolve(pattern, null); + + assertSame(realtime, scheduled); + assertSame(realtime.getTripTimes(tripIndex), scheduled.getTripTimes(tripIndex)); + assertEquals(0, realtime.getTripTimes(tripIndex).getArrivalDelay(STOP_SEQUENCE)); + assertEquals(0, realtime.getTripTimes(tripIndex).getDepartureDelay(STOP_SEQUENCE)); + + assertEquals(RealTimeState.SCHEDULED, scheduled.getTripTimes(tripIndex).getRealTimeState()); + assertEquals(RealTimeState.SCHEDULED, realtime.getTripTimes(tripIndex).getRealTimeState()); + } } } diff --git a/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/SkippedTest.java b/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/SkippedTest.java index 4af2e282a09..9d8a06cdc06 100644 --- a/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/SkippedTest.java +++ b/src/test/java/org/opentripplanner/updater/trip/moduletests/delay/SkippedTest.java @@ -17,6 +17,9 @@ import org.opentripplanner.updater.trip.GtfsRealtimeTestEnvironment; import org.opentripplanner.updater.trip.TripUpdateBuilder; +/** + * A mixture of delayed and skipped stops should result in both delayed and cancelled stops. + */ public class SkippedTest { @Test