Skip to content

Commit 6b89b6f

Browse files
Merge pull request opentripplanner#7407 from Skanetrafiken/relax-visit-number-validation
Relax the visit number validation logic
2 parents 6b745da + d97a7a7 commit 6b89b6f

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

application/src/main/java/org/opentripplanner/updater/trip/siri/CallWrapper.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
public interface CallWrapper {
2828
/**
2929
* Parse and validate all calls from an {@link EstimatedVehicleJourney}. Each call must have a
30-
* non-empty stop point ref and exactly one of Order or VisitNumber. All calls must use the same
31-
* strategy (all Order or all VisitNumber). The returned list is sorted by sort order.
30+
* non-empty stop point ref and at least one of Order or VisitNumber (Order is preferred when both
31+
* are present). All calls must use the same strategy (all Order or all VisitNumber). The returned
32+
* list is sorted by sort order.
3233
*
3334
* @return a successful sorted list of calls, or a failure with the appropriate error type
3435
*/
3536
static List<CallWrapper> of(EstimatedVehicleJourney estimatedVehicleJourney)
3637
throws UpdateException {
3738
List<CallWrapper> result = new ArrayList<>();
38-
boolean hasOrderCalls = false;
39-
boolean hasVisitNumberCalls = false;
39+
boolean hasCallWithOrder = false;
40+
boolean hasCallWithoutOrder = false;
4041

4142
if (estimatedVehicleJourney.getRecordedCalls() != null) {
4243
for (var call : estimatedVehicleJourney.getRecordedCalls().getRecordedCalls()) {
@@ -45,8 +46,9 @@ static List<CallWrapper> of(EstimatedVehicleJourney estimatedVehicleJourney)
4546
call.getOrder(),
4647
call.getVisitNumber()
4748
);
48-
hasOrderCalls |= call.getOrder() != null;
49-
hasVisitNumberCalls |= call.getVisitNumber() != null;
49+
var hasOrder = call.getOrder() != null;
50+
hasCallWithOrder |= hasOrder;
51+
hasCallWithoutOrder |= !hasOrder;
5052
result.add(new RecordedCallWrapper(call, sortOrder));
5153
}
5254
}
@@ -58,16 +60,14 @@ static List<CallWrapper> of(EstimatedVehicleJourney estimatedVehicleJourney)
5860
call.getOrder(),
5961
call.getVisitNumber()
6062
);
61-
hasOrderCalls |= call.getOrder() != null;
62-
hasVisitNumberCalls |= call.getVisitNumber() != null;
63+
var hasOrder = call.getOrder() != null;
64+
hasCallWithOrder |= hasOrder;
65+
hasCallWithoutOrder |= !hasOrder;
6366
result.add(new EstimatedCallWrapper(call, sortOrder));
6467
}
6568
}
6669

67-
// we reject messages that contain both Order and VisitNumber since we do not see any obvious
68-
// use case that requires both, and making them mutually exclusive make the implementation
69-
// simpler. We can relax this validation rule later if valid use cases are identified.
70-
if (hasOrderCalls && hasVisitNumberCalls) {
70+
if (hasCallWithOrder && hasCallWithoutOrder) {
7171
throw UpdateException.of(UpdateErrorType.MIXED_CALL_ORDER_AND_VISIT_NUMBER);
7272
}
7373

@@ -90,9 +90,6 @@ private static int validateCall(
9090
if (order == null && visitNumber == null) {
9191
throw UpdateException.of(UpdateErrorType.MISSING_CALL_ORDER);
9292
}
93-
if (order != null && visitNumber != null) {
94-
throw UpdateException.of(UpdateErrorType.MIXED_CALL_ORDER_AND_VISIT_NUMBER);
95-
}
9693
return order != null ? order.intValueExact() : visitNumber.intValueExact();
9794
}
9895

application/src/test/java/org/opentripplanner/updater/trip/siri/CallWrapperParsingTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ void rejectMissingOrderAndVisitNumber() {
5858
}
5959

6060
@Test
61-
void rejectBothOrderAndVisitNumber() {
61+
void preferOrderBeforeVisitNumber() {
6262
var journey = new EstimatedVehicleJourney();
6363
var estimatedCalls = new EstimatedVehicleJourney.EstimatedCalls();
64-
estimatedCalls.getEstimatedCalls().add(estimatedCall("STOP_A", 1, 1));
64+
estimatedCalls.getEstimatedCalls().add(estimatedCall("STOP_A", 1, 3));
65+
estimatedCalls.getEstimatedCalls().add(estimatedCall("STOP_B", 2, 6));
6566
journey.setEstimatedCalls(estimatedCalls);
6667

67-
assertFailure(UpdateErrorType.MIXED_CALL_ORDER_AND_VISIT_NUMBER, () -> CallWrapper.of(journey));
68+
var calls = CallWrapper.of(journey);
69+
70+
assertEquals(2, calls.size());
71+
assertEquals(1, calls.get(0).getSortOrder());
72+
assertEquals(2, calls.get(1).getSortOrder());
6873
}
6974

7075
@Test

application/src/test/java/org/opentripplanner/updater/trip/siri/moduletests/rejection/MissingCallOrderTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.opentripplanner.updater.trip.siri.moduletests.rejection;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.opentripplanner.updater.spi.UpdateErrorType.MISSING_CALL_ORDER;
45
import static org.opentripplanner.updater.spi.UpdateErrorType.MIXED_CALL_ORDER_AND_VISIT_NUMBER;
56
import static org.opentripplanner.updater.spi.UpdateResultAssertions.assertFailure;
@@ -107,7 +108,7 @@ void acceptVisitNumberAsFallback() {
107108
}
108109

109110
@Test
110-
void rejectMixedOrderAndVisitNumber() {
111+
void preferOrderToVisitNumber() {
111112
var env = ENV_BUILDER.addTrip(TRIP_INPUT).build();
112113
var siri = SiriTestHelper.of(env);
113114

@@ -120,14 +121,20 @@ void rejectMixedOrderAndVisitNumber() {
120121
.withVisitNumber(1)
121122
.departAimedExpected("00:00:11", "00:00:15")
122123
.call(STOP_B)
124+
.withVisitNumber(3)
123125
.departAimedExpected("00:00:21", "00:00:25")
124126
.call(STOP_C)
127+
.withVisitNumber(2)
125128
.arriveAimedExpected("00:00:40", "00:00:45")
126129
)
127130
.buildEstimatedTimetableDeliveries();
128131

129132
var result = siri.applyEstimatedTimetable(updates);
130-
assertFailure(MIXED_CALL_ORDER_AND_VISIT_NUMBER, result);
133+
assertSuccess(result);
134+
assertEquals(
135+
"UPDATED | A 0:00:15 0:00:15 | B 0:00:20 0:00:25 | C 0:00:45 0:00:45",
136+
env.tripData(TRIP_1_ID).showTimetable()
137+
);
131138
}
132139

133140
@Test

0 commit comments

Comments
 (0)