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();
}
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();
}
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");
}
}
}
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();
}
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();
}
Aggregations