Skip to content

Commit c9ae45c

Browse files
authored
feat: added missing_pickup_booking_rule_id notice (#1966)
1 parent 7146461 commit c9ae45c

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.mobilitydata.gtfsvalidator.validator;
2+
3+
import javax.annotation.Nullable;
4+
import javax.inject.Inject;
5+
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;
6+
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.FileRefs;
7+
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator;
8+
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
9+
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel;
10+
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
11+
import org.mobilitydata.gtfsvalidator.table.*;
12+
13+
@GtfsValidator
14+
public class PickupBookingRuleIdValidator extends FileValidator {
15+
private final GtfsStopTimeTableContainer stopTimeTable;
16+
private final GtfsBookingRulesTableContainer bookingRulesTable;
17+
18+
@Inject
19+
public PickupBookingRuleIdValidator(
20+
GtfsStopTimeTableContainer stopTimeTable, GtfsBookingRulesTableContainer bookingRulesTable) {
21+
this.stopTimeTable = stopTimeTable;
22+
this.bookingRulesTable = bookingRulesTable;
23+
}
24+
25+
public void validate(GtfsStopTime entity, NoticeContainer noticeContainer) {
26+
if (entity.hasPickupType()
27+
&& entity.pickupType() == GtfsPickupDropOff.MUST_PHONE
28+
&& !entity.hasPickupBookingRuleId()) {
29+
noticeContainer.addValidationNotice(
30+
new MissingPickupDropOffBookingRuleIdNotice(
31+
entity.csvRowNumber(),
32+
entity.pickupType(),
33+
entity.hasDropOffType() ? entity.dropOffType() : null));
34+
}
35+
if (entity.hasDropOffType()
36+
&& entity.dropOffType() == GtfsPickupDropOff.MUST_PHONE
37+
&& !entity.hasDropOffBookingRuleId()) {
38+
noticeContainer.addValidationNotice(
39+
new MissingPickupDropOffBookingRuleIdNotice(
40+
entity.csvRowNumber(),
41+
entity.hasPickupType() ? entity.pickupType() : null,
42+
entity.dropOffType()));
43+
}
44+
}
45+
46+
@Override
47+
public void validate(NoticeContainer noticeContainer) {
48+
for (GtfsStopTime stopTime : stopTimeTable.getEntities()) {
49+
validate(stopTime, noticeContainer);
50+
}
51+
}
52+
53+
@Override
54+
public boolean shouldCallValidate() {
55+
return bookingRulesTable != null
56+
&& !bookingRulesTable.isMissingFile()
57+
&& stopTimeTable != null
58+
&& !stopTimeTable.isMissingFile()
59+
&& (stopTimeTable.hasColumn(GtfsStopTime.PICKUP_TYPE_FIELD_NAME)
60+
|| stopTimeTable.hasColumn(GtfsStopTime.DROP_OFF_TYPE_FIELD_NAME));
61+
}
62+
63+
/**
64+
* `pickup_booking_rule_id` is recommended when `pickup_type=2` and `drop_off_booking_rule_id` is
65+
* recommended when `drop_off_type=2`
66+
*/
67+
@GtfsValidationNotice(
68+
severity = SeverityLevel.WARNING,
69+
files = @FileRefs(GtfsStopTimeSchema.class))
70+
static class MissingPickupDropOffBookingRuleIdNotice extends ValidationNotice {
71+
/** The row number of the faulty record in `stop_times.txt` */
72+
private final int csvRowNumber;
73+
74+
/** The pickup type of the faulty record. */
75+
@Nullable private final GtfsPickupDropOff pickupType;
76+
77+
/** The drop-off type of the faulty record. */
78+
@Nullable private final GtfsPickupDropOff dropOffType;
79+
80+
public MissingPickupDropOffBookingRuleIdNotice(
81+
int csvRowNumber, GtfsPickupDropOff pickupType, GtfsPickupDropOff dropOffType) {
82+
this.csvRowNumber = csvRowNumber;
83+
this.pickupType = pickupType;
84+
this.dropOffType = dropOffType;
85+
}
86+
}
87+
}

main/src/test/java/org/mobilitydata/gtfsvalidator/validator/NoticeFieldsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void testNoticeClassFieldNames() {
7979
"departureTime",
8080
"departureTime1",
8181
"distanceKm",
82+
"dropOffType",
8283
"endFieldName",
8384
"endPickupDropOffWindow",
8485
"endPickupDropOffWindow1",
@@ -151,6 +152,7 @@ public void testNoticeClassFieldNames() {
151152
"parsedContent",
152153
"pathwayId",
153154
"pathwayMode",
155+
"pickupType",
154156
"prevCsvRowNumber",
155157
"prevEndTime",
156158
"prevShapeDistTraveled",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.mobilitydata.gtfsvalidator.validator;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import java.util.List;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.JUnit4;
9+
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
10+
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
11+
import org.mobilitydata.gtfsvalidator.table.GtfsPickupDropOff;
12+
import org.mobilitydata.gtfsvalidator.table.GtfsStopTime;
13+
14+
@RunWith(JUnit4.class)
15+
public class PickupBookingRuleIdValidatorTest {
16+
static PickupBookingRuleIdValidator validator = new PickupBookingRuleIdValidator(null, null);
17+
18+
private static List<ValidationNotice> generateNotices(GtfsStopTime stopTime) {
19+
NoticeContainer noticeContainer = new NoticeContainer();
20+
validator.validate(stopTime, noticeContainer);
21+
return noticeContainer.getValidationNotices();
22+
}
23+
24+
@Test
25+
public void missingBookingRuleIdShouldGenerateNotice() {
26+
GtfsStopTime stopTime =
27+
new GtfsStopTime.Builder()
28+
.setCsvRowNumber(1)
29+
.setPickupType(GtfsPickupDropOff.MUST_PHONE)
30+
.build();
31+
assertThat(generateNotices(stopTime))
32+
.containsExactly(
33+
new PickupBookingRuleIdValidator.MissingPickupDropOffBookingRuleIdNotice(
34+
1, GtfsPickupDropOff.MUST_PHONE, null));
35+
}
36+
37+
@Test
38+
public void existingBookingRuleIdShouldNotGenerateNotice() {
39+
GtfsStopTime stopTime =
40+
new GtfsStopTime.Builder()
41+
.setCsvRowNumber(2)
42+
.setPickupType(GtfsPickupDropOff.MUST_PHONE)
43+
.setPickupBookingRuleId("bookingRuleId")
44+
.build();
45+
assertThat(generateNotices(stopTime)).isEmpty();
46+
}
47+
48+
@Test
49+
public void pickUpTypeNotMustPhoneShouldNotGenerateNotice() {
50+
GtfsStopTime stopTime =
51+
new GtfsStopTime.Builder()
52+
.setCsvRowNumber(3)
53+
.setPickupType(GtfsPickupDropOff.NOT_AVAILABLE)
54+
.build();
55+
assertThat(generateNotices(stopTime)).isEmpty();
56+
}
57+
}

0 commit comments

Comments
 (0)