From bfd802b9a9c2d6bdc04c510a01d40f543aa754f0 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Wed, 10 Jan 2024 17:27:28 +0100 Subject: [PATCH] Refactor some aspects of alert handling --- .../routing/alertpatch/TimePeriod.java | 19 +-- .../updater/alert/AlertsUpdateHandler.java | 22 ++-- .../alert/GtfsRealtimeAlertsUpdater.java | 2 +- .../alert/AlertsUpdateHandlerTest.java | 116 +++++++++--------- 4 files changed, 72 insertions(+), 87 deletions(-) diff --git a/src/main/java/org/opentripplanner/routing/alertpatch/TimePeriod.java b/src/main/java/org/opentripplanner/routing/alertpatch/TimePeriod.java index 0aa5498720d..b79ca6c29ca 100644 --- a/src/main/java/org/opentripplanner/routing/alertpatch/TimePeriod.java +++ b/src/main/java/org/opentripplanner/routing/alertpatch/TimePeriod.java @@ -1,42 +1,29 @@ package org.opentripplanner.routing.alertpatch; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; - /** * Represents a period of time, in terms of seconds in [start, end) - * - * @author novalis */ public class TimePeriod { - // TODO OTP2 - This class is used both for internal and external API representation, - // - a external API version should be created to decouple the internal model - // - from any API usage. - public static final long OPEN_ENDED = Long.MAX_VALUE; - @JsonSerialize - public long startTime; + public final long startTime; - @JsonSerialize - public long endTime; + public final long endTime; public TimePeriod(long start, long end) { this.startTime = start; this.endTime = end; } - public TimePeriod() {} - public int hashCode() { return (int) ((startTime & 0x7fff) + (endTime & 0x7fff)); } public boolean equals(Object o) { - if (!(o instanceof TimePeriod)) { + if (!(o instanceof TimePeriod other)) { return false; } - TimePeriod other = (TimePeriod) o; return other.startTime == startTime && other.endTime == endTime; } } diff --git a/src/main/java/org/opentripplanner/updater/alert/AlertsUpdateHandler.java b/src/main/java/org/opentripplanner/updater/alert/AlertsUpdateHandler.java index 948eac0bc41..32fe1057e32 100644 --- a/src/main/java/org/opentripplanner/updater/alert/AlertsUpdateHandler.java +++ b/src/main/java/org/opentripplanner/updater/alert/AlertsUpdateHandler.java @@ -10,7 +10,6 @@ import com.google.transit.realtime.GtfsRealtime.TimeRange; import com.google.transit.realtime.GtfsRealtime.TripDescriptor; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.opentripplanner.framework.i18n.I18NString; @@ -27,8 +26,6 @@ /** * This updater only includes GTFS-Realtime Service Alert feeds. - * - * @author novalis */ public class AlertsUpdateHandler { @@ -46,15 +43,16 @@ public class AlertsUpdateHandler { private final DirectionMapper directionMapper = new DirectionMapper(DataImportIssueStore.NOOP); public void update(FeedMessage message) { - Collection alerts = new ArrayList<>(); - for (FeedEntity entity : message.getEntityList()) { - if (!entity.hasAlert()) { - continue; - } - GtfsRealtime.Alert alert = entity.getAlert(); - String id = entity.getId(); - alerts.add(mapAlert(id, alert)); - } + var alerts = message + .getEntityList() + .stream() + .filter(FeedEntity::hasAlert) + .map(e -> { + var alert = e.getAlert(); + var id = e.getId(); + return mapAlert(id, alert); + }) + .toList(); transitAlertService.setAlerts(alerts); } diff --git a/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java b/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java index da10324dd86..d1358f58051 100644 --- a/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java +++ b/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java @@ -78,7 +78,7 @@ protected void runPolling() { final FeedMessage feed = otpHttpClient.getAndMap( URI.create(url), this.headers.asMap(), - FeedMessage.PARSER::parseFrom + FeedMessage::parseFrom ); long feedTimestamp = feed.getHeader().getTimestamp(); diff --git a/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java b/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java index 0474c328162..3ee475a636a 100644 --- a/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java +++ b/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java @@ -34,7 +34,7 @@ public class AlertsUpdateHandlerTest { private final TransitAlertService service = new TransitAlertServiceImpl(new TransitModel()); @BeforeEach - public void setUp() { + void setUp() { handler = new AlertsUpdateHandler(); handler.setFeedId("1"); handler.setEarlyStart(5); @@ -42,8 +42,8 @@ public void setUp() { } @Test - public void testAlertWithTimePeriodConsideringEarlyStart() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testAlertWithTimePeriodConsideringEarlyStart() { + var alert = GtfsRealtime.Alert .newBuilder() .addActivePeriod(GtfsRealtime.TimeRange.newBuilder().setStart(10).setEnd(20).build()) .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) @@ -54,8 +54,8 @@ public void testAlertWithTimePeriodConsideringEarlyStart() { } @Test - public void testAlertStartConsideringEarlyStart() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testAlertStartConsideringEarlyStart() { + var alert = GtfsRealtime.Alert .newBuilder() .addActivePeriod(GtfsRealtime.TimeRange.newBuilder().setStart(10).build()) .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) @@ -66,8 +66,8 @@ public void testAlertStartConsideringEarlyStart() { } @Test - public void testAlertEnd() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testAlertEnd() { + var alert = GtfsRealtime.Alert .newBuilder() .addActivePeriod(GtfsRealtime.TimeRange.newBuilder().setEnd(20).build()) .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) @@ -78,8 +78,8 @@ public void testAlertEnd() { } @Test - public void testWithoutUrl() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testWithoutUrl() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .build(); @@ -88,8 +88,8 @@ public void testWithoutUrl() { } @Test - public void testWithoutUrlTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithoutUrlTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setUrl( @@ -107,8 +107,8 @@ public void testWithoutUrlTranslations() { } @Test - public void testWithUrlTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithUrlTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setUrl( @@ -145,8 +145,8 @@ public void testWithUrlTranslations() { } @Test - public void testWithoutHeaderTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithoutHeaderTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setHeaderText( @@ -161,8 +161,8 @@ public void testWithoutHeaderTranslations() { } @Test - public void testWithHeaderTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithHeaderTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setHeaderText( @@ -185,8 +185,8 @@ public void testWithHeaderTranslations() { } @Test - public void testWithoutDescriptionTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithoutDescriptionTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setDescriptionText( @@ -201,8 +201,8 @@ public void testWithoutDescriptionTranslations() { } @Test - public void testWithDescriptionTranslations() { - GtfsRealtime.Alert alert = Alert + void testWithDescriptionTranslations() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setDescriptionText( @@ -231,8 +231,8 @@ public void testWithDescriptionTranslations() { } @Test - public void testMissingAlertSeverity() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testMissingAlertSeverity() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .build(); @@ -241,8 +241,8 @@ public void testMissingAlertSeverity() { } @Test - public void testSetAlertSeverity() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testSetAlertSeverity() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setSeverityLevel(SeverityLevel.SEVERE) @@ -252,8 +252,8 @@ public void testSetAlertSeverity() { } @Test - public void testMissingAlertCause() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testMissingAlertCause() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .build(); @@ -262,8 +262,8 @@ public void testMissingAlertCause() { } @Test - public void testSetAlertCause() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testSetAlertCause() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setCause(Cause.MAINTENANCE) @@ -273,8 +273,8 @@ public void testSetAlertCause() { } @Test - public void testMissingAlertEffect() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testMissingAlertEffect() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .build(); @@ -283,8 +283,8 @@ public void testMissingAlertEffect() { } @Test - public void testSetAlertEffect() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testSetAlertEffect() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .setEffect(Effect.MODIFIED_SERVICE) @@ -294,8 +294,8 @@ public void testSetAlertEffect() { } @Test - public void testAgencySelector() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testAgencySelector() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .build(); @@ -311,8 +311,8 @@ public void testAgencySelector() { } @Test - public void testRouteSelector() { - GtfsRealtime.Alert alert = GtfsRealtime.Alert + void testRouteSelector() { + var alert = GtfsRealtime.Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setRouteId("1")) .build(); @@ -328,8 +328,8 @@ public void testRouteSelector() { } @Test - public void testTripSelectorWithTripId() { - GtfsRealtime.Alert alert = Alert + void testTripSelectorWithTripId() { + var alert = Alert .newBuilder() .addInformedEntity( GtfsRealtime.EntitySelector @@ -349,8 +349,8 @@ public void testTripSelectorWithTripId() { } @Test - public void testStopSelector() { - GtfsRealtime.Alert alert = Alert + void testStopSelector() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setStopId("1")) .build(); @@ -366,8 +366,8 @@ public void testStopSelector() { } @Test - public void testStopAndRouteSelector() { - GtfsRealtime.Alert alert = Alert + void testStopAndRouteSelector() { + var alert = Alert .newBuilder() .addInformedEntity(0, GtfsRealtime.EntitySelector.newBuilder().setStopId("1").setRouteId("1")) .build(); @@ -383,8 +383,8 @@ public void testStopAndRouteSelector() { } @Test - public void testStopAndTripSelector() { - GtfsRealtime.Alert alert = Alert + void testStopAndTripSelector() { + var alert = Alert .newBuilder() .addInformedEntity( 0, @@ -406,8 +406,8 @@ public void testStopAndTripSelector() { } @Test - public void testMultipleSelectors() { - GtfsRealtime.Alert alert = Alert + void testMultipleSelectors() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("1")) .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setAgencyId("2")) @@ -431,8 +431,8 @@ public void testMultipleSelectors() { } @Test - public void testMissingSelector() { - GtfsRealtime.Alert alert = Alert.newBuilder().build(); + void testMissingSelector() { + var alert = Alert.newBuilder().build(); TransitAlert transitAlert = processOneAlert(alert); long totalSelectorCount = transitAlert.entities().size(); assertEquals(1l, totalSelectorCount); @@ -449,9 +449,9 @@ public void testMissingSelector() { } @Test - public void testUnknownSelector() { + void testUnknownSelector() { // Setting just direction is not supported and should result in entity not being handled - GtfsRealtime.Alert alert = Alert + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setDirectionId(1).build()) .build(); @@ -471,8 +471,8 @@ public void testUnknownSelector() { } @Test - public void testDirectionAndRouteSelector() { - GtfsRealtime.Alert alert = Alert + void testDirectionAndRouteSelector() { + var alert = Alert .newBuilder() .addInformedEntity( GtfsRealtime.EntitySelector.newBuilder().setDirectionId(1).setRouteId("1").build() @@ -490,8 +490,8 @@ public void testDirectionAndRouteSelector() { } @Test - public void testRouteTypeSelector() { - GtfsRealtime.Alert alert = Alert + void testRouteTypeSelector() { + var alert = Alert .newBuilder() .addInformedEntity(GtfsRealtime.EntitySelector.newBuilder().setRouteType(1).build()) .build(); @@ -507,8 +507,8 @@ public void testRouteTypeSelector() { } @Test - public void testRouteTypeAndAgencySelector() { - GtfsRealtime.Alert alert = Alert + void testRouteTypeAndAgencySelector() { + var alert = Alert .newBuilder() .addInformedEntity( GtfsRealtime.EntitySelector.newBuilder().setRouteType(1).setAgencyId("1").build() @@ -525,7 +525,7 @@ public void testRouteTypeAndAgencySelector() { assertEquals(1l, RouteTypeAndAgencySelectorCount); } - private TransitAlert processOneAlert(GtfsRealtime.Alert alert) { + private TransitAlert processOneAlert(Alert alert) { GtfsRealtime.FeedMessage message = GtfsRealtime.FeedMessage .newBuilder() .setHeader(GtfsRealtime.FeedHeader.newBuilder().setGtfsRealtimeVersion("2.0"))