Response types checked

This commit is contained in:
Stefan Kalscheuer 2016-12-27 17:29:59 +01:00
parent 4b6b09ce52
commit 727e7bd171
2 changed files with 17 additions and 13 deletions

View File

@ -40,6 +40,9 @@ public class UraClient {
private static final String PAR_TRIP_ID = "TripID"; private static final String PAR_TRIP_ID = "TripID";
private static final String PAR_ESTTIME = "EstimatedTime"; private static final String PAR_ESTTIME = "EstimatedTime";
private static final Integer RES_TYPE_STOP = 0;
private static final Integer RES_TYPE_PREDICTION = 1;
private static final String[] REQUEST_STOP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION}; private static final String[] REQUEST_STOP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION};
private static final String[] REQUEST_TRIP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION, private static final String[] REQUEST_TRIP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION,
@ -142,13 +145,11 @@ public class UraClient {
try (InputStream is = requestInstant(REQUEST_TRIP, stops, lines); try (InputStream is = requestInstant(REQUEST_TRIP, stops, lines);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) { BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line; String line;
boolean first = false; while ((line = br.readLine()) != null && (limit == null || trips.size() < limit)) {
while ((line = br.readLine()) != null) { List l = mapper.readValue(line, List.class);
if (!first) { /* Check if result exists and has correct response type */
first = true; if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_PREDICTION))
continue; trips.add(new Trip(l));
}
trips.add(new Trip(mapper.readValue(line, List.class)));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -166,13 +167,11 @@ public class UraClient {
try (InputStream is = requestInstant(REQUEST_STOP, null, null); try (InputStream is = requestInstant(REQUEST_STOP, null, null);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) { BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line; String line;
boolean first = false;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
if (!first) { List l = mapper.readValue(line, List.class);
first = true; /* Check if result exists and has correct response type */
continue; if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_STOP))
} stops.add(new Stop(l));
stops.add(new Stop(mapper.readValue(line, List.class)));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -63,6 +63,11 @@ public class UraClientTest {
assertThat(trips.get(7).getEstimatedTime(), is(1482854580000L)); assertThat(trips.get(7).getEstimatedTime(), is(1482854580000L));
assertThat(trips.get(8).getVisitID(), is(30)); assertThat(trips.get(8).getVisitID(), is(30));
assertThat(trips.get(9).getStop().getId(), is("100002")); assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Get limited number of trips */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_trips_all.txt"));
trips = new UraClient("mocked").getTrips(5);
assertThat(trips, hasSize(5));
} }
@Test @Test