From 5bf5dc50102337d3c27ac1444657b36e3a7df04b Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Tue, 12 Mar 2024 00:48:58 +0100 Subject: [PATCH 1/5] #23 Do not override by extend logback configuration in tests --- src/test/resources/{logback.xml => logback-test.xml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/resources/{logback.xml => logback-test.xml} (100%) diff --git a/src/test/resources/logback.xml b/src/test/resources/logback-test.xml similarity index 100% rename from src/test/resources/logback.xml rename to src/test/resources/logback-test.xml From 5258724f851ee62c3299cadbbcc1d969f214edfe Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Tue, 12 Mar 2024 01:05:31 +0100 Subject: [PATCH 2/5] Plan query - passing searchWindow parameter when not null --- .../java/org/opentripplanner/client/OtpApiClient.java | 3 +++ .../client/parameters/TripPlanParameters.java | 1 + .../client/parameters/TripPlanParametersBuilder.java | 8 ++++++++ src/main/resources/queries/plan.graphql | 1 + 4 files changed, 13 insertions(+) diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index 863d9d5..aa4ebe9 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -63,6 +63,9 @@ public TripPlan plan(TripPlanParameters req) throws IOException { req.time().toLocalDate().toString(), req.time().toLocalTime().truncatedTo(ChronoUnit.SECONDS).toString(), req.searchDirection().isArriveBy(), + req.searchWindow() == null + ? "" + : String.format("searchWindow : %d", req.searchWindow()), req.walkReluctance(), req.wheelchair()); diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java index 8ae3a94..f27ca40 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java @@ -14,6 +14,7 @@ public record TripPlanParameters( int numItineraries, Set modes, SearchDirection searchDirection, + Long searchWindow, float walkReluctance, boolean wheelchair) { diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java index da2fcbf..4bd11b7 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java @@ -14,6 +14,7 @@ public class TripPlanParametersBuilder { private LocalDateTime time; private Set modes; private SearchDirection searchDirection = SearchDirection.DEPART_AT; + private Long searchWindow; private float walkReluctance = 1.4f; private int numItineraries = 5; private boolean wheelchair = false; @@ -48,6 +49,11 @@ public TripPlanParametersBuilder withSearchDirection(SearchDirection searchDirec return this; } + public TripPlanParametersBuilder withSearchWindow(Long searchWindow) { + this.searchWindow = searchWindow; + return this; + } + public TripPlanParametersBuilder withWalkReluctance(float wr) { this.walkReluctance = wr; return this; @@ -70,6 +76,7 @@ public TripPlanParametersBuilder copy() { .withTime(time) .withModes(modes) .withSearchDirection(searchDirection) + .withSearchWindow(searchWindow) .withWalkReluctance(walkReluctance) .withNumberOfItineraries(numItineraries) .withWheelchair(wheelchair); @@ -83,6 +90,7 @@ public TripPlanParameters build() { numItineraries, modes, searchDirection, + searchWindow, walkReluctance, wheelchair); } diff --git a/src/main/resources/queries/plan.graphql b/src/main/resources/queries/plan.graphql index f944e21..ffb654d 100644 --- a/src/main/resources/queries/plan.graphql +++ b/src/main/resources/queries/plan.graphql @@ -7,6 +7,7 @@ query { date: "%s" time: "%s" arriveBy: %s + %s walkReluctance: %s wheelchair: %s ) { From d8459cf7676beafeae0aca6da0229c3530813162 Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Tue, 12 Mar 2024 08:55:58 +0100 Subject: [PATCH 3/5] #23 SearchWindow as Duration --- .../opentripplanner/client/OtpApiClient.java | 6 ++-- .../client/parameters/TripPlanParameters.java | 8 +++++- .../parameters/TripPlanParametersBuilder.java | 5 ++-- .../org/opentripplanner/IntegrationTest.java | 28 +++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index aa4ebe9..01e478c 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -63,9 +63,9 @@ public TripPlan plan(TripPlanParameters req) throws IOException { req.time().toLocalDate().toString(), req.time().toLocalTime().truncatedTo(ChronoUnit.SECONDS).toString(), req.searchDirection().isArriveBy(), - req.searchWindow() == null - ? "" - : String.format("searchWindow : %d", req.searchWindow()), + req.getSearchWindow().isPresent() + ? String.format("searchWindow : %d", req.getSearchWindow().get().toMinutes()) + : "", req.walkReluctance(), req.wheelchair()); diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java index f27ca40..6d8def1 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java @@ -1,7 +1,9 @@ package org.opentripplanner.client.parameters; +import java.time.Duration; import java.time.LocalDateTime; import java.util.Objects; +import java.util.Optional; import java.util.Set; import org.opentripplanner.client.model.PlaceParameter; import org.opentripplanner.client.model.RequestMode; @@ -14,7 +16,7 @@ public record TripPlanParameters( int numItineraries, Set modes, SearchDirection searchDirection, - Long searchWindow, + Duration searchWindow, float walkReluctance, boolean wheelchair) { @@ -27,6 +29,10 @@ public record TripPlanParameters( Objects.requireNonNull(searchDirection); } + public Optional getSearchWindow() { + return Optional.ofNullable(searchWindow()); + } + public enum SearchDirection { DEPART_AT, ARRIVE_BY; diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java index 4bd11b7..8e6d64b 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParametersBuilder.java @@ -1,5 +1,6 @@ package org.opentripplanner.client.parameters; +import java.time.Duration; import java.time.LocalDateTime; import java.util.Arrays; import java.util.Set; @@ -14,7 +15,7 @@ public class TripPlanParametersBuilder { private LocalDateTime time; private Set modes; private SearchDirection searchDirection = SearchDirection.DEPART_AT; - private Long searchWindow; + private Duration searchWindow; private float walkReluctance = 1.4f; private int numItineraries = 5; private boolean wheelchair = false; @@ -49,7 +50,7 @@ public TripPlanParametersBuilder withSearchDirection(SearchDirection searchDirec return this; } - public TripPlanParametersBuilder withSearchWindow(Long searchWindow) { + public TripPlanParametersBuilder withSearchWindow(Duration searchWindow) { this.searchWindow = searchWindow; return this; } diff --git a/src/test/java/org/opentripplanner/IntegrationTest.java b/src/test/java/org/opentripplanner/IntegrationTest.java index fff2349..9fd0b77 100644 --- a/src/test/java/org/opentripplanner/IntegrationTest.java +++ b/src/test/java/org/opentripplanner/IntegrationTest.java @@ -84,6 +84,34 @@ public void planPlaceToPlace() throws IOException { assertEquals(List.of(), leg.fareProducts()); } + @Test + public void planPlaceToPlaceWithSearchWindow() throws IOException { + var result = + client.plan( + TripPlanParameters.builder() + .withFrom(OSLO_LUFTHAVN_ID) + .withTo(OSLO_S_ID) + .withTime(LocalDateTime.now()) + .withModes(RequestMode.TRANSIT) + .withNumberOfItineraries(1) + .withSearchWindow(java.time.Duration.ofDays(1)) + .build()); + + LOG.info("Received {} itineraries", result.itineraries().size()); + assertEquals(1, result.itineraries().size()); + + assertNotNull(result.itineraries().get(0).legs().get(0).startTime()); + + var leg = result.itineraries().get(0).legs().get(0); + + var transitLeg = result.transitItineraries().get(0).transitLegs().get(0); + assertFalse(transitLeg.from().stop().isEmpty()); + assertFalse(transitLeg.to().stop().isEmpty()); + assertNotNull(transitLeg.from().stop().get().id()); + + assertEquals(List.of(), leg.fareProducts()); + } + @Test public void arriveByPlan() throws IOException { From d6d6f460f5aa707c9a2b59cf321a1d8cd0cf3877 Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Tue, 12 Mar 2024 09:03:29 +0100 Subject: [PATCH 4/5] #23 TripPlanParameters converted from record to class --- .../opentripplanner/client/OtpApiClient.java | 4 +- .../client/parameters/TripPlanParameters.java | 141 ++++++++++++++++-- 2 files changed, 129 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index 01e478c..0dcd1dc 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -63,8 +63,8 @@ public TripPlan plan(TripPlanParameters req) throws IOException { req.time().toLocalDate().toString(), req.time().toLocalTime().truncatedTo(ChronoUnit.SECONDS).toString(), req.searchDirection().isArriveBy(), - req.getSearchWindow().isPresent() - ? String.format("searchWindow : %d", req.getSearchWindow().get().toMinutes()) + req.searchWindow().isPresent() + ? String.format("searchWindow : %d", req.searchWindow().get().toMinutes()) : "", req.walkReluctance(), req.wheelchair()); diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java index 6d8def1..872707e 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java @@ -9,28 +9,47 @@ import org.opentripplanner.client.model.RequestMode; import org.opentripplanner.client.validation.CollectionUtils; -public record TripPlanParameters( - PlaceParameter fromPlace, - PlaceParameter toPlace, - LocalDateTime time, - int numItineraries, - Set modes, - SearchDirection searchDirection, - Duration searchWindow, - float walkReluctance, - boolean wheelchair) { - - public TripPlanParameters { +public final class TripPlanParameters { + private final PlaceParameter fromPlace; + private final PlaceParameter toPlace; + private final LocalDateTime time; + private final int numItineraries; + private final Set modes; + private final SearchDirection searchDirection; + private final Duration searchWindow; + private final float walkReluctance; + private final boolean wheelchair; + + public TripPlanParameters( + PlaceParameter fromPlace, + PlaceParameter toPlace, + LocalDateTime time, + int numItineraries, + Set modes, + SearchDirection searchDirection, + Duration searchWindow, + float walkReluctance, + boolean wheelchair) { Objects.requireNonNull(fromPlace); Objects.requireNonNull(toPlace); Objects.requireNonNull(time); Objects.requireNonNull(modes); CollectionUtils.assertHasValue(modes); Objects.requireNonNull(searchDirection); + + this.fromPlace = fromPlace; + this.toPlace = toPlace; + this.time = time; + this.numItineraries = numItineraries; + this.modes = modes; + this.searchDirection = searchDirection; + this.searchWindow = searchWindow; + this.walkReluctance = walkReluctance; + this.wheelchair = wheelchair; } - public Optional getSearchWindow() { - return Optional.ofNullable(searchWindow()); + public Optional searchWindow() { + return Optional.ofNullable(searchWindow); } public enum SearchDirection { @@ -45,4 +64,98 @@ public boolean isArriveBy() { public static TripPlanParametersBuilder builder() { return new TripPlanParametersBuilder(); } + + public PlaceParameter fromPlace() { + return fromPlace; + } + + public PlaceParameter toPlace() { + return toPlace; + } + + public LocalDateTime time() { + return time; + } + + public int numItineraries() { + return numItineraries; + } + + public Set modes() { + return modes; + } + + public SearchDirection searchDirection() { + return searchDirection; + } + + public float walkReluctance() { + return walkReluctance; + } + + public boolean wheelchair() { + return wheelchair; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (TripPlanParameters) obj; + return Objects.equals(this.fromPlace, that.fromPlace) + && Objects.equals(this.toPlace, that.toPlace) + && Objects.equals(this.time, that.time) + && this.numItineraries == that.numItineraries + && Objects.equals(this.modes, that.modes) + && Objects.equals(this.searchDirection, that.searchDirection) + && Objects.equals(this.searchWindow, that.searchWindow) + && Float.floatToIntBits(this.walkReluctance) == Float.floatToIntBits(that.walkReluctance) + && this.wheelchair == that.wheelchair; + } + + @Override + public int hashCode() { + return Objects.hash( + fromPlace, + toPlace, + time, + numItineraries, + modes, + searchDirection, + searchWindow, + walkReluctance, + wheelchair); + } + + @Override + public String toString() { + return "TripPlanParameters[" + + "fromPlace=" + + fromPlace + + ", " + + "toPlace=" + + toPlace + + ", " + + "time=" + + time + + ", " + + "numItineraries=" + + numItineraries + + ", " + + "modes=" + + modes + + ", " + + "searchDirection=" + + searchDirection + + ", " + + "searchWindow=" + + searchWindow + + ", " + + "walkReluctance=" + + walkReluctance + + ", " + + "wheelchair=" + + wheelchair + + ']'; + } } From 81dec03b3a4ea1170e10a51a7c0e5b3a41ca3a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Petr=C3=A1k?= Date: Tue, 12 Mar 2024 09:58:34 +0100 Subject: [PATCH 5/5] #23 Code review changes --- .../opentripplanner/client/OtpApiClient.java | 4 +--- .../client/parameters/TripPlanParameters.java | 21 +++++++++---------- .../org/opentripplanner/IntegrationTest.java | 21 ++++++++++--------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index 0dcd1dc..606044e 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -63,9 +63,7 @@ public TripPlan plan(TripPlanParameters req) throws IOException { req.time().toLocalDate().toString(), req.time().toLocalTime().truncatedTo(ChronoUnit.SECONDS).toString(), req.searchDirection().isArriveBy(), - req.searchWindow().isPresent() - ? String.format("searchWindow : %d", req.searchWindow().get().toMinutes()) - : "", + req.searchWindow().map(sw -> "searchWindow : %d".formatted(sw.toSeconds())).orElse(""), req.walkReluctance(), req.wheelchair()); diff --git a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java index 872707e..e6879c9 100644 --- a/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java +++ b/src/main/java/org/opentripplanner/client/parameters/TripPlanParameters.java @@ -5,6 +5,8 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; + +import jakarta.annotation.Nullable; import org.opentripplanner.client.model.PlaceParameter; import org.opentripplanner.client.model.RequestMode; import org.opentripplanner.client.validation.CollectionUtils; @@ -16,6 +18,7 @@ public final class TripPlanParameters { private final int numItineraries; private final Set modes; private final SearchDirection searchDirection; + @Nullable private final Duration searchWindow; private final float walkReluctance; private final boolean wheelchair; @@ -29,20 +32,16 @@ public TripPlanParameters( SearchDirection searchDirection, Duration searchWindow, float walkReluctance, - boolean wheelchair) { - Objects.requireNonNull(fromPlace); - Objects.requireNonNull(toPlace); - Objects.requireNonNull(time); - Objects.requireNonNull(modes); + boolean wheelchair + ) { CollectionUtils.assertHasValue(modes); - Objects.requireNonNull(searchDirection); - this.fromPlace = fromPlace; - this.toPlace = toPlace; - this.time = time; + this.fromPlace = Objects.requireNonNull(fromPlace); + this.toPlace = Objects.requireNonNull(toPlace); + this.time = Objects.requireNonNull(time); this.numItineraries = numItineraries; - this.modes = modes; - this.searchDirection = searchDirection; + this.modes = Objects.requireNonNull(modes); + this.searchDirection = Objects.requireNonNull(searchDirection); this.searchWindow = searchWindow; this.walkReluctance = walkReluctance; this.wheelchair = wheelchair; diff --git a/src/test/java/org/opentripplanner/IntegrationTest.java b/src/test/java/org/opentripplanner/IntegrationTest.java index 9fd0b77..fefd449 100644 --- a/src/test/java/org/opentripplanner/IntegrationTest.java +++ b/src/test/java/org/opentripplanner/IntegrationTest.java @@ -9,6 +9,7 @@ import static org.opentripplanner.StationParameters.OSLO_WEST; import java.io.IOException; +import java.time.Duration; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.List; @@ -94,22 +95,22 @@ public void planPlaceToPlaceWithSearchWindow() throws IOException { .withTime(LocalDateTime.now()) .withModes(RequestMode.TRANSIT) .withNumberOfItineraries(1) - .withSearchWindow(java.time.Duration.ofDays(1)) + .withSearchWindow(Duration.ofDays(1)) .build()); - LOG.info("Received {} itineraries", result.itineraries().size()); - assertEquals(1, result.itineraries().size()); + LOG.info("Received {} itineraries", result.itineraries().size()); + assertEquals(1, result.itineraries().size()); - assertNotNull(result.itineraries().get(0).legs().get(0).startTime()); + assertNotNull(result.itineraries().get(0).legs().get(0).startTime()); - var leg = result.itineraries().get(0).legs().get(0); + var leg = result.itineraries().get(0).legs().get(0); - var transitLeg = result.transitItineraries().get(0).transitLegs().get(0); - assertFalse(transitLeg.from().stop().isEmpty()); - assertFalse(transitLeg.to().stop().isEmpty()); - assertNotNull(transitLeg.from().stop().get().id()); + var transitLeg = result.transitItineraries().get(0).transitLegs().get(0); + assertFalse(transitLeg.from().stop().isEmpty()); + assertFalse(transitLeg.to().stop().isEmpty()); + assertNotNull(transitLeg.from().stop().get().id()); - assertEquals(List.of(), leg.fareProducts()); + assertEquals(List.of(), leg.fareProducts()); } @Test