Skip to content

Commit 44fe92c

Browse files
Ensure valid values for transferCount
1 parent 333f453 commit 44fe92c

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

application/src/ext/java/org/opentripplanner/ext/fares/model/FareTransferRule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.annotation.Nullable;
99
import org.opentripplanner.core.model.id.FeedScopedId;
1010
import org.opentripplanner.model.fare.FareProduct;
11+
import org.opentripplanner.utils.lang.IntUtils;
1112
import org.opentripplanner.utils.tostring.ToStringBuilder;
1213

1314
public final class FareTransferRule implements Serializable {
@@ -33,7 +34,11 @@ public final class FareTransferRule implements Serializable {
3334
this.fareProducts = List.copyOf(b.fareProducts());
3435
this.fromLegGroup = b.fromLegGroup();
3536
this.toLegGroup = b.toLegGroup();
36-
this.transferCount = b.transferCount();
37+
this.transferCount = IntUtils.requireInRange(
38+
b.transferCount(),
39+
UNLIMITED_TRANSFERS,
40+
Integer.MAX_VALUE
41+
);
3742
this.timeLimit = b.timeLimit();
3843
}
3944

application/src/main/java/org/opentripplanner/gtfs/mapping/FareTransferRuleMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class FareTransferRuleMapper {
1414

15-
public final int MISSING_VALUE = -999;
16-
1715
private final IdFactory idFactory;
1816
private final FareProductMapper fareProductMapper;
1917

@@ -36,9 +34,11 @@ private FareTransferRule doMap(org.onebusaway.gtfs.model.FareTransferRule rhs) {
3634
.withId(idFactory.createId(rhs.getId(), "fare transfer rule"))
3735
.withFromLegGroup(idFactory.createNullableId(rhs.getFromLegGroupId()))
3836
.withToLegGroup(idFactory.createNullableId(rhs.getToLegGroupId()))
39-
.withTransferCount(rhs.getTransferCount())
4037
.withFareProducts(products);
41-
if (rhs.getDurationLimit() != MISSING_VALUE) {
38+
if (rhs.isTransferCountSet()) {
39+
builder.withTransferCount(rhs.getTransferCount());
40+
}
41+
if (rhs.isDurationLimitSet()) {
4242
var duration = Duration.ofSeconds(rhs.getDurationLimit());
4343
var type = mapLimitType(rhs.getDurationLimitType());
4444
builder.withTimeLimit(type, duration);

application/src/test/java/org/opentripplanner/gtfs/mapping/FareTransferRuleMapperTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,37 @@ void ruleWithoutProductIsFree() {
9999
assertTrue(transferRule.isFree());
100100
}
101101

102+
@Test
103+
void defaultTransferCount() {
104+
var rule = new FareTransferRule();
105+
rule.setFromLegGroupId(groupId1);
106+
rule.setToLegGroupId(groupId2);
107+
108+
var transferRule = map(fareProduct(), rule);
109+
assertTrue(transferRule.unlimitedTransfers());
110+
}
111+
112+
@Test
113+
void transferCount() {
114+
var rule = new FareTransferRule();
115+
rule.setFromLegGroupId(groupId1);
116+
rule.setToLegGroupId(groupId2);
117+
rule.setTransferCount(2);
118+
119+
var transferRule = map(fareProduct(), rule);
120+
assertFalse(transferRule.unlimitedTransfers());
121+
}
122+
123+
@Test
124+
void invalidTransferCount() {
125+
var rule = new FareTransferRule();
126+
rule.setFromLegGroupId(groupId1);
127+
rule.setToLegGroupId(groupId2);
128+
rule.setTransferCount(-2);
129+
130+
assertThrows(IllegalArgumentException.class, () -> map(fareProduct(), rule));
131+
}
132+
102133
private FareProduct fareProduct() {
103134
var fareProduct = new FareProduct();
104135
fareProduct.setId(id);

0 commit comments

Comments
 (0)