diff --git a/src/main/java/org/entur/lamassu/util/SpatialIndexIdUtil.java b/src/main/java/org/entur/lamassu/util/SpatialIndexIdUtil.java index b64f364a..842d8df6 100644 --- a/src/main/java/org/entur/lamassu/util/SpatialIndexIdUtil.java +++ b/src/main/java/org/entur/lamassu/util/SpatialIndexIdUtil.java @@ -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; @@ -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; } } diff --git a/src/test/java/org/entur/lamassu/util/SpatialIndexIdFilterTest.java b/src/test/java/org/entur/lamassu/util/SpatialIndexIdFilterTest.java index 1ee99f30..5c1fb36e 100644 --- a/src/test/java/org/entur/lamassu/util/SpatialIndexIdFilterTest.java +++ b/src/test/java/org/entur/lamassu/util/SpatialIndexIdFilterTest.java @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -129,46 +141,52 @@ 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); @@ -176,7 +194,7 @@ private VehicleType scooterVehicle() { return type; } - private FeedProvider testProvider() { + private FeedProvider aProvider() { var provider = new FeedProvider(); provider.setCodespace("TST"); provider.setSystemId("TST:System:testprovider"); @@ -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; + } }