Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flex - added forbidden_same_day_booking_field_value notice #1847

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class BookingRulesEntityValidator extends SingleEntityValidator<GtfsBooki

@Override
public void validate(GtfsBookingRules entity, NoticeContainer noticeContainer) {
validateForbiddenRealTimeFields(entity, noticeContainer);
validateSameDayFields(entity, noticeContainer);
}

private static void validateForbiddenRealTimeFields(
GtfsBookingRules entity, NoticeContainer noticeContainer) {
// Only validate entities with REALTIME booking type
if (entity.bookingType() != GtfsBookingType.REALTIME) {
return;
Expand All @@ -32,11 +38,44 @@ public void validate(GtfsBookingRules entity, NoticeContainer noticeContainer) {
}
}

private static void validateSameDayFields(
GtfsBookingRules entity, NoticeContainer noticeContainer) {
// Only validate entities with SAME_DAY booking type
if (entity.bookingType() != GtfsBookingType.SAMEDAY) {
return;
}

// Retrieve the list of forbidden fields
List<String> forbiddenFields = findForbiddenSameDayFields(entity);

// If there are any forbidden fields, add a validation notice
if (!forbiddenFields.isEmpty()) {
noticeContainer.addValidationNotice(
new ForbiddenSameDayBookingFieldValueNotice(entity, forbiddenFields));
}
}

private static List<String> findForbiddenSameDayFields(GtfsBookingRules bookingRule) {
List<String> fields = new ArrayList<>();

// Check each forbidden field and add its name to the list if it's present
if (bookingRule.hasPriorNoticeLastDay()) {
fields.add(GtfsBookingRules.PRIOR_NOTICE_LAST_DAY_FIELD_NAME);
}
if (bookingRule.hasPriorNoticeLastTime()) {
fields.add(GtfsBookingRules.PRIOR_NOTICE_LAST_TIME_FIELD_NAME);
}
if (bookingRule.hasPriorNoticeServiceId()) {
fields.add(GtfsBookingRules.PRIOR_NOTICE_SERVICE_ID_FIELD_NAME);
}
return fields;
}

/** Finds forbidden fields that should not be present for real-time booking rules. */
public static List<String> findForbiddenRealTimeFields(GtfsBookingRules bookingRule) {
List<String> fields = new ArrayList<>();

// Check each field and add its name to the list if it's present
// Check each forbidden field and add its name to the list if it's present
if (bookingRule.hasPriorNoticeDurationMin()) {
fields.add(GtfsBookingRules.PRIOR_NOTICE_DURATION_MIN_FIELD_NAME);
}
Expand Down Expand Up @@ -83,4 +122,26 @@ static class ForbiddenRealTimeBookingFieldValueNotice extends ValidationNotice {
this.fieldNames = String.join(", ", forbiddenFields);
}
}

/** A forbidden field value is present for a same-day booking rule in `booking_rules.txt`. */
@GtfsValidationNotice(
severity = SeverityLevel.ERROR,
files = @FileRefs(GtfsBookingRulesSchema.class))
static class ForbiddenSameDayBookingFieldValueNotice extends ValidationNotice {
/** The row number of the faulty record. */
private final int csvRowNumber;

/** The `booking_rules.booking_rule_id` of the faulty record. */
private final String bookingRuleId;

/** The names of the forbidden fields comma-separated. */
private final String fieldNames;

ForbiddenSameDayBookingFieldValueNotice(
GtfsBookingRules bookingRule, List<String> forbiddenFields) {
this.csvRowNumber = bookingRule.csvRowNumber();
this.bookingRuleId = bookingRule.bookingRuleId();
this.fieldNames = String.join(", ", forbiddenFields);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,33 @@ public void realTimeBookingWithMultipleForbiddenFieldsShouldGenerateNotice() {
GtfsBookingRules.PRIOR_NOTICE_START_TIME_FIELD_NAME,
GtfsBookingRules.PRIOR_NOTICE_SERVICE_ID_FIELD_NAME)));
}

@Test
public void sameDayBookingWithForbiddenFieldsShouldGenerateNotice() {
GtfsBookingRules bookingRule =
new GtfsBookingRules.Builder()
.setCsvRowNumber(1)
.setBookingRuleId("rule-5")
.setBookingType(GtfsBookingType.SAMEDAY)
.setPriorNoticeLastDay(2) // Forbidden field
.setPriorNoticeStartTime(GtfsTime.fromSecondsSinceMidnight(5000)) // Forbidden field
.build();

assertThat(generateNotices(bookingRule))
.containsExactly(
new BookingRulesEntityValidator.ForbiddenSameDayBookingFieldValueNotice(
bookingRule, List.of(GtfsBookingRules.PRIOR_NOTICE_LAST_DAY_FIELD_NAME)));
}

@Test
public void sameDayBookingWithoutForbiddenFieldsShouldNotGenerateNotice() {
GtfsBookingRules bookingRule =
new GtfsBookingRules.Builder()
.setCsvRowNumber(1)
.setBookingRuleId("rule-6")
.setBookingType(GtfsBookingType.SAMEDAY)
.build();

assertThat(generateNotices(bookingRule)).isEmpty();
}
}
Loading