Skip to content

Commit

Permalink
refactor(Created builder to handle basic traveler position needs):
Browse files Browse the repository at this point in the history
  • Loading branch information
br648 committed Dec 13, 2024
1 parent bc20b84 commit 4e788c5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public class TravelerPosition {
/** The first leg of the trip. **/
public Leg firstLegOfTrip;

public TravelerPosition(Builder builder) {
this.expectedLeg = builder.expectedLeg;
this.currentPosition = builder.currentPosition;
this.speed = builder.speed;
this.firstLegOfTrip = builder.firstLegOfTrip;
if (expectedLeg != null && currentPosition != null) {
this.legSegmentFromPosition = getSegmentFromPosition(expectedLeg, currentPosition);
}
this.nextLeg = builder.nextLeg;
this.currentTime = builder.currentTime;
this.trackedJourney = builder.trackedJourney;

}

public TravelerPosition(TrackedJourney trackedJourney, Itinerary itinerary, OtpUser otpUser) {
TrackingLocation lastLocation = trackedJourney.locations.get(trackedJourney.locations.size() - 1);
currentTime = lastLocation.timestamp.toInstant();
Expand All @@ -68,50 +82,61 @@ public TravelerPosition(TrackedJourney trackedJourney, Itinerary itinerary, OtpU
firstLegOfTrip = getFirstLeg(itinerary);
}

/** Used for unit testing. */
public TravelerPosition(Leg expectedLeg, Coordinates currentPosition, int speed) {
this.expectedLeg = expectedLeg;
this.currentPosition = currentPosition;
this.speed = speed;
legSegmentFromPosition = getSegmentFromPosition(expectedLeg, currentPosition);
/** Computes the current deviation in meters from the expected itinerary. */
public double getDeviationMeters() {
return getDistanceFromLine(legSegmentFromPosition.start, legSegmentFromPosition.end, currentPosition);
}

/** Used for unit testing. */
public TravelerPosition(Leg expectedLeg, Coordinates currentPosition) {
// Anywhere the speed is zero means that speed is not considered for a specific logic.
this(expectedLeg, currentPosition, 0);
}
/**
* Builder to handle basic unit test requirements.
*/
public static final class Builder {

private Leg expectedLeg;
private Coordinates currentPosition;
private int speed;
private Leg firstLegOfTrip;
private Leg nextLeg;
private Instant currentTime;
private TrackedJourney trackedJourney;

public Builder setExpectedLeg(Leg expectedLeg) {
this.expectedLeg = expectedLeg;
return this;
}

/** Used for unit testing. */
public TravelerPosition(Leg expectedLeg, Coordinates currentPosition, Leg firstLegOfTrip) {
// Anywhere the speed is zero means that speed is not considered for a specific logic.
this(expectedLeg, currentPosition, 0);
this.firstLegOfTrip = firstLegOfTrip;
}
public Builder setCurrentPosition(Coordinates currentPosition) {
this.currentPosition = currentPosition;
return this;
}

/** Used for unit testing. */
public TravelerPosition(Leg nextLeg, Instant currentTime) {
this.nextLeg = nextLeg;
this.currentTime = currentTime;
}
public Builder setSpeed(int speed) {
this.speed = speed;
return this;
}

/** Used for unit testing. */
public TravelerPosition(Leg expectedLeg, TrackedJourney trackedJourney, Leg first, Coordinates currentPosition) {
this.expectedLeg = expectedLeg;
this.trackedJourney = trackedJourney;
this.firstLegOfTrip = first;
this.currentPosition = currentPosition;
}
public Builder setFirstLegOfTrip(Leg firstLegOfTrip) {
this.firstLegOfTrip = firstLegOfTrip;
return this;
}

/** Used for unit testing. */
public TravelerPosition(Leg expectedLeg, Leg nextLeg, Coordinates currentPosition) {
this.expectedLeg = expectedLeg;
this.nextLeg = nextLeg;
this.currentPosition = currentPosition;
}
public Builder setNextLeg(Leg nextLeg) {
this.nextLeg = nextLeg;
return this;
}

/** Computes the current deviation in meters from the expected itinerary. */
public double getDeviationMeters() {
return getDistanceFromLine(legSegmentFromPosition.start, legSegmentFromPosition.end, currentPosition);
public Builder setCurrentTime(Instant currentTime) {
this.currentTime = currentTime;
return this;
}

public Builder setTrackedJourney(TrackedJourney trackedJourney) {
this.trackedJourney = trackedJourney;
return this;
}

public TravelerPosition build() {
return new TravelerPosition(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public static void tearDown() {
PersistenceTestUtils.deleteOtpUser(false, primary, companion, observer);
}


@ParameterizedTest
@MethodSource("createLegTransitionNotificationTestCases")
void testLegTransitionNotifications(
Expand Down Expand Up @@ -72,21 +71,33 @@ private static Stream<Arguments> createLegTransitionNotificationTestCases() thro
Arguments.of(
NotificationType.ARRIVED_AND_MODE_CHANGE_NOTIFICATION,
travelerName,
new TravelerPosition(expectedLeg, nextLeg, expectedLegDestinationCoords),
new TravelerPosition.Builder()
.setExpectedLeg(expectedLeg)
.setNextLeg(nextLeg)
.setCurrentPosition(expectedLegDestinationCoords)
.build(),
locale,
"Obi-Wan has arrived at transit stop Pioneer Square South MAX Station."
),
Arguments.of(
NotificationType.DEPARTED_NOTIFICATION,
travelerName,
new TravelerPosition(expectedLeg, nextLeg, nextLegDepartureCoords),
new TravelerPosition.Builder()
.setExpectedLeg(expectedLeg)
.setNextLeg(nextLeg)
.setCurrentPosition(nextLegDepartureCoords)
.build(),
locale,
"Obi-Wan has departed Providence Park MAX Station."
),
Arguments.of(
NotificationType.ARRIVED_NOTIFICATION,
travelerName,
new TravelerPosition(expectedLeg, nextLeg, expectedLegDestinationCoords),
new TravelerPosition.Builder()
.setExpectedLeg(expectedLeg)
.setNextLeg(nextLeg)
.setCurrentPosition(expectedLegDestinationCoords)
.build(),
locale,
"Obi-Wan has arrived at Pioneer Square South MAX Station."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ private static Stream<Arguments> createTrace() {
@ParameterizedTest
@MethodSource("createTurnByTurnTrace")
void canTrackTurnByTurn(Leg firstLeg, TraceData traceData) {
TravelerPosition travelerPosition = new TravelerPosition(firstLeg, traceData.position, firstLeg);
TravelerPosition travelerPosition = new TravelerPosition.Builder()
.setExpectedLeg(firstLeg)
.setCurrentPosition(traceData.position)
.setFirstLegOfTrip(firstLeg)
.setSpeed(0)
.build();
TripInstruction tripInstruction = TravelerLocator.getInstruction(traceData.tripStatus, travelerPosition, traceData.isStartOfTrip);
assertEquals(traceData.expectedInstruction, tripInstruction != null ? tripInstruction.build() : NO_INSTRUCTION, traceData.message);
}
Expand Down Expand Up @@ -411,7 +416,11 @@ void canTrackTransitRide(TraceData traceData) {
transitLeg.intermediateStops = null;
}

TravelerPosition travelerPosition = new TravelerPosition(transitLeg, traceData.position, traceData.speed);
TravelerPosition travelerPosition = new TravelerPosition.Builder()
.setExpectedLeg(transitLeg)
.setCurrentPosition(traceData.position)
.setSpeed(traceData.speed)
.build();
TripInstruction tripInstruction = TravelerLocator.getInstruction(traceData.tripStatus, travelerPosition, false);
assertEquals(traceData.expectedInstruction, tripInstruction != null ? tripInstruction.build() : NO_INSTRUCTION, traceData.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ void shouldCancelBusNotificationForStartOfTrip(boolean expected, Leg expectedLeg
Leg first = firstLegBusTransit.legs.get(0);
TrackedJourney journey = new TrackedJourney();
journey.busNotificationMessages.put(ROUTE_ID, "{\"msg_type\": 1}");
TravelerPosition travelerPosition = new TravelerPosition(expectedLeg, journey, first, currentPosition);
TravelerPosition travelerPosition = new TravelerPosition.Builder()
.setExpectedLeg(expectedLeg)
.setTrackedJourney(journey)
.setFirstLegOfTrip(first)
.setCurrentPosition(currentPosition).build();
assertEquals(expected, ManageTripTracking.shouldCancelBusNotificationForStartOfTrip(travelerPosition), message);
}

Expand Down Expand Up @@ -249,24 +253,33 @@ private static Stream<Arguments> createWithinOperationalNotifyWindowTrace() {
return Stream.of(
Arguments.of(
true,
new TravelerPosition(busLeg, busDepartureTime),
new TravelerPosition.Builder()
.setNextLeg(busLeg)
.setCurrentTime(busDepartureTime)
.build(),
"Traveler is on schedule, notification can be sent."
),
Arguments.of(
false,
new TravelerPosition(busLeg, busDepartureTime.plusSeconds(60)),
new TravelerPosition.Builder()
.setNextLeg(busLeg)
.setCurrentTime(busDepartureTime.plusSeconds(60))
.build(),
"Traveler is behind schedule, notification can not be sent."
),
Arguments.of(
true,
new TravelerPosition(busLeg, busDepartureTime.minusSeconds(60)),
new TravelerPosition.Builder()
.setNextLeg(busLeg)
.setCurrentTime(busDepartureTime.minusSeconds(60))
.build(),
"Traveler is ahead of schedule, but within the notify window."
),
Arguments.of(false,
new TravelerPosition(
busLeg,
busDepartureTime.plusSeconds((ACCEPTABLE_AHEAD_OF_SCHEDULE_IN_MINUTES + 1) * 60)
),
new TravelerPosition.Builder()
.setNextLeg(busLeg)
.setCurrentTime(busDepartureTime.plusSeconds((ACCEPTABLE_AHEAD_OF_SCHEDULE_IN_MINUTES + 1) * 60))
.build(),
"Too far ahead of schedule to notify bus operator.")
);
}
Expand All @@ -284,12 +297,18 @@ private static Stream<Arguments> shouldSendBusNotificationAtStartOfTripTrace() {
return Stream.of(
Arguments.of(
true,
new TravelerPosition(busLeg, getBusDepartureTime(busLeg)),
new TravelerPosition.Builder()
.setNextLeg(busLeg)
.setCurrentTime(getBusDepartureTime(busLeg))
.build(),
"Traveler at the start of a trip which starts with a bus leg, should notify."
),
Arguments.of(
false,
new TravelerPosition(walkLeg, getBusDepartureTime(walkLeg)),
new TravelerPosition.Builder()
.setNextLeg(walkLeg)
.setCurrentTime(getBusDepartureTime(walkLeg))
.build(),
"Traveler at the start of a trip which starts with a walk leg, should not notify."
)
);
Expand Down

0 comments on commit 4e788c5

Please sign in to comment.