From 1bfc8332c5a71a2ba390c03e5f75c1c287f506ac Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Wed, 8 Jan 2025 17:52:37 +0100 Subject: [PATCH] Fix ScheduledTransitAlertBuilder --- .../model/plan/ScheduledTransitLeg.java | 1 + .../plan/ScheduledTransitLegBuilder.java | 9 ++++++ .../plan/ScheduledTransitLegBuilderTest.java | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLeg.java b/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLeg.java index 55e4bccfdec..ab912eae85b 100644 --- a/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLeg.java +++ b/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLeg.java @@ -98,6 +98,7 @@ protected ScheduledTransitLeg(ScheduledTransitLegBuilder builder) { getDistanceFromCoordinates( List.of(transitLegCoordinates.getFirst(), transitLegCoordinates.getLast()) ); + this.transitAlerts.addAll(builder.alerts()); } public ZoneId getZoneId() { diff --git a/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilder.java b/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilder.java index 3e9b5540b1c..e132137e982 100644 --- a/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilder.java +++ b/application/src/main/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilder.java @@ -3,7 +3,10 @@ import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.HashSet; +import java.util.Set; import org.opentripplanner.model.transfer.ConstrainedTransfer; +import org.opentripplanner.routing.alertpatch.TransitAlert; import org.opentripplanner.transit.model.network.TripPattern; import org.opentripplanner.transit.model.timetable.TripOnServiceDate; import org.opentripplanner.transit.model.timetable.TripTimes; @@ -23,6 +26,7 @@ public class ScheduledTransitLegBuilder> private ConstrainedTransfer transferToNextLeg; private int generalizedCost; private Float accessibilityScore; + private Set alerts = new HashSet<>(); public ScheduledTransitLegBuilder() {} @@ -40,6 +44,7 @@ public ScheduledTransitLegBuilder(ScheduledTransitLeg original) { generalizedCost = original.getGeneralizedCost(); accessibilityScore = original.accessibilityScore(); zoneId = original.getZoneId(); + alerts = original.getTransitAlerts(); } public B withTripTimes(TripTimes tripTimes) { @@ -159,6 +164,10 @@ public Float accessibilityScore() { return accessibilityScore; } + public Set alerts() { + return alerts; + } + public ScheduledTransitLeg build() { return new ScheduledTransitLeg(this); } diff --git a/application/src/test/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilderTest.java b/application/src/test/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilderTest.java index 8d3d6ed7c1e..83871257ce2 100644 --- a/application/src/test/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilderTest.java +++ b/application/src/test/java/org/opentripplanner/model/plan/ScheduledTransitLegBuilderTest.java @@ -1,15 +1,25 @@ package org.opentripplanner.model.plan; +import static com.google.common.truth.Truth.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.opentripplanner.transit.model._data.TimetableRepositoryForTest.id; import java.time.LocalDate; +import java.util.Set; import org.junit.jupiter.api.Test; import org.opentripplanner._support.time.ZoneIds; +import org.opentripplanner.framework.i18n.I18NString; +import org.opentripplanner.routing.alertpatch.TransitAlert; import org.opentripplanner.transit.model._data.TimetableRepositoryForTest; import org.opentripplanner.transit.model.basic.TransitMode; class ScheduledTransitLegBuilderTest { + private static final TransitAlert ALERT = TransitAlert + .of(id("alert")) + .withDescriptionText(I18NString.of("alert")) + .build(); + @Test void transferZoneId() { var pattern = TimetableRepositoryForTest.of().pattern(TransitMode.BUS).build(); @@ -28,4 +38,24 @@ void transferZoneId() { assertEquals(ZoneIds.BERLIN, withScore.getZoneId()); } + + @Test + void alerts() { + var pattern = TimetableRepositoryForTest.of().pattern(TransitMode.BUS).build(); + var leg = new ScheduledTransitLegBuilder<>() + .withZoneId(ZoneIds.BERLIN) + .withServiceDate(LocalDate.of(2023, 11, 15)) + .withTripPattern(pattern) + .withBoardStopIndexInPattern(0) + .withAlightStopIndexInPattern(1) + .build(); + + leg.addAlert(ALERT); + + var newLeg = new ScheduledTransitLegBuilder<>(leg); + + var withScore = newLeg.withAccessibilityScore(4f).build(); + + assertEquals(Set.of(ALERT), withScore.getTransitAlerts()); + } }