Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Fix #254 - New rule E052 - "vehicle.id is not unique"
Browse files Browse the repository at this point in the history
  • Loading branch information
barbeau committed Feb 22, 2018
1 parent cad0439 commit 121173f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
14 changes: 14 additions & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Rules are declared in the [`ValidationRules` class](https://github.com/CUTR-at-U
| [E049](#E049) | `header` `incrementality` not populated (GTFS-rt v2.0 and higher)
| [E050](#E050) | `timestamp` is in the future
| [E051](#E051) | GTFS-rt `stop_sequence` not found in GTFS data
| [E052](#E052) | `vehicle.id` is not unique

### Table of Warnings

Expand Down Expand Up @@ -703,6 +704,19 @@ See [this issue](https://github.com/CUTR-at-USF/gtfs-realtime-validator/issues/2
* [`stop_time_update`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-stoptimeupdate)
* [GTFS `stop_times.txt`](https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#stop_timestxt)

<a name="E052"/>

### E052 - `vehicle.id` is not unique

Each vehicle should have a unique ID.

From [VehiclePosition.VehicleDescriptor](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-vehicledescriptor) for `vehicle.id`:

>Internal system identification of the vehicle. Should be unique per vehicle, and is used for tracking the vehicle as it proceeds through the system. This id should not be made visible to the end-user; for that purpose use the label field
#### References:
* [`vehicle.id`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-vehicledescriptor)

# Warnings

<a name="W001"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ public class ValidationRules {
"All stop_time_update stop_sequences in GTFS-realtime data must appear in GTFS stop_times.txt for that trip",
"that does not exist in GTFS stop_times.txt for this trip");

public static final ValidationRule E052 = new ValidationRule("E052", "ERROR", "vehicle.id is not unique",
"Each vehicle should have a unique ID",
"which is used by more than one vehicle in the feed");

private static List<ValidationRule> mAllRules = new ArrayList<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static edu.usf.cutr.gtfsrtvalidator.lib.util.GtfsUtils.getTripId;
Expand All @@ -46,6 +47,7 @@
* E029 - Vehicle position outside trip shape buffer
* W002 - vehicle_id not populated
* W004 - vehicle speed is unrealistic
* E052 - vehicle.id is not unique
*/

public class VehicleValidator implements FeedEntityValidator {
Expand All @@ -63,6 +65,9 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsDaoImpl g
List<OccurrenceModel> e029List = new ArrayList<>();
List<OccurrenceModel> w002List = new ArrayList<>();
List<OccurrenceModel> w004List = new ArrayList<>();
List<OccurrenceModel> e052List = new ArrayList<>();

HashSet<String> vehicleIds = new HashSet<>(entityList.size());

for (GtfsRealtime.FeedEntity entity : entityList) {
if (entity.hasTripUpdate()) {
Expand All @@ -79,6 +84,13 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsDaoImpl g
if (StringUtils.isEmpty(v.getVehicle().getId())) {
// W002 - vehicle_id not populated
RuleUtils.addOccurrence(W002, "entity ID " + entity.getId(), w002List, _log);
} else {
if (vehicleIds.contains(v.getVehicle().getId())) {
// E052 - vehicle.id is not unique
RuleUtils.addOccurrence(E052, "entity ID " + entity.getId() + " has vehicle.id " + v.getVehicle().getId(), e052List, _log);
} else {
vehicleIds.add(v.getVehicle().getId());
}
}

if (v.hasPosition() && v.getPosition().hasSpeed()) {
Expand Down Expand Up @@ -135,6 +147,9 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsDaoImpl g
if (!w004List.isEmpty()) {
errors.add(new ErrorListHelperModel(new MessageLogModel(W004), w004List));
}
if (!e052List.isEmpty()) {
errors.add(new ErrorListHelperModel(new MessageLogModel(E052), e052List));
}
return errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,6 @@ public void testIsInFuture() {
@Test
public void testGetAllRules() {
List<ValidationRule> rules = ValidationRules.getRules();
assertEquals(60, rules.size());
assertEquals(61, rules.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,39 @@ public void testE029IgnoreShapes() {

clearAndInitRequiredFeedFields();
}

/**
* 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();
}
}

0 comments on commit 121173f

Please sign in to comment.