|
| 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.List; |
| 7 | +import java.util.function.Consumer; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.junit.runners.JUnit4; |
| 11 | +import org.mobilitydata.gtfsvalidator.notice.ForeignKeyViolationNotice; |
| 12 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 13 | +import org.mobilitydata.gtfsvalidator.table.*; |
| 14 | + |
| 15 | +@RunWith(JUnit4.class) |
| 16 | +public class LocationIdForeignKeyValidatorTest { |
| 17 | + private static <T> T configure(T obj, Consumer<T> configurator) { |
| 18 | + configurator.accept(obj); |
| 19 | + return obj; |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void missingGeoJSONId_yieldsNotice() { |
| 24 | + NoticeContainer noticeContainer = new NoticeContainer(); |
| 25 | + GtfsGeoJsonFileDescriptor descriptor = new GtfsGeoJsonFileDescriptor(); |
| 26 | + List<GtfsGeoJsonFeature> geoJsonFeatures = |
| 27 | + ImmutableList.of( |
| 28 | + configure( |
| 29 | + new GtfsGeoJsonFeature(), |
| 30 | + f -> { |
| 31 | + f.setFeatureId("locationId"); |
| 32 | + })); |
| 33 | + GtfsGeoJsonFeaturesContainer geoJsonFeaturesContainer = |
| 34 | + descriptor.createContainerForEntities(geoJsonFeatures, noticeContainer); |
| 35 | + List<GtfsStopTime> stopTimes = |
| 36 | + ImmutableList.of( |
| 37 | + new GtfsStopTime.Builder().setCsvRowNumber(1).setLocationId("locationId2").build()); |
| 38 | + GtfsStopTimeTableContainer stopTimeTableContainer = |
| 39 | + GtfsStopTimeTableContainer.forEntities(stopTimes, noticeContainer); |
| 40 | + new LocationIdForeignKeyValidator(geoJsonFeaturesContainer, stopTimeTableContainer) |
| 41 | + .validate(noticeContainer); |
| 42 | + assertThat(noticeContainer.getValidationNotices()) |
| 43 | + .containsExactly( |
| 44 | + new ForeignKeyViolationNotice( |
| 45 | + "stop_times.txt", "location_id", "locations.geojson", "id", "locationId2", 1)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void existingGeoJSONId_yieldsNoNotice() { |
| 50 | + NoticeContainer noticeContainer = new NoticeContainer(); |
| 51 | + GtfsGeoJsonFileDescriptor descriptor = new GtfsGeoJsonFileDescriptor(); |
| 52 | + List<GtfsGeoJsonFeature> geoJsonFeatures = |
| 53 | + ImmutableList.of( |
| 54 | + configure( |
| 55 | + new GtfsGeoJsonFeature(), |
| 56 | + f -> { |
| 57 | + f.setFeatureId("locationId"); |
| 58 | + })); |
| 59 | + GtfsGeoJsonFeaturesContainer geoJsonFeaturesContainer = |
| 60 | + descriptor.createContainerForEntities(geoJsonFeatures, noticeContainer); |
| 61 | + List<GtfsStopTime> stopTimes = |
| 62 | + ImmutableList.of( |
| 63 | + new GtfsStopTime.Builder().setCsvRowNumber(1).setLocationId("locationId").build()); |
| 64 | + GtfsStopTimeTableContainer stopTimeTableContainer = |
| 65 | + GtfsStopTimeTableContainer.forEntities(stopTimes, noticeContainer); |
| 66 | + new LocationIdForeignKeyValidator(geoJsonFeaturesContainer, stopTimeTableContainer) |
| 67 | + .validate(noticeContainer); |
| 68 | + assertThat(noticeContainer.getValidationNotices()).isEmpty(); |
| 69 | + } |
| 70 | +} |
0 commit comments