|
| 1 | +package org.opentripplanner.ext.fares.impl.gtfs; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.opentripplanner.ext.fares.model.FareTransferRule.UNLIMITED_TRANSFERS; |
| 5 | +import static org.opentripplanner.ext.fares.model.TimeLimitType.DEPARTURE_TO_ARRIVAL; |
| 6 | +import static org.opentripplanner.model.plan.TestItineraryBuilder.newItinerary; |
| 7 | +import static org.opentripplanner.transit.model._data.TimetableRepositoryForTest.id; |
| 8 | +import static org.opentripplanner.utils.time.TimeUtils.time; |
| 9 | + |
| 10 | +import com.google.common.collect.ImmutableMultimap; |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.List; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.opentripplanner.ext.fares.impl._support.FareTestConstants; |
| 15 | +import org.opentripplanner.ext.fares.model.FareLegRule; |
| 16 | +import org.opentripplanner.ext.fares.model.FareTransferRule; |
| 17 | +import org.opentripplanner.model.fare.FareOffer; |
| 18 | +import org.opentripplanner.model.plan.PlanTestConstants; |
| 19 | +import org.opentripplanner.transit.model.framework.FeedScopedId; |
| 20 | + |
| 21 | +class DepartureToArrivalTimeLimitTest implements PlanTestConstants, FareTestConstants { |
| 22 | + |
| 23 | + private static final FeedScopedId LEG_GROUP = id("leg-group-a"); |
| 24 | + |
| 25 | + private static final GtfsFaresV2Service SERVICE = new GtfsFaresV2Service( |
| 26 | + List.of(FareLegRule.of(id("r1"), FARE_PRODUCT_A).withLegGroupId(LEG_GROUP).build()), |
| 27 | + List.of( |
| 28 | + FareTransferRule.of() |
| 29 | + .withId(id("transfer")) |
| 30 | + .withFromLegGroup(LEG_GROUP) |
| 31 | + .withToLegGroup(LEG_GROUP) |
| 32 | + .withTransferCount(UNLIMITED_TRANSFERS) |
| 33 | + .withFareProducts(List.of(TRANSFER_1)) |
| 34 | + .withTimeLimit(DEPARTURE_TO_ARRIVAL, Duration.ofMinutes(10)) |
| 35 | + .build() |
| 36 | + ), |
| 37 | + ImmutableMultimap.of() |
| 38 | + ); |
| 39 | + |
| 40 | + @Test |
| 41 | + void twoLegsAboveLimit() { |
| 42 | + var i1 = newItinerary(A, time("10:00")) |
| 43 | + .bus(1, time("10:00"), time("10:11"), B) |
| 44 | + .bus(2, time("10:12"), time("10:22"), C) |
| 45 | + .build(); |
| 46 | + |
| 47 | + var result = SERVICE.calculateFares(i1); |
| 48 | + |
| 49 | + assertThat(result.itineraryProducts()).isEmpty(); |
| 50 | + |
| 51 | + var first = i1.legs().getFirst(); |
| 52 | + var last = i1.legs().getLast(); |
| 53 | + assertThat(result.offersForLeg(first)).containsExactly( |
| 54 | + FareOffer.of(first.startTime(), FARE_PRODUCT_A) |
| 55 | + ); |
| 56 | + assertThat(result.offersForLeg(last)).containsExactly( |
| 57 | + FareOffer.of(last.startTime(), FARE_PRODUCT_A) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void twoLegsBelowLimit() { |
| 63 | + var i1 = newItinerary(A, time("10:00")) |
| 64 | + .bus(1, time("10:00"), time("10:03"), B) |
| 65 | + .bus(2, time("10:03"), time("10:06"), C) |
| 66 | + .build(); |
| 67 | + |
| 68 | + var result = SERVICE.calculateFares(i1); |
| 69 | + |
| 70 | + assertThat(result.itineraryProducts()).isEmpty(); |
| 71 | + |
| 72 | + var first = i1.legs().getFirst(); |
| 73 | + var last = i1.legs().getLast(); |
| 74 | + assertThat(result.offersForLeg(first)).containsExactly( |
| 75 | + FareOffer.of(first.startTime(), FARE_PRODUCT_A) |
| 76 | + ); |
| 77 | + assertThat(result.offersForLeg(last)).containsExactly( |
| 78 | + FareOffer.of(last.startTime(), FARE_PRODUCT_A), |
| 79 | + FareOffer.of(first.startTime(), TRANSFER_1, List.of(FARE_PRODUCT_A)) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void threeLegs() { |
| 85 | + var i1 = newItinerary(A, time("10:00")) |
| 86 | + .bus(1, time("10:00"), time("10:03"), B) |
| 87 | + .bus(2, time("10:03"), time("10:06"), C) |
| 88 | + .bus(2, time("10:09"), time("10:15"), D) |
| 89 | + .build(); |
| 90 | + |
| 91 | + var result = SERVICE.calculateFares(i1); |
| 92 | + |
| 93 | + assertThat(result.itineraryProducts()).isEmpty(); |
| 94 | + |
| 95 | + var first = i1.legs().getFirst(); |
| 96 | + var second = i1.legs().get(1); |
| 97 | + var last = i1.legs().getLast(); |
| 98 | + assertThat(result.offersForLeg(first)).containsExactly( |
| 99 | + FareOffer.of(first.startTime(), FARE_PRODUCT_A) |
| 100 | + ); |
| 101 | + assertThat(result.offersForLeg(second)).containsExactly( |
| 102 | + FareOffer.of(second.startTime(), FARE_PRODUCT_A), |
| 103 | + FareOffer.of(first.startTime(), TRANSFER_1, List.of(FARE_PRODUCT_A)) |
| 104 | + ); |
| 105 | + assertThat(result.offersForLeg(last)).containsExactly( |
| 106 | + FareOffer.of(last.startTime(), FARE_PRODUCT_A) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void fourLegs() { |
| 112 | + var i1 = newItinerary(A, time("10:00")) |
| 113 | + .bus(1, time("10:00"), time("10:03"), B) |
| 114 | + .bus(2, time("10:03"), time("10:06"), C) |
| 115 | + .bus(3, time("10:09"), time("10:15"), D) |
| 116 | + .bus(4, time("10:15"), time("10:17"), E) |
| 117 | + .build(); |
| 118 | + |
| 119 | + var result = SERVICE.calculateFares(i1); |
| 120 | + |
| 121 | + assertThat(result.itineraryProducts()).isEmpty(); |
| 122 | + |
| 123 | + var first = i1.legs().getFirst(); |
| 124 | + var second = i1.legs().get(1); |
| 125 | + var third = i1.legs().get(2); |
| 126 | + var last = i1.legs().getLast(); |
| 127 | + assertThat(result.offersForLeg(first)).containsExactly( |
| 128 | + FareOffer.of(first.startTime(), FARE_PRODUCT_A) |
| 129 | + ); |
| 130 | + assertThat(result.offersForLeg(second)).containsExactly( |
| 131 | + FareOffer.of(second.startTime(), FARE_PRODUCT_A), |
| 132 | + FareOffer.of(first.startTime(), TRANSFER_1, List.of(FARE_PRODUCT_A)) |
| 133 | + ); |
| 134 | + assertThat(result.offersForLeg(third)).containsExactly( |
| 135 | + FareOffer.of(third.startTime(), FARE_PRODUCT_A) |
| 136 | + ); |
| 137 | + assertThat(result.offersForLeg(last)).containsExactly( |
| 138 | + FareOffer.of(last.startTime(), FARE_PRODUCT_A), |
| 139 | + FareOffer.of(third.startTime(), TRANSFER_1, List.of(FARE_PRODUCT_A)) |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
0 commit comments