use of edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator in project gtfs-realtime-validator by CUTR-at-USF.
the class TimestampValidatorTest method testW001.
@Test
public void testW001() {
TimestampValidator timestampValidator = new TimestampValidator();
Map<ValidationRule, Integer> expected = new HashMap<>();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
final long CURRENT_TIME_MILLIS = TimeUnit.SECONDS.toMillis(MIN_POSIX_TIME);
// Timestamp will be zero initially in FeedHeader, TripUpdate and VehiclePosition. Should return 3 results.
vehiclePositionBuilder.setVehicle(GtfsRealtime.VehicleDescriptor.newBuilder());
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(W001, 3);
TestUtils.assertResults(expected, results);
// Populate timestamp to any value greater than zero in FeedHeader
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
// Invalid timestamp in TripUpdate and VehiclePosition. Should return 2 results.
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(W001, 2);
TestUtils.assertResults(expected, results);
// TripDescriptor is a required field in tripUpdate
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// Populate timestamp to any value greater than zero in TripUpdate.
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// Invalid timestamp only in VehiclePosition. Should return 1 results.
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(W001, 1);
TestUtils.assertResults(expected, results);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// Now timestamp is populated in FeedHeader, TripUpdate and VehiclePosition . Should return no error.
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
clearAndInitRequiredFeedFields();
}
use of edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator in project gtfs-realtime-validator by CUTR-at-USF.
the class TimestampValidatorTest method testW008.
/**
* W008 - Header timestamp is older than 65 seconds
*/
@Test
public void testW008() {
TimestampValidator timestampValidator = new TimestampValidator();
Map<ValidationRule, Integer> expected = new HashMap<>();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
// Set valid trip_id = 1.1
tripDescriptorBuilder.setTripId("1.1");
long currentTimeMillis = System.currentTimeMillis();
long currentTimeSec = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis);
/**
* Use current time - no warnings
*/
feedHeaderBuilder.setTimestamp(currentTimeSec);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(currentTimeSec);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(currentTimeSec);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
GtfsRealtime.FeedMessage currentIteration = feedMessageBuilder.setEntity(0, feedEntityBuilder.build()).build();
results = timestampValidator.validate(currentTimeMillis, gtfsData, gtfsDataMetadata, currentIteration, null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* Use current time minus 70 seconds (feed is 1 min 10 sec old) - 1 warning
*/
feedHeaderBuilder.setTimestamp(currentTimeSec - 70);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(currentTimeSec - 70);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(currentTimeSec - 70);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
currentIteration = feedMessageBuilder.setEntity(0, feedEntityBuilder.build()).build();
results = timestampValidator.validate(currentTimeMillis, gtfsData, gtfsDataMetadata, currentIteration, null, null);
expected.put(W008, 1);
TestUtils.assertResults(expected, results);
clearAndInitRequiredFeedFields();
}
use of edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator in project gtfs-realtime-validator by CUTR-at-USF.
the class TimestampValidatorTest method testE018.
@Test
public void testE018() {
TimestampValidator timestampValidator = new TimestampValidator();
Map<ValidationRule, Integer> expected = new HashMap<>();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
// Set valid trip_id = 1.1
tripDescriptorBuilder.setTripId("1.1");
final long CURRENT_TIME_MILLIS = TimeUnit.SECONDS.toMillis(MIN_POSIX_TIME);
/**
* No previous feed message (i.e., it's the first iteration) - no errors
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME + 1);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME + 1);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME + 1);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
GtfsRealtime.FeedMessage currentIteration = feedMessageBuilder.setEntity(0, feedEntityBuilder.build()).build();
// No previous iteration - no errors
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, currentIteration, null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* Set the previous iteration header timestamp so it's less that the current iteration - no errors
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
GtfsRealtime.FeedMessage previousIteration = feedMessageBuilder.setEntity(0, feedEntityBuilder.build()).build();
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, currentIteration, previousIteration, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* Set the previous iteration header timestamp so it's greater than that the current iteration - 1 error
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME + 2);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME + 2);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME + 2);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
previousIteration = feedMessageBuilder.setEntity(0, feedEntityBuilder.build()).build();
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, currentIteration, previousIteration, null);
expected.put(E018, 1);
TestUtils.assertResults(expected, results);
clearAndInitRequiredFeedFields();
}
use of edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator in project gtfs-realtime-validator by CUTR-at-USF.
the class TimestampValidatorTest method testE025.
/**
* E025 - stop_time_update departure time is before arrival time
*/
@Test
public void testE025() {
TimestampValidator timestampValidator = new TimestampValidator();
Map<ValidationRule, Integer> expected = new HashMap<>();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
final long CURRENT_TIME_MILLIS = TimeUnit.SECONDS.toMillis(MIN_POSIX_TIME);
/**
* Set timestamps on objects (without StopTimeUpdates first) so no errors
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* StopTimeUpdate has departure time equal to arrival time - 0 errors
*/
GtfsRealtime.TripUpdate.StopTimeUpdate.Builder stopTimeUpdateBuilder = GtfsRealtime.TripUpdate.StopTimeUpdate.newBuilder();
GtfsRealtime.TripUpdate.StopTimeEvent.Builder stopTimeEventBuilder = GtfsRealtime.TripUpdate.StopTimeEvent.newBuilder();
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
tripUpdateBuilder.addStopTimeUpdate(0, stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* StopTimeUpdate has departure time greater than arrival time - 0 errors
*/
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(MIN_POSIX_TIME + 1));
tripUpdateBuilder.setStopTimeUpdate(0, stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* StopTimeUpdate has departure time less than arrival time - 1 error
*/
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(MIN_POSIX_TIME + 1));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
tripUpdateBuilder.setStopTimeUpdate(0, stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E025, 1);
TestUtils.assertResults(expected, results);
clearAndInitRequiredFeedFields();
}
use of edu.usf.cutr.gtfsrtvalidator.lib.validation.rules.TimestampValidator in project gtfs-realtime-validator by CUTR-at-USF.
the class TimestampValidatorTest method testE001.
@Test
public void testE001() {
TimestampValidator timestampValidator = new TimestampValidator();
Map<ValidationRule, Integer> expected = new HashMap<>();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
final long CURRENT_TIME_MILLIS = TimeUnit.SECONDS.toMillis(MIN_POSIX_TIME);
/**
* All times are POSIX - no errors
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* Header isn't POSIX - should be 1 error
*/
// Convert a valid POSIX time to milliseconds - this is a common error in feeds (providing time in milliseconds past epoch instead of seconds)
final long BAD_TIME = TimeUnit.SECONDS.toMillis(MIN_POSIX_TIME);
feedHeaderBuilder.setTimestamp(BAD_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E001, 1);
TestUtils.assertResults(expected, results);
/**
* Header and TripUpdate aren't POSIX - 2 errors
*/
tripUpdateBuilder.setTimestamp(BAD_TIME);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E001, 2);
TestUtils.assertResults(expected, results);
/**
* Header, TripUpdate, and VehiclePosition aren't POSIX - 3 errors
*/
vehiclePositionBuilder.setTimestamp(BAD_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E001, 3);
TestUtils.assertResults(expected, results);
/**
* StopTimeUpdates are all POSIX - no errors
*/
feedHeaderBuilder.setTimestamp(MIN_POSIX_TIME);
feedMessageBuilder.setHeader(feedHeaderBuilder.build());
tripUpdateBuilder.setTimestamp(MIN_POSIX_TIME);
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
GtfsRealtime.TripUpdate.StopTimeUpdate.Builder stopTimeUpdateBuilder = GtfsRealtime.TripUpdate.StopTimeUpdate.newBuilder();
GtfsRealtime.TripUpdate.StopTimeEvent.Builder stopTimeEventBuilder = GtfsRealtime.TripUpdate.StopTimeEvent.newBuilder();
// First StopTimeUpdate
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(MIN_POSIX_TIME));
tripUpdateBuilder.addStopTimeUpdate(0, stopTimeUpdateBuilder.build());
// Second StopTimeUpdate
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(MAX_POSIX_TIME));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(MAX_POSIX_TIME));
tripUpdateBuilder.addStopTimeUpdate(1, stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
vehiclePositionBuilder.setTimestamp(MIN_POSIX_TIME);
feedEntityBuilder.setVehicle(vehiclePositionBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* 2 StopTimeUpdates, which each have an arrival AND departure POSIX error - so 4 errors total
*/
// First StopTimeUpdate
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(BAD_TIME));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(BAD_TIME));
tripUpdateBuilder.setStopTimeUpdate(0, stopTimeUpdateBuilder.build());
// Second StopTimeUpdate
stopTimeUpdateBuilder.setArrival(stopTimeEventBuilder.setTime(BAD_TIME + 1));
stopTimeUpdateBuilder.setDeparture(stopTimeEventBuilder.setTime(BAD_TIME + 1));
tripUpdateBuilder.setStopTimeUpdate(1, stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E001, 4);
TestUtils.assertResults(expected, results);
// Remove bad POSIX StopTimeUpdates to prep for next assertion
tripUpdateBuilder.clearStopTimeUpdate();
feedEntityBuilder.setTripUpdate(tripUpdateBuilder);
/**
* Alert active_period ranges - both start and end are valid POSIX, so 0 errors
*/
GtfsRealtime.Alert.Builder alertBuilder = GtfsRealtime.Alert.newBuilder();
GtfsRealtime.TimeRange.Builder timeRangeBuilder = GtfsRealtime.TimeRange.newBuilder();
timeRangeBuilder.setStart(MIN_POSIX_TIME);
timeRangeBuilder.setEnd(MIN_POSIX_TIME);
alertBuilder.addActivePeriod(timeRangeBuilder.build());
feedEntityBuilder.setAlert(alertBuilder);
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.clear();
TestUtils.assertResults(expected, results);
/**
* Alert active_period ranges - neither start nor end are valid POSIX, so 2 errors
*/
timeRangeBuilder.setStart(BAD_TIME);
timeRangeBuilder.setEnd(BAD_TIME);
alertBuilder.addActivePeriod(timeRangeBuilder.build());
feedEntityBuilder.setAlert(alertBuilder);
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
results = timestampValidator.validate(CURRENT_TIME_MILLIS, gtfsData, gtfsDataMetadata, feedMessageBuilder.build(), null, null);
expected.put(E001, 2);
TestUtils.assertResults(expected, results);
clearAndInitRequiredFeedFields();
}
Aggregations