Skip to content

Commit

Permalink
Merge pull request #251 from ibi-group/handle-missing-otp-intermediat…
Browse files Browse the repository at this point in the history
…e-stops

fix(TravelerLocator): Handle missing intermediateStops field from OTP
  • Loading branch information
JymDyerIBI authored Aug 23, 2024
2 parents 018da94 + 7412d9e commit b56f924
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ private static int getNearestPointIndex(List<Coordinates> positions, Coordinates
}

private static List<Place> getIntermediateAndLastStop(Leg leg) {
ArrayList<Place> stops = new ArrayList<>(leg.intermediateStops);
ArrayList<Place> stops = leg.intermediateStops == null
? new ArrayList<>()
: new ArrayList<>(leg.intermediateStops);
stops.add(leg.to);
return stops;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.leonard.PolylineUtils;
import io.leonard.Position;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -11,6 +12,7 @@
import org.opentripplanner.middleware.models.TrackedJourney;
import org.opentripplanner.middleware.otp.response.Itinerary;
import org.opentripplanner.middleware.otp.response.Leg;
import org.opentripplanner.middleware.otp.response.Place;
import org.opentripplanner.middleware.otp.response.Step;
import org.opentripplanner.middleware.testutils.CommonTestUtils;
import org.opentripplanner.middleware.triptracker.instruction.DeviatedInstruction;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class ManageLegTraversalTest {

private static Itinerary adairAvenueToMonroeDriveItinerary;
private static Itinerary midtownToAnsleyItinerary;
private static List<Place> midtownToAnsleyIntermediateStops;

private static final Locale locale = Locale.US;

Expand All @@ -70,6 +73,13 @@ public static void setUp() throws IOException {
CommonTestUtils.getTestResourceAsString("controllers/api/27nb-midtown-to-ansley.json"),
Itinerary.class
);
// Hold on to the original list of intermediate stops (some tests will overwrite it)
midtownToAnsleyIntermediateStops = midtownToAnsleyItinerary.legs.get(1).intermediateStops;
}

@BeforeEach
void beforeEach() {
midtownToAnsleyItinerary.legs.get(1).intermediateStops = midtownToAnsleyIntermediateStops;
}

@ParameterizedTest
Expand Down Expand Up @@ -323,6 +333,12 @@ private static Stream<Arguments> createTurnByTurnTrace() {
void canTrackTransitRide(TraceData traceData) {
Itinerary itinerary = midtownToAnsleyItinerary;
Leg transitLeg = itinerary.legs.get(1);

// In some cases, simulate missing intermediateStops field from OTP. Tests should still run to end.
if (traceData.dismissIntermediateStops) {
transitLeg.intermediateStops = null;
}

TravelerPosition travelerPosition = new TravelerPosition(transitLeg, traceData.position, traceData.speed);
String tripInstruction = TravelerLocator.getInstruction(traceData.tripStatus, travelerPosition, false);
assertEquals(traceData.expectedInstruction, Objects.requireNonNullElse(tripInstruction, NO_INSTRUCTION), traceData.message);
Expand Down Expand Up @@ -386,7 +402,8 @@ private static Stream<Arguments> createTransitRideTrace() {
new TraceData(
new Coordinates(33.79478, -84.37127),
String.format("Get off at next stop (%s)", destinationName),
"Past the one-stop warning from the stop where you should get off."
"Past the one-stop warning from the stop where you should get off.",
true
)
),
Arguments.of(
Expand Down Expand Up @@ -505,6 +522,7 @@ private static class TraceData {
int speed;
String expectedInstruction;
boolean isStartOfTrip;
boolean dismissIntermediateStops;
String message;

public TraceData(Coordinates position, String expectedInstruction, boolean isStartOfTrip, String message) {
Expand All @@ -518,6 +536,11 @@ public TraceData(Coordinates position, String expectedInstruction, String messag
this(position, expectedInstruction, false, message);
}

public TraceData(Coordinates position, String expectedInstruction, String message, boolean dismissIntermediateStops) {
this(position, expectedInstruction, false, message);
this.dismissIntermediateStops = true;
}

public TraceData(Coordinates position, int speed, String expectedInstruction, String message) {
this(position, expectedInstruction, false, message);
this.speed = speed;
Expand Down

0 comments on commit b56f924

Please sign in to comment.