15 Commits

Author SHA1 Message Date
93acfad184 Bump version to 1.1.1 2018-02-20 19:40:24 +01:00
daa84f61c5 Minor dependency updates 2018-02-20 19:32:38 +01:00
0304f6e0f4 Update copyright notice to 2018 2018-01-01 17:28:24 +01:00
56d482b723 Test framework migrated to Mockito/Powermock 2
This simple migration with the bare minimum of changes necessary solves
compatibility issues with Java 9 build environments and presumably makes
JaCoCo offline analysis superfluous.
2017-11-25 18:56:34 +01:00
afffaa4a6f Bundled error messages for model creation in interface 2017-08-29 20:13:28 +02:00
e12775b6ae Refactored empty-checks on result lists 2017-08-29 20:13:27 +02:00
6b0481a5e1 Encapsulated errors in IllegalStateException
getList() and getStops() no longer silently ignore IOExceptions and also
do not print StackTraces any more.
2017-08-29 20:13:20 +02:00
ca9578a4d3 JaCoCo offline analysis
As PowerMock instrumented classes (used to mock API requests here)
messes up with JaCoCo coverage analysis, Maven build is changed to
offline instrumentation.
2017-08-19 17:34:39 +02:00
9ecb3763af Add SonarCloud to Travis 2017-08-18 14:08:35 +02:00
a8fc93fb41 Model classes and input parameters declared final
As model classes are not designed for inheritance and about all method
parameters are already effectively final, they have been explicitly
declared final.
2017-08-04 10:12:12 +02:00
1f4bd9f411 Code style
Various code style improvements:
lines trimmed to 120 characters,
complete JavaDoc for public methods,
braces on multiline if-else,
fixed numbers defined as constants
2017-08-04 10:06:44 +02:00
ee1d8bd2c8 Minor ReadMe adjustments 2017-08-02 12:36:46 +02:00
4b3a255e18 Bump Jackson to 2.9.0 2017-08-02 12:31:38 +02:00
6fab3ab6fe POM and changelog updates 2017-05-14 10:37:32 +02:00
35728fa9b1 Badges in ReadMe 2017-01-07 19:09:00 +01:00
12 changed files with 650 additions and 211 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target/
.idea/
*.iml

View File

@ -1,3 +1,15 @@
language: java language: java
jdk: jdk:
- oraclejdk8 - oraclejdk8
install: true
addons:
sonarcloud:
organization: "stklcode-github"
token:
secure: "FkEe/+MKpF4pSX3ZYOgu7oeIKf0460Q3XVLUhIX9bk2dyY8hoab74oCo4FtD7jim0+ZC13JVHGDX7iOQMUtS5EZ+x+pA0qpppzCK5zV8afN/l46HJ07kJldvr+EH0klbDVMFZQ5dT7r/w6CoDzjtENHzKQAJLcheUVDNpkcuBdaplTqIAVf3lQpKtOuVjQJ5qZDwwS5wsHNqPcYbcEGrPmcKDVnp3mD3bfI6dT1bbRt845QcD73rPYkQKxen8eIwJxFf5MZStgvbj7yphPxPGwoLAsoLP6LpThTDYcrg+vgUnSs+l9GckL3IbhPAmecixLbKVnphBZzRTvpdMTt5KeOoAJ2nM6RLs5cRCqiEgLEioWkVaSH5WxoBj38Z1h4fTsDV3dTcCuQWX8GFxdeeTelu+XbatdRWMnUgiF7oax+uNvR62fasTbAc7dWPJbARiD7ZbkWH4nHEY07xKKx87xzUz36ZeEHGoBXgqnLmv/FCwqMrEpOoIT41fc0WYtdIA4wjRoAyG0u+wNBMbVlf4PK72seM4b/bmU+TtmaaVla/SvNOiz+A3DHxtJEUScPcL3QGjviddglMf+wyD6VXVViq9VuYRKZFyjpuoNpb5lwEbwmRnmLabBx8jBgyPinjpmqHYlIntcPAwuyLRaqTHFcmCrbeeZEf7KaPRYKx+Cs="
cache:
directories:
- '$HOME/.m2/repository'
- '$HOME/.sonar/cache'
script:
- mvn -P jacoco clean package sonar:sonar

12
CHANGELOG.md Normal file
View File

@ -0,0 +1,12 @@
## 1.1.1 [2018-02-20]
* [improvement] On connection or parsing errors, the `IOException` is no longer ignored, but encapsulated in `RuntimeException` (no StackTraces printed)
* [internal] Code cleanup and minor improvements
* [dependeny] Minor dependency updates
## 1.1.0 [2017-01-07]
* [feature] Filter stops by coordinates and radius
* [feature] Filter trips by destination and and towards fields
* [test] Test coverage 100% (line); tested against ASEAG and TFL APIs
## 1.0.0 [2017-01-02]
* Initial release

View File

@ -1,4 +1,8 @@
# jURAclient [![Build status](https://travis-ci.org/stklcode/juraclient.svg?branch=master)](https://travis-ci.org/stklcode/juraclient) # jURAclient
[![Build status](https://travis-ci.org/stklcode/juraclient.svg?branch=master)](https://travis-ci.org/stklcode/juraclient)
[![Quality Gate](https://sonarcloud.io/api/badges/gate?key=de.stklcode.pubtrans%3Ajuraclient)](https://sonarcloud.io/dashboard?id=de.stklcode.pubtrans%3Ajuraclient)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/stklcode/juraclient/blob/master/LICENSE.txt)
[![Maven Central](https://img.shields.io/maven-central/v/de.stklcode.pubtrans/juraclient.svg)](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.stklcode.pubtrans%22%20AND%20a%3A%22juraclient%22)
Java client for URA based public transport APIs. Java client for URA based public transport APIs.
@ -50,7 +54,7 @@ List<Trip> trips = ura.forStopByName("Piccadilly Circus")
<dependency> <dependency>
<groupId>de.stklcode.pubtrans</groupId> <groupId>de.stklcode.pubtrans</groupId>
<artifactId>juraclient</artifactId> <artifactId>juraclient</artifactId>
<version>1.1.0</version> <version>1.1.1</version>
</dependency> </dependency>
``` ```
@ -61,4 +65,4 @@ List<Trip> trips = ura.forStopByName("Piccadilly Circus")
## License ## License
The project is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). The project is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).

104
pom.xml
View File

@ -6,22 +6,56 @@
<groupId>de.stklcode.pubtrans</groupId> <groupId>de.stklcode.pubtrans</groupId>
<artifactId>juraclient</artifactId> <artifactId>juraclient</artifactId>
<version>1.1.0</version> <version>1.1.1</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<packaging>jar</packaging>
<name>jURAclient</name>
<description>Java client for URA based public transport API</description>
<url>https://github.com/stklcode/juraclient</url>
<inceptionYear>2016</inceptionYear>
<licenses>
<license>
<name>Apache License 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<name>Stefan Kalscheuer</name>
<email>stefan@stklcode.de</email>
<timezone>+1</timezone>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/stklcode/juraclient.git</connection>
<developerConnection>scm:git:git@github.com:stklcode/juraclient.git</developerConnection>
<url>https://github.com/stklcode/juraclient</url>
</scm>
<issueManagement>
<system>github</system>
<url>https://github.com/stklcode/juraclient/issues</url>
</issueManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
<version>2.8.5</version> <version>2.9.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.8.5</version> <version>2.9.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -38,13 +72,13 @@
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId> <artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version> <version>2.0.0-beta.5</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito2</artifactId>
<version>1.6.6</version> <version>2.0.0-beta.5</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -54,7 +88,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version> <version>3.7.0</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
@ -62,4 +96,60 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>jacoco</id>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.0</version>
<classifier>runtime</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,20 +70,20 @@ public class UraClient {
/** /**
* Constructor with base URL and default API paths. * Constructor with base URL and default API paths.
* *
* @param baseURL the base URL (with protocol, without trailing slash) * @param baseURL The base URL (with protocol, without trailing slash).
*/ */
public UraClient(String baseURL) { public UraClient(final String baseURL) {
this(baseURL, DEFAULT_INSTANT_URL, DEFAULT_STREAM_URL); this(baseURL, DEFAULT_INSTANT_URL, DEFAULT_STREAM_URL);
} }
/** /**
* Constructor with base URL and custom API paths * Constructor with base URL and custom API paths.
* *
* @param baseURL the base URL (including protocol) * @param baseURL The base URL (including protocol).
* @param instantURL the path for instant requests * @param instantURL The path for instant requests.
* @param streamURL the path for stream requests * @param streamURL The path for stream requests.
*/ */
public UraClient(String baseURL, String instantURL, String streamURL) { public UraClient(final String baseURL, final String instantURL, final String streamURL) {
this.baseURL = baseURL; this.baseURL = baseURL;
this.instantURL = instantURL; this.instantURL = instantURL;
this.streamURL = streamURL; this.streamURL = streamURL;
@ -96,7 +96,7 @@ public class UraClient {
* @param stops Stop IDs * @param stops Stop IDs
* @return the request * @return the request
*/ */
public Query forStops(final String... stops) { public final Query forStops(final String... stops) {
return new Query().forStops(stops); return new Query().forStops(stops);
} }
@ -106,72 +106,72 @@ public class UraClient {
* @param stopNames Stop Point Names * @param stopNames Stop Point Names
* @return the request * @return the request
*/ */
public Query forStopsByName(final String... stopNames) { public final Query forStopsByName(final String... stopNames) {
return new Query().forStopsByName(stopNames); return new Query().forStopsByName(stopNames);
} }
/** /**
* Builder pattern to request given line IDs. * Builder pattern to request given line IDs.
* *
* @param lines line IDs * @param lines Line IDs.
* @return the request * @return The request.
*/ */
public Query forLines(final String... lines) { public final Query forLines(final String... lines) {
return new Query().forLines(lines); return new Query().forLines(lines);
} }
/** /**
* Builder pattern to request given line names. * Builder pattern to request given line names.
* *
* @param lineNames line names * @param lineNames Line names.
* @return the request * @return The request.
*/ */
public Query forLinesByName(final String... lineNames) { public final Query forLinesByName(final String... lineNames) {
return new Query().forLinesByName(lineNames); return new Query().forLinesByName(lineNames);
} }
/** /**
* Builder pattern to request given direction. * Builder pattern to request given direction.
* *
* @param direction the direction ID * @param direction The direction ID.
* @return the request * @return The request.
*/ */
public Query forDirection(final Integer direction) { public final Query forDirection(final Integer direction) {
return new Query().forDirection(direction); return new Query().forDirection(direction);
} }
/** /**
* Builder pattern to request given destination names. * Builder pattern to request given destination names.
* *
* @param destinationNames destination names * @param destinationNames Destination names.
* @return the request * @return The request.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query forDestinationNames(final String... destinationNames) { public final Query forDestinationNames(final String... destinationNames) {
return new Query().forDestinationNames(destinationNames); return new Query().forDestinationNames(destinationNames);
} }
/** /**
* Builder pattern to request given direction defined by stop point name. * Builder pattern to request given direction defined by stop point name.
* *
* @param towards towards stop point names * @param towards Towards stop point names.
* @return the request * @return The request.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query towards(final String... towards) { public final Query towards(final String... towards) {
return new Query().towards(towards); return new Query().towards(towards);
} }
/** /**
* Builder pattern to request given destination names. * Builder pattern to request given destination names.
* *
* @param latitude Latitude (WGS84) * @param latitude Latitude (WGS84).
* @param longitude Longitude (WGS84) * @param longitude Longitude (WGS84).
* @param radius Search radius (meters) * @param radius Search radius (meters).
* @return the request * @return The request.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) { public final Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
return new Query().forPosition(latitude, longitude, radius); return new Query().forPosition(latitude, longitude, radius);
} }
@ -179,7 +179,7 @@ public class UraClient {
* Get list of trips. * Get list of trips.
* If forStops() and/or forLines() has been called, those will be used as filter. * If forStops() and/or forLines() has been called, those will be used as filter.
* *
* @return list of trips * @return List of trips.
*/ */
public List<Trip> getTrips() { public List<Trip> getTrips() {
return getTrips(new Query(), null); return getTrips(new Query(), null);
@ -189,8 +189,8 @@ public class UraClient {
* Get list of trips with limit. * Get list of trips with limit.
* If forStops() and/or forLines() has been called, those will be used as filter. * If forStops() and/or forLines() has been called, those will be used as filter.
* *
* @param limit maximum number of results * @param limit Maximum number of results.
* @return list of trips * @return List of trips.
*/ */
public List<Trip> getTrips(final Integer limit) { public List<Trip> getTrips(final Integer limit) {
return getTrips(new Query(), limit); return getTrips(new Query(), limit);
@ -200,38 +200,40 @@ public class UraClient {
* Get list of trips. * Get list of trips.
* If forStops() and/or forLines() has been called, those will be used as filter. * If forStops() and/or forLines() has been called, those will be used as filter.
* *
* @param query the query * @param query The query.
* @return list of trips * @return List of trips.
*/ */
public List<Trip> getTrips(Query query) { public List<Trip> getTrips(final Query query) {
return getTrips(query, null); return getTrips(query, null);
} }
/** /**
* Get list of trips for given stopIDs and lineIDs with result limit. * Get list of trips for given stopIDs and lineIDs with result limit.
* *
* @param query the query * @param query The query.
* @param limit maximum number of results * @param limit Maximum number of results.
* @return list of trips * @return List of trips.
*/ */
public List<Trip> getTrips(final Query query, final Integer limit) { public List<Trip> getTrips(final Query query, final Integer limit) {
List<Trip> trips = new ArrayList<>(); List<Trip> trips = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_TRIP, query); try (InputStream is = requestInstant(REQUEST_TRIP, query);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) { BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String version = null; String version = null;
String line; String line = br.readLine();
while ((line = br.readLine()) != null && (limit == null || trips.size() < limit)) { while (line != null && (limit == null || trips.size() < limit)) {
List l = mapper.readValue(line, List.class); List l = mapper.readValue(line, List.class);
/* Check if result exists and has correct response type */ /* Check if result exists and has correct response type */
if (l != null && l.size() > 0) { if (l != null && !l.isEmpty()) {
if (l.get(0).equals(RES_TYPE_URA_VERSION)) if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
version = l.get(1).toString(); version = l.get(1).toString();
else if (l.get(0).equals(RES_TYPE_PREDICTION)) } else if (l.get(0).equals(RES_TYPE_PREDICTION)) {
trips.add(new Trip(l, version)); trips.add(new Trip(l, version));
}
} }
line = br.readLine();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IllegalStateException("Failed to read from API", e);
} }
return trips; return trips;
} }
@ -239,7 +241,7 @@ public class UraClient {
/** /**
* Get list of stops without filters. * Get list of stops without filters.
* *
* @return the list * @return Lhe list.
*/ */
public List<Stop> getStops() { public List<Stop> getStops() {
return getStops(new Query()); return getStops(new Query());
@ -249,23 +251,23 @@ public class UraClient {
* List available stopIDs. * List available stopIDs.
* If forStops() and/or forLines() has been called, those will be used as filter. * If forStops() and/or forLines() has been called, those will be used as filter.
* *
* @param query the query * @param query The query.
* @return the list * @return The list.
*/ */
public List<Stop> getStops(Query query) { public List<Stop> getStops(final Query query) {
List<Stop> stops = new ArrayList<>(); List<Stop> stops = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_STOP, query); try (InputStream is = requestInstant(REQUEST_STOP, query);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) { BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line; String line;
String version;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
List l = mapper.readValue(line, List.class); List l = mapper.readValue(line, List.class);
/* Check if result exists and has correct response type */ /* Check if result exists and has correct response type */
if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_STOP)) if (l != null && !l.isEmpty() && l.get(0).equals(RES_TYPE_STOP)) {
stops.add(new Stop(l)); stops.add(new Stop(l));
}
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IllegalStateException("Failed to read from API", e);
} }
return stops; return stops;
} }
@ -273,37 +275,48 @@ public class UraClient {
/** /**
* Issue request to instant endpoint and return input stream. * Issue request to instant endpoint and return input stream.
* *
* @param returnList fields to fetch * @param returnList Fields to fetch.
* @param query the query * @param query The query.
* @return Input stream of the URL * @return Input stream of the URL
* @throws IOException on errors * @throws IOException on errors
*/ */
private InputStream requestInstant(String[] returnList, Query query) throws IOException { private InputStream requestInstant(final String[] returnList, final Query query) throws IOException {
String urlStr = baseURL + instantURL + "?ReturnList=" + String.join(",", returnList); String urlStr = baseURL + instantURL + "?ReturnList=" + String.join(",", returnList);
if (query.stopIDs != null && query.stopIDs.length > 0)
if (query.stopIDs != null && query.stopIDs.length > 0) {
urlStr += "&" + PAR_STOP_ID + "=" + String.join(",", query.stopIDs); urlStr += "&" + PAR_STOP_ID + "=" + String.join(",", query.stopIDs);
if (query.stopNames != null && query.stopNames.length > 0) }
if (query.stopNames != null && query.stopNames.length > 0) {
urlStr += "&" + PAR_STOP_NAME + "=" + String.join(",", query.stopNames); urlStr += "&" + PAR_STOP_NAME + "=" + String.join(",", query.stopNames);
if (query.lineIDs != null && query.lineIDs.length > 0) }
if (query.lineIDs != null && query.lineIDs.length > 0) {
urlStr += "&" + PAR_LINE_ID + "=" + String.join(",", query.lineIDs); urlStr += "&" + PAR_LINE_ID + "=" + String.join(",", query.lineIDs);
if (query.lineNames != null && query.lineNames.length > 0) }
if (query.lineNames != null && query.lineNames.length > 0) {
urlStr += "&" + PAR_LINE_NAME + "=" + String.join(",", query.lineNames); urlStr += "&" + PAR_LINE_NAME + "=" + String.join(",", query.lineNames);
if (query.direction != null) }
if (query.direction != null) {
urlStr += "&" + PAR_DIR_ID + "=" + query.direction; urlStr += "&" + PAR_DIR_ID + "=" + query.direction;
if (query.destinationNames != null) }
if (query.destinationNames != null) {
urlStr += "&" + PAR_DEST_NAME + "=" + String.join(",", query.destinationNames); urlStr += "&" + PAR_DEST_NAME + "=" + String.join(",", query.destinationNames);
if (query.towards != null) }
if (query.towards != null) {
urlStr += "&" + PAR_TOWARDS + "=" + String.join(",", query.towards); urlStr += "&" + PAR_TOWARDS + "=" + String.join(",", query.towards);
if (query.circle != null) }
if (query.circle != null) {
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle); urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
}
URL url = new URL(urlStr); URL url = new URL(urlStr);
return url.openStream(); return url.openStream();
} }
/** /**
* Request meta object. * Request meta object.
*/ */
public class Query { public final class Query {
private String[] stopIDs; private String[] stopIDs;
private String[] stopNames; private String[] stopNames;
private String[] lineIDs; private String[] lineIDs;
@ -316,8 +329,8 @@ public class UraClient {
/** /**
* Builder pattern to request given line IDs. * Builder pattern to request given line IDs.
* *
* @param lineIDs line IDs * @param lineIDs Line IDs.
* @return the query * @return The query.
*/ */
public Query forLines(final String... lineIDs) { public Query forLines(final String... lineIDs) {
this.lineIDs = lineIDs; this.lineIDs = lineIDs;
@ -327,8 +340,8 @@ public class UraClient {
/** /**
* Builder pattern to request given line names. * Builder pattern to request given line names.
* *
* @param lineNames line names * @param lineNames Line names.
* @return the query * @return The query.
*/ */
public Query forLinesByName(final String... lineNames) { public Query forLinesByName(final String... lineNames) {
this.lineNames = lineNames; this.lineNames = lineNames;
@ -338,8 +351,8 @@ public class UraClient {
/** /**
* Builder pattern to request given stop IDs. * Builder pattern to request given stop IDs.
* *
* @param stopIDs stop IDs * @param stopIDs Stop IDs.
* @return the query * @return The query.
*/ */
public Query forStops(final String... stopIDs) { public Query forStops(final String... stopIDs) {
this.stopIDs = stopIDs; this.stopIDs = stopIDs;
@ -349,8 +362,8 @@ public class UraClient {
/** /**
* Builder pattern to request given stop names. * Builder pattern to request given stop names.
* *
* @param stopNames line names * @param stopNames Line names.
* @return the query * @return The query.
*/ */
public Query forStopsByName(final String... stopNames) { public Query forStopsByName(final String... stopNames) {
this.stopNames = stopNames; this.stopNames = stopNames;
@ -360,8 +373,8 @@ public class UraClient {
/** /**
* Builder pattern to request given direction. * Builder pattern to request given direction.
* *
* @param direction the direction * @param direction The direction.
* @return the query * @return The query.
*/ */
public Query forDirection(final Integer direction) { public Query forDirection(final Integer direction) {
this.direction = direction; this.direction = direction;
@ -369,10 +382,10 @@ public class UraClient {
} }
/** /**
* Builder pattern to request given destination names * Builder pattern to request given destination names.
* *
* @param destinationNames names of destinations * @param destinationNames Names of destinations.
* @return the query * @return The query.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query forDestinationNames(final String... destinationNames) { public Query forDestinationNames(final String... destinationNames) {
@ -383,8 +396,8 @@ public class UraClient {
/** /**
* Builder pattern to request given direction defined by stop point name. * Builder pattern to request given direction defined by stop point name.
* *
* @param towards towards stop point names * @param towards Towards stop point names.
* @return the request * @return The request.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query towards(final String... towards) { public Query towards(final String... towards) {
@ -395,10 +408,10 @@ public class UraClient {
/** /**
* Builder pattern to request given position and radius. * Builder pattern to request given position and radius.
* *
* @param latitude Latitude (WGS84) * @param latitude Latitude (WGS84).
* @param longitude Longitude (WGS84) * @param longitude Longitude (WGS84).
* @param radius Search radius (meters) * @param radius Search radius (meters).
* @return the query * @return The query.
* @since 1.1.0 * @since 1.1.0
*/ */
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) { public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
@ -409,7 +422,7 @@ public class UraClient {
/** /**
* Get stops for set filters. * Get stops for set filters.
* *
* @return List of matching trips * @return List of matching trips.
*/ */
public List<Stop> getStops() { public List<Stop> getStops() {
return UraClient.this.getStops(this); return UraClient.this.getStops(this);
@ -418,7 +431,7 @@ public class UraClient {
/** /**
* Get trips for set filters. * Get trips for set filters.
* *
* @return List of matching trips * @return List of matching trips.
*/ */
public List<Trip> getTrips() { public List<Trip> getTrips() {
return UraClient.this.getTrips(this); return UraClient.this.getTrips(this);

View File

@ -0,0 +1,49 @@
/*
* Copyright 2016-2018 Stefan Kalscheuer
*
* 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 de.stklcode.pubtrans.ura.model;
import java.io.IOException;
/**
* @author Stefan Kalscheuer
* @since 1.1.1
*/
interface Model {
/**
* Generate exception for unmatched type when String is expected.
*
* @param field Field number.
* @param actual Actual class.
* @return The Exception.
*/
static IOException typeErrorString(int field, Class actual) {
return typeError(field, actual, "String");
}
/**
* Generate exception for unmatched type.
*
* @param field Field number.
* @param actual Actual class.
* @param expected Expected type.
* @return The Exception.
*/
static IOException typeError(int field, Class actual, String expected) {
return new IOException(String.format("Field %d not of expected type %s, found %s",
field, expected, actual.getSimpleName()));
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,15 @@ import java.util.List;
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class Stop { public final class Stop implements Model {
private static final int F_STOP_NAME = 1;
private static final int F_STOP_ID = 2;
private static final int F_INDICATOR = 3;
private static final int F_STATE = 4;
private static final int F_LATITUDE = 5;
private static final int F_LONGITUDE = 6;
private static final int F_NUM_OF_FIELDS = 7;
private final String id; private final String id;
private final String name; private final String name;
private final String indicator; private final String indicator;
@ -32,7 +40,22 @@ public class Stop {
private final Double latitude; private final Double latitude;
private final Double longitude; private final Double longitude;
public Stop(String id, String name, String indicator, Integer state, Double latitude, Double longitude) { /**
* Construct Stop object.
*
* @param id Stop ID.
* @param name Stop name.
* @param indicator Stop indicator.
* @param state Stop state.
* @param latitude Stop geolocation latitude.
* @param longitude Stop geolocation longitude.
*/
public Stop(final String id,
final String name,
final String indicator,
final Integer state,
final Double latitude,
final Double longitude) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.indicator = indicator; this.indicator = indicator;
@ -41,58 +64,94 @@ public class Stop {
this.longitude = longitude; this.longitude = longitude;
} }
public Stop(List raw) throws IOException { /**
if (raw == null || raw.size() < 7) * Construct Stop object from raw list of attributes parsed from JSON.
*
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Stop(final List raw) throws IOException {
if (raw == null || raw.size() < F_NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields"); throw new IOException("Invalid number of fields");
}
if (raw.get(1) instanceof String) if (raw.get(1) instanceof String) {
name = (String)raw.get(1); name = (String) raw.get(F_STOP_NAME);
else } else {
throw new IOException("Field 1 not of expected type String, found " + raw.get(1).getClass().getSimpleName()); throw Model.typeErrorString(F_STOP_NAME, raw.get(F_STOP_NAME).getClass());
if (raw.get(2) instanceof String) }
id = (String)raw.get(2);
else if (raw.get(F_STOP_ID) instanceof String) {
throw new IOException("Field 2 not of expected type String, found " + raw.get(2).getClass().getSimpleName()); id = (String) raw.get(F_STOP_ID);
if (raw.get(3) instanceof String) } else {
indicator = (String)raw.get(3); throw Model.typeErrorString(F_STOP_ID, raw.get(F_STOP_ID).getClass());
else if (raw.get(3) == null) }
if (raw.get(F_INDICATOR) instanceof String) {
indicator = (String) raw.get(F_INDICATOR);
} else if (raw.get(F_INDICATOR) == null) {
indicator = null; indicator = null;
else } else {
throw new IOException("Field 3 not of expected type String, found " + raw.get(3).getClass().getSimpleName()); throw Model.typeErrorString(F_INDICATOR, raw.get(F_INDICATOR).getClass());
if (raw.get(4) instanceof Integer) }
state = (Integer)raw.get(4);
else if (raw.get(F_STATE) instanceof Integer) {
throw new IOException("Field 4 not of expected type Integer, found " + raw.get(4).getClass().getSimpleName()); state = (Integer) raw.get(F_STATE);
if (raw.get(5) instanceof Double) } else {
latitude = (Double)raw.get(5); throw Model.typeError(F_STATE, raw.get(F_STATE).getClass(), "Integer");
else }
throw new IOException("Field 5 not of expected type Double, found " + raw.get(5).getClass().getSimpleName());
if (raw.get(6) instanceof Double) if (raw.get(F_LATITUDE) instanceof Double) {
longitude = (Double)raw.get(6); latitude = (Double) raw.get(F_LATITUDE);
else } else {
throw new IOException("Field 6 not of expected type Double, found " + raw.get(6).getClass().getSimpleName()); throw Model.typeError(F_LATITUDE, raw.get(F_LATITUDE).getClass(), "Double");
}
if (raw.get(F_LONGITUDE) instanceof Double) {
longitude = (Double) raw.get(F_LONGITUDE);
} else {
throw Model.typeError(F_LONGITUDE, raw.get(F_LONGITUDE).getClass(), "Double");
}
} }
/**
* @return The stop ID.
*/
public String getId() { public String getId() {
return id; return id;
} }
/**
* @return The stop name.
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* @return The stop indicator.
*/
public String getIndicator() { public String getIndicator() {
return indicator; return indicator;
} }
/**
* @return The stop indicator.
*/
public Integer getState() { public Integer getState() {
return state; return state;
} }
/**
* @return The stop geoloaction latitude.
*/
public Double getLatitude() { public Double getLatitude() {
return latitude; return latitude;
} }
/**
* @return The stop geolocation longitude.
*/
public Double getLongitude() { public Double getLongitude() {
return longitude; return longitude;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,18 @@ import java.util.List;
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class Trip { public final class Trip implements Model {
private static final int VISIT_ID = 7;
private static final int LINE_ID = 8;
private static final int LINE_NAME = 9;
private static final int DIRECTION_ID = 10;
private static final int DESTINATION_NAME = 11;
private static final int DESTINATION_TEXT = 12;
private static final int VEHICLE_ID = 13;
private static final int TRIP_ID = 14;
private static final int ESTIMATED_TIME = 15;
private static final int NUM_OF_FIELDS = 16;
private final Stop stop; private final Stop stop;
private final String id; private final String id;
private final Integer visitID; private final Integer visitID;
@ -36,13 +47,81 @@ public class Trip {
private final Long estimatedTime; private final Long estimatedTime;
private final String vehicleID; private final String vehicleID;
public Trip(String stopID, String stopName, String stopIndicator, Integer stopState, Double stopLatitude, Double stopLongitude, /**
Integer visitID, String lineID, String lineName, Integer directionID, String destinationName, String destinationText, String vehicleID, String tripID, Long estimatedTime) { * Construct Trip object from complete set of data.
this(new Stop(stopID, stopName, stopIndicator, stopState, stopLatitude, stopLongitude), *
visitID, lineID, lineName, directionID, destinationName, destinationText, vehicleID, tripID, estimatedTime); * @param stopID Stop ID.
* @param stopName Stop name.
* @param stopIndicator Stop Indicator.
* @param stopState Stop state.
* @param stopLatitude Stop geolocation latitude.
* @param stopLongitude Stop geolocation latitude.
* @param visitID Visit ID.
* @param lineID Line ID.
* @param lineName Line name.
* @param directionID Direction ID.
* @param destinationName Destination name.
* @param destinationText Destination text.
* @param vehicleID Vehicle ID.
* @param tripID Trip ID.
* @param estimatedTime Estimated time.
*/
public Trip(final String stopID,
final String stopName,
final String stopIndicator,
final Integer stopState,
final Double stopLatitude,
final Double stopLongitude,
final Integer visitID,
final String lineID,
final String lineName,
final Integer directionID,
final String destinationName,
final String destinationText,
final String vehicleID,
final String tripID,
final Long estimatedTime) {
this(new Stop(stopID,
stopName,
stopIndicator,
stopState,
stopLatitude,
stopLongitude),
visitID,
lineID,
lineName,
directionID,
destinationName,
destinationText,
vehicleID,
tripID,
estimatedTime);
} }
public Trip(Stop stop, Integer visitID, String lineID, String lineName, Integer directionID, String destinationName, String destinationText, String vehicleID, String tripID, Long estimatedTime) { /**
* Construct Trip object from Stop model and set of additional data.
*
* @param stop Stop model
* @param visitID Visit ID
* @param lineID Line ID
* @param lineName Line name
* @param directionID Direction ID
* @param destinationName Destination name
* @param destinationText Destination text
* @param vehicleID Vehicle ID
* @param tripID Trip ID
* @param estimatedTime Estimated time
*/
public Trip(final Stop stop,
final Integer visitID,
final String lineID,
final String lineName,
final Integer directionID,
final String destinationName,
final String destinationText,
final String vehicleID,
final String tripID,
final Long estimatedTime) {
this.stop = stop; this.stop = stop;
this.visitID = visitID; this.visitID = visitID;
this.lineID = lineID; this.lineID = lineID;
@ -55,94 +134,159 @@ public class Trip {
this.estimatedTime = estimatedTime; this.estimatedTime = estimatedTime;
} }
public Trip(List raw) throws IOException { /**
* Construct Trip object from raw list of attributes parsed from JSON.
*
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Trip(final List raw) throws IOException {
this(raw, null); this(raw, null);
} }
public Trip(List raw, String version) throws IOException { /**
if (raw == null || raw.size() < 16) * Construct Stop object from raw list of attributes parsed from JSON with explicitly specified version.
*
* @param raw List of attributes from JSON line
* @param version API version
* @throws IOException Thrown on invalid line format.
*/
public Trip(final List raw, final String version) throws IOException {
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields"); throw new IOException("Invalid number of fields");
}
stop = new Stop(raw); stop = new Stop(raw);
if (raw.get(7) instanceof Integer) if (raw.get(VISIT_ID) instanceof Integer) {
visitID = (Integer) raw.get(7); visitID = (Integer) raw.get(VISIT_ID);
else } else {
throw new IOException("Field 7 not of expected type Integer, found " + raw.get(7).getClass().getSimpleName()); throw Model.typeError(VISIT_ID, raw.get(VISIT_ID).getClass(), "Integer");
if (raw.get(8) instanceof String)
lineID = (String)raw.get(8);
else
throw new IOException("Field 8 not of expected type String, found " + raw.get(8).getClass().getSimpleName());
if (raw.get(9) instanceof String)
lineName = (String)raw.get(9);
else
throw new IOException("Field 9 not of expected type String, found " + raw.get(9).getClass().getSimpleName());
if (raw.get(10) instanceof Integer) {
directionID = (Integer) raw.get(10);
if (directionID < 0 || directionID > 2)
throw new IOException("Direction out of range. Expected 1 or 2, found " + directionID);
} }
else
throw new IOException("Field 10 not of expected type Integer, found " + raw.get(10).getClass().getSimpleName()); if (raw.get(LINE_ID) instanceof String) {
if (raw.get(11) instanceof String) lineID = (String) raw.get(LINE_ID);
destinationName = (String)raw.get(11); } else {
else throw Model.typeErrorString(LINE_ID, raw.get(LINE_ID).getClass());
throw new IOException("Field 11 not of expected type String, found " + raw.get(11).getClass().getSimpleName()); }
if (raw.get(12) instanceof String)
destinationText = (String)raw.get(12); if (raw.get(LINE_NAME) instanceof String) {
else lineName = (String) raw.get(LINE_NAME);
throw new IOException("Field 12 not of expected type String, found " + raw.get(12).getClass().getSimpleName()); } else {
throw Model.typeErrorString(LINE_NAME, raw.get(LINE_NAME).getClass());
}
if (raw.get(DIRECTION_ID) instanceof Integer) {
directionID = (Integer) raw.get(DIRECTION_ID);
if (directionID < 0 || directionID > 2) {
throw new IOException("Direction out of range. Expected 1 or 2, found " + directionID);
}
} else {
throw Model.typeError(DIRECTION_ID, raw.get(DIRECTION_ID).getClass(), "Integer");
}
if (raw.get(DESTINATION_NAME) instanceof String) {
destinationName = (String) raw.get(DESTINATION_NAME);
} else {
throw Model.typeErrorString(DESTINATION_NAME, raw.get(DESTINATION_NAME).getClass());
}
if (raw.get(DESTINATION_TEXT) instanceof String) {
destinationText = (String) raw.get(DESTINATION_TEXT);
} else {
throw Model.typeErrorString(DESTINATION_TEXT, raw.get(DESTINATION_TEXT).getClass());
}
/* TFL and ASEAG deliver different types with the same API version, so this field is a little more tolerant */ /* TFL and ASEAG deliver different types with the same API version, so this field is a little more tolerant */
if (raw.get(13) instanceof String || raw.get(13) instanceof Integer || raw.get(13) instanceof Long) if (raw.get(VEHICLE_ID) instanceof String
vehicleID = raw.get(13).toString(); || raw.get(VEHICLE_ID) instanceof Integer
else || raw.get(VEHICLE_ID) instanceof Long) {
throw new IOException("Field 13 not of expected type String/Integer/Long, found " + raw.get(13).getClass().getSimpleName()); vehicleID = raw.get(VEHICLE_ID).toString();
if (raw.get(14) instanceof String || raw.get(14) instanceof Integer || raw.get(14) instanceof Long) } else {
id = raw.get(14).toString(); throw Model.typeError(VEHICLE_ID, raw.get(VEHICLE_ID).getClass(), "String/Integer/Long");
else }
throw new IOException("Field 14 not of expected type String/Integer/Long, found " + raw.get(14).getClass().getSimpleName());
if (raw.get(15) instanceof Long) if (raw.get(TRIP_ID) instanceof String
estimatedTime = (Long)raw.get(15); || raw.get(TRIP_ID) instanceof Integer
else || raw.get(TRIP_ID) instanceof Long) {
throw new IOException("Field 15 not of expected type Long, found " + raw.get(15).getClass().getSimpleName()); id = raw.get(TRIP_ID).toString();
} else {
throw Model.typeError(TRIP_ID, raw.get(TRIP_ID).getClass(), "String/Integer/Long");
}
if (raw.get(ESTIMATED_TIME) instanceof Long) {
estimatedTime = (Long) raw.get(ESTIMATED_TIME);
} else {
throw Model.typeError(ESTIMATED_TIME, raw.get(ESTIMATED_TIME).getClass(), "Long");
}
} }
/**
* @return The (starting) stop.
*/
public Stop getStop() { public Stop getStop() {
return stop; return stop;
} }
/**
* @return The trip ID.
*/
public String getId() { public String getId() {
return id; return id;
} }
/**
* @return The visit ID.
*/
public Integer getVisitID() { public Integer getVisitID() {
return visitID; return visitID;
} }
/**
* @return The line ID.
*/
public String getLineID() { public String getLineID() {
return lineID; return lineID;
} }
/**
* @return The line name.
*/
public String getLineName() { public String getLineName() {
return lineName; return lineName;
} }
/**
* @return The direction ID.
*/
public Integer getDirectionID() { public Integer getDirectionID() {
return directionID; return directionID;
} }
/**
* @return The destination name.
*/
public String getDestinationName() { public String getDestinationName() {
return destinationName; return destinationName;
} }
/**
* @return The destination text.
*/
public String getDestinationText() { public String getDestinationText() {
return destinationText; return destinationText;
} }
/**
* @return The estimated departure time.
*/
public Long getEstimatedTime() { public Long getEstimatedTime() {
return estimatedTime; return estimatedTime;
} }
/**
* @return The vehicle ID.
*/
public String getVehicleID() { public String getVehicleID() {
return vehicleID; return vehicleID;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
@ -50,7 +51,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").getStops(); List<Stop> stops = new UraClient("mocked").getStops();
@ -68,9 +70,13 @@ public class UraClientTest {
throw new IOException("Provoked exception 1."); throw new IOException("Provoked exception 1.");
} }
}); });
assertThat(new UraClient("mocked").getStops(), hasSize(0)); try {
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2.")); new UraClient("mocked").getStops();
assertThat(new UraClient("mocked").getStops(), hasSize(0)); } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1."));
}
} }
@Test @Test
@ -78,7 +84,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forLines("33").getStops(); List<Stop> stops = new UraClient("mocked").forLines("33").getStops();
@ -96,10 +103,13 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").forPosition(51.51009, -0.1345734, 200).getStops(); List<Stop> stops = new UraClient("mocked")
.forPosition(51.51009, -0.1345734, 200)
.getStops();
assertThat(stops, hasSize(13)); assertThat(stops, hasSize(13));
assertThat(stops.get(0).getId(), is("156")); assertThat(stops.get(0).getId(), is("156"));
assertThat(stops.get(1).getName(), is("Piccadilly Circus")); assertThat(stops.get(1).getName(), is("Piccadilly Circus"));
@ -108,8 +118,12 @@ public class UraClientTest {
assertThat(stops.get(4).getLongitude(), is(-0.134172)); assertThat(stops.get(4).getLongitude(), is(-0.134172));
assertThat(stops.get(5).getIndicator(), is(nullValue())); assertThat(stops.get(5).getIndicator(), is(nullValue()));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle_name.txt")); PowerMockito.when(mockURL.openStream())
stops = new UraClient("mocked").forStopsByName("Piccadilly Circus").forPosition(51.51009, -0.1345734, 200).getStops(); .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, hasSize(7));
assertThat(stops.stream().filter(t -> !t.getName().equals("Piccadilly Circus")).findAny(), is(Optional.empty())); assertThat(stops.stream().filter(t -> !t.getName().equals("Piccadilly Circus")).findAny(), is(Optional.empty()));
} }
@ -119,18 +133,26 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips(); List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips();
assertThat(trips, hasSize(9)); assertThat(trips, hasSize(9));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(), is(Optional.empty())); 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")); PowerMockito.when(mockURL.openStream())
trips = new UraClient("mocked").forStops("156").forDestinationNames("Marble Arch").getTrips(); .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, hasSize(5));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(),
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Marble Arch")).findAny(), is(Optional.empty())); is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Marble Arch")).findAny(),
is(Optional.empty()));
} }
@Test @Test
@ -138,13 +160,15 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips(); List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt"));
trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips(); trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(17)); assertThat(trips, hasSize(17));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
@ -155,7 +179,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
/* Get trips without filters and verify some values */ /* Get trips without filters and verify some values */
List<Trip> trips = new UraClient("mocked").getTrips(); List<Trip> trips = new UraClient("mocked").getTrips();
@ -172,7 +197,8 @@ public class UraClientTest {
assertThat(trips.get(9).getStop().getId(), is("100002")); assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Repeat test for API V2 */ /* Repeat test for API V2 */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt"));
/* Get trips without filters and verify some values */ /* Get trips without filters and verify some values */
trips = new UraClient("mocked").getTrips(); trips = new UraClient("mocked").getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
@ -188,7 +214,8 @@ public class UraClientTest {
assertThat(trips.get(9).getStop().getId(), is("100002")); assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Get limited number of trips */ /* Get limited number of trips */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
trips = new UraClient("mocked").getTrips(5); trips = new UraClient("mocked").getTrips(5);
assertThat(trips, hasSize(5)); assertThat(trips, hasSize(5));
@ -199,9 +226,13 @@ public class UraClientTest {
throw new IOException("Provoked exception 1."); throw new IOException("Provoked exception 1.");
} }
}); });
assertThat(new UraClient("mocked").getTrips(), hasSize(0)); try {
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2.")); new UraClient("mocked").getTrips();
assertThat(new UraClient("mocked").getTrips(), hasSize(0)); } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1."));
}
} }
@Test @Test
@ -209,7 +240,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop.txt"));
/* Get trips for stop ID 100000 (Aachen Bushof) and verify some values */ /* Get trips for stop ID 100000 (Aachen Bushof) and verify some values */
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
@ -223,12 +255,14 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getIndicator(), is("H.15")); assertThat(trips.get(3).getStop().getIndicator(), is("H.15"));
/* Get trips for stop name "Uniklinik" and verify some values */ /* Get trips for stop name "Uniklinik" and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forStopsByName("Uniklinik") .forStopsByName("Uniklinik")
.getTrips(); .getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getStop().getName().equals("Uniklinik")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getName().equals("Uniklinik")).findAny(),
is(Optional.empty()));
assertThat(trips.get(0).getId(), is("92000043013001")); assertThat(trips.get(0).getId(), is("92000043013001"));
assertThat(trips.get(1).getLineID(), is("5")); assertThat(trips.get(1).getLineID(), is("5"));
assertThat(trips.get(2).getVehicleID(), is("317"));; assertThat(trips.get(2).getVehicleID(), is("317"));;
@ -240,7 +274,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line.txt"));
/* Get trips for line ID 3 and verify some values */ /* Get trips for line ID 3 and verify some values */
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
@ -254,7 +289,8 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)")); assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)"));
/* Get trips for line name "3.A" and verify some values */ /* Get trips for line name "3.A" and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forLinesByName("3.A") .forLinesByName("3.A")
.getTrips(); .getTrips();
@ -266,7 +302,8 @@ public class UraClientTest {
assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße")); assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße"));
/* Get trips for line 3 with direction 1 and verify some values */ /* Get trips for line 3 with direction 1 and verify some values */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forLines("412") .forLines("412")
.forDirection(2) .forDirection(2)
@ -276,7 +313,8 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty()));
/* Test lineID and direction in different order */ /* Test lineID and direction in different order */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forDirection(2) .forDirection(2)
.forLines("412") .forLines("412")
@ -291,7 +329,8 @@ public class UraClientTest {
/* Mock the HTTP call */ /* Mock the HTTP call */
URL mockURL = PowerMockito.mock(URL.class); URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_line.txt")); PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_line.txt"));
/* Get trips for line ID 25 and 25 at stop 100000 and verify some values */ /* Get trips for line ID 25 and 25 at stop 100000 and verify some values */
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
@ -299,7 +338,8 @@ public class UraClientTest {
.forStops("100000") .forStops("100000")
.getTrips(); .getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getLineID().equals("25") && !t.getLineID().equals("35")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getLineID().equals("25") && !t.getLineID().equals("35")).findAny(),
is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("27000078014001")); assertThat(trips.get(0).getId(), is("27000078014001"));
assertThat(trips.get(1).getLineID(), is("25")); assertThat(trips.get(1).getLineID(), is("25"));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 Stefan Kalscheuer * Copyright 2016-2018 Stefan Kalscheuer
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,8 +36,21 @@ import static org.junit.Assert.fail;
public class TripTest { public class TripTest {
@Test @Test
public void basicConstructorTest() { public void basicConstructorTest() {
Trip trip = new Trip("sid", "name", "indicator", 1, 2.345, 6.789, Trip trip = new Trip("sid",
123, "lineid", "linename", 0, "destination name", "destination text", "vehicle", "id", 123456789123456789L); "name",
"indicator",
1,
2.345,
6.789,
123,
"lineid",
"linename",
0,
"destination name",
"destination text",
"vehicle",
"id",
123456789123456789L);
assertThat(trip.getStop().getId(), is("sid")); assertThat(trip.getStop().getId(), is("sid"));
assertThat(trip.getStop().getName(), is("name")); assertThat(trip.getStop().getName(), is("name"));
assertThat(trip.getStop().getIndicator(), is("indicator")); assertThat(trip.getStop().getIndicator(), is("indicator"));