Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev-2.x' into upstream-merge-2…
Browse files Browse the repository at this point in the history
…024-12-02
  • Loading branch information
leonardehrenfried committed Dec 2, 2024
2 parents e129338 + c7fe2f2 commit e3315aa
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private static ApiBookingInfo mapBookingInfo(BookingInfo info, boolean isPickup)
BookingMethodMapper.mapBookingMethods(info.bookingMethods()),
BookingTimeMapper.mapBookingTime(info.getEarliestBookingTime()),
BookingTimeMapper.mapBookingTime(info.getLatestBookingTime()),
info.getMinimumBookingNotice(),
info.getMaximumBookingNotice(),
info.getMinimumBookingNotice().orElse(null),
info.getMaximumBookingNotice().orElse(null),
info.getMessage(),
isPickup ? info.getPickupMessage() : null,
!isPickup ? info.getDropOffMessage() : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.time.Duration;
import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers;
import org.opentripplanner.transit.model.organization.ContactInfo;
import org.opentripplanner.transit.model.timetable.booking.BookingInfo;
Expand Down Expand Up @@ -31,7 +32,8 @@ public DataFetcher<BookingTime> latestBookingTime() {

@Override
public DataFetcher<Long> maximumBookingNoticeSeconds() {
return environment -> getSource(environment).getMaximumBookingNotice().toSeconds();
return environment ->
getSource(environment).getMaximumBookingNotice().map(Duration::toSeconds).orElse(null);
}

@Override
Expand All @@ -41,7 +43,8 @@ public DataFetcher<String> message() {

@Override
public DataFetcher<Long> minimumBookingNoticeSeconds() {
return environment -> getSource(environment).getMinimumBookingNotice().toSeconds();
return environment ->
getSource(environment).getMinimumBookingNotice().map(Duration::toSeconds).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;
import java.time.Duration;
import java.util.EnumSet;
import java.util.Optional;
import javax.annotation.Nullable;
import org.opentripplanner.transit.model.organization.ContactInfo;
import org.opentripplanner.utils.tostring.ToStringBuilder;
Expand Down Expand Up @@ -99,14 +100,12 @@ public BookingTime getLatestBookingTime() {
return latestBookingTime;
}

@Nullable
public Duration getMinimumBookingNotice() {
return minimumBookingNotice;
public Optional<Duration> getMinimumBookingNotice() {
return Optional.ofNullable(minimumBookingNotice);
}

@Nullable
public Duration getMaximumBookingNotice() {
return maximumBookingNotice;
public Optional<Duration> getMaximumBookingNotice() {
return Optional.ofNullable(maximumBookingNotice);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ Builder withBookingInfo(@Nullable BookingInfo bookingInfo) {
return this;
}
withLatestBookingTime(bookingInfo.getLatestBookingTime());
withMinimumBookingNotice(bookingInfo.getMinimumBookingNotice());
withMinimumBookingNotice(bookingInfo.getMinimumBookingNotice().orElse(null));
return this;
}

public Builder withLatestBookingTime(BookingTime latestBookingTime) {
public Builder withLatestBookingTime(@Nullable BookingTime latestBookingTime) {
this.latestBookingTime =
latestBookingTime == null ? NOT_SET : latestBookingTime.relativeTimeSeconds();
return this;
}

public Builder withMinimumBookingNotice(Duration minimumBookingNotice) {
public Builder withMinimumBookingNotice(@Nullable Duration minimumBookingNotice) {
this.minimumBookingNotice =
minimumBookingNotice == null ? NOT_SET : (int) minimumBookingNotice.toSeconds();
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.opentripplanner.apis.gtfs.datafetchers;

import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import graphql.execution.ExecutionId;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model.timetable.booking.BookingInfo;

class BookingInfoImplTest {

private static final BookingInfoImpl SUBJECT = new BookingInfoImpl();
private static final Duration TEN_MINUTES = Duration.ofMinutes(10);

@Test
void emptyDurations() throws Exception {
var env = dataFetchingEnvironment(BookingInfo.of().build());
assertNull(SUBJECT.minimumBookingNoticeSeconds().get(env));
assertNull(SUBJECT.maximumBookingNoticeSeconds().get(env));
}

@Test
void durations() throws Exception {
var env = dataFetchingEnvironment(
BookingInfo
.of()
.withMinimumBookingNotice(TEN_MINUTES)
.withMaximumBookingNotice(TEN_MINUTES)
.build()
);
assertEquals(600, SUBJECT.minimumBookingNoticeSeconds().get(env));
assertEquals(600, SUBJECT.maximumBookingNoticeSeconds().get(env));
}

private DataFetchingEnvironment dataFetchingEnvironment(BookingInfo bookingInfo) {
var executionContext = newExecutionContextBuilder()
.executionId(ExecutionId.from(this.getClass().getName()))
.build();
return DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.source(bookingInfo)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,6 @@ void testMapMinimumBookingNotice() {

BookingInfo bookingInfo = subject.map(stopPoint, null, null);

assertEquals(THIRTY_MINUTES, bookingInfo.getMinimumBookingNotice());
assertEquals(THIRTY_MINUTES, bookingInfo.getMinimumBookingNotice().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void testBookingInfoWithMinBookingNotice() {
.build();

assertNull(subject.getLatestBookingTime());
assertEquals(minimumBookingNotice, subject.getMinimumBookingNotice());
assertEquals(minimumBookingNotice, subject.getMinimumBookingNotice().get());

assertEquals(
"BookingInfo{bookingMethods: [CALL_DRIVER], minimumBookingNotice: 45m}",
Expand Down

0 comments on commit e3315aa

Please sign in to comment.