Skip to content

Commit

Permalink
Merge branch 'dev' into handle-request-batchid
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Aug 16, 2024
2 parents 5432ce3 + 3435d21 commit 579dcb7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void cancelNotification(TravelerPosition travelerPosition) {
try {
if (
isBusLeg(travelerPosition.nextLeg) && routeId != null &&
hasNotCancelledNotificationForRoute(travelerPosition.trackedJourney, routeId)
hasNotCanceledNotificationForRoute(travelerPosition.trackedJourney, routeId)
) {
Map<String, String> busNotificationRequests = travelerPosition.trackedJourney.busNotificationMessages;
if (busNotificationRequests.containsKey(routeId)) {
Expand All @@ -106,9 +106,10 @@ public void cancelNotification(TravelerPosition travelerPosition) {
);
// Changed the saved message type from notify to cancel.
body.msg_type = 0;
var httpStatus = doPost(JsonUtils.toJson(body));
String bodyJson = JsonUtils.toJson(body);
var httpStatus = doPost(bodyJson);
if (httpStatus == HttpStatus.OK_200) {
travelerPosition.trackedJourney.updateNotificationMessage(routeId, JsonUtils.toJson(body));
travelerPosition.trackedJourney.updateNotificationMessage(routeId, bodyJson);
} else {
LOG.error("Error {} while trying to cancel Ride Gwinnett notification to bus operator.", httpStatus);
}
Expand Down Expand Up @@ -165,7 +166,7 @@ public static boolean hasNotSentNotificationForRoute(TrackedJourney trackedJourn
/**
* Has a previous notification already been cancelled.
*/
public static boolean hasNotCancelledNotificationForRoute(
public static boolean hasNotCanceledNotificationForRoute(
TrackedJourney trackedJourney,
String routeId
) throws JsonProcessingException {
Expand All @@ -174,7 +175,7 @@ public static boolean hasNotCancelledNotificationForRoute(
throw new IllegalStateException("A notification must exist before it can be cancelled!");
}
UsRideGwinnettBusOpNotificationMessage message = getNotificationMessage(messageBody);
return message.msg_type != 1;
return message.msg_type != 0;
}

public static UsRideGwinnettBusOpNotificationMessage getNotificationMessage(String body) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.opentripplanner.middleware.utils.JsonUtils;

import java.io.IOException;
import java.sql.Date;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
Expand Down Expand Up @@ -103,27 +103,54 @@ void canNotifyBusOperatorForDelayedDeparture() throws CloneNotSupportedException
}

@Test
void canCancelBusOperatorNotification() throws JsonProcessingException {
void canCancelBusOperatorNotification() throws JsonProcessingException, InterruptedException {
trackedJourney = createAndPersistTrackedJourney(getEndOfWalkLegCoordinates());
TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, walkToBusTransition, createOtpUser());

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

busOperatorActions.handleCancelNotificationAction(travelerPosition);
UsRideGwinnettBusOpNotificationMessage cancelMessage1 = getMessage(
Persistence.trackedJourneys.getById(trackedJourney.id)
);
assertEquals(0, cancelMessage1.msg_type);


// A second request to cancel should not touch the previous request.
Thread.sleep(20);
busOperatorActions.handleCancelNotificationAction(travelerPosition);
updated = Persistence.trackedJourneys.getById(trackedJourney.id);
UsRideGwinnettBusOpNotificationMessage cancelMessage2 = getMessage(
Persistence.trackedJourneys.getById(trackedJourney.id)
);
assertEquals(0, cancelMessage2.msg_type);
assertEquals(cancelMessage1.timestamp, cancelMessage2.timestamp);
}

private static UsRideGwinnettBusOpNotificationMessage getMessage(TrackedJourney updated) throws JsonProcessingException {
String messageBody = updated.busNotificationMessages.get(routeId);
UsRideGwinnettBusOpNotificationMessage message = getNotificationMessage(messageBody);
assertEquals(1, message.msg_type);
return getNotificationMessage(messageBody);
}

@Test
void canNotifyBusOperatorOnlyOnce() {
void canNotifyBusOperatorOnlyOnce() throws InterruptedException, JsonProcessingException {
trackedJourney = createAndPersistTrackedJourney(getEndOfWalkLegCoordinates());
TravelerPosition travelerPosition = new TravelerPosition(trackedJourney, walkToBusTransition, createOtpUser());

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

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

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.opentripplanner.middleware.triptracker.interactions.busnotifiers;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.opentripplanner.middleware.models.TrackedJourney;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class UsRideGwinnettNotifyBusOperatorTest {
@Test
void hasNotCanceledNotificationForRoute() throws JsonProcessingException {
TrackedJourney journey = new TrackedJourney();
// A request was sent to the bus driver on bus route 10.
journey.busNotificationMessages.put("Route10", "{\"msg_type\": 1}");
// A request was sent then canceled to the bus driver on bus route 20.
journey.busNotificationMessages.put("Route20", "{\"msg_type\": 0}");

assertTrue(UsRideGwinnettNotifyBusOperator.hasNotSentNotificationForRoute(journey, "Route30"));
assertThrows(IllegalStateException.class, () -> UsRideGwinnettNotifyBusOperator.hasNotCanceledNotificationForRoute(journey, "Route30"));

assertTrue(UsRideGwinnettNotifyBusOperator.hasNotCanceledNotificationForRoute(journey, "Route10"));

assertFalse(UsRideGwinnettNotifyBusOperator.hasNotCanceledNotificationForRoute(journey, "Route20"));
}
}

0 comments on commit 579dcb7

Please sign in to comment.