From ba60a8be7a8e2d1e4c1fa4b0a8d9f60ef5ad77c1 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 31 Oct 2023 13:11:57 +0100 Subject: [PATCH] Add longName to Route --- pom.xml | 5 +++++ .../org/opentripplanner/client/model/Route.java | 14 +++++++++++++- .../client/serialization/ObjectMappers.java | 9 +++++---- src/main/resources/queries/routes.graphql | 1 + .../java/org/opentripplanner/IntegrationTest.java | 13 ++++++------- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index ebb16b8..f1cd021 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,11 @@ jackson-databind ${jackson.version} + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + ${jackson.version} + org.apache.httpcomponents.client5 httpclient5 diff --git a/src/main/java/org/opentripplanner/client/model/Route.java b/src/main/java/org/opentripplanner/client/model/Route.java index 9df9fb4..143fd8b 100644 --- a/src/main/java/org/opentripplanner/client/model/Route.java +++ b/src/main/java/org/opentripplanner/client/model/Route.java @@ -1,3 +1,15 @@ package org.opentripplanner.client.model; -public record Route(String shortName, TransitMode mode) {} +import java.util.Optional; + +public record Route(Optional shortName, Optional longName, TransitMode mode) { + + /** + * Either the short name (if it has one) or the long name. + * + *

Either is optional but one must exist. + */ + public String name() { + return shortName.or(() -> longName).orElseThrow(); + } +} diff --git a/src/main/java/org/opentripplanner/client/serialization/ObjectMappers.java b/src/main/java/org/opentripplanner/client/serialization/ObjectMappers.java index 6fc2eb9..efbfdc3 100644 --- a/src/main/java/org/opentripplanner/client/serialization/ObjectMappers.java +++ b/src/main/java/org/opentripplanner/client/serialization/ObjectMappers.java @@ -3,18 +3,19 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import java.time.Duration; import java.time.OffsetDateTime; import java.time.ZoneId; public class ObjectMappers { public static ObjectMapper withTimezone(ZoneId timezone) { - var mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); SimpleModule module = new SimpleModule(); module.addDeserializer(OffsetDateTime.class, new OffsetDateTimeSerializer(timezone)); module.addDeserializer(Duration.class, new DurationSerializer()); - mapper.registerModule(module); - return mapper; + return new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .registerModule(module) + .registerModule(new Jdk8Module()); } } diff --git a/src/main/resources/queries/routes.graphql b/src/main/resources/queries/routes.graphql index 933735f..096a953 100644 --- a/src/main/resources/queries/routes.graphql +++ b/src/main/resources/queries/routes.graphql @@ -1,5 +1,6 @@ query { routes { + longName shortName mode agency { diff --git a/src/test/java/org/opentripplanner/IntegrationTest.java b/src/test/java/org/opentripplanner/IntegrationTest.java index 29c8207..3207175 100644 --- a/src/test/java/org/opentripplanner/IntegrationTest.java +++ b/src/test/java/org/opentripplanner/IntegrationTest.java @@ -86,7 +86,7 @@ public void bikeRoute() throws IOException { } @Test - public void rentalStations() throws IOException, InterruptedException { + public void rentalStations() throws IOException { var result = client.vehicleRentalStations(); @@ -97,12 +97,11 @@ public void rentalStations() throws IOException, InterruptedException { @Test public void routes() throws IOException { - - var result = client.routes(); - - LOG.info("Received {} routes", result.size()); - - assertFalse(result.isEmpty()); + var routes = client.routes(); + LOG.info("Received {} routes", routes.size()); + + assertFalse(routes.isEmpty()); + routes.forEach(r -> assertFalse(r.name().isEmpty(), "Route %s has no name.".formatted(r))); } @Test