Skip to content

Commit

Permalink
Merge pull request #257 from mobidata-bw/missing_vht
Browse files Browse the repository at this point in the history
Handle missing vehicle_types_available gracefully
  • Loading branch information
testower authored Sep 20, 2023
2 parents 0fe6e65 + cece99e commit ad76781
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 47 deletions.
38 changes: 24 additions & 14 deletions src/main/java/org/entur/lamassu/util/SpatialIndexIdUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.entur.lamassu.util;

import java.util.Collections;
import java.util.stream.Collectors;
import org.entur.lamassu.cache.StationSpatialIndexId;
import org.entur.lamassu.cache.VehicleSpatialIndexId;
Expand Down Expand Up @@ -37,20 +38,29 @@ public static StationSpatialIndexId createStationSpatialIndexId(
id.setCodespace(feedProvider.getCodespace());
id.setSystemId(feedProvider.getSystemId());
id.setOperatorId(feedProvider.getOperatorId());
id.setAvailableFormFactors(
station
.getVehicleTypesAvailable()
.stream()
.map(vta -> vta.getVehicleType().getFormFactor())
.collect(Collectors.toList())
);
id.setAvailablePropulsionTypes(
station
.getVehicleTypesAvailable()
.stream()
.map(vta -> vta.getVehicleType().getPropulsionType())
.collect(Collectors.toList())
);
if (station.getVehicleTypesAvailable() != null) {
id.setAvailableFormFactors(
station
.getVehicleTypesAvailable()
.stream()
.map(vta -> vta.getVehicleType().getFormFactor())
.collect(Collectors.toList())
);
id.setAvailablePropulsionTypes(
station
.getVehicleTypesAvailable()
.stream()
.map(vta -> vta.getVehicleType().getPropulsionType())
.collect(Collectors.toList())
);
} else {
// Note: in case no validation is activated, this issue would slip
// silently. On the other hand, logging it here for every station would
// be overwhelming...
id.setAvailableFormFactors(Collections.EMPTY_LIST);
id.setAvailablePropulsionTypes(Collections.EMPTY_LIST);
}

return id;
}
}
89 changes: 56 additions & 33 deletions src/test/java/org/entur/lamassu/util/SpatialIndexIdFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@ public class SpatialIndexIdFilterTest {
@Test
public void testNoFilter() {
Assert.assertTrue(
SpatialIndexIdFilter.filterVehicle(testVehicleId(), testVehicleFilterParams())
SpatialIndexIdFilter.filterVehicle(aVehicleId(), aVehicleFilterParams())
);
}

@Test
public void testNoFilterReturnsStationWithoutVehicleTypesAvailable() {
StationSpatialIndexId stationSpatialIndexId =
SpatialIndexIdUtil.createStationSpatialIndexId(
aStationWithoutVehicleTypeAvailability(),
aProvider()
);
Assert.assertTrue(
SpatialIndexIdFilter.filterStation(stationSpatialIndexId, aStationFilterParams())
);
}

@Test
public void testCodespaceFilter() {
var testId = testVehicleId();
var params = testVehicleFilterParams();
var testId = aVehicleId();
var params = aVehicleFilterParams();

params.setCodespaces(List.of("TST"));
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -38,8 +50,8 @@ public void testCodespaceFilter() {

@Test
public void testSystemFilter() {
var testId = testVehicleId();
var params = testVehicleFilterParams();
var testId = aVehicleId();
var params = aVehicleFilterParams();

params.setSystems(List.of("TST:System:testprovider"));
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -50,8 +62,8 @@ public void testSystemFilter() {

@Test
public void testOperatorFilter() {
var testId = testVehicleId();
var params = testVehicleFilterParams();
var testId = aVehicleId();
var params = aVehicleFilterParams();

params.setOperators(List.of("TST:Operator:test"));
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -62,8 +74,8 @@ public void testOperatorFilter() {

@Test
public void testFormFactorFilter() {
var testId = testVehicleId();
var params = testVehicleFilterParams();
var testId = aVehicleId();
var params = aVehicleFilterParams();

params.setFormFactors(List.of(FormFactor.SCOOTER));
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -74,8 +86,8 @@ public void testFormFactorFilter() {

@Test
public void testPropulsionTypeFilter() {
var testId = testVehicleId();
var params = testVehicleFilterParams();
var testId = aVehicleId();
var params = aVehicleFilterParams();

params.setPropulsionTypes(List.of(PropulsionType.ELECTRIC));
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -86,8 +98,8 @@ public void testPropulsionTypeFilter() {

@Test
public void testIncludeReservedFilter() {
var testId = testReservedId();
var params = testVehicleFilterParams();
var testId = aReservedId();
var params = aVehicleFilterParams();

params.setIncludeReserved(true);
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -98,8 +110,8 @@ public void testIncludeReservedFilter() {

@Test
public void testIncludeDisabledFilter() {
var testId = testDisabledId();
var params = testVehicleFilterParams();
var testId = aDisabledId();
var params = aVehicleFilterParams();

params.setIncludeDisabled(true);
Assert.assertTrue(SpatialIndexIdFilter.filterVehicle(testId, params));
Expand All @@ -110,7 +122,7 @@ public void testIncludeDisabledFilter() {

@Test
public void testVehicleTypesAvailableFilter() {
var testId = testStationId();
var testId = aStationId();
var params = new StationFilterParameters();

params.setAvailableFormFactors(List.of(FormFactor.SCOOTER));
Expand All @@ -129,54 +141,60 @@ public void testVehicleTypesAvailableFilter() {
Assert.assertFalse(SpatialIndexIdFilter.filterStation(testId, params));
}

private VehicleSpatialIndexId testVehicleId() {
return SpatialIndexIdUtil.createVehicleSpatialIndexId(testVehicle(), testProvider());
private VehicleSpatialIndexId aVehicleId() {
return SpatialIndexIdUtil.createVehicleSpatialIndexId(aVehicle(), aProvider());
}

private StationSpatialIndexId testStationId() {
return SpatialIndexIdUtil.createStationSpatialIndexId(testStation(), testProvider());
private StationSpatialIndexId aStationId() {
return SpatialIndexIdUtil.createStationSpatialIndexId(aStation(), aProvider());
}

private VehicleSpatialIndexId testReservedId() {
var vehicle = testVehicle();
private VehicleSpatialIndexId aReservedId() {
var vehicle = aVehicle();
vehicle.setReserved(true);
return SpatialIndexIdUtil.createVehicleSpatialIndexId(vehicle, testProvider());
return SpatialIndexIdUtil.createVehicleSpatialIndexId(vehicle, aProvider());
}

private VehicleSpatialIndexId testDisabledId() {
var vehicle = testVehicle();
private VehicleSpatialIndexId aDisabledId() {
var vehicle = aVehicle();
vehicle.setDisabled(true);
return SpatialIndexIdUtil.createVehicleSpatialIndexId(vehicle, testProvider());
return SpatialIndexIdUtil.createVehicleSpatialIndexId(vehicle, aProvider());
}

private Vehicle testVehicle() {
private Vehicle aVehicle() {
var vehicle = new Vehicle();
vehicle.setId("TST:Vehicle:abc123");
vehicle.setReserved(false);
vehicle.setDisabled(false);
vehicle.setVehicleType(scooterVehicle());
vehicle.setVehicleType(aScooterVehicle());
return vehicle;
}

private Station testStation() {
private Station aStation() {
var station = new Station();
station.setId("TST:Station:foobar");
var vehicleTypeAvailability = new VehicleTypeAvailability();
vehicleTypeAvailability.setVehicleType(scooterVehicle());
vehicleTypeAvailability.setVehicleType(aScooterVehicle());
vehicleTypeAvailability.setCount(1);
station.setVehicleTypesAvailable(List.of(vehicleTypeAvailability));
return station;
}

private VehicleType scooterVehicle() {
private Station aStationWithoutVehicleTypeAvailability() {
var station = new Station();
station.setId("TST:Station:no_vta");
return station;
}

private VehicleType aScooterVehicle() {
var type = new VehicleType();
type.setId("TST:VehicleType:Scooter");
type.setFormFactor(FormFactor.SCOOTER);
type.setPropulsionType(PropulsionType.ELECTRIC);
return type;
}

private FeedProvider testProvider() {
private FeedProvider aProvider() {
var provider = new FeedProvider();
provider.setCodespace("TST");
provider.setSystemId("TST:System:testprovider");
Expand All @@ -185,10 +203,15 @@ private FeedProvider testProvider() {
return provider;
}

private VehicleFilterParameters testVehicleFilterParams() {
private VehicleFilterParameters aVehicleFilterParams() {
var params = new VehicleFilterParameters();
params.setIncludeReserved(false);
params.setIncludeDisabled(false);
return params;
}

private StationFilterParameters aStationFilterParams() {
var params = new StationFilterParameters();
return params;
}
}

0 comments on commit ad76781

Please sign in to comment.