forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract mapper for bookWhen, add test
- Loading branch information
1 parent
1030408
commit 5defddc
Showing
3 changed files
with
123 additions
and
28 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
application/src/main/java/org/opentripplanner/apis/transmodel/mapping/BookingInfoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.opentripplanner.apis.transmodel.mapping; | ||
|
||
import org.opentripplanner.transit.model.timetable.booking.BookingInfo; | ||
import org.opentripplanner.transit.model.timetable.booking.BookingTime; | ||
|
||
/** | ||
* Maps the {@link BookingInfo} to enum value (as a string) returned by the API. | ||
*/ | ||
public class BookingInfoMapper { | ||
|
||
public static String mapToBookWhen(BookingInfo bookingInfo) { | ||
if (bookingInfo.getMinimumBookingNotice().isPresent()) { | ||
return null; | ||
} | ||
BookingTime latestBookingTime = bookingInfo.getLatestBookingTime(); | ||
BookingTime earliestBookingTime = bookingInfo.getEarliestBookingTime(); | ||
|
||
// Try to deduce the original enum from stored values | ||
if (earliestBookingTime == null) { | ||
if (latestBookingTime == null) { | ||
return "timeOfTravelOnly"; | ||
} else if (latestBookingTime.getDaysPrior() == 1) { | ||
return "untilPreviousDay"; | ||
} else if (latestBookingTime.getDaysPrior() == 0) { | ||
return "advanceAndDayOfTravel"; | ||
} else { | ||
return "other"; | ||
} | ||
} else if ( | ||
earliestBookingTime.getDaysPrior() == 0 && | ||
(latestBookingTime == null || latestBookingTime.getDaysPrior() == 0) | ||
) { | ||
return "dayOfTravelOnly"; | ||
} else { | ||
return "other"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...tion/src/test/java/org/opentripplanner/apis/transmodel/mapping/BookingInfoMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.opentripplanner.apis.transmodel.mapping; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.opentripplanner.apis.transmodel.mapping.BookingInfoMapper.mapToBookWhen; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalTime; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.opentripplanner.transit.model.timetable.booking.BookingInfo; | ||
import org.opentripplanner.transit.model.timetable.booking.BookingTime; | ||
|
||
class BookingInfoMapperTest { | ||
|
||
private static final Duration TEN_MINUTES = Duration.ofMinutes(10); | ||
private static final BookingTime BOOKING_TIME_ZERO_DAYS_PRIOR = new BookingTime( | ||
LocalTime.of(10, 0), | ||
0 | ||
); | ||
|
||
@Test | ||
void bookingNotice() { | ||
assertNull(mapToBookWhen(BookingInfo.of().withMinimumBookingNotice(TEN_MINUTES).build())); | ||
} | ||
|
||
@Test | ||
void timeOfTravelOnly() { | ||
assertEquals("timeOfTravelOnly", mapToBookWhen(BookingInfo.of().build())); | ||
} | ||
|
||
@Test | ||
void untilPreviousDay() { | ||
var info = daysPrior(1); | ||
assertEquals("untilPreviousDay", mapToBookWhen(info)); | ||
} | ||
|
||
@Test | ||
void advanceAndDayOfTravel() { | ||
var info = daysPrior(0); | ||
assertEquals("advanceAndDayOfTravel", mapToBookWhen(info)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = { 2, 3, 4, 14, 28 }) | ||
void other(int days) { | ||
var info = daysPrior(days); | ||
assertEquals("other", mapToBookWhen(info)); | ||
} | ||
|
||
@Test | ||
void dayOfTravelOnly() { | ||
var info = BookingInfo.of().withEarliestBookingTime(BOOKING_TIME_ZERO_DAYS_PRIOR).build(); | ||
assertEquals("dayOfTravelOnly", mapToBookWhen(info)); | ||
} | ||
|
||
@Test | ||
void foo() { | ||
var info = BookingInfo | ||
.of() | ||
.withEarliestBookingTime(BOOKING_TIME_ZERO_DAYS_PRIOR) | ||
.withLatestBookingTime(BOOKING_TIME_ZERO_DAYS_PRIOR) | ||
.build(); | ||
assertEquals("dayOfTravelOnly", mapToBookWhen(info)); | ||
} | ||
|
||
@Test | ||
void earliestBookingTimeZero() { | ||
var info = BookingInfo | ||
.of() | ||
.withEarliestBookingTime(new BookingTime(LocalTime.of(10, 0), 10)) | ||
.build(); | ||
assertEquals("other", mapToBookWhen(info)); | ||
} | ||
|
||
private static BookingInfo daysPrior(int daysPrior) { | ||
return BookingInfo | ||
.of() | ||
.withLatestBookingTime(new BookingTime(LocalTime.of(10, 0), daysPrior)) | ||
.build(); | ||
} | ||
} |