From 47e49c1f501bbd082e2464bbdac24d2e75baea6f Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Thu, 26 Sep 2024 14:08:25 +0200 Subject: [PATCH] Adapt to new API --- .../LegacyRouteRequestMapper.java | 11 +--- .../PassThroughLocationMapper.java | 41 ------------- .../routerequest/ViaLocationMapper.java | 58 +++++++++++++++++++ .../LegacyRouteRequestMapperTest.java | 10 ++-- .../PassThroughLocationMapperTest.java | 34 ----------- .../collection/CollectionUtilsTest.java | 2 +- 6 files changed, 68 insertions(+), 88 deletions(-) delete mode 100644 src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapper.java create mode 100644 src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/ViaLocationMapper.java delete mode 100644 src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapperTest.java diff --git a/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapper.java b/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapper.java index a107f1f1aad..42468eafe02 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapper.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapper.java @@ -32,7 +32,6 @@ import org.opentripplanner.routing.core.VehicleRoutingOptimizeType; import org.opentripplanner.transit.model.basic.MainAndSubMode; import org.opentripplanner.transit.model.basic.TransitMode; -import org.opentripplanner.transit.service.TransitService; public class LegacyRouteRequestMapper { @@ -57,7 +56,7 @@ public static RouteRequest toRouteRequest( callWith.argument("from", (Map v) -> request.setFrom(toGenericLocation(v))); callWith.argument("to", (Map v) -> request.setTo(toGenericLocation(v))); - mapViaPoints(request, callWith, context.transitService()); + mapViaPoints(request, callWith); request.setDateTime( environment.getArgument("date"), @@ -260,15 +259,11 @@ public static RouteRequest toRouteRequest( return request; } - static void mapViaPoints( - RouteRequest request, - CallerWithEnvironment callWith, - TransitService transitService - ) { + static void mapViaPoints(RouteRequest request, CallerWithEnvironment callWith) { callWith.argument( "via", (List> v) -> - request.setPassThroughPoints(PassThroughLocationMapper.toLocations(transitService, v)) + request.setViaLocations(ViaLocationMapper.mapToViaLocations(v)) ); } diff --git a/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapper.java b/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapper.java deleted file mode 100644 index 5c126531390..00000000000 --- a/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapper.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.opentripplanner.apis.gtfs.mapping.routerequest; - -import java.util.List; -import java.util.Map; -import org.opentripplanner.framework.collection.CollectionUtils; -import org.opentripplanner.routing.api.request.PassThroughPoint; -import org.opentripplanner.transit.model.framework.FeedScopedId; -import org.opentripplanner.transit.service.TransitService; - -class PassThroughLocationMapper { - - static List toLocations( - final TransitService transitService, - final List> passThroughPoints - ) { - return passThroughPoints.stream().map(p -> handlePoint(transitService, p)).toList(); - } - - private static PassThroughPoint handlePoint( - final TransitService transitService, - Map map - ) { - Map element = (Map) map.get("passThrough"); - List ids = (List) element.get("stopLocationIds"); - - final String name = (String) element.get("name"); - if (CollectionUtils.isEmpty(ids)) { - throw new IllegalArgumentException("No stops in pass-through point"); - } - - var stopLocations = ids - .stream() - .map(FeedScopedId::parse) - .flatMap(id -> transitService.getStopOrChildStops(id).stream()) - .toList(); - if (stopLocations.isEmpty()) { - throw new IllegalArgumentException("No stop locations found for %s.".formatted(ids)); - } - return new PassThroughPoint(stopLocations, name); - } -} diff --git a/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/ViaLocationMapper.java b/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/ViaLocationMapper.java new file mode 100644 index 00000000000..e2fbac8979e --- /dev/null +++ b/src/main/java/org/opentripplanner/apis/gtfs/mapping/routerequest/ViaLocationMapper.java @@ -0,0 +1,58 @@ +package org.opentripplanner.apis.gtfs.mapping.routerequest; + +import static java.util.stream.Collectors.toList; + +import java.time.Duration; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper; +import org.opentripplanner.apis.transmodel.model.plan.TripQuery; +import org.opentripplanner.apis.transmodel.model.plan.ViaLocationInputType; +import org.opentripplanner.apis.transmodel.support.OneOfInputValidator; +import org.opentripplanner.routing.api.request.via.PassThroughViaLocation; +import org.opentripplanner.routing.api.request.via.ViaLocation; +import org.opentripplanner.routing.api.request.via.VisitViaLocation; +import org.opentripplanner.transit.model.framework.FeedScopedId; + +class ViaLocationMapper { + + static List mapToViaLocations(final List> via) { + return via.stream().map(ViaLocationMapper::mapViaLocation).collect(toList()); + } + + private static ViaLocation mapViaLocation(Map inputMap) { + var fieldName = OneOfInputValidator.validateOneOf( + inputMap, + TripQuery.FIELD_VIA, + ViaLocationInputType.FIELD_VISIT, + ViaLocationInputType.FIELD_PASS_THROUGH + ); + + Map value = (Map) inputMap.get(fieldName); + + return switch (fieldName) { + case ViaLocationInputType.FIELD_VISIT -> mapVisitViaLocation(value); + case ViaLocationInputType.FIELD_PASS_THROUGH -> mapPassThroughViaLocation(value); + default -> throw new IllegalArgumentException("Unknown field: " + fieldName); + }; + } + + private static VisitViaLocation mapVisitViaLocation(Map inputMap) { + var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL); + var minimumWaitTime = (Duration) inputMap.get(ViaLocationInputType.FIELD_MINIMUM_WAIT_TIME); + var stopLocationIds = mapStopLocationIds(inputMap); + return new VisitViaLocation(label, minimumWaitTime, stopLocationIds, List.of()); + } + + private static PassThroughViaLocation mapPassThroughViaLocation(Map inputMap) { + var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL); + var stopLocationIds = mapStopLocationIds(inputMap); + return new PassThroughViaLocation(label, stopLocationIds); + } + + private static List mapStopLocationIds(Map map) { + var c = (Collection) map.get(ViaLocationInputType.FIELD_STOP_LOCATION_IDS); + return c.stream().map(TransitIdMapper::mapIDToDomain).toList(); + } +} diff --git a/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapperTest.java b/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapperTest.java index a8bff772863..97b5dff62e9 100644 --- a/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapperTest.java +++ b/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/LegacyRouteRequestMapperTest.java @@ -265,7 +265,9 @@ void transferSlack() { void passThroughPoints() { Map arguments = Map.of( "via", - List.of(Map.of("passThrough", Map.of("stopLocationIds", List.of("F:stop1")))) + List.of( + Map.of("passThrough", Map.of("stopLocationIds", List.of("F:stop1"), "label", "a label")) + ) ); var routeRequest = LegacyRouteRequestMapper.toRouteRequest( @@ -273,12 +275,12 @@ void passThroughPoints() { context ); assertEquals( - "[PassThroughPoint[stopLocations=[RegularStop{F:stop1 stop1}], name=null]]", - routeRequest.getPassThroughPoints().toString() + "[PassThroughViaLocation{label: a label, stopLocationIds: [F:stop1]}]", + routeRequest.getViaLocations().toString() ); var noParamsReq = LegacyRouteRequestMapper.toRouteRequest(executionContext(Map.of()), context); - assertEquals(List.of(), noParamsReq.getPassThroughPoints()); + assertEquals(List.of(), noParamsReq.getViaLocations()); } private DataFetchingEnvironment executionContext(Map arguments) { diff --git a/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapperTest.java b/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapperTest.java deleted file mode 100644 index 391f3bf9aee..00000000000 --- a/src/test/java/org/opentripplanner/apis/gtfs/mapping/routerequest/PassThroughLocationMapperTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.opentripplanner.apis.gtfs.mapping.routerequest; - -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.List; -import java.util.Map; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; - -/** - * A test for the successful case is at {@link LegacyRouteRequestMapperTest#passThroughPoints()} - */ -class PassThroughLocationMapperTest { - - public static List>> failureCases() { - return List.of( - List.of(Map.of("passThrough", Map.of("stopLocationIds", List.of("fantasy:id")))), - List.of(Map.of("passThrough", Map.of())), - List.of(Map.of("passThrough", Map.of()), Map.of("passThroughLocation", Map.of())) - ); - } - - @ParameterizedTest - @MethodSource("failureCases") - void throwException(List> params) { - var service = new DefaultTransitService(new TransitModel()); - assertThrows( - IllegalArgumentException.class, - () -> PassThroughLocationMapper.toLocations(service, params) - ); - } -} diff --git a/src/test/java/org/opentripplanner/framework/collection/CollectionUtilsTest.java b/src/test/java/org/opentripplanner/framework/collection/CollectionUtilsTest.java index 5b89811e9fe..6686ac8e0d9 100644 --- a/src/test/java/org/opentripplanner/framework/collection/CollectionUtilsTest.java +++ b/src/test/java/org/opentripplanner/framework/collection/CollectionUtilsTest.java @@ -21,7 +21,7 @@ class CollectionUtilsTest { @Test void testIsEmpty() { - assertTrue(CollectionUtils.isEmpty(null)); + assertTrue(CollectionUtils.isEmpty((List) null)); assertTrue(CollectionUtils.isEmpty(List.of())); assertFalse(CollectionUtils.isEmpty(List.of(1))); }