Skip to content

Commit

Permalink
feat(UsRide...Message): Populate trusted companion field for RideGwin…
Browse files Browse the repository at this point in the history
…nett messages.
  • Loading branch information
binh-dam-ibigroup committed Dec 4, 2024
1 parent 01a2c75 commit 398d7f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public class TripTrackingData {
public final TrackedJourney journey;
public final List<TrackingLocation> locations;

private TripTrackingData(MonitoredTrip trip, TrackedJourney journey, List<TrackingLocation> locations) {
public TripTrackingData(MonitoredTrip trip, TrackedJourney journey, List<TrackingLocation> locations) {
this.trip = trip;
this.journey = journey;
this.journey.trip = trip;
this.locations = locations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public UsRideGwinnettBusOpNotificationMessage(Instant currentTime, TravelerPosit
// 1 = Notify, 0 = Cancel.
this.msg_type = 1;
this.mobility_codes = getMobilityCode(travelerPosition.mobilityMode);
this.trusted_companion = false;
this.trusted_companion = travelerPosition.trackedJourney.trip.hasConfirmedCompanion();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentripplanner.middleware.models.MobilityProfile;
import org.opentripplanner.middleware.models.MonitoredTrip;
import org.opentripplanner.middleware.models.OtpUser;
import org.opentripplanner.middleware.models.RelatedUser;
import org.opentripplanner.middleware.models.TrackedJourney;
import org.opentripplanner.middleware.otp.response.Itinerary;
import org.opentripplanner.middleware.otp.response.Leg;
Expand All @@ -33,6 +35,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.ACCEPTABLE_AHEAD_OF_SCHEDULE_IN_MINUTES;
import static org.opentripplanner.middleware.triptracker.TravelerLocator.getBusDepartureTime;
Expand All @@ -46,7 +49,7 @@ class NotifyBusOperatorTest extends OtpMiddlewareTestEnvironment {

private static TrackedJourney trackedJourney;

private static final String routeId = "GwinnettCountyTransit:40";
private static final String ROUTE_ID = "GwinnettCountyTransit:40";

private static final Locale locale = Locale.US;

Expand All @@ -66,7 +69,7 @@ public static void setUp() throws IOException {
Itinerary.class
);
UsRideGwinnettNotifyBusOperator.IS_TEST = true;
UsRideGwinnettNotifyBusOperator.US_RIDE_GWINNETT_QUALIFYING_BUS_NOTIFIER_ROUTES = List.of(routeId);
UsRideGwinnettNotifyBusOperator.US_RIDE_GWINNETT_QUALIFYING_BUS_NOTIFIER_ROUTES = List.of(ROUTE_ID);
}

@AfterEach
Expand All @@ -86,7 +89,7 @@ void canNotifyBusOperatorForScheduledDeparture(Leg busLeg, Itinerary itinerary,
String tripInstruction = TravelerLocator.getInstruction(TripStatus.ON_SCHEDULE, travelerPosition, isStartOfTrip);
TripInstruction expectInstruction = new WaitForTransitInstruction(busLeg, busDepartureTime, locale);
TrackedJourney updated = Persistence.trackedJourneys.getById(trackedJourney.id);
assertTrue(updated.busNotificationMessages.containsKey(routeId));
assertTrue(updated.busNotificationMessages.containsKey(ROUTE_ID));
assertEquals(expectInstruction.build(), tripInstruction, message);
}

Expand All @@ -112,7 +115,7 @@ private static Stream<Arguments> creatNotifyBusOperatorForScheduledDepartureTrac
void shouldCancelBusNotificationForStartOfTrip(boolean expected, Leg expectedLeg, Coordinates currentPosition, String message) {
Leg first = firstLegBusTransit.legs.get(0);
TrackedJourney journey = new TrackedJourney();
journey.busNotificationMessages.put(routeId, "{\"msg_type\": 1}");
journey.busNotificationMessages.put(ROUTE_ID, "{\"msg_type\": 1}");
TravelerPosition travelerPosition = new TravelerPosition(expectedLeg, journey, first, currentPosition);
assertEquals(expected, ManageTripTracking.shouldCancelBusNotificationForStartOfTrip(travelerPosition), message);
}
Expand Down Expand Up @@ -161,9 +164,7 @@ void canCancelBusOperatorNotification() throws JsonProcessingException, Interrup
trackedJourney = createAndPersistTrackedJourney(getEndOfWalkLegCoordinates());
TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, walkToBusTransition, createOtpUser());

busOperatorActions.handleSendNotificationAction(travelerPosition, travelerPosition.nextLeg);
TrackedJourney updated = Persistence.trackedJourneys.getById(trackedJourney.id);
assertTrue(updated.busNotificationMessages.containsKey(routeId));
TrackedJourney updated = sendAndCheckInitialBusOperatorNotification(travelerPosition);
assertEquals(1, getMessage(updated).msg_type);

busOperatorActions.handleCancelNotificationAction(travelerPosition, travelerPosition.nextLeg);
Expand All @@ -184,7 +185,7 @@ void canCancelBusOperatorNotification() throws JsonProcessingException, Interrup
}

private static UsRideGwinnettBusOpNotificationMessage getMessage(TrackedJourney updated) throws JsonProcessingException {
String messageBody = updated.busNotificationMessages.get(routeId);
String messageBody = updated.busNotificationMessages.get(ROUTE_ID);
return getNotificationMessage(messageBody);
}

Expand All @@ -193,18 +194,41 @@ void canNotifyBusOperatorOnlyOnce() throws InterruptedException, JsonProcessingE
trackedJourney = createAndPersistTrackedJourney(getEndOfWalkLegCoordinates());
TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, walkToBusTransition, createOtpUser());

busOperatorActions.handleSendNotificationAction(travelerPosition, travelerPosition.nextLeg);
TrackedJourney updated = Persistence.trackedJourneys.getById(trackedJourney.id);
assertTrue(updated.busNotificationMessages.containsKey(routeId));
assertFalse(UsRideGwinnettNotifyBusOperator.hasNotSentNotificationForRoute(updated, routeId));
UsRideGwinnettBusOpNotificationMessage notifyMessage = getMessage(updated);
TrackedJourney updated = sendAndCheckInitialBusOperatorNotification(travelerPosition);

// A second request to notify the operator should not touch the previous request.
Thread.sleep(20);
busOperatorActions.handleSendNotificationAction(travelerPosition, travelerPosition.nextLeg);
TrackedJourney updated2 = Persistence.trackedJourneys.getById(trackedJourney.id);
assertFalse(UsRideGwinnettNotifyBusOperator.hasNotSentNotificationForRoute(updated2, routeId));
assertEquals(notifyMessage.timestamp, getMessage(updated2).timestamp);
assertFalse(UsRideGwinnettNotifyBusOperator.hasNotSentNotificationForRoute(updated2, ROUTE_ID));
assertEquals(getMessage(updated).timestamp, getMessage(updated2).timestamp);
}

private TrackedJourney sendAndCheckInitialBusOperatorNotification(TravelerPosition travelerPosition) throws JsonProcessingException {
Coordinates position = travelerPosition.currentPosition;
MonitoredTrip trip = new MonitoredTrip();
// Add a confirmed companion to this trip
trip.companion = new RelatedUser();
trip.companion.status = RelatedUser.RelatedUserStatus.CONFIRMED;

// Endpoints indirectly call the TripTrackingData constructor that sets the trip field in Trackedjourney,
// so we add that here to replicate those steps.
TripTrackingData tripData = new TripTrackingData(
trip,
trackedJourney,
List.of(new TrackingLocation(travelerPosition.currentTime, position.lat, position.lon))
);
assertNotNull(tripData.journey.trip);
assertNotNull(trackedJourney.trip);

busOperatorActions.handleSendNotificationAction(travelerPosition, travelerPosition.nextLeg);

TrackedJourney updated = Persistence.trackedJourneys.getById(trackedJourney.id);
assertTrue(updated.busNotificationMessages.containsKey(ROUTE_ID));
assertFalse(UsRideGwinnettNotifyBusOperator.hasNotSentNotificationForRoute(updated, ROUTE_ID));
assertTrue(getMessage(updated).trusted_companion);

return updated;
}

@ParameterizedTest
Expand Down

0 comments on commit 398d7f6

Please sign in to comment.