-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the OBA REST API trips-for-route method
* See docs at http://developer.onebusaway.org/modules/onebusaway-application-modules/current/api/where/methods/trips-for-route.html * This allows us to get active information about all current trips for a route, including status information, which includes vehicle position (towards #176) * Also add unit test for API * This also fixes a bug in ObaTripStatus that was preventing vehicle position from being correctly populated from the JSON response. More unit tests have been added to check that vehicle position is being parsed correctly in both ArrivalsAndDeparturesForStop and TripsForRoute API responses.
- Loading branch information
Showing
8 changed files
with
4,044 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...android/src/androidTest/java/org/onebusaway/android/io/test/TripsForRouteRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (C) 2015 University of South Florida (sjbarbeau@gmail.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.onebusaway.android.io.test; | ||
|
||
import org.onebusaway.android.UriAssert; | ||
import org.onebusaway.android.app.Application; | ||
import org.onebusaway.android.io.elements.ObaRegion; | ||
import org.onebusaway.android.io.elements.ObaTripDetails; | ||
import org.onebusaway.android.io.request.ObaTripsForRouteRequest; | ||
import org.onebusaway.android.io.request.ObaTripsForRouteResponse; | ||
import org.onebusaway.android.mock.MockRegion; | ||
|
||
import java.util.HashMap; | ||
|
||
@SuppressWarnings("serial") | ||
public class TripsForRouteRequestTest extends ObaTestCase { | ||
|
||
protected final String TEST_ROUTE_ID = "Hillsborough Area Regional Transit_5"; | ||
|
||
public void testHARTTripsForRouteRequest() { | ||
// Test by setting API directly | ||
Application.get().setCustomApiUrl("api.tampa.onebusaway.org/api"); | ||
callHARTTripsForRouteRequest(); | ||
Application.get().setCustomApiUrl(null); | ||
|
||
// Test by setting region | ||
ObaRegion tampa = MockRegion.getTampa(getContext()); | ||
assertNotNull(tampa); | ||
Application.get().setCurrentRegion(tampa); | ||
callHARTTripsForRouteRequest(); | ||
Application.get().setCurrentRegion(null); | ||
} | ||
|
||
private void callHARTTripsForRouteRequest() { | ||
ObaTripsForRouteRequest.Builder builder = new ObaTripsForRouteRequest.Builder(getContext(), | ||
TEST_ROUTE_ID); | ||
ObaTripsForRouteRequest request = builder.build(); | ||
UriAssert.assertUriMatch( | ||
"http://api.tampa.onebusaway.org/api/api/where/trips-for-route/" + TEST_ROUTE_ID | ||
+ ".json", | ||
new HashMap<String, String>() {{ | ||
put("key", "*"); | ||
put("version", "2"); | ||
}}, | ||
request | ||
); | ||
} | ||
|
||
public void testHARTTripsForRouteResponse() throws Exception { | ||
// Test by setting region | ||
ObaRegion tampa = MockRegion.getTampa(getContext()); | ||
assertNotNull(tampa); | ||
Application.get().setCurrentRegion(tampa); | ||
|
||
// No trip status included | ||
ObaTripsForRouteResponse response = | ||
new ObaTripsForRouteRequest.Builder(getContext(), TEST_ROUTE_ID) | ||
.build() | ||
.call(); | ||
assertOK(response); | ||
ObaTripDetails[] trips = response.getTrips(); | ||
|
||
assertEquals("Hillsborough Area Regional Transit_98682", trips[0].getId()); | ||
assertNull(trips[0].getStatus()); | ||
|
||
// Include vehicles status | ||
response = | ||
new ObaTripsForRouteRequest.Builder(getContext(), TEST_ROUTE_ID) | ||
.setIncludeStatus(true) | ||
.build() | ||
.call(); | ||
assertOK(response); | ||
trips = response.getTrips(); | ||
|
||
assertEquals("Hillsborough Area Regional Transit_101446", trips[0].getId()); | ||
assertEquals("Hillsborough Area Regional Transit_2415", | ||
trips[0].getStatus().getVehicleId()); | ||
assertEquals("Hillsborough Area Regional Transit_4707", trips[0].getStatus().getNextStop()); | ||
assertEquals(420, trips[0].getStatus().getScheduleDeviation()); | ||
assertEquals(28.063130557136404, trips[0].getStatus().getPosition().getLatitude()); | ||
assertEquals(-82.43457, trips[0].getStatus().getPosition().getLongitude()); | ||
} | ||
|
||
public void testNewRequest() { | ||
// Test by setting API directly | ||
Application.get().setCustomApiUrl("api.tampa.onebusaway.org/api"); | ||
callNewRequest(); | ||
Application.get().setCustomApiUrl(null); | ||
|
||
// Test by setting region | ||
ObaRegion tampa = MockRegion.getTampa(getContext()); | ||
assertNotNull(tampa); | ||
Application.get().setCurrentRegion(tampa); | ||
callNewRequest(); | ||
Application.get().setCurrentRegion(null); | ||
} | ||
|
||
private void callNewRequest() { | ||
// This is just to make sure we copy and call newRequest() at least once | ||
ObaTripsForRouteRequest request = ObaTripsForRouteRequest | ||
.newRequest(getContext(), TEST_ROUTE_ID); | ||
UriAssert.assertUriMatch( | ||
"http://api.tampa.onebusaway.org/api/api/where/trips-for-route/" + TEST_ROUTE_ID | ||
+ ".json", | ||
new HashMap<String, String>() {{ | ||
put("key", "*"); | ||
put("version", "2"); | ||
}}, | ||
request | ||
); | ||
} | ||
} |
Oops, something went wrong.