Skip to content

Commit

Permalink
add test for new transit service methods
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Nov 14, 2024
1 parent 29db2ef commit 3b3afbd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 18 deletions.
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.transit.model.network.TripPattern;
Expand Down Expand Up @@ -286,4 +287,22 @@ public BookingInfo getPickupBookingInfo() {
public BookingInfo getDropOffBookingInfo() {
return tripTimes.getDropOffBookingInfo(stopIndex);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
TripTimeOnDate that = (TripTimeOnDate) o;
return (
stopIndex == that.stopIndex &&
midnight == that.midnight &&
Objects.equals(tripTimes, that.tripTimes) &&
Objects.equals(tripPattern, that.tripPattern) &&
Objects.equals(serviceDate, that.serviceDate)
);
}

@Override
public int hashCode() {
return Objects.hash(tripTimes, stopIndex, tripPattern, serviceDate, midnight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,86 @@
import static org.opentripplanner.transit.model.basic.TransitMode.RAIL;
import static org.opentripplanner.transit.model.basic.TransitMode.TRAM;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.model.RealTimeTripUpdate;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.model.calendar.CalendarServiceData;
import org.opentripplanner.transit.model._data.TimetableRepositoryForTest;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.ScheduledTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.utils.time.ServiceDateUtils;

class DefaultTransitServiceTest {

private static final TimetableRepositoryForTest TEST_MODEL = TimetableRepositoryForTest.of();

static TransitService service;
static Station STATION = TEST_MODEL.station("C").build();
static RegularStop STOP_A = TEST_MODEL
private static TransitService service;
private static final Station STATION = TEST_MODEL.station("C").build();
private static final RegularStop STOP_A = TEST_MODEL
.stop("A")
.withVehicleType(TRAM)
.withParentStation(STATION)
.build();
static RegularStop STOP_B = TEST_MODEL.stop("B").withParentStation(STATION).build();
static TripPattern RAIL_PATTERN = TEST_MODEL.pattern(RAIL).build();
static TripPattern FERRY_PATTERN = TEST_MODEL.pattern(FERRY).build();
static TripPattern BUS_PATTERN = TEST_MODEL.pattern(BUS).build();
private static final RegularStop STOP_B = TEST_MODEL.stop("B").withParentStation(STATION).build();

static StopPattern REAL_TIME_STOP_PATTERN = TimetableRepositoryForTest.stopPattern(
private static final FeedScopedId SERVICE_ID = new FeedScopedId("FEED", "SERVICE");
private static final int SERVICE_CODE = 0;
private static final Trip TRIP = TimetableRepositoryForTest
.trip("REAL_TIME_TRIP")
.withServiceId(SERVICE_ID)
.build();
private static final ScheduledTripTimes SCHEDULED_TRIP_TIMES = ScheduledTripTimes
.of()
.withTrip(TRIP)
.withArrivalTimes(new int[] { 0, 1 })
.withDepartureTimes(new int[] { 0, 1 })
.withServiceCode(SERVICE_CODE)
.build();

private static final TripPattern RAIL_PATTERN = TEST_MODEL
.pattern(RAIL)
.withScheduledTimeTableBuilder(builder -> builder.addTripTimes(SCHEDULED_TRIP_TIMES))
.build();
private static final TripPattern FERRY_PATTERN = TEST_MODEL.pattern(FERRY).build();
private static final TripPattern BUS_PATTERN = TEST_MODEL.pattern(BUS).build();

private static final StopPattern REAL_TIME_STOP_PATTERN = TimetableRepositoryForTest.stopPattern(
STOP_A,
STOP_B
);
static TripPattern REAL_TIME_PATTERN = TEST_MODEL
private static final TripPattern REAL_TIME_PATTERN = TEST_MODEL
.pattern(BUS)
.withStopPattern(REAL_TIME_STOP_PATTERN)
.withCreatedByRealtimeUpdater(true)
.build();
private static final int DELAY = 120;
private static final RealTimeTripTimes REALTIME_TRIP_TIMES = SCHEDULED_TRIP_TIMES.copyScheduledTimes();

static {
for (var i = 0; i < REALTIME_TRIP_TIMES.getNumStops(); ++i) {
REALTIME_TRIP_TIMES.updateArrivalDelay(i, DefaultTransitServiceTest.DELAY);
REALTIME_TRIP_TIMES.updateDepartureDelay(i, DefaultTransitServiceTest.DELAY);
}
}

private static final LocalDate SERVICE_DATE = LocalDate.of(2024, 1, 1);
private static final LocalDate NO_SERVICE_DATE = LocalDate.of(2000, 1, 1);

@BeforeAll
static void setup() {
Expand All @@ -60,20 +97,18 @@ static void setup() {
.build();

var timetableRepository = new TimetableRepository(siteRepository, new Deduplicator());
var calendar = new CalendarServiceData();
calendar.putServiceDatesForServiceId(SERVICE_ID, List.of(SERVICE_DATE));
var serviceCodes = timetableRepository.getServiceCodes();
serviceCodes.put(SERVICE_ID, SERVICE_CODE);
timetableRepository.updateCalendarServiceData(true, calendar, DataImportIssueStore.NOOP);
timetableRepository.addTripPattern(RAIL_PATTERN.getId(), RAIL_PATTERN);
timetableRepository.index();

timetableRepository.initTimetableSnapshotProvider(() -> {
TimetableSnapshot timetableSnapshot = new TimetableSnapshot();
RealTimeTripTimes tripTimes = RealTimeTripTimes.of(
ScheduledTripTimes
.of()
.withTrip(TimetableRepositoryForTest.trip("REAL_TIME_TRIP").build())
.withDepartureTimes(new int[] { 0, 1 })
.build()
);
timetableSnapshot.update(
new RealTimeTripUpdate(REAL_TIME_PATTERN, tripTimes, LocalDate.now())
new RealTimeTripUpdate(REAL_TIME_PATTERN, REALTIME_TRIP_TIMES, SERVICE_DATE)
);

return timetableSnapshot.commit();
Expand Down Expand Up @@ -121,4 +156,35 @@ void getPatternForStopsWithRealTime() {
Collection<TripPattern> patternsForStop = service.getPatternsForStop(STOP_B, true);
assertEquals(Set.of(FERRY_PATTERN, RAIL_PATTERN, REAL_TIME_PATTERN), patternsForStop);
}

@Test
void getScheduledTripTimes() {
assertEquals(
List.of(
new TripTimeOnDate(SCHEDULED_TRIP_TIMES, 0, RAIL_PATTERN),
new TripTimeOnDate(SCHEDULED_TRIP_TIMES, 1, RAIL_PATTERN)
),
service.getScheduledTripTimes(TRIP)
);
}

@Test
void getRealtimeTripTimes() {
Instant midnight = ServiceDateUtils
.asStartOfService(SERVICE_DATE, service.getTimeZone())
.toInstant();

assertEquals(
List.of(
new TripTimeOnDate(REALTIME_TRIP_TIMES, 0, REAL_TIME_PATTERN, SERVICE_DATE, midnight),
new TripTimeOnDate(REALTIME_TRIP_TIMES, 1, REAL_TIME_PATTERN, SERVICE_DATE, midnight)
),
service.getTripTimeOnDates(TRIP, SERVICE_DATE)
);
}

@Test
void getTripTimesOnNoServiceDay() {
assertEquals(List.of(), service.getTripTimeOnDates(TRIP, NO_SERVICE_DATE));
}
}

0 comments on commit 3b3afbd

Please sign in to comment.