From ce80db29f544a6726d691bea3bb95160e76c7ca3 Mon Sep 17 00:00:00 2001 From: cka-y <60586858+cka-y@users.noreply.github.com> Date: Tue, 4 Jul 2023 12:04:12 -0400 Subject: [PATCH] feat: validator for ferry trips + bikes allowance (#1510) * feat: validator for ferry trips + bike allocation * feat: removed "info" suffix * feat: fixed formatting --- .../gtfsvalidator/table/GtfsRouteSchema.java | 1 + .../gtfsvalidator/table/GtfsTripSchema.java | 1 + .../validator/BikesAllowanceValidator.java | 88 +++++++++++++++++++ .../validator/BikeAllowanceValidatorTest.java | 88 +++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 main/src/main/java/org/mobilitydata/gtfsvalidator/validator/BikesAllowanceValidator.java create mode 100644 main/src/test/java/org/mobilitydata/gtfsvalidator/validator/BikeAllowanceValidatorTest.java diff --git a/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsRouteSchema.java b/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsRouteSchema.java index 480fedb469..c26a308552 100644 --- a/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsRouteSchema.java +++ b/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsRouteSchema.java @@ -44,6 +44,7 @@ public interface GtfsRouteSchema extends GtfsEntity { String routeDesc(); @Required + @Index GtfsRouteType routeType(); @FieldType(FieldTypeEnum.URL) diff --git a/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsTripSchema.java b/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsTripSchema.java index 3a944fe40f..1357927c09 100644 --- a/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsTripSchema.java +++ b/main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsTripSchema.java @@ -29,6 +29,7 @@ public interface GtfsTripSchema extends GtfsEntity { @FieldType(FieldTypeEnum.ID) @Required @ForeignKey(table = "routes.txt", field = "route_id") + @Index String routeId(); @FieldType(FieldTypeEnum.ID) diff --git a/main/src/main/java/org/mobilitydata/gtfsvalidator/validator/BikesAllowanceValidator.java b/main/src/main/java/org/mobilitydata/gtfsvalidator/validator/BikesAllowanceValidator.java new file mode 100644 index 0000000000..7c3de5961a --- /dev/null +++ b/main/src/main/java/org/mobilitydata/gtfsvalidator/validator/BikesAllowanceValidator.java @@ -0,0 +1,88 @@ +/* + * Copyright 2021 MobilityData IO + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mobilitydata.gtfsvalidator.validator; + +import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.WARNING; + +import java.util.List; +import javax.inject.Inject; +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.FileRefs; +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; +import org.mobilitydata.gtfsvalidator.table.*; + +/** + * Validates that bikes_allowed information is present for ferry trips + * + *

Generated notice: {@link MissingBikeAllowanceNotice}. + */ +@GtfsValidator +public class BikesAllowanceValidator extends FileValidator { + + private final GtfsTripTableContainer tripTable; + + private final GtfsRouteTableContainer routeTable; + + @Inject + BikesAllowanceValidator(GtfsTripTableContainer tripTable, GtfsRouteTableContainer routeTable) { + this.tripTable = tripTable; + this.routeTable = routeTable; + } + + @Override + public void validate(NoticeContainer noticeContainer) { + List routes = routeTable.byRouteType(GtfsRouteType.FERRY); + for (GtfsRoute route : routes) { + List trips = tripTable.byRouteId(route.routeId()); + for (GtfsTrip trip : trips) { + if (trip.bikesAllowed() != GtfsBikesAllowed.UNRECOGNIZED + && trip.bikesAllowed() != GtfsBikesAllowed.UNKNOWN) { + continue; + } + noticeContainer.addValidationNotice( + new MissingBikeAllowanceNotice(trip.csvRowNumber(), trip.routeId(), trip.tripId())); + } + } + } + + /** + * Ferry trips should include bike allowance information. + * + *

All ferry trips should have a valid value in the bikes_allowed field in trips.txt. + */ + @GtfsValidationNotice( + severity = WARNING, + files = @FileRefs({GtfsRouteSchema.class, GtfsTripSchema.class})) + static class MissingBikeAllowanceNotice extends ValidationNotice { + + /** The row number of the faulty record. */ + private final int csvRowNumber; + + /** The faulty record's route id. */ + private final String routeId; + + /** The faulty record's trip id. */ + private final String tripId; + + MissingBikeAllowanceNotice(int csvRowNumber, String routeId, String tripId) { + this.csvRowNumber = csvRowNumber; + this.routeId = routeId; + this.tripId = tripId; + } + } +} diff --git a/main/src/test/java/org/mobilitydata/gtfsvalidator/validator/BikeAllowanceValidatorTest.java b/main/src/test/java/org/mobilitydata/gtfsvalidator/validator/BikeAllowanceValidatorTest.java new file mode 100644 index 0000000000..499b756480 --- /dev/null +++ b/main/src/test/java/org/mobilitydata/gtfsvalidator/validator/BikeAllowanceValidatorTest.java @@ -0,0 +1,88 @@ +package org.mobilitydata.gtfsvalidator.validator; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; +import org.mobilitydata.gtfsvalidator.table.*; + +@RunWith(JUnit4.class) +public class BikeAllowanceValidatorTest { + + private static List createTripTable( + String routeId, String serviceId, GtfsBikesAllowed bikesAllowed, int rows) { + ArrayList trips = new ArrayList<>(); + for (int i = 0; i < rows; i++) { + trips.add( + new GtfsTrip.Builder() + .setCsvRowNumber(i + 1) + .setTripId("t" + i) + .setServiceId(serviceId) + .setRouteId(routeId) + .setBikesAllowed(bikesAllowed) + .build()); + } + return trips; + } + + private static List createRouteTable(String routeId, GtfsRouteType routeType) { + return List.of( + new GtfsRoute.Builder() + .setCsvRowNumber(1) + .setRouteId(routeId) + .setRouteType(routeType) + .build()); + } + + private static List generateNotices( + List trips, List routes) { + NoticeContainer noticeContainer = new NoticeContainer(); + new BikesAllowanceValidator( + GtfsTripTableContainer.forEntities(trips, noticeContainer), + GtfsRouteTableContainer.forEntities(routes, noticeContainer)) + .validate(noticeContainer); + return noticeContainer.getValidationNotices(); + } + + @Test + public void testValidFeedForFerryTrips() { + assertThat( + generateNotices( + createTripTable("route1", "service1", GtfsBikesAllowed.ALLOWED, 2), + createRouteTable("route1", GtfsRouteType.FERRY))) + .isEmpty(); + } + + @Test + public void testValidFeedForNonFerryTrips() { + assertThat( + generateNotices( + createTripTable("route1", "service1", GtfsBikesAllowed.UNRECOGNIZED, 2), + createRouteTable("route1", GtfsRouteType.BUS))) + .isEmpty(); + assertThat( + generateNotices( + createTripTable("route1", "service1", null, 2), + createRouteTable("route1", GtfsRouteType.BUS))) + .isEmpty(); + } + + @Test + public void testInvalidBikesAllowedValueForFerryTrips() { + assertThat( + generateNotices( + createTripTable("route1", "service1", GtfsBikesAllowed.UNRECOGNIZED, 2), + createRouteTable("route1", GtfsRouteType.FERRY))) + .hasSize(2); + assertThat( + generateNotices( + createTripTable("route1", "service1", null, 2), + createRouteTable("route1", GtfsRouteType.FERRY))) + .hasSize(2); + } +}