|
| 1 | +package org.transitclock.pipelinetests; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.Date; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.atomic.AtomicLong; |
| 8 | + |
| 9 | +import org.junit.ClassRule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.transitclock.core.AvlProcessor; |
| 12 | +import org.transitclock.core.VehicleState; |
| 13 | +import org.transitclock.core.dataCache.StopArrivalDepartureCacheFactory; |
| 14 | +import org.transitclock.core.dataCache.StopArrivalDepartureCacheKey; |
| 15 | +import org.transitclock.core.dataCache.VehicleStateManager; |
| 16 | +import org.transitclock.db.structs.AvlReport; |
| 17 | +import org.transitclock.db.structs.AvlReport.AssignmentType; |
| 18 | +import org.transitclock.ipc.data.IpcArrivalDeparture; |
| 19 | + |
| 20 | +/** |
| 21 | + * Behavior tests for {@link org.transitclock.core.ArrivalDepartureGeneratorDefaultImpl} |
| 22 | + * reached via {@code MatchProcessor.processArrivalDepartures}. |
| 23 | + * |
| 24 | + * <p>Drives a vehicle through two stops on block SE-08's first trip |
| 25 | + * (868588900) and asserts that arrival/departure records show up in |
| 26 | + * {@link org.transitclock.core.dataCache.StopArrivalDepartureCacheInterface}. |
| 27 | + * The current pipeline suite only checks {@code isPredictable()} — a |
| 28 | + * regression that breaks stop-traversal detection would leave the vehicle |
| 29 | + * predictable but silently stop logging arrivals/departures, making this a |
| 30 | + * distinct layer of coverage from {@link AvlProcessorBehaviorTest} and |
| 31 | + * {@link PredictionGeneratorBehaviorTest}. |
| 32 | + * |
| 33 | + * <p>Trip 868588900 stop_times (verified via the 5A fixture): |
| 34 | + * <pre> |
| 35 | + * stop_sequence 1: 14253 Dulles Airport 11:55:00 |
| 36 | + * stop_sequence 4: 13056 Herndon-Monroe Park&Ride 12:03:00 |
| 37 | + * stop_sequence 6: 14078 N Moore + 19th 12:29:57 |
| 38 | + * </pre> |
| 39 | + * The happy-path report anchors the vehicle at stop 14253 at 11:50; the |
| 40 | + * advancement report places it at 13056 a few minutes after its scheduled |
| 41 | + * arrival there. |
| 42 | + */ |
| 43 | +public class ArrivalDepartureBehaviorTest { |
| 44 | + |
| 45 | + @ClassRule |
| 46 | + public static final CoreHarness CORE = CoreHarness.withWmata5A(); |
| 47 | + |
| 48 | + private static final String BLOCK_ID = "SE-08"; |
| 49 | + |
| 50 | + // First stop of trip 868588900. |
| 51 | + private static final String STOP_FIRST_ID = "14253"; |
| 52 | + private static final double STOP_FIRST_LAT = 38.953562; |
| 53 | + private static final double STOP_FIRST_LON = -77.447485; |
| 54 | + |
| 55 | + // A later stop on the same trip (stop_sequence 4). |
| 56 | + private static final String STOP_ADVANCED_ID = "13056"; |
| 57 | + private static final double STOP_ADVANCED_LAT = 38.951704; |
| 58 | + private static final double STOP_ADVANCED_LON = -77.383009; |
| 59 | + |
| 60 | + /** 2016-06-20 11:50:00 America/New_York (EDT = UTC-4) → 15:50:00 UTC. |
| 61 | + * Anchors the vehicle at the first stop 5 minutes before its scheduled |
| 62 | + * departure, matching the known-good configuration of the happy-path |
| 63 | + * test in {@link AvlProcessorBehaviorTest}. */ |
| 64 | + private static final long AVL_AT_FIRST_STOP_EPOCH_MS = 1466437800000L; |
| 65 | + |
| 66 | + /** Keeps AVL timestamps strictly increasing across tests in this class. */ |
| 67 | + private static final AtomicLong nextTime = new AtomicLong(AVL_AT_FIRST_STOP_EPOCH_MS); |
| 68 | + |
| 69 | + private static AvlReport avlReport(String vehicleId, double lat, double lon, long timeMs) { |
| 70 | + return new AvlReport(vehicleId, timeMs, lat, lon, |
| 71 | + Float.NaN, Float.NaN, "test"); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Pushes two AVL reports for {@code vehicleId}: one at the first stop of |
| 76 | + * trip 868588900, then one at the next timepoint ~8 minutes later. The |
| 77 | + * second report creates a previous-match / current-match pair with stops |
| 78 | + * traversed in between, which is the precondition for |
| 79 | + * ArrivalDepartureGenerator to actually produce records (without a |
| 80 | + * previous match and stopPathIndex=0 it short-circuits). |
| 81 | + * |
| 82 | + * @return the VehicleState after the second report was processed. |
| 83 | + */ |
| 84 | + private static VehicleState advanceVehicleFromFirstStopToNext(String vehicleId) { |
| 85 | + // Step 1: report at the first stop — establishes the match but |
| 86 | + // generates no arrival/departure records because there is no |
| 87 | + // previous match and stopPathIndex is 0. |
| 88 | + long t1 = nextTime.getAndAdd(1); |
| 89 | + CORE.setNow(t1); |
| 90 | + AvlReport first = avlReport(vehicleId, STOP_FIRST_LAT, STOP_FIRST_LON, t1); |
| 91 | + first.setAssignment(BLOCK_ID, AssignmentType.BLOCK_ID); |
| 92 | + AvlProcessor.getInstance().processAvlReport(first); |
| 93 | + |
| 94 | + // Step 2: report at the advanced stop. Core's clock jumps forward so |
| 95 | + // that travel-time-based extrapolation is plausible; the |
| 96 | + // previous-match is the at-stop match from step 1. |
| 97 | + long t2 = t1 + (8 * 60_000L); // 8 minutes later |
| 98 | + nextTime.updateAndGet(cur -> Math.max(cur, t2 + 1)); |
| 99 | + CORE.setNow(t2); |
| 100 | + AvlReport second = avlReport(vehicleId, STOP_ADVANCED_LAT, STOP_ADVANCED_LON, t2); |
| 101 | + second.setAssignment(BLOCK_ID, AssignmentType.BLOCK_ID); |
| 102 | + AvlProcessor.getInstance().processAvlReport(second); |
| 103 | + |
| 104 | + return VehicleStateManager.getInstance().getVehicleState(vehicleId); |
| 105 | + } |
| 106 | + |
| 107 | + private static List<IpcArrivalDeparture> stopHistory(String stopId, long atEpochMs) { |
| 108 | + StopArrivalDepartureCacheKey key = new StopArrivalDepartureCacheKey(stopId, new Date(atEpochMs)); |
| 109 | + List<IpcArrivalDeparture> events = |
| 110 | + StopArrivalDepartureCacheFactory.getInstance().getStopHistory(key); |
| 111 | + // Cache returns null for "no entries" (see ehcache StopArrivalDepartureCache). |
| 112 | + // Normalize to an empty list so callers can use a uniform filter/assert pattern. |
| 113 | + return events == null ? List.of() : events; |
| 114 | + } |
| 115 | + |
| 116 | + // ---------- Tests ---------- |
| 117 | + |
| 118 | + @Test |
| 119 | + public void advancingVehicleRemainsPredictableWithNewerMatch() { |
| 120 | + // Precondition for all other assertions in this class — if the second |
| 121 | + // report doesn't match, no arrival/departure can be generated and the |
| 122 | + // rest of the suite fails for a pre-check reason rather than a real |
| 123 | + // regression. Run this first. |
| 124 | + VehicleState state = advanceVehicleFromFirstStopToNext("v-ad-pre"); |
| 125 | + assertThat(state.isPredictable()) |
| 126 | + .as("vehicle should remain predictable after advancing to next stop") |
| 127 | + .isTrue(); |
| 128 | + assertThat(state.getPreviousMatch()) |
| 129 | + .as("advancing produces a previous-match snapshot (required for AD generation)") |
| 130 | + .isNotNull(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void advancingVehicleLogsDepartureAtFirstStop() { |
| 135 | + // When the vehicle was at stop 14253 and then moved away, the |
| 136 | + // generator should emit a Departure at 14253 with isArrival=false. |
| 137 | + VehicleState state = advanceVehicleFromFirstStopToNext("v-ad-depart"); |
| 138 | + long avlTime = state.getAvlReport().getTime(); |
| 139 | + |
| 140 | + List<IpcArrivalDeparture> history = stopHistory(STOP_FIRST_ID, avlTime); |
| 141 | + assertThat(history) |
| 142 | + .as("StopArrivalDepartureCache should have at least one record for stop %s", STOP_FIRST_ID) |
| 143 | + .isNotEmpty(); |
| 144 | + |
| 145 | + boolean hasDeparture = history.stream() |
| 146 | + .filter(e -> "v-ad-depart".equals(e.getVehicleId())) |
| 147 | + .anyMatch(e -> !e.isArrival()); |
| 148 | + assertThat(hasDeparture) |
| 149 | + .as("a departure record for vehicle v-ad-depart at stop %s should exist", STOP_FIRST_ID) |
| 150 | + .isTrue(); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void advancingVehicleLogsArrivalAtNextStop() { |
| 155 | + // Counterpart to the departure test: when the vehicle arrives at |
| 156 | + // 13056, the generator should emit an Arrival there. |
| 157 | + VehicleState state = advanceVehicleFromFirstStopToNext("v-ad-arrive"); |
| 158 | + long avlTime = state.getAvlReport().getTime(); |
| 159 | + |
| 160 | + List<IpcArrivalDeparture> history = stopHistory(STOP_ADVANCED_ID, avlTime); |
| 161 | + assertThat(history) |
| 162 | + .as("StopArrivalDepartureCache should have at least one record for stop %s", STOP_ADVANCED_ID) |
| 163 | + .isNotEmpty(); |
| 164 | + |
| 165 | + boolean hasArrival = history.stream() |
| 166 | + .filter(e -> "v-ad-arrive".equals(e.getVehicleId())) |
| 167 | + .anyMatch(IpcArrivalDeparture::isArrival); |
| 168 | + assertThat(hasArrival) |
| 169 | + .as("an arrival record for vehicle v-ad-arrive at stop %s should exist", STOP_ADVANCED_ID) |
| 170 | + .isTrue(); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void generatedRecordsCarryCorrectBlockAndTripMetadata() { |
| 175 | + // Pull any record generated for our vehicle out of the first stop's |
| 176 | + // cache and assert its block/trip/stop fields line up with what we |
| 177 | + // assigned. Defends against a silent regression that swaps |
| 178 | + // identifying fields during IpcArrivalDeparture marshalling. |
| 179 | + // |
| 180 | + // NOTE: IpcArrivalDeparture marks routeId, routeShortName, and |
| 181 | + // serviceId as `transient`, so they are null after the Kyro |
| 182 | + // serializer round-trips through ehcache. Assertions therefore only |
| 183 | + // cover fields that survive serialization. |
| 184 | + VehicleState state = advanceVehicleFromFirstStopToNext("v-ad-meta"); |
| 185 | + long avlTime = state.getAvlReport().getTime(); |
| 186 | + |
| 187 | + List<IpcArrivalDeparture> history = stopHistory(STOP_FIRST_ID, avlTime); |
| 188 | + IpcArrivalDeparture mine = history.stream() |
| 189 | + .filter(e -> "v-ad-meta".equals(e.getVehicleId())) |
| 190 | + .findFirst() |
| 191 | + .orElseThrow(() -> new AssertionError( |
| 192 | + "no arrival/departure recorded for v-ad-meta at stop " + STOP_FIRST_ID)); |
| 193 | + |
| 194 | + assertThat(mine.getBlockId()).isEqualTo(BLOCK_ID); |
| 195 | + assertThat(mine.getStopId()).isEqualTo(STOP_FIRST_ID); |
| 196 | + assertThat(mine.getTripId()).isEqualTo("868588900"); |
| 197 | + assertThat(mine.getDirectionId()).isEqualTo("0"); |
| 198 | + } |
| 199 | +} |
0 commit comments