use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleModifiedTrip.
@Test
public void testHandleModifiedTrip() throws ParseException {
// TODO
// GIVEN
// Get service date of today because old dates will be purged after applying updates
ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
String modifiedTripId = "10.1";
TripUpdate tripUpdate;
{
final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId(modifiedTripId);
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.MODIFIED);
tripDescriptorBuilder.setStartDate(serviceDate.getAsString());
final Calendar calendar = serviceDate.getAsCalendar(graph.getTimeZone());
final long midnightSecondsSinceEpoch = calendar.getTimeInMillis() / 1000;
final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
{
// Stop O
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("O");
stopTimeUpdateBuilder.setStopSequence(10);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (30 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop C
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("C");
stopTimeUpdateBuilder.setStopSequence(30);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (40 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (45 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop D
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SKIPPED);
stopTimeUpdateBuilder.setStopId("D");
stopTimeUpdateBuilder.setStopSequence(40);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (50 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (51 * 60));
departureBuilder.setDelay(0);
}
}
{
// Stop P
final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeUpdateBuilder.setStopId("P");
stopTimeUpdateBuilder.setStopSequence(50);
{
// Arrival
final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
arrivalBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
arrivalBuilder.setDelay(0);
}
{
// Departure
final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
departureBuilder.setTime(midnightSecondsSinceEpoch + (12 * 3600) + (55 * 60));
departureBuilder.setDelay(0);
}
}
tripUpdate = tripUpdateBuilder.build();
}
// WHEN
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
// THEN
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
// Original trip pattern
{
final AgencyAndId tripId = new AgencyAndId(feedId, modifiedTripId);
final Trip trip = graph.index.tripForId.get(tripId);
final TripPattern originalTripPattern = graph.index.patternForTrip.get(trip);
final Timetable originalTimetableForToday = snapshot.resolve(originalTripPattern, serviceDate);
final Timetable originalTimetableScheduled = snapshot.resolve(originalTripPattern, null);
assertNotSame(originalTimetableForToday, originalTimetableScheduled);
final int originalTripIndexScheduled = originalTimetableScheduled.getTripIndex(modifiedTripId);
assertTrue("Original trip should be found in scheduled time table", originalTripIndexScheduled > -1);
final TripTimes originalTripTimesScheduled = originalTimetableScheduled.getTripTimes(originalTripIndexScheduled);
assertFalse("Original trip times should not be canceled in scheduled time table", originalTripTimesScheduled.isCanceled());
assertEquals(RealTimeState.SCHEDULED, originalTripTimesScheduled.getRealTimeState());
final int originalTripIndexForToday = originalTimetableForToday.getTripIndex(modifiedTripId);
assertTrue("Original trip should be found in time table for service date", originalTripIndexForToday > -1);
final TripTimes originalTripTimesForToday = originalTimetableForToday.getTripTimes(originalTripIndexForToday);
assertTrue("Original trip times should be canceled in time table for service date", originalTripTimesForToday.isCanceled());
assertEquals(RealTimeState.CANCELED, originalTripTimesForToday.getRealTimeState());
}
// New trip pattern
{
final TripPattern newTripPattern = snapshot.getLastAddedTripPattern(feedId, modifiedTripId, serviceDate);
assertNotNull("New trip pattern should be found", newTripPattern);
final Timetable newTimetableForToday = snapshot.resolve(newTripPattern, serviceDate);
final Timetable newTimetableScheduled = snapshot.resolve(newTripPattern, null);
assertNotSame(newTimetableForToday, newTimetableScheduled);
final int newTimetableForTodayModifiedTripIndex = newTimetableForToday.getTripIndex(modifiedTripId);
assertTrue("New trip should be found in time table for service date", newTimetableForTodayModifiedTripIndex > -1);
assertEquals(RealTimeState.MODIFIED, newTimetableForToday.getTripTimes(newTimetableForTodayModifiedTripIndex).getRealTimeState());
assertEquals("New trip should not be found in scheduled time table", -1, newTimetableScheduled.getTripIndex(modifiedTripId));
}
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSourceTest method testHandleCanceledTrip.
@Test
public void testHandleCanceledTrip() throws InvalidProtocolBufferException {
final AgencyAndId tripId = new AgencyAndId(feedId, "1.1");
final AgencyAndId tripId2 = new AgencyAndId(feedId, "1.2");
final Trip trip = graph.index.tripForId.get(tripId);
final TripPattern pattern = graph.index.patternForTrip.get(trip);
final int tripIndex = pattern.scheduledTimetable.getTripIndex(tripId);
final int tripIndex2 = pattern.scheduledTimetable.getTripIndex(tripId2);
updater.applyTripUpdates(graph, fullDataset, Arrays.asList(TripUpdate.parseFrom(cancellation)), feedId);
final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
final Timetable forToday = snapshot.resolve(pattern, 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));
final TripTimes tripTimes = forToday.getTripTimes(tripIndex);
for (int i = 0; i < tripTimes.getNumStops(); i++) {
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getDepartureTime(i));
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getArrivalTime(i));
}
assertEquals(RealTimeState.CANCELED, tripTimes.getRealTimeState());
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class TimetableTest method testUpdate.
@Test
public void testUpdate() {
TripUpdate tripUpdate;
TripUpdate.Builder tripUpdateBuilder;
TripDescriptor.Builder tripDescriptorBuilder;
StopTimeUpdate.Builder stopTimeUpdateBuilder;
StopTimeEvent.Builder stopTimeEventBuilder;
String feedId = graph.getFeedIds().iterator().next();
int trip_1_1_index = timetable.getTripIndex(new AgencyAndId("agency", "1.1"));
Vertex stop_a = graph.getVertex(feedId + ":A");
Vertex stop_c = graph.getVertex(feedId + ":C");
RoutingRequest options = new RoutingRequest();
ShortestPathTree spt;
GraphPath path;
// non-existing trip
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("b");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.CANCELED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
tripUpdate = tripUpdateBuilder.build();
TripTimes updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNull(updatedTripTimes);
// update trip with bad data
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(0);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SKIPPED);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNull(updatedTripTimes);
// update trip with non-increasing data
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(2);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
stopTimeEventBuilder.setTime(TestUtils.dateInSeconds("America/New_York", 2009, AUGUST, 7, 0, 10, 1));
stopTimeEventBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
stopTimeEventBuilder.setTime(TestUtils.dateInSeconds("America/New_York", 2009, AUGUST, 7, 0, 10, 0));
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNull(updatedTripTimes);
// ---
long startTime = TestUtils.dateInSeconds("America/New_York", 2009, AUGUST, 7, 0, 0, 0);
long endTime;
options.dateTime = startTime;
// ---
options.setRoutingContext(graph, stop_a, stop_c);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(stop_c, false);
assertNotNull(path);
endTime = startTime + 20 * 60;
assertEquals(endTime, path.getEndTime());
// update trip
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(1);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
stopTimeEventBuilder.setTime(TestUtils.dateInSeconds("America/New_York", 2009, AUGUST, 7, 0, 2, 0));
stopTimeEventBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
stopTimeEventBuilder.setTime(TestUtils.dateInSeconds("America/New_York", 2009, AUGUST, 7, 0, 2, 0));
tripUpdate = tripUpdateBuilder.build();
assertEquals(20 * 60, timetable.getTripTimes(trip_1_1_index).getArrivalTime(2));
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
assertEquals(20 * 60 + 120, timetable.getTripTimes(trip_1_1_index).getArrivalTime(2));
// ---
options.setRoutingContext(graph, stop_a, stop_c);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(stop_c, false);
assertNotNull(path);
endTime = startTime + 20 * 60 + 120;
assertEquals(endTime, path.getEndTime());
// cancel trip
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.CANCELED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
TripTimes tripTimes = timetable.getTripTimes(trip_1_1_index);
for (int i = 0; i < tripTimes.getNumStops(); i++) {
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getDepartureTime(i));
assertEquals(TripTimes.UNAVAILABLE, tripTimes.getArrivalTime(i));
}
// ---
options.setRoutingContext(graph, stop_a, stop_c);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(stop_c, false);
assertNotNull(path);
endTime = startTime + 40 * 60;
assertEquals(endTime, path.getEndTime());
// update trip arrival time incorrectly
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(1);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
stopTimeEventBuilder.setDelay(0);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
// update trip arrival time only
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(2);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
stopTimeEventBuilder.setDelay(1);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
// update trip departure time only
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(2);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
stopTimeEventBuilder.setDelay(-1);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
// update trip using stop id
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopId("B");
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
stopTimeEventBuilder.setDelay(-1);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNotNull(updatedTripTimes);
timetable.setTripTimes(trip_1_1_index, updatedTripTimes);
// update trip arrival time at first stop and make departure time incoherent at second stop
tripDescriptorBuilder = TripDescriptor.newBuilder();
tripDescriptorBuilder.setTripId("1.1");
tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.SCHEDULED);
tripUpdateBuilder = TripUpdate.newBuilder();
tripUpdateBuilder.setTrip(tripDescriptorBuilder);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(0);
stopTimeUpdateBuilder.setStopSequence(1);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
stopTimeEventBuilder.setDelay(0);
stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder(1);
stopTimeUpdateBuilder.setStopSequence(2);
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
stopTimeEventBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
stopTimeEventBuilder.setDelay(-1);
tripUpdate = tripUpdateBuilder.build();
updatedTripTimes = timetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
assertNull(updatedTripTimes);
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class OnBoardDepartServiceImplTest method testOnBoardAtStation.
@Test
public final void testOnBoardAtStation() {
TransitStop station0 = mock(TransitStop.class);
TransitStop station1 = mock(TransitStop.class);
TransitStop station2 = mock(TransitStop.class);
PatternDepartVertex depart = mock(PatternDepartVertex.class);
PatternArriveVertex dwell = mock(PatternArriveVertex.class);
PatternArriveVertex arrive = mock(PatternArriveVertex.class);
Graph graph = mock(Graph.class);
RoutingRequest routingRequest = mock(RoutingRequest.class);
ServiceDay serviceDay = mock(ServiceDay.class);
// You're probably not supposed to do this to mocks (access their fields directly)
// But I know of no other way to do this since the mock object has only action-free stub methods.
routingRequest.modes = new TraverseModeSet("WALK,TRANSIT");
when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("GMT"));
ArrayList<Edge> hops = new ArrayList<Edge>(2);
RoutingContext routingContext = new RoutingContext(routingRequest, graph, null, arrive);
Agency agency = new Agency();
AgencyAndId agencyAndId = new AgencyAndId("Agency", "ID");
Route route = new Route();
ArrayList<StopTime> stopTimes = new ArrayList<StopTime>(2);
StopTime stopDepartTime = new StopTime();
StopTime stopDwellTime = new StopTime();
StopTime stopArriveTime = new StopTime();
Stop stopDepart = new Stop();
Stop stopDwell = new Stop();
Stop stopArrive = new Stop();
Trip trip = new Trip();
routingContext.serviceDays = new ArrayList<ServiceDay>(Collections.singletonList(serviceDay));
agency.setId(agencyAndId.getAgencyId());
route.setId(agencyAndId);
route.setAgency(agency);
stopDepart.setId(new AgencyAndId("Station", "0"));
stopDwell.setId(new AgencyAndId("Station", "1"));
stopArrive.setId(new AgencyAndId("Station", "2"));
stopDepartTime.setStop(stopDepart);
stopDepartTime.setDepartureTime(0);
stopDwellTime.setArrivalTime(20);
stopDwellTime.setStop(stopDwell);
stopDwellTime.setDepartureTime(40);
stopArriveTime.setArrivalTime(60);
stopArriveTime.setStop(stopArrive);
stopTimes.add(stopDepartTime);
stopTimes.add(stopDwellTime);
stopTimes.add(stopArriveTime);
trip.setId(agencyAndId);
trip.setRoute(route);
TripTimes tripTimes = new TripTimes(trip, stopTimes, new Deduplicator());
StopPattern stopPattern = new StopPattern(stopTimes);
TripPattern tripPattern = new TripPattern(route, stopPattern);
TripPattern.generateUniqueIds(Arrays.asList(tripPattern));
when(depart.getTripPattern()).thenReturn(tripPattern);
when(dwell.getTripPattern()).thenReturn(tripPattern);
PatternHop patternHop0 = new PatternHop(depart, dwell, stopDepart, stopDwell, 0);
PatternHop patternHop1 = new PatternHop(dwell, arrive, stopDwell, stopArrive, 1);
hops.add(patternHop0);
hops.add(patternHop1);
when(graph.getEdges()).thenReturn(hops);
when(depart.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(dwell.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(arrive.getCoordinate()).thenReturn(new Coordinate(0, 0));
routingRequest.from = new GenericLocation();
routingRequest.startingTransitTripId = agencyAndId;
when(graph.getVertex("Station_0")).thenReturn(station0);
when(graph.getVertex("Station_1")).thenReturn(station1);
when(graph.getVertex("Station_2")).thenReturn(station2);
tripPattern.add(tripTimes);
graph.index = new GraphIndex(graph);
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(0);
assertEquals(station0, onBoardDepartServiceImpl.setupDepartOnBoard(routingContext));
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(20);
assertEquals(station1, onBoardDepartServiceImpl.setupDepartOnBoard(routingContext));
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(30);
assertEquals(station1, onBoardDepartServiceImpl.setupDepartOnBoard(routingContext));
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(40);
assertEquals(station1, onBoardDepartServiceImpl.setupDepartOnBoard(routingContext));
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(60);
assertEquals(station2, onBoardDepartServiceImpl.setupDepartOnBoard(routingContext));
}
use of org.opentripplanner.routing.trippattern.TripTimes in project OpenTripPlanner by opentripplanner.
the class OnBoardDepartServiceImplTest method testOnBoardDepartureAtArrivalTime.
@Test
public final void testOnBoardDepartureAtArrivalTime() {
Coordinate[] coordinates = new Coordinate[2];
coordinates[0] = new Coordinate(0.0, 0.0);
coordinates[1] = new Coordinate(0.0, 1.0);
TransitStop station0 = mock(TransitStop.class);
TransitStop station1 = mock(TransitStop.class);
PatternDepartVertex depart = mock(PatternDepartVertex.class);
PatternArriveVertex arrive = mock(PatternArriveVertex.class);
Graph graph = mock(Graph.class);
RoutingRequest routingRequest = mock(RoutingRequest.class);
ServiceDay serviceDay = mock(ServiceDay.class);
// You're probably not supposed to do this to mocks (access their fields directly)
// But I know of no other way to do this since the mock object has only action-free stub methods.
routingRequest.modes = new TraverseModeSet("WALK,TRANSIT");
when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("GMT"));
when(station0.getX()).thenReturn(coordinates[0].x);
when(station0.getY()).thenReturn(coordinates[0].y);
when(station1.getX()).thenReturn(coordinates[1].x);
when(station1.getY()).thenReturn(coordinates[1].y);
RoutingContext routingContext = new RoutingContext(routingRequest, graph, null, arrive);
AgencyAndId agencyAndId = new AgencyAndId("Agency", "ID");
Agency agency = new Agency();
Route route = new Route();
ArrayList<StopTime> stopTimes = new ArrayList<StopTime>(2);
StopTime stopDepartTime = new StopTime();
StopTime stopArriveTime = new StopTime();
Stop stopDepart = new Stop();
Stop stopArrive = new Stop();
Trip trip = new Trip();
routingContext.serviceDays = new ArrayList<ServiceDay>(Collections.singletonList(serviceDay));
agency.setId(agencyAndId.getAgencyId());
route.setId(agencyAndId);
route.setAgency(agency);
stopDepart.setId(new AgencyAndId("Station", "0"));
stopArrive.setId(new AgencyAndId("Station", "1"));
stopDepartTime.setStop(stopDepart);
stopDepartTime.setDepartureTime(0);
stopArriveTime.setArrivalTime(10);
stopArriveTime.setStop(stopArrive);
stopTimes.add(stopDepartTime);
stopTimes.add(stopArriveTime);
trip.setId(agencyAndId);
trip.setRoute(route);
TripTimes tripTimes = new TripTimes(trip, stopTimes, new Deduplicator());
StopPattern stopPattern = new StopPattern(stopTimes);
TripPattern tripPattern = new TripPattern(route, stopPattern);
TripPattern.generateUniqueIds(Arrays.asList(tripPattern));
when(depart.getTripPattern()).thenReturn(tripPattern);
PatternHop patternHop = new PatternHop(depart, arrive, stopDepart, stopArrive, 0);
when(graph.getEdges()).thenReturn(Collections.<Edge>singletonList(patternHop));
when(depart.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(arrive.getCoordinate()).thenReturn(new Coordinate(0, 0));
routingRequest.from = new GenericLocation();
routingRequest.startingTransitTripId = agencyAndId;
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(10);
when(graph.getVertex("Station_0")).thenReturn(station0);
when(graph.getVertex("Station_1")).thenReturn(station1);
tripPattern.add(tripTimes);
graph.index = new GraphIndex(graph);
Vertex vertex = onBoardDepartServiceImpl.setupDepartOnBoard(routingContext);
assertEquals(coordinates[1].x, vertex.getX(), 0.0);
assertEquals(coordinates[1].y, vertex.getY(), 0.0);
}
Aggregations