|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2026 Open Transit Software Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.onebusaway.android.extrapolation.test |
| 17 | + |
| 18 | +import androidx.test.InstrumentationRegistry.getTargetContext |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Assert.assertNotNull |
| 21 | +import org.junit.Assert.assertNull |
| 22 | +import org.junit.Assert.assertSame |
| 23 | +import org.junit.Assert.assertTrue |
| 24 | +import org.junit.Test |
| 25 | +import org.onebusaway.android.app.Application |
| 26 | +import org.onebusaway.android.extrapolation.data.toObservations |
| 27 | +import org.onebusaway.android.io.request.ObaTripDetailsRequest |
| 28 | +import org.onebusaway.android.io.request.ObaTripsForRouteRequest |
| 29 | +import org.onebusaway.android.io.test.ObaTestCase |
| 30 | +import org.onebusaway.android.mock.MockRegion |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests the response adapters (Adapters.kt) against mock API responses from /res/raw: the |
| 34 | + * distillation of trip-details and trips-for-route responses into TripObservations, including |
| 35 | + * the skip guards for responses without a status or an active trip ID. |
| 36 | + */ |
| 37 | +class AdaptersTest : ObaTestCase() { |
| 38 | + |
| 39 | + companion object { |
| 40 | + private const val HART_TRIP_ID = "Hillsborough Area Regional Transit_1389962" |
| 41 | + private const val HART_ROUTE_ID = "Hillsborough Area Regional Transit_5" |
| 42 | + } |
| 43 | + |
| 44 | + // --- ObaTripDetailsResponse.toObservations --- |
| 45 | + |
| 46 | + @Test |
| 47 | + fun detailsResponseDistillsOneObservation() { |
| 48 | + Application.get().setCurrentRegion(MockRegion.getTampa(getTargetContext())) |
| 49 | + val response = |
| 50 | + ObaTripDetailsRequest.Builder(getTargetContext(), HART_TRIP_ID).build().call() |
| 51 | + assertOK(response) |
| 52 | + |
| 53 | + val observations = response.toObservations() |
| 54 | + assertEquals(1, observations.size) |
| 55 | + |
| 56 | + val observation = observations[0] |
| 57 | + assertEquals(HART_TRIP_ID, observation.tripId) |
| 58 | + assertSame(response.status, observation.status) |
| 59 | + assertEquals(response.currentTime, observation.serverTimeMs) |
| 60 | + assertEquals(1545886800000L, observation.serviceDate) |
| 61 | + // Resolved through the refs: trip 1389962 -> route 16 -> type 3 (bus) |
| 62 | + assertEquals(3, observation.routeType ?: -1) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun detailsResponseWithoutStatusYieldsNoObservations() { |
| 67 | + // Puget Sound fixture: schedule and refs present, no vehicle status |
| 68 | + val response = |
| 69 | + ObaTripDetailsRequest.Builder(getTargetContext(), "1_18196913").build().call() |
| 70 | + assertOK(response) |
| 71 | + assertNull(response.status) |
| 72 | + |
| 73 | + assertTrue(response.toObservations().isEmpty()) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun detailsResponseWithoutActiveTripIdYieldsNoObservations() { |
| 78 | + Application.get().setCurrentRegion(MockRegion.getTampa(getTargetContext())) |
| 79 | + val response = |
| 80 | + ObaTripDetailsRequest.Builder( |
| 81 | + getTargetContext(), |
| 82 | + "${HART_TRIP_ID}_no_active_trip" |
| 83 | + ) |
| 84 | + .build() |
| 85 | + .call() |
| 86 | + assertOK(response) |
| 87 | + // Status present but the vehicle reports no active trip (e.g. between runs) |
| 88 | + assertNotNull(response.status) |
| 89 | + assertNull(response.status!!.activeTripId) |
| 90 | + |
| 91 | + assertTrue(response.toObservations().isEmpty()) |
| 92 | + } |
| 93 | + |
| 94 | + // --- ObaTripsForRouteResponse.toObservations --- |
| 95 | + |
| 96 | + @Test |
| 97 | + fun tripsForRouteResponseDistillsOneObservationPerActiveTrip() { |
| 98 | + Application.get().setCurrentRegion(MockRegion.getTampa(getTargetContext())) |
| 99 | + val response = |
| 100 | + ObaTripsForRouteRequest.Builder(getTargetContext(), HART_ROUTE_ID) |
| 101 | + .setIncludeStatus(true) |
| 102 | + .build() |
| 103 | + .call() |
| 104 | + assertOK(response) |
| 105 | + |
| 106 | + val observations = response.toObservations() |
| 107 | + assertEquals(38, observations.size) |
| 108 | + |
| 109 | + val first = observations[0] |
| 110 | + assertEquals("Hillsborough Area Regional Transit_101446", first.tripId) |
| 111 | + assertEquals(response.currentTime, first.serverTimeMs) |
| 112 | + assertEquals(1444017600000L, first.serviceDate) |
| 113 | + assertEquals(3, first.routeType ?: -1) |
| 114 | + |
| 115 | + // Each observation is keyed by the trip its vehicle reported as active |
| 116 | + for (observation in observations) { |
| 117 | + assertEquals(observation.status.activeTripId, observation.tripId) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun tripsForRouteResponseWithoutStatusesYieldsNoObservations() { |
| 123 | + Application.get().setCurrentRegion(MockRegion.getTampa(getTargetContext())) |
| 124 | + val response = |
| 125 | + ObaTripsForRouteRequest.Builder(getTargetContext(), HART_ROUTE_ID) |
| 126 | + .build() |
| 127 | + .call() |
| 128 | + assertOK(response) |
| 129 | + |
| 130 | + assertTrue(response.toObservations().isEmpty()) |
| 131 | + } |
| 132 | +} |
0 commit comments