|
| 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.Arrays; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; |
| 10 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 11 | +import org.mobilitydata.gtfsvalidator.table.GtfsTransfer; |
| 12 | +import org.mobilitydata.gtfsvalidator.table.GtfsTransferTableContainer; |
| 13 | +import org.mobilitydata.gtfsvalidator.table.GtfsTransferType; |
| 14 | + |
| 15 | +public class TransferStopIdsConditionalValidatorTest { |
| 16 | + |
| 17 | + private NoticeContainer noticeContainer; |
| 18 | + |
| 19 | + @Before |
| 20 | + public void setUp() { |
| 21 | + noticeContainer = new NoticeContainer(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * This test is used to verify that the validator does not generate a notice when the {@code |
| 26 | + * from_stop_id} is missing for in-seat transfer types. |
| 27 | + */ |
| 28 | + @Test |
| 29 | + public void testTransferFromStopIdNoInSeatTransferNoNotice() { |
| 30 | + // In seat transfer types are 4 and 5, so we test for all other types |
| 31 | + for (int i = 4; i <= 5; i++) { |
| 32 | + GtfsTransferType transferType = GtfsTransferType.forNumber(i); |
| 33 | + GtfsTransferTableContainer gtfsTransferTableContainer = |
| 34 | + GtfsTransferTableContainer.forEntities( |
| 35 | + ImmutableList.of(new GtfsTransfer.Builder().setTransferType(transferType).build()), |
| 36 | + noticeContainer); |
| 37 | + |
| 38 | + new TransferStopIdsConditionalValidator(gtfsTransferTableContainer).validate(noticeContainer); |
| 39 | + |
| 40 | + assertThat(noticeContainer.getValidationNotices()).isEmpty(); |
| 41 | + noticeContainer.getValidationNotices().clear(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * This test is used to verify that the validator generates a notice when the {@code from_stop_id} |
| 47 | + * is missing for transfer types other than in-seat transfer types. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + public void testTransferMissingFromStopIdNoInSeatTransfer() { |
| 51 | + for (int i = 0; i < 4; i++) { |
| 52 | + GtfsTransferType transferType = GtfsTransferType.forNumber(i); |
| 53 | + GtfsTransferTableContainer gtfsTransferTableContainer = |
| 54 | + GtfsTransferTableContainer.forEntities( |
| 55 | + ImmutableList.of(new GtfsTransfer.Builder().setTransferType(transferType).build()), |
| 56 | + noticeContainer); |
| 57 | + |
| 58 | + new TransferStopIdsConditionalValidator(gtfsTransferTableContainer).validate(noticeContainer); |
| 59 | + |
| 60 | + assertThat(noticeContainer.getValidationNotices()).isNotEmpty(); |
| 61 | + assertThat(noticeContainer.getValidationNotices()) |
| 62 | + .containsExactlyElementsIn( |
| 63 | + Arrays.asList( |
| 64 | + new MissingRequiredFieldNotice( |
| 65 | + GtfsTransfer.FILENAME, 0, GtfsTransfer.FROM_STOP_ID_FIELD_NAME), |
| 66 | + new MissingRequiredFieldNotice( |
| 67 | + GtfsTransfer.FILENAME, 0, GtfsTransfer.TO_STOP_ID_FIELD_NAME))); |
| 68 | + |
| 69 | + noticeContainer.getValidationNotices().clear(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * This test is used to verify that the validator doesn't generate a notice when the {@code |
| 75 | + * to_stop_id} and {@code from_stop_id} are present for all transfer types. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void testTransferFromStopIdNoInSeatTransfer() { |
| 79 | + for (int i = 0; i <= 5; i++) { |
| 80 | + GtfsTransferType transferType = GtfsTransferType.forNumber(i); |
| 81 | + GtfsTransferTableContainer gtfsTransferTableContainer = |
| 82 | + GtfsTransferTableContainer.forEntities( |
| 83 | + ImmutableList.of( |
| 84 | + new GtfsTransfer.Builder() |
| 85 | + .setFromStopId("stop1") |
| 86 | + .setToStopId("stop2") |
| 87 | + .setTransferType(transferType) |
| 88 | + .build()), |
| 89 | + noticeContainer); |
| 90 | + |
| 91 | + new TransferStopIdsConditionalValidator(gtfsTransferTableContainer).validate(noticeContainer); |
| 92 | + |
| 93 | + assertThat(noticeContainer.getValidationNotices()).isEmpty(); |
| 94 | + noticeContainer.getValidationNotices().clear(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments