Skip to content

Commit

Permalink
feat: flex - added forbidden_same_day_booking_field_value notice (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cka-y authored Oct 1, 2024
1 parent cf33356 commit ed02ed1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
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();
}
}

0 comments on commit ed02ed1

Please sign in to comment.