Skip to content

Commit

Permalink
Make GtfsTableDescriptor the canonical source for whether a file is r…
Browse files Browse the repository at this point in the history
…equired or not, removing duplicate methods in GtfsTableContainer. Additionally, make "required" a mutable field in GtfsTableDescriptor to support dynamic configuration of which files are required and which aren't. (#1566)

Co-authored-by: David Gamez <[email protected]>
  • Loading branch information
bdferris-v2 and davidgamez authored Sep 12, 2023
1 parent 6398021 commit 00888ba
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public Collection<GtfsTableContainer<?>> getTables() {
public String tableTotalsText() {
List<String> totalList = new ArrayList<>();
for (GtfsTableContainer<?> table : tables.values()) {
if (table.getTableStatus() == TableStatus.MISSING_FILE && !table.isRequired()) continue;
if (table.getTableStatus() == TableStatus.MISSING_FILE
&& !table.getDescriptor().isRequired()) {
continue;
}
totalList.add(
table.gtfsFilename()
+ "\t"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@
*/
public abstract class GtfsTableContainer<T extends GtfsEntity> {

private final GtfsTableDescriptor<T> descriptor;

private final TableStatus tableStatus;

private final CsvHeader header;

public GtfsTableContainer(TableStatus tableStatus, CsvHeader header) {
public GtfsTableContainer(
GtfsTableDescriptor<T> descriptor, TableStatus tableStatus, CsvHeader header) {
this.descriptor = descriptor;
this.tableStatus = tableStatus;
this.header = header;
}

public GtfsTableDescriptor<T> getDescriptor() {
return descriptor;
}

public TableStatus getTableStatus() {
return tableStatus;
}
Expand Down Expand Up @@ -96,24 +104,6 @@ public boolean isMissingFile() {
return tableStatus == TableStatus.MISSING_FILE;
}

/**
* Tells if the file is recommended according to GTFS.
*
* <p>Note that a recommended file may be empty.
*
* @return true if the file is recommended, false otherwise
*/
public abstract boolean isRecommended();

/**
* Tells if the file is required according to GTFS.
*
* <p>Note that a required file may be empty.
*
* @return true if the file is required, false otherwise
*/
public abstract boolean isRequired();

/**
* Tells if the file was successfully parsed.
*
Expand All @@ -135,7 +125,7 @@ public boolean isParsedSuccessfully() {
case PARSABLE_HEADERS_AND_ROWS:
return true;
case MISSING_FILE:
return !isRequired();
return !descriptor.isRequired();
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.mobilitydata.gtfsvalidator.parsing.CsvHeader;

public abstract class GtfsTableDescriptor<T extends GtfsEntity> {

// True if the specified file is required in a feed.
private boolean required;

public abstract GtfsTableContainer createContainerForInvalidStatus(
GtfsTableContainer.TableStatus tableStatus);

Expand All @@ -24,7 +28,13 @@ public abstract GtfsTableContainer createContainerForHeaderAndEntities(

public abstract boolean isRecommended();

public abstract boolean isRequired();
public boolean isRequired() {
return this.required;
}

public void setRequired(boolean required) {
this.required = required;
}

public abstract Optional<Integer> maxCharsPerColumn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class GtfsTestTableContainer extends GtfsTableContainer<GtfsTestEntity> {

private List<GtfsTestEntity> entities;

private GtfsTestTableContainer(CsvHeader header, List<GtfsTestEntity> entities) {
super(TableStatus.PARSABLE_HEADERS_AND_ROWS, header);
private GtfsTestTableContainer(
GtfsTestTableDescriptor descriptor, CsvHeader header, List<GtfsTestEntity> entities) {
super(descriptor, TableStatus.PARSABLE_HEADERS_AND_ROWS, header);
this.entities = entities;
}

public GtfsTestTableContainer(TableStatus tableStatus) {
super(tableStatus, CsvHeader.EMPTY);
super(new GtfsTestTableDescriptor(), tableStatus, CsvHeader.EMPTY);
this.entities = new ArrayList<>();
}

Expand All @@ -50,37 +51,21 @@ public String gtfsFilename() {
return GtfsTestEntity.FILENAME;
}

@Override
public boolean isRecommended() {
return false;
}

@Override
public boolean isRequired() {
return true;
}

@Override
public List<GtfsTestEntity> getEntities() {
return entities;
}

/** Creates a table with given header and entities */
public static GtfsTestTableContainer forHeaderAndEntities(
CsvHeader header, List<GtfsTestEntity> entities, NoticeContainer noticeContainer) {
GtfsTestTableContainer table = new GtfsTestTableContainer(header, entities);
GtfsTestTableDescriptor descriptor,
CsvHeader header,
List<GtfsTestEntity> entities,
NoticeContainer noticeContainer) {
GtfsTestTableContainer table = new GtfsTestTableContainer(descriptor, header, entities);
return table;
}

/**
* Creates a table with given entities and empty header. This method is intended to be used in
* tests.
*/
public static GtfsTestTableContainer forEntities(
List<GtfsTestEntity> entities, NoticeContainer noticeContainer) {
return forHeaderAndEntities(CsvHeader.EMPTY, entities, noticeContainer);
}

@Override
public ImmutableList<String> getKeyColumnNames() {
return KEY_COLUMN_NAMES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public GtfsTableContainer createContainerForInvalidStatus(
@Override
public GtfsTableContainer createContainerForHeaderAndEntities(
CsvHeader header, List<GtfsTestEntity> entities, NoticeContainer noticeContainer) {
return GtfsTestTableContainer.forHeaderAndEntities(header, entities, noticeContainer);
return GtfsTestTableContainer.forHeaderAndEntities(this, header, entities, noticeContainer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
import org.mobilitydata.gtfsvalidator.table.*;
import org.mobilitydata.gtfsvalidator.table.GtfsTableContainer.TableStatus;
import org.mobilitydata.gtfsvalidator.type.GtfsDate;
import org.mobilitydata.gtfsvalidator.util.CalendarUtilTest;

Expand Down Expand Up @@ -85,8 +86,8 @@ private List<ValidationNotice> validateSimpleServiceWindow(
ImmutableSet.copyOf(DayOfWeek.values()));
var calendarTable =
GtfsCalendarTableContainer.forEntities(ImmutableList.of(calendar), noticeContainer);
var dateTable = new GtfsCalendarDateTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var frequencyTable = new GtfsFrequencyTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var dateTable = GtfsCalendarDateTableContainer.forStatus(TableStatus.EMPTY_FILE);
var frequencyTable = GtfsFrequencyTableContainer.forStatus(TableStatus.EMPTY_FILE);

var tripBlock = createTripBlock(serviceId, 6, "b1");
var tripContainer = GtfsTripTableContainer.forEntities(tripBlock, noticeContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.table.*;
import org.mobilitydata.gtfsvalidator.table.GtfsTableContainer.TableStatus;
import org.mobilitydata.gtfsvalidator.type.GtfsDate;

@RunWith(JUnit4.class)
Expand Down Expand Up @@ -66,7 +67,7 @@ public void calendarEndDateOneDayAgoShouldGenerateNotice() {
GtfsCalendarTableContainer calendarTable =
GtfsCalendarTableContainer.forEntities(calendars, container);

var dateTable = new GtfsCalendarDateTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var dateTable = GtfsCalendarDateTableContainer.forStatus(TableStatus.EMPTY_FILE);
new ExpiredCalendarValidator(new CurrentDateTime(TEST_NOW), calendarTable, dateTable)
.validate(container);
assertThat(container.getValidationNotices())
Expand All @@ -93,7 +94,7 @@ public void calendarEndDateTodayShouldNotGenerateNotice() {
GtfsCalendarTableContainer calendarTable =
GtfsCalendarTableContainer.forEntities(calendars, container);

var dateTable = new GtfsCalendarDateTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var dateTable = GtfsCalendarDateTableContainer.forStatus(TableStatus.EMPTY_FILE);
new ExpiredCalendarValidator(new CurrentDateTime(TEST_NOW), calendarTable, dateTable)
.validate(container);
assertThat(container.getValidationNotices()).isEmpty();
Expand All @@ -119,7 +120,7 @@ public void calendarEndDateOneDayFromNowShouldNotGenerateNotice() {
GtfsCalendarTableContainer calendarTable =
GtfsCalendarTableContainer.forEntities(calendars, container);

var dateTable = new GtfsCalendarDateTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var dateTable = GtfsCalendarDateTableContainer.forStatus(TableStatus.EMPTY_FILE);
new ExpiredCalendarValidator(new CurrentDateTime(TEST_NOW), calendarTable, dateTable)
.validate(container);
assertThat(container.getValidationNotices()).isEmpty();
Expand Down Expand Up @@ -211,7 +212,7 @@ public void calendarWithNoDaysShouldNotGenerateNotice() {

GtfsCalendarTableContainer calendarTable =
GtfsCalendarTableContainer.forEntities(calendars, container);
var dateTable = new GtfsCalendarDateTableContainer(GtfsTableContainer.TableStatus.EMPTY_FILE);
var dateTable = GtfsCalendarDateTableContainer.forStatus(TableStatus.EMPTY_FILE);
new ExpiredCalendarValidator(new CurrentDateTime(TEST_NOW), calendarTable, dateTable)
.validate(container);
assertThat(container.getValidationNotices()).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public static GtfsFeedInfo createFeedInfo(int csvRowNumber, @Nullable Locale fee
public void noFeedInfoShouldNotGenerateNotice() {
assertThat(
generateNotices(
new GtfsAgencyTableContainer(TableStatus.EMPTY_FILE),
new GtfsFeedInfoTableContainer(TableStatus.EMPTY_FILE)))
GtfsAgencyTableContainer.forStatus(TableStatus.EMPTY_FILE),
GtfsFeedInfoTableContainer.forStatus(TableStatus.EMPTY_FILE)))
.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public void calendarDateOnlyProvidedShouldNotGenerateNotice() {
public void bothMissingFilesShouldGenerateNotice() {
assertThat(
generateNotices(
new GtfsCalendarTableContainer(TableStatus.MISSING_FILE),
new GtfsCalendarDateTableContainer(TableStatus.MISSING_FILE)))
GtfsCalendarTableContainer.forStatus(TableStatus.MISSING_FILE),
GtfsCalendarDateTableContainer.forStatus(TableStatus.MISSING_FILE)))
.containsExactly(new MissingCalendarAndCalendarDateFilesNotice());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.mobilitydata.gtfsvalidator.parsing.CsvHeader;
import org.mobilitydata.gtfsvalidator.table.GtfsStopTime;
import org.mobilitydata.gtfsvalidator.table.GtfsStopTimeTableContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsStopTimeTableDescriptor;
import org.mobilitydata.gtfsvalidator.type.GtfsTime;
import org.mobilitydata.gtfsvalidator.validator.TimepointTimeValidator.MissingTimepointValueNotice;
import org.mobilitydata.gtfsvalidator.validator.TimepointTimeValidator.StopTimeTimepointWithoutTimesNotice;
Expand All @@ -46,7 +47,8 @@ public class TimepointTimeValidatorTest {

private static GtfsStopTimeTableContainer createTable(
CsvHeader header, List<GtfsStopTime> stopTimes, NoticeContainer noticeContainer) {
return GtfsStopTimeTableContainer.forHeaderAndEntities(header, stopTimes, noticeContainer);
return GtfsStopTimeTableContainer.forHeaderAndEntities(
new GtfsStopTimeTableDescriptor(), header, stopTimes, noticeContainer);
}

private static List<ValidationNotice> generateNotices(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.mobilitydata.gtfsvalidator.table.GtfsStopTimeTableContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsTranslation;
import org.mobilitydata.gtfsvalidator.table.GtfsTranslationTableContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsTranslationTableDescriptor;
import org.mobilitydata.gtfsvalidator.validator.TranslationFieldAndReferenceValidator.TranslationForeignKeyViolationNotice;
import org.mobilitydata.gtfsvalidator.validator.TranslationFieldAndReferenceValidator.TranslationUnexpectedValueNotice;
import org.mobilitydata.gtfsvalidator.validator.TranslationFieldAndReferenceValidator.TranslationUnknownTableNameNotice;
Expand Down Expand Up @@ -65,7 +66,7 @@ private static List<ValidationNotice> generateNotices(
NoticeContainer noticeContainer = new NoticeContainer();
GtfsTranslationTableContainer translationTable =
GtfsTranslationTableContainer.forHeaderAndEntities(
translationHeader, translations, noticeContainer);
new GtfsTranslationTableDescriptor(), translationHeader, translations, noticeContainer);
new TranslationFieldAndReferenceValidator(
translationTable,
new GtfsFeedContainer(
Expand Down
Loading

0 comments on commit 00888ba

Please sign in to comment.