Skip to content

Commit

Permalink
Use original realtime pattern as key
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jul 25, 2024
1 parent 984f199 commit 1b0e1bd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository;
Expand All @@ -35,6 +34,9 @@ public DefaultRealtimeVehicleService(TransitService transitService) {

@Override
public void setRealtimeVehicles(TripPattern pattern, List<RealtimeVehicle> updates) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
vehicles.put(pattern, List.copyOf(updates));
}

Expand All @@ -45,6 +47,9 @@ public void clearRealtimeVehicles(TripPattern pattern) {

@Override
public List<RealtimeVehicle> getRealtimeVehicles(@Nonnull TripPattern pattern) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
// the list is made immutable during insertion, so we can safely return them
return vehicles.getOrDefault(pattern, List.of());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.opentripplanner.service.realtimevehicles.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.framework.geometry.WgsCoordinate.GREENWICH;
import static org.opentripplanner.transit.model._data.TransitModelForTest.route;
import static org.opentripplanner.transit.model._data.TransitModelForTest.tripPattern;

import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;

class DefaultRealtimeVehicleServiceTest {

private static final Route ROUTE = route("r1").build();
private static final TransitModelForTest MODEL = TransitModelForTest.of();
private static final StopPattern STOP_PATTERN = TransitModelForTest.stopPattern(
MODEL.stop("1").build(),
MODEL.stop("2").build()
);
private static final TripPattern ORIGINAL = tripPattern("original", ROUTE)
.withStopPattern(STOP_PATTERN)
.build();
private static final Instant TIME = Instant.ofEpochSecond(1000);
private static final List<RealtimeVehicle> VEHICLES = List.of(
RealtimeVehicle.builder().withTime(TIME).withCoordinates(GREENWICH).build()
);

@Test
void originalPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
service.setRealtimeVehicles(ORIGINAL, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}

@Test
void realtimeAddedPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
var realtimePattern = tripPattern("realtime-added", ROUTE)
.withStopPattern(STOP_PATTERN)
.withOriginalTripPattern(ORIGINAL)
.withCreatedByRealtimeUpdater(true)
.build();
service.setRealtimeVehicles(realtimePattern, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}
}

0 comments on commit 1b0e1bd

Please sign in to comment.