Queries for destination, towards and GPS coordinates

This commit is contained in:
Stefan Kalscheuer 2017-01-07 18:33:29 +01:00
parent 746696405c
commit d7f6c34fc0
10 changed files with 221 additions and 15 deletions

View File

@ -51,6 +51,8 @@ public class UraClient {
private static final String PAR_VEHICLE_ID = "VehicleID";
private static final String PAR_TRIP_ID = "TripID";
private static final String PAR_ESTTIME = "EstimatedTime";
private static final String PAR_TOWARDS = "Towards";
private static final String PAR_CIRCLE = "Circle";
private static final Integer RES_TYPE_STOP = 0;
private static final Integer RES_TYPE_PREDICTION = 1;
@ -138,6 +140,38 @@ public class UraClient {
return new Query().forDirection(direction);
}
/**
* Builder pattern to request given destination names.
*
* @param destinationNames destination names
* @return the request
*/
public Query forDestinationNames(final String... destinationNames) {
return new Query().forDestinationNames(destinationNames);
}
/**
* Builder pattern to request given direction defined by stop point name.
*
* @param towards towards stop point names
* @return the request
*/
public Query towards(final String... towards) {
return new Query().towards(towards);
}
/**
* Builder pattern to request given destination names.
*
* @param latitude Latitude (WGS84)
* @param longitude Longitude (WGS84)
* @param radius Search radius (meters)
* @return the request
*/
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
return new Query().forPosition(latitude, longitude, radius);
}
/**
* Get list of trips.
* If forStops() and/or forLines() has been called, those will be used as filter.
@ -179,7 +213,7 @@ public class UraClient {
*/
public List<Trip> getTrips(final Query query, final Integer limit) {
List<Trip> trips = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_TRIP, query.stopIDs, query.stopNames, query.lineIDs, query.lineNames, query.direction);
try (InputStream is = requestInstant(REQUEST_TRIP, query);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String version = null;
String line;
@ -217,7 +251,7 @@ public class UraClient {
*/
public List<Stop> getStops(Query query) {
List<Stop> stops = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_STOP, query.stopIDs, query.stopNames, query.lineIDs, query.lineNames, query.direction);
try (InputStream is = requestInstant(REQUEST_STOP, query);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
String version;
@ -237,21 +271,28 @@ public class UraClient {
* Issue request to instant endpoint and return input stream.
*
* @param returnList fields to fetch
* @param query the query
* @return Input stream of the URL
* @throws IOException on errors
*/
private InputStream requestInstant(String[] returnList, String[] stopIDs, String[] stopNames, String[] lineIDs, String[] lineNames, Integer direction) throws IOException {
private InputStream requestInstant(String[] returnList, Query query) throws IOException {
String urlStr = baseURL + instantURL + "?ReturnList=" + String.join(",", returnList);
if (stopIDs != null && stopIDs.length > 0)
urlStr += "&" + PAR_STOP_ID + "=" + String.join(",", stopIDs);
if (stopNames != null && stopNames.length > 0)
urlStr += "&" + PAR_STOP_NAME + "=" + String.join(",", stopNames);
if (lineIDs != null && lineIDs.length > 0)
urlStr += "&" + PAR_LINE_ID + "=" + String.join(",", lineIDs);
if (lineNames != null && lineNames.length > 0)
urlStr += "&" + PAR_LINE_NAME + "=" + String.join(",", lineNames);
if (direction != null)
urlStr += "&" + PAR_DIR_ID + "=" + direction;
if (query.stopIDs != null && query.stopIDs.length > 0)
urlStr += "&" + PAR_STOP_ID + "=" + String.join(",", query.stopIDs);
if (query.stopNames != null && query.stopNames.length > 0)
urlStr += "&" + PAR_STOP_NAME + "=" + String.join(",", query.stopNames);
if (query.lineIDs != null && query.lineIDs.length > 0)
urlStr += "&" + PAR_LINE_ID + "=" + String.join(",", query.lineIDs);
if (query.lineNames != null && query.lineNames.length > 0)
urlStr += "&" + PAR_LINE_NAME + "=" + String.join(",", query.lineNames);
if (query.direction != null)
urlStr += "&" + PAR_DIR_ID + "=" + query.direction;
if (query.destinationNames != null)
urlStr += "&" + PAR_DEST_NAME + "=" + String.join(",", query.destinationNames);
if (query.towards != null)
urlStr += "&" + PAR_TOWARDS + "=" + String.join(",", query.towards);
if (query.circle != null)
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
URL url = new URL(urlStr);
return url.openStream();
}
@ -265,6 +306,9 @@ public class UraClient {
private String[] lineIDs;
private String[] lineNames;
private Integer direction;
private String[] destinationNames;
private String[] towards;
private String circle;
/**
* Builder pattern to request given line IDs.
@ -321,6 +365,41 @@ public class UraClient {
return this;
}
/**
* Builder pattern to request given destination names
*
* @param destinationNames names of destinations
* @return the query
*/
public Query forDestinationNames(final String... destinationNames) {
this.destinationNames = destinationNames;
return this;
}
/**
* Builder pattern to request given direction defined by stop point name.
*
* @param towards towards stop point names
* @return the request
*/
public Query towards(final String... towards) {
this.towards = towards;
return this;
}
/**
* Builder pattern to request given position and radius.
*
* @param latitude Latitude (WGS84)
* @param longitude Longitude (WGS84)
* @param radius Search radius (meters)
* @return the query
*/
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
this.circle = latitude.toString() + "," + longitude.toString() + "," + radius.toString();
return this;
}
/**
* Get stops for set filters.
*

View File

@ -32,6 +32,7 @@ import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
/**
@ -49,7 +50,7 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_stops.txt"));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").getStops();
@ -77,7 +78,7 @@ public class UraClientTest {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_stops_line.txt"));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forLines("33").getStops();
@ -90,6 +91,65 @@ public class UraClientTest {
assertThat(stops.get(5).getLongitude(), is(6.2314072));
}
@Test
public void getStopsForPositionTest() throws Exception {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forPosition(51.51009, -0.1345734, 200).getStops();
assertThat(stops, hasSize(13));
assertThat(stops.get(0).getId(), is("156"));
assertThat(stops.get(1).getName(), is("Piccadilly Circus"));
assertThat(stops.get(2).getState(), is(0));;
assertThat(stops.get(3).getLatitude(), is(51.509154));
assertThat(stops.get(4).getLongitude(), is(-0.134172));
assertThat(stops.get(5).getIndicator(), is(nullValue()));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle_name.txt"));
stops = new UraClient("mocked").forStopsByName("Piccadilly Circus").forPosition(51.51009, -0.1345734, 200).getStops();
assertThat(stops, hasSize(7));
assertThat(stops.stream().filter(t -> !t.getName().equals("Piccadilly Circus")).findAny(), is(Optional.empty()));
}
@Test
public void getTripsForDestinationNamesTest() throws Exception {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt"));
/* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips();
assertThat(trips, hasSize(9));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(), is(Optional.empty()));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_destination.txt"));
trips = new UraClient("mocked").forStops("156").forDestinationNames("Marble Arch").getTrips();
assertThat(trips, hasSize(5));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Marble Arch")).findAny(), is(Optional.empty()));
}
@Test
public void getTripsTowardsTest() throws Exception {
/* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt"));
/* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(10));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt"));
trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(17));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
}
@Test
public void getTripsTest() throws Exception {
/* Mock the HTTP call */

View File

@ -0,0 +1,14 @@
[4,"1.0",1483808583971]
[0,"Piccadilly Circus","156","D",0,51.509822,-0.136967]
[0,"Piccadilly Circus","149","G",0,51.51006,-0.135545]
[0,"Piccadilly Circus","155","C",0,51.50986,-0.135971]
[0,"Piccadilly Circus","34716","S",0,51.509154,-0.136245]
[0,"Trocadero / Haymarket","161","H",0,51.510515,-0.134172]
[0,"Piccadilly Circus, Haymarket","PCIRHM S",null,0,51.509709,-0.13272]
[0,"Regent Street / St James's","17683","Z",0,51.508732,-0.134043]
[0,"Regent Street / St James's","154","Y",0,51.508942,-0.134207]
[0,"Piccadilly Circus","BP5840","E",0,51.509882,-0.137325]
[0,"Piccadilly Circus","150","F",0,51.509894,-0.136402]
[0,"Trocadero / Haymarket","37229","K",0,51.511194,-0.133366]
[0,"Piccadilly Circus","37122","B",0,51.509306,-0.13621]
[0,"Haymarket / Jermyn Street","29867","R",0,51.509691,-0.132706]

View File

@ -0,0 +1,8 @@
[4,"1.0",1483808692732]
[0,"Piccadilly Circus","156","D",0,51.509822,-0.136967]
[0,"Piccadilly Circus","149","G",0,51.51006,-0.135545]
[0,"Piccadilly Circus","155","C",0,51.50986,-0.135971]
[0,"Piccadilly Circus","34716","S",0,51.509154,-0.136245]
[0,"Piccadilly Circus","BP5840","E",0,51.509882,-0.137325]
[0,"Piccadilly Circus","150","F",0,51.509894,-0.136402]
[0,"Piccadilly Circus","37122","B",0,51.509306,-0.13621]

View File

@ -0,0 +1,10 @@
[4,"1.0",1483809193233]
[1,"New Bond Street","829","OE",0,51.514814,-0.146117,1,"94","94",1,"Piccadilly Cir","Piccadilly Circus",15925,443778,1483810113000]
[1,"New Bond Street","829","OE",0,51.514814,-0.146117,1,"94","94",1,"Piccadilly Cir","Piccadilly Circus",15209,443756,1483810614000]
[1,"Hobury Street / Worlds End","11296","QH",0,51.482463,-0.179129,1,"22","22",1,"Piccadilly Cir","Piccadilly Circus",8923,390782,1483810037000]
[1,"Abinger Road","11333",null,0,51.496971,-0.24965,1,"94","94",1,"Piccadilly Cir","Piccadilly Circus",15554,444015,1483809664000]
[1,"Knightsbridge Station","210","KJ",0,51.500899,-0.160183,1,"22","22",1,"Piccadilly Cir","Piccadilly Circus",7988,392720,1483810456000]
[1,"Piccadilly Circus","37122","B",0,51.509306,-0.13621,1,"22","22",1,"Piccadilly Cir","Piccadilly Circus",8027,502319,1483809941000]
[1,"Edith Grove / Worlds End","37444",null,0,51.481603,-0.182217,1,"22","22",1,"Piccadilly Cir","Piccadilly Circus",8015,392809,1483810291000]
[1,"Selfridges","153","BB",0,51.514067,-0.152921,1,"94","94",1,"Piccadilly Cir","Piccadilly Circus",15105,444021,1483809200000]
[1,"Edith Grove / Worlds End","37444",null,0,51.481603,-0.182217,1,"22","22",1,"Piccadilly Cir","Piccadilly Circus",7988,392720,1483809409000]

View File

@ -0,0 +1,6 @@
[4,"1.0",1483809267076]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16588,309783,1483809502000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16589,239190,1483809796000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16211,310616,1483810325000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16549,238574,1483810733000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16213,216852,1483810840000]

View File

@ -0,0 +1,18 @@
[4,"1.0",1483809773487]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"94","94",2,"Acton Green","Acton Green",15294,445809,1483809898000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16589,239190,1483809806000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"6","6",2,"Willesden Gar","Willesden, Bus Garage",10600,216033,1483810030000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"23","23",2,"Westbourne Park","Westbourne Park",20276,685207,1483810231000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"94","94",2,"Acton Green","Acton Green",15296,445944,1483810322000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"6","6",2,"Willesden Gar","Willesden, Bus Garage",10603,216027,1483810421000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16211,310616,1483810135000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"94","94",2,"Acton Green","Acton Green",15105,445989,1483810449000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"23","23",2,"Westbourne Park","Westbourne Park",20322,690792,1483810642000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"94","94",2,"Acton Green","Acton Green",15575,445855,1483810750000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16213,216852,1483810840000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"6","6",2,"Willesden Gar","Willesden, Bus Garage",10602,216822,1483810854000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"23","23",2,"Westbourne Park","Westbourne Park",20337,689508,1483811038000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16549,238574,1483810791000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"94","94",2,"Acton Green","Acton Green",15925,445840,1483811317000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"6","6",2,"Willesden Gar","Willesden, Bus Garage",10596,216025,1483811331000]
[1,"Piccadilly Circus","156","D",0,51.509822,-0.136967,1,"159","159",1,"Marble Arch","Marble Arch",16713,310640,1483811409000]

View File

@ -0,0 +1,11 @@
[4,"1.0",1483810124081]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"36","36",1,"New Cross Gate","New Cross Gate",8864,595212,1483810191000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"7","7",1,"Oxford Circus","Oxford Circus",9932,109224,1483810173000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"36","36",1,"New Cross Gate","New Cross Gate",8862,597163,1483810551000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"23","23",1,"Liverpool St","Liverpool Street",20333,690728,1483810367000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"7","7",1,"Oxford Circus","Oxford Circus",9861,106419,1483810702000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"36","36",1,"New Cross Gate","New Cross Gate",9407,599527,1483810830000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"23","23",1,"Liverpool St","Liverpool Street",20335,683419,1483810677000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"36","36",1,"New Cross Gate","New Cross Gate",9379,599540,1483811196000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"7","7",1,"Oxford Circus","Oxford Circus",10262,109245,1483811093000]
[1,"Porchester Terrace North","16972","L",0,51.516451,-0.186379,1,"23","23",1,"Liverpool St","Liverpool Street",20099,684947,1483811250000]