|
| 1 | +package org.mobilitydata.gtfsvalidator.validator; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import com.google.common.collect.ImmutableList; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mobilitydata.gtfsvalidator.notice.MissingRecommendedFileNotice; |
| 10 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 11 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 12 | +import org.mobilitydata.gtfsvalidator.table.GtfsFeedContainer; |
| 13 | +import org.mobilitydata.gtfsvalidator.table.GtfsLocationGroups; |
| 14 | +import org.mobilitydata.gtfsvalidator.table.GtfsLocationGroupsTableContainer; |
| 15 | +import org.mobilitydata.gtfsvalidator.table.GtfsShape; |
| 16 | +import org.mobilitydata.gtfsvalidator.table.GtfsShapeTableContainer; |
| 17 | +import org.mobilitydata.gtfsvalidator.table.TableStatus; |
| 18 | + |
| 19 | +public class MissingShapesFileValidatorTest { |
| 20 | + |
| 21 | + private static GtfsFeedContainer createFeedContainer( |
| 22 | + List<GtfsShape> shapes, List<GtfsLocationGroups> locationGroups) { |
| 23 | + NoticeContainer noticeContainer = new NoticeContainer(); |
| 24 | + return new GtfsFeedContainer( |
| 25 | + ImmutableList.of( |
| 26 | + GtfsShapeTableContainer.forEntities(shapes, noticeContainer), |
| 27 | + GtfsLocationGroupsTableContainer.forEntities(locationGroups, noticeContainer))); |
| 28 | + } |
| 29 | + |
| 30 | + private static List<GtfsShape> createShapeTable(int rows) { |
| 31 | + ArrayList<GtfsShape> shapes = new ArrayList<>(); |
| 32 | + for (int i = 0; i < rows; i++) { |
| 33 | + shapes.add(new GtfsShape.Builder().setCsvRowNumber(i + 1).setShapeId("s" + i).build()); |
| 34 | + } |
| 35 | + return shapes; |
| 36 | + } |
| 37 | + |
| 38 | + private static List<GtfsLocationGroups> createLocationGroupsTable( |
| 39 | + int rows, String groupId, String groupName) { |
| 40 | + ArrayList<GtfsLocationGroups> locationGroups = new ArrayList<>(); |
| 41 | + for (int i = 0; i < rows; i++) { |
| 42 | + locationGroups.add( |
| 43 | + new GtfsLocationGroups.Builder() |
| 44 | + .setCsvRowNumber(i + 1) |
| 45 | + .setLocationGroupId(groupId) |
| 46 | + .setLocationGroupName(groupName) |
| 47 | + .build()); |
| 48 | + } |
| 49 | + return locationGroups; |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testShapesFileAndFixedDrtPresent() { |
| 54 | + List<ValidationNotice> notices = |
| 55 | + generateNotices( |
| 56 | + createShapeTable(1), |
| 57 | + createLocationGroupsTable(1, "b", "testgroup"), |
| 58 | + createFeedContainer( |
| 59 | + createShapeTable(1), createLocationGroupsTable(1, "b", "testgroup"))); |
| 60 | + boolean found = |
| 61 | + notices.stream().anyMatch(notice -> notice instanceof MissingRecommendedFileNotice); |
| 62 | + assertThat(found).isFalse(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testShapesFileAndZoneBasedDrtPresent() { |
| 67 | + List<ValidationNotice> notices = |
| 68 | + generateNotices( |
| 69 | + createShapeTable(1), |
| 70 | + createLocationGroupsTable(1, "d", "t3stgroup"), |
| 71 | + createFeedContainer( |
| 72 | + createShapeTable(1), createLocationGroupsTable(1, "d", "t3stgroup"))); |
| 73 | + boolean found = |
| 74 | + notices.stream().anyMatch(notice -> notice instanceof MissingRecommendedFileNotice); |
| 75 | + assertThat(found).isFalse(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testNoShapesFileAndNoDrtPresent() { |
| 80 | + // Create containers where shapes.txt is missing and location_groups is empty |
| 81 | + var shapeContainer = GtfsShapeTableContainer.forStatus(TableStatus.MISSING_FILE); |
| 82 | + var locationGroupsContainer = |
| 83 | + GtfsLocationGroupsTableContainer.forEntities( |
| 84 | + createLocationGroupsTable(0, null, null), new NoticeContainer()); |
| 85 | + GtfsFeedContainer feedContainer = createFeedContainer(shapeContainer, locationGroupsContainer); |
| 86 | + |
| 87 | + List<ValidationNotice> notices = |
| 88 | + generateNotices(shapeContainer, locationGroupsContainer, feedContainer); |
| 89 | + long missingRecommendedFileNoticesCount = |
| 90 | + notices.stream().filter(notice -> notice instanceof MissingRecommendedFileNotice).count(); |
| 91 | + assertThat(missingRecommendedFileNoticesCount).isAtLeast(1); |
| 92 | + } |
| 93 | + |
| 94 | + private static GtfsFeedContainer createFeedContainer( |
| 95 | + GtfsShapeTableContainer shapeContainer, |
| 96 | + GtfsLocationGroupsTableContainer locationGroupsContainer) { |
| 97 | + return new GtfsFeedContainer(ImmutableList.of(shapeContainer, locationGroupsContainer)); |
| 98 | + } |
| 99 | + |
| 100 | + private static List<ValidationNotice> generateNotices( |
| 101 | + GtfsShapeTableContainer shapeTable, |
| 102 | + GtfsLocationGroupsTableContainer locationGroups, |
| 103 | + GtfsFeedContainer feedContainer) { |
| 104 | + NoticeContainer noticeContainer = new NoticeContainer(); |
| 105 | + new MissingShapesFileValidator(shapeTable, locationGroups, feedContainer) |
| 106 | + .validate(noticeContainer); |
| 107 | + return noticeContainer.getValidationNotices(); |
| 108 | + } |
| 109 | + |
| 110 | + private static List<ValidationNotice> generateNotices( |
| 111 | + List<GtfsShape> shapes, |
| 112 | + List<GtfsLocationGroups> locationGroups, |
| 113 | + GtfsFeedContainer feedContainer) { |
| 114 | + NoticeContainer noticeContainer = new NoticeContainer(); |
| 115 | + new MissingShapesFileValidator( |
| 116 | + GtfsShapeTableContainer.forEntities(shapes, noticeContainer), |
| 117 | + GtfsLocationGroupsTableContainer.forEntities(locationGroups, noticeContainer), |
| 118 | + feedContainer) |
| 119 | + .validate(noticeContainer); |
| 120 | + return noticeContainer.getValidationNotices(); |
| 121 | + } |
| 122 | +} |
0 commit comments