Search in sources :

Example 16 with ValidationRule

use of edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule in project gtfs-realtime-validator by CUTR-at-USF.

the class TripDescriptorValidatorTest method testE032.

/**
 * E032 - Alert does not have an informed_entity
 */
@Test
public void testE032() {
    TripDescriptorValidator tripDescriptorValidator = new TripDescriptorValidator();
    Map<ValidationRule, Integer> expected = new HashMap<>();
    GtfsRealtime.EntitySelector.Builder entitySelectorBuilder = GtfsRealtime.EntitySelector.newBuilder();
    // Don't set an informed_entity - 1 error
    feedEntityBuilder.setAlert(alertBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripDescriptorValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.put(E032, 1);
    TestUtils.assertResults(expected, results);
    // Add an informed_entity with at least one specifier - no errors
    entitySelectorBuilder.setRouteId("A");
    alertBuilder.addInformedEntity(entitySelectorBuilder.build());
    feedEntityBuilder.setAlert(alertBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripDescriptorValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    clearAndInitRequiredFeedFields();
}
Also used : TripDescriptorValidator(edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TripDescriptorValidator) HashMap(java.util.HashMap) ValidationRule(edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule) Test(org.junit.Test) FeedMessageTest(edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)

Example 17 with ValidationRule

use of edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule in project gtfs-realtime-validator by CUTR-at-USF.

the class TripDescriptorValidatorTest method testE024.

/**
 * E024 - trip direction_id does not match GTFS data
 */
@Test
public void testE024() {
    /**
     * In testagency2.txt, trips.txt has the following:
     *
     * route_id,service_id,trip_id,shape_id,block_id,wheelchair_accessible,trip_bikes_allowed,direction_id
     * 1,alldays,1.1,,,1,,0
     * 1,alldays,1.2,,,1,,0
     * 1,alldays,1.3,,,1,,0
     * 2,alldays,2.1,,,0,2,1
     * 2,alldays,2.2,,,0,2,1
     * 3,alldays,3.1,,,1,,
     *
     * So, direction_id for trip 1.1 is 0, and direction_id for trip 2.1 is 1.  trip 3.1 has no direction_id
     */
    TripDescriptorValidator tripIdValidator = new TripDescriptorValidator();
    Map<ValidationRule, Integer> expected = new HashMap<>();
    GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
    tripDescriptorBuilder.setTripId("1.1");
    tripDescriptorBuilder.setScheduleRelationship(GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    // No GTFS-rt direction_id - no errors
    expected.clear();
    TestUtils.assertResults(expected, results);
    /**
     * Correct GTFS-rt direction_id value 0 - no errors
     */
    tripDescriptorBuilder.setTripId("1.1");
    tripDescriptorBuilder.setDirectionId(0);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    /**
     * Correct GTFS-rt direction_id value 1 - no errors
     */
    tripDescriptorBuilder.setTripId("2.1");
    tripDescriptorBuilder.setDirectionId(1);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    /**
     * Wrong GTFS-rt direction_id value 1 - 2 errors
     */
    tripDescriptorBuilder.setTripId("1.1");
    tripDescriptorBuilder.setDirectionId(1);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    expected.put(E024, 2);
    TestUtils.assertResults(expected, results);
    /**
     * Wrong GTFS-rt direction_id value 0 - 2 errors
     */
    tripDescriptorBuilder.setTripId("2.1");
    tripDescriptorBuilder.setDirectionId(0);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    expected.put(E024, 2);
    TestUtils.assertResults(expected, results);
    /**
     * GTFS-rt direction_id = 0, but no GTFS direction_id - 2 errors
     */
    tripDescriptorBuilder.setTripId("3.1");
    tripDescriptorBuilder.setDirectionId(0);
    tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
    vehiclePositionBuilder.setTrip(tripDescriptorBuilder.build());
    feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = tripIdValidator.validate(MIN_POSIX_TIME, gtfsData2, gtfsData2Metadata, feedMessageBuilder.build(), null, null);
    expected.put(E024, 2);
    TestUtils.assertResults(expected, results);
    clearAndInitRequiredFeedFields();
}
Also used : TripDescriptorValidator(edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TripDescriptorValidator) HashMap(java.util.HashMap) ValidationRule(edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule) Test(org.junit.Test) FeedMessageTest(edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)

Example 18 with ValidationRule

use of edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule in project gtfs-realtime-validator by CUTR-at-USF.

the class TestUtils method assertResults.

/**
 * Asserts that for a given map of rules to expected number of warnings/errors (expected) and
 * error/warning results (results), there should be a certain number of errors warnings for each rule.  There should
 * be 0 errors/warnings for all other rules not included in the map.  In expected, the key is the
 * ValidationRule, and the value is the number of expected warnings/errors for that rule.
 *
 * @param expected      A map of the ValidationRules to the number of expected warnings/errors for each rule.  If a ValidationRule isn't included in this map, it is expected that there are 0 errors/warnings for that rule.
 * @param results       list of errors or warnings output from validation
 */
public static void assertResults(@NotNull Map<ValidationRule, Integer> expected, @NotNull List<ErrorListHelperModel> results) {
    if (expected == null) {
        throw new IllegalArgumentException("expected cannot be null - it must be a list of expected errors or warnings");
    }
    if (results == null) {
        throw new IllegalArgumentException("results cannot be null - it must be a list of actual errors or warnings");
    }
    // We need to create a map of actual results to actual count, so we can loop through
    Map<ValidationRule, Integer> actual = new HashMap<>();
    /**
     * First, confirm that all actual count for all rules in results match the expected count
     */
    for (ErrorListHelperModel error : results) {
        // Save the actual count to a map for quick access in the next FOR loop
        ValidationRule rule = error.getErrorMessage().getValidationRule();
        Integer actualCount = error.getOccurrenceList().size();
        actual.put(rule, actualCount);
        // Get the expected count for this rule
        Integer expectedCount = expected.get(rule);
        if (expectedCount != null) {
            // Make sure we have expected number of errors/warnings
            assertEquals(expectedCount, actualCount);
        } else {
            // Make sure there aren't any errors/warnings for this rule, as it wasn't in the expected HashMap
            assertEquals(0, actualCount.intValue());
        }
    }
    /**
     * Second, make sure that all expected counts for all rules in expected match the actual count.  We need this loop
     * to make sure that there isn't an expected rule in expected that's not included in results.
     */
    for (Map.Entry<ValidationRule, Integer> entry : expected.entrySet()) {
        ValidationRule rule = entry.getKey();
        Integer expectedCount = entry.getValue();
        // Get the actual count for this rule
        Integer actualCount = actual.get(rule);
        if (actualCount != null) {
            // Make sure we have expected number of errors/warnings for this rule
            assertEquals(actualCount, expectedCount);
        } else {
            // We're expecting errors/warnings for a rule that wasn't included in the actual results
            fail("Expected " + expectedCount + " occurrences for " + rule.getErrorId() + " but found 0 occurrences in actual results");
        }
    }
}
Also used : ErrorListHelperModel(edu.usf.cutr.gtfsrtvalidator.lib.model.helper.ErrorListHelperModel) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) ValidationRule(edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule)

Example 19 with ValidationRule

use of edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule in project gtfs-realtime-validator by CUTR-at-USF.

the class VehicleValidatorTest method testE026.

/**
 * E026 - Invalid vehicle position
 */
@Test
public void testE026() {
    VehicleValidator vehicleValidator = new VehicleValidator();
    Map<ValidationRule, Integer> expected = new HashMap<>();
    GtfsRealtime.VehicleDescriptor.Builder vehicleDescriptorBuilder = GtfsRealtime.VehicleDescriptor.newBuilder();
    vehicleDescriptorBuilder.setId("1");
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    // No warnings, if position isn't populated
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    GtfsRealtime.Position.Builder positionBuilder = GtfsRealtime.Position.newBuilder();
    // Valid lat and long (USF Campus in Tampa, FL)
    positionBuilder.setLatitude(28.0587f);
    positionBuilder.setLongitude(-82.4139f);
    vehiclePositionBuilder.setPosition(positionBuilder.build());
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    // Invalid lat - 1 error
    positionBuilder.setLatitude(1000f);
    positionBuilder.setLongitude(-82.4572f);
    vehiclePositionBuilder.setPosition(positionBuilder.build());
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.put(E026, 1);
    TestUtils.assertResults(expected, results);
    // Invalid long - 1 error
    positionBuilder.setLatitude(27.9506f);
    positionBuilder.setLongitude(-1000);
    vehiclePositionBuilder.setPosition(positionBuilder.build());
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.put(E026, 1);
    TestUtils.assertResults(expected, results);
    clearAndInitRequiredFeedFields();
}
Also used : HashMap(java.util.HashMap) VehicleValidator(edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.VehicleValidator) ValidationRule(edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule) Test(org.junit.Test) FeedMessageTest(edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)

Example 20 with ValidationRule

use of edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule in project gtfs-realtime-validator by CUTR-at-USF.

the class VehicleValidatorTest method testE052.

/**
 * E052 - vehicle.id is not unique
 */
@Test
public void testE052() {
    VehicleValidator vehicleValidator = new VehicleValidator();
    Map<ValidationRule, Integer> expected = new HashMap<>();
    GtfsRealtime.VehicleDescriptor.Builder vehicleDescriptorBuilder = GtfsRealtime.VehicleDescriptor.newBuilder();
    vehicleDescriptorBuilder.setId("1");
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
    // No error, as there is only one vehicle in the feed
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.clear();
    TestUtils.assertResults(expected, results);
    GtfsRealtime.VehicleDescriptor.Builder vehicleDescriptorBuilderConflict = GtfsRealtime.VehicleDescriptor.newBuilder();
    vehicleDescriptorBuilderConflict.setId("1");
    vehiclePositionBuilder.setVehicle(vehicleDescriptorBuilder.build());
    feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
    feedMessageBuilder.addEntity(1, feedEntityBuilder.build());
    // 1 error for duplicate vehicle ID of 1
    results = vehicleValidator.validate(MIN_POSIX_TIME, bullRunnerGtfs, bullRunnerGtfsMetadata, feedMessageBuilder.build(), null, null);
    expected.put(E052, 1);
    TestUtils.assertResults(expected, results);
    clearAndInitRequiredFeedFields();
}
Also used : HashMap(java.util.HashMap) VehicleValidator(edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.VehicleValidator) ValidationRule(edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule) Test(org.junit.Test) FeedMessageTest(edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)

Aggregations

ValidationRule (edu.usf.cutr.gtfsrtvalidator.lib.model.ValidationRule)65 Test (org.junit.Test)62 HashMap (java.util.HashMap)59 FeedMessageTest (edu.usf.cutr.gtfsrtvalidator.lib.test.FeedMessageTest)58 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)23 TripDescriptorValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TripDescriptorValidator)14 StopTimeUpdateValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.StopTimeUpdateValidator)13 TimestampValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator)11 VehicleValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.VehicleValidator)8 ErrorListHelperModel (edu.usf.cutr.gtfsrtvalidator.lib.model.helper.ErrorListHelperModel)6 MessageLogModel (edu.usf.cutr.gtfsrtvalidator.lib.model.MessageLogModel)4 OccurrenceModel (edu.usf.cutr.gtfsrtvalidator.lib.model.OccurrenceModel)4 FrequencyTypeZeroValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.FrequencyTypeZeroValidator)3 HeaderValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.HeaderValidator)3 CrossFeedDescriptorValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.CrossFeedDescriptorValidator)2 StopValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.StopValidator)2 GtfsMetadata (edu.usf.cutr.gtfsrtvalidator.lib.validation.GtfsMetadata)1 StopLocationTypeValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.gtfs.StopLocationTypeValidator)1 FrequencyTypeOneValidator (edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.FrequencyTypeOneValidator)1 Field (java.lang.reflect.Field)1