From 884706110910e4b6f22529974f81d60e0cbfab57 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:05:05 -0500 Subject: [PATCH] fix(LocalizedAlert): Consider 2 alerts w/ same header, text, url, start but different end dates to b --- .../otp/response/LocalizedAlert.java | 4 +++- .../TripMonitorAlertNotificationTest.java | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/otp/response/LocalizedAlert.java b/src/main/java/org/opentripplanner/middleware/otp/response/LocalizedAlert.java index 3fae5af91..07798ebfa 100644 --- a/src/main/java/org/opentripplanner/middleware/otp/response/LocalizedAlert.java +++ b/src/main/java/org/opentripplanner/middleware/otp/response/LocalizedAlert.java @@ -44,7 +44,9 @@ public String getAlertDescriptionForHtml() { @Override public int hashCode() { - return Objects.hash(alertHeaderText, alertDescriptionText, alertUrl, effectiveStartDate, effectiveEndDate); + // Exclude effectiveEndDate from the hash code for cases where a given alert is "extended", + // e.g. incidents that take longer to resolve than initially planned. + return Objects.hash(alertHeaderText, alertDescriptionText, alertUrl, effectiveStartDate); } public boolean equals(Object o) { diff --git a/src/test/java/org/opentripplanner/middleware/models/TripMonitorAlertNotificationTest.java b/src/test/java/org/opentripplanner/middleware/models/TripMonitorAlertNotificationTest.java index 905c7c2fe..ba5bc86e5 100644 --- a/src/test/java/org/opentripplanner/middleware/models/TripMonitorAlertNotificationTest.java +++ b/src/test/java/org/opentripplanner/middleware/models/TripMonitorAlertNotificationTest.java @@ -4,6 +4,8 @@ import org.opentripplanner.middleware.otp.response.LocalizedAlert; import org.opentripplanner.middleware.tripmonitor.jobs.NotificationType; +import java.time.temporal.ChronoUnit; +import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -69,10 +71,24 @@ void shouldNotifyOnDisjointAlerts() { void shouldNotNotifyOnSameAlerts() { Set previousAlerts = new HashSet<>(); Set newAlerts = new HashSet<>(); + Date now = new Date(); - previousAlerts.add(createAlert()); - newAlerts.add(createAlert()); + // Create two alerts with the same header and description, and effectiveStartDate. + LocalizedAlert previousAlert = createAlert(); + LocalizedAlert newAlert = createAlert(); + previousAlert.effectiveStartDate = now; + newAlert.effectiveStartDate = now; + + // Assign different end dates to each alert. + // This is to reflect the cases where a given alert is "extended", + // e.g. incidents take longer to resolve than initially planned. + previousAlert.effectiveEndDate = Date.from(now.toInstant().plus(1, ChronoUnit.HOURS)); + newAlert.effectiveEndDate = Date.from(now.toInstant().plus(2, ChronoUnit.HOURS)); + + previousAlerts.add(previousAlert); + newAlerts.add(newAlert); + // These two alerts should be considered the same, and no new alert notifications should be triggered. TripMonitorNotification notification = TripMonitorAlertNotification.createAlertNotification(previousAlerts, newAlerts); assertNull(notification); }