Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
ee91f05898 | |||
83e6e446ba | |||
e8ca106037 | |||
5808365777 | |||
9e7a2a92ee | |||
4de8f3f073 | |||
8631a43ed8 | |||
c9444bbc97 | |||
0d0859bd3a | |||
d5e09392ef | |||
d12c585083 | |||
ffa8af2d5d | |||
eb400b52ca | |||
0e21c0d64c | |||
7e3af36e22 | |||
93acfad184 | |||
daa84f61c5 | |||
0304f6e0f4 | |||
56d482b723 | |||
afffaa4a6f | |||
e12775b6ae | |||
6b0481a5e1 | |||
ca9578a4d3 | |||
9ecb3763af | |||
a8fc93fb41 | |||
1f4bd9f411 | |||
ee1d8bd2c8 | |||
4b3a255e18 | |||
6fab3ab6fe | |||
35728fa9b1 |
35
.drone.yml
Normal file
35
.drone.yml
Normal file
@ -0,0 +1,35 @@
|
||||
kind: pipeline
|
||||
name: java8
|
||||
|
||||
clone:
|
||||
disable: true
|
||||
|
||||
steps:
|
||||
- name: clone
|
||||
image: plugins/git
|
||||
settings:
|
||||
depth: 10
|
||||
skip_verify: true
|
||||
- name: test
|
||||
image: maven:3.5-jdk-8-alpine
|
||||
commands:
|
||||
- mvn clean test
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: java11
|
||||
|
||||
clone:
|
||||
disable: true
|
||||
|
||||
steps:
|
||||
- name: clone
|
||||
image: plugins/git
|
||||
settings:
|
||||
depth: 10
|
||||
skip_verify: true
|
||||
- name: test
|
||||
image: maven:3.5-jdk-11-slim
|
||||
commands:
|
||||
- mvn clean test
|
||||
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
target/
|
||||
.idea/
|
||||
*.iml
|
15
.travis.yml
15
.travis.yml
@ -1,3 +1,16 @@
|
||||
language: java
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
- oraclejdk8
|
||||
- oraclejdk11
|
||||
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
|
||||
|
20
CHANGELOG.md
Normal file
20
CHANGELOG.md
Normal file
@ -0,0 +1,20 @@
|
||||
## 1.1.3 [2018-11-13]
|
||||
* [security] Updates Jackson dependency 2.9.4 to 2.9.7 (CVE-2018-7489)
|
||||
* [improvement] Client and model classes implement `Serializable`
|
||||
* [dependeny] Dependency updates
|
||||
|
||||
## 1.1.2 [2018-03-24]
|
||||
* [improvement] Added automatic module name for JPMS compatibility
|
||||
|
||||
## 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
|
11
README.md
11
README.md
@ -1,4 +1,9 @@
|
||||
# jURAclient [](https://travis-ci.org/stklcode/juraclient)
|
||||
# jURAclient
|
||||
[](https://travis-ci.org/stklcode/juraclient)
|
||||
[](https://sonarcloud.io/dashboard?id=de.stklcode.pubtrans%3Ajuraclient)
|
||||
[](https://www.javadoc.io/doc/de.stklcode.pubtrans/juraclient)
|
||||
[](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.stklcode.pubtrans%22%20AND%20a%3A%22juraclient%22)
|
||||
[](https://github.com/stklcode/juraclient/blob/master/LICENSE.txt)
|
||||
|
||||
Java client for URA based public transport APIs.
|
||||
|
||||
@ -50,7 +55,7 @@ List<Trip> trips = ura.forStopByName("Piccadilly Circus")
|
||||
<dependency>
|
||||
<groupId>de.stklcode.pubtrans</groupId>
|
||||
<artifactId>juraclient</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.3</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
@ -61,4 +66,4 @@ List<Trip> trips = ura.forStopByName("Piccadilly Circus")
|
||||
|
||||
## 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).
|
||||
|
185
pom.xml
185
pom.xml
@ -6,27 +6,61 @@
|
||||
|
||||
<groupId>de.stklcode.pubtrans</groupId>
|
||||
<artifactId>juraclient</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.3</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</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>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.8.5</version>
|
||||
<version>2.9.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.8.5</version>
|
||||
<version>2.9.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.3.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -36,15 +70,15 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>1.6.6</version>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>1.9.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>1.6.6</version>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>1.9.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -54,12 +88,137 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Automatic-Module-Name>de.stklcode.pubtrans.juraclient</Automatic-Module-Name>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>jacoco</id>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>org.jacoco.agent</artifactId>
|
||||
<version>0.8.2</version>
|
||||
<classifier>runtime</classifier>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.2</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.22.1</version>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>sources</id>
|
||||
<activation>
|
||||
<activeByDefault>false</activeByDefault>
|
||||
</activation>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>javadoc</id>
|
||||
<activation>
|
||||
<activeByDefault>false</activeByDefault>
|
||||
</activation>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<configuration>
|
||||
<overview>${basedir}/src/main/javadoc/overview.html</overview>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
</project>
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -20,20 +20,19 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.pubtrans.ura.model.Stop;
|
||||
import de.stklcode.pubtrans.ura.model.Trip;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Client for URA based public transport API.
|
||||
* <p>
|
||||
* This client features builder pattern style query functionality to obtain {@link Trip} and {@link Stop} information.
|
||||
*
|
||||
* @author Stefan Kalscheuer
|
||||
*/
|
||||
public class UraClient {
|
||||
public class UraClient implements Serializable {
|
||||
private static final String DEFAULT_INSTANT_URL = "/interfaces/ura/instant_V1";
|
||||
private static final String DEFAULT_STREAM_URL = "/interfaces/ura/stream_V1";
|
||||
|
||||
@ -70,20 +69,20 @@ public class UraClient {
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with base URL and custom API paths
|
||||
* Constructor with base URL and custom API paths.
|
||||
*
|
||||
* @param baseURL the base URL (including protocol)
|
||||
* @param instantURL the path for instant requests
|
||||
* @param streamURL the path for stream requests
|
||||
* @param baseURL The base URL (including protocol).
|
||||
* @param instantURL The path for instant 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.instantURL = instantURL;
|
||||
this.streamURL = streamURL;
|
||||
@ -96,7 +95,7 @@ public class UraClient {
|
||||
* @param stops Stop IDs
|
||||
* @return the request
|
||||
*/
|
||||
public Query forStops(final String... stops) {
|
||||
public final Query forStops(final String... stops) {
|
||||
return new Query().forStops(stops);
|
||||
}
|
||||
|
||||
@ -106,72 +105,72 @@ public class UraClient {
|
||||
* @param stopNames Stop Point Names
|
||||
* @return the request
|
||||
*/
|
||||
public Query forStopsByName(final String... stopNames) {
|
||||
public final Query forStopsByName(final String... stopNames) {
|
||||
return new Query().forStopsByName(stopNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern to request given line IDs.
|
||||
*
|
||||
* @param lines line IDs
|
||||
* @return the request
|
||||
* @param lines Line IDs.
|
||||
* @return The request.
|
||||
*/
|
||||
public Query forLines(final String... lines) {
|
||||
public final Query forLines(final String... lines) {
|
||||
return new Query().forLines(lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern to request given line names.
|
||||
*
|
||||
* @param lineNames line names
|
||||
* @return the request
|
||||
* @param lineNames Line names.
|
||||
* @return The request.
|
||||
*/
|
||||
public Query forLinesByName(final String... lineNames) {
|
||||
public final Query forLinesByName(final String... lineNames) {
|
||||
return new Query().forLinesByName(lineNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern to request given direction.
|
||||
*
|
||||
* @param direction the direction ID
|
||||
* @return the request
|
||||
* @param direction The direction ID.
|
||||
* @return The request.
|
||||
*/
|
||||
public Query forDirection(final Integer direction) {
|
||||
public final Query forDirection(final Integer direction) {
|
||||
return new Query().forDirection(direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern to request given destination names.
|
||||
*
|
||||
* @param destinationNames destination names
|
||||
* @return the request
|
||||
* @param destinationNames Destination names.
|
||||
* @return The request.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public Query forDestinationNames(final String... destinationNames) {
|
||||
public final 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
|
||||
* @param towards Towards stop point names.
|
||||
* @return The request.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public Query towards(final String... towards) {
|
||||
public final 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
|
||||
* @param latitude Latitude (WGS84).
|
||||
* @param longitude Longitude (WGS84).
|
||||
* @param radius Search radius (meters).
|
||||
* @return The request.
|
||||
* @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);
|
||||
}
|
||||
|
||||
@ -179,7 +178,7 @@ public class UraClient {
|
||||
* Get list of trips.
|
||||
* 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() {
|
||||
return getTrips(new Query(), null);
|
||||
@ -189,8 +188,8 @@ public class UraClient {
|
||||
* Get list of trips with limit.
|
||||
* If forStops() and/or forLines() has been called, those will be used as filter.
|
||||
*
|
||||
* @param limit maximum number of results
|
||||
* @return list of trips
|
||||
* @param limit Maximum number of results.
|
||||
* @return List of trips.
|
||||
*/
|
||||
public List<Trip> getTrips(final Integer limit) {
|
||||
return getTrips(new Query(), limit);
|
||||
@ -200,38 +199,40 @@ public class UraClient {
|
||||
* Get list of trips.
|
||||
* If forStops() and/or forLines() has been called, those will be used as filter.
|
||||
*
|
||||
* @param query the query
|
||||
* @return list of trips
|
||||
* @param query The query.
|
||||
* @return List of trips.
|
||||
*/
|
||||
public List<Trip> getTrips(Query query) {
|
||||
public List<Trip> getTrips(final Query query) {
|
||||
return getTrips(query, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of trips for given stopIDs and lineIDs with result limit.
|
||||
*
|
||||
* @param query the query
|
||||
* @param limit maximum number of results
|
||||
* @return list of trips
|
||||
* @param query The query.
|
||||
* @param limit Maximum number of results.
|
||||
* @return List of trips.
|
||||
*/
|
||||
public List<Trip> getTrips(final Query query, final Integer limit) {
|
||||
List<Trip> trips = new ArrayList<>();
|
||||
try (InputStream is = requestInstant(REQUEST_TRIP, query);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||
String version = null;
|
||||
String line;
|
||||
while ((line = br.readLine()) != null && (limit == null || trips.size() < limit)) {
|
||||
String line = br.readLine();
|
||||
while (line != null && (limit == null || trips.size() < limit)) {
|
||||
List l = mapper.readValue(line, List.class);
|
||||
/* Check if result exists and has correct response type */
|
||||
if (l != null && l.size() > 0) {
|
||||
if (l.get(0).equals(RES_TYPE_URA_VERSION))
|
||||
if (l != null && !l.isEmpty()) {
|
||||
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
line = br.readLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalStateException("Failed to read from API", e);
|
||||
}
|
||||
return trips;
|
||||
}
|
||||
@ -239,7 +240,7 @@ public class UraClient {
|
||||
/**
|
||||
* Get list of stops without filters.
|
||||
*
|
||||
* @return the list
|
||||
* @return Lhe list.
|
||||
*/
|
||||
public List<Stop> getStops() {
|
||||
return getStops(new Query());
|
||||
@ -249,23 +250,23 @@ public class UraClient {
|
||||
* List available stopIDs.
|
||||
* If forStops() and/or forLines() has been called, those will be used as filter.
|
||||
*
|
||||
* @param query the query
|
||||
* @return the list
|
||||
* @param query The query.
|
||||
* @return The list.
|
||||
*/
|
||||
public List<Stop> getStops(Query query) {
|
||||
public List<Stop> getStops(final Query query) {
|
||||
List<Stop> stops = new ArrayList<>();
|
||||
try (InputStream is = requestInstant(REQUEST_STOP, query);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||
String line;
|
||||
String version;
|
||||
while ((line = br.readLine()) != null) {
|
||||
List l = mapper.readValue(line, List.class);
|
||||
/* 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));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalStateException("Failed to read from API", e);
|
||||
}
|
||||
return stops;
|
||||
}
|
||||
@ -273,37 +274,57 @@ public class UraClient {
|
||||
/**
|
||||
* Issue request to instant endpoint and return input stream.
|
||||
*
|
||||
* @param returnList fields to fetch
|
||||
* @param query the query
|
||||
* @param returnList Fields to fetch.
|
||||
* @param query The query.
|
||||
* @return Input stream of the URL
|
||||
* @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);
|
||||
if (query.stopIDs != null && query.stopIDs.length > 0)
|
||||
|
||||
if (query.stopIDs != null && query.stopIDs.length > 0) {
|
||||
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);
|
||||
if (query.lineIDs != null && query.lineIDs.length > 0)
|
||||
}
|
||||
if (query.lineIDs != null && query.lineIDs.length > 0) {
|
||||
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);
|
||||
if (query.direction != null)
|
||||
}
|
||||
if (query.direction != null) {
|
||||
urlStr += "&" + PAR_DIR_ID + "=" + query.direction;
|
||||
if (query.destinationNames != null)
|
||||
}
|
||||
if (query.destinationNames != null) {
|
||||
urlStr += "&" + PAR_DEST_NAME + "=" + String.join(",", query.destinationNames);
|
||||
if (query.towards != null)
|
||||
}
|
||||
if (query.towards != null) {
|
||||
urlStr += "&" + PAR_TOWARDS + "=" + String.join(",", query.towards);
|
||||
if (query.circle != null)
|
||||
}
|
||||
if (query.circle != null) {
|
||||
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
|
||||
URL url = new URL(urlStr);
|
||||
return url.openStream();
|
||||
}
|
||||
|
||||
return request(urlStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open given URL as InputStream.
|
||||
*
|
||||
* @param url The URL.
|
||||
* @return Input Stream of results.
|
||||
* @throws IOException Error opening conneciton or reading data.
|
||||
*/
|
||||
private InputStream request(String url) throws IOException {
|
||||
return new URL(url).openStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request meta object.
|
||||
*/
|
||||
public class Query {
|
||||
public final class Query {
|
||||
private String[] stopIDs;
|
||||
private String[] stopNames;
|
||||
private String[] lineIDs;
|
||||
@ -316,8 +337,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given line IDs.
|
||||
*
|
||||
* @param lineIDs line IDs
|
||||
* @return the query
|
||||
* @param lineIDs Line IDs.
|
||||
* @return The query.
|
||||
*/
|
||||
public Query forLines(final String... lineIDs) {
|
||||
this.lineIDs = lineIDs;
|
||||
@ -327,8 +348,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given line names.
|
||||
*
|
||||
* @param lineNames line names
|
||||
* @return the query
|
||||
* @param lineNames Line names.
|
||||
* @return The query.
|
||||
*/
|
||||
public Query forLinesByName(final String... lineNames) {
|
||||
this.lineNames = lineNames;
|
||||
@ -338,8 +359,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given stop IDs.
|
||||
*
|
||||
* @param stopIDs stop IDs
|
||||
* @return the query
|
||||
* @param stopIDs Stop IDs.
|
||||
* @return The query.
|
||||
*/
|
||||
public Query forStops(final String... stopIDs) {
|
||||
this.stopIDs = stopIDs;
|
||||
@ -349,8 +370,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given stop names.
|
||||
*
|
||||
* @param stopNames line names
|
||||
* @return the query
|
||||
* @param stopNames Line names.
|
||||
* @return The query.
|
||||
*/
|
||||
public Query forStopsByName(final String... stopNames) {
|
||||
this.stopNames = stopNames;
|
||||
@ -360,8 +381,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given direction.
|
||||
*
|
||||
* @param direction the direction
|
||||
* @return the query
|
||||
* @param direction The direction.
|
||||
* @return The query.
|
||||
*/
|
||||
public Query forDirection(final Integer direction) {
|
||||
this.direction = direction;
|
||||
@ -369,10 +390,10 @@ public class UraClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern to request given destination names
|
||||
* Builder pattern to request given destination names.
|
||||
*
|
||||
* @param destinationNames names of destinations
|
||||
* @return the query
|
||||
* @param destinationNames Names of destinations.
|
||||
* @return The query.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public Query forDestinationNames(final String... destinationNames) {
|
||||
@ -383,8 +404,8 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given direction defined by stop point name.
|
||||
*
|
||||
* @param towards towards stop point names
|
||||
* @return the request
|
||||
* @param towards Towards stop point names.
|
||||
* @return The request.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public Query towards(final String... towards) {
|
||||
@ -395,10 +416,10 @@ public class UraClient {
|
||||
/**
|
||||
* Builder pattern to request given position and radius.
|
||||
*
|
||||
* @param latitude Latitude (WGS84)
|
||||
* @param longitude Longitude (WGS84)
|
||||
* @param radius Search radius (meters)
|
||||
* @return the query
|
||||
* @param latitude Latitude (WGS84).
|
||||
* @param longitude Longitude (WGS84).
|
||||
* @param radius Search radius (meters).
|
||||
* @return The query.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public Query forPosition(final Double latitude, final Double longitude, final Integer radius) {
|
||||
@ -409,7 +430,7 @@ public class UraClient {
|
||||
/**
|
||||
* Get stops for set filters.
|
||||
*
|
||||
* @return List of matching trips
|
||||
* @return List of matching trips.
|
||||
*/
|
||||
public List<Stop> getStops() {
|
||||
return UraClient.this.getStops(this);
|
||||
@ -418,7 +439,7 @@ public class UraClient {
|
||||
/**
|
||||
* Get trips for set filters.
|
||||
*
|
||||
* @return List of matching trips
|
||||
* @return List of matching trips.
|
||||
*/
|
||||
public List<Trip> getTrips() {
|
||||
return UraClient.this.getTrips(this);
|
||||
|
52
src/main/java/de/stklcode/pubtrans/ura/model/Model.java
Normal file
52
src/main/java/de/stklcode/pubtrans/ura/model/Model.java
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Interface for model classes to bundle common methods.
|
||||
*
|
||||
* @author Stefan Kalscheuer
|
||||
* @since 1.1.1
|
||||
*/
|
||||
interface Model extends Serializable {
|
||||
/**
|
||||
* 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()));
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -24,7 +24,15 @@ import java.util.List;
|
||||
*
|
||||
* @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 name;
|
||||
private final String indicator;
|
||||
@ -32,7 +40,22 @@ public class Stop {
|
||||
private final Double latitude;
|
||||
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.name = name;
|
||||
this.indicator = indicator;
|
||||
@ -41,58 +64,94 @@ public class Stop {
|
||||
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");
|
||||
}
|
||||
|
||||
if (raw.get(1) instanceof String)
|
||||
name = (String)raw.get(1);
|
||||
else
|
||||
throw new IOException("Field 1 not of expected type String, found " + raw.get(1).getClass().getSimpleName());
|
||||
if (raw.get(2) instanceof String)
|
||||
id = (String)raw.get(2);
|
||||
else
|
||||
throw new IOException("Field 2 not of expected type String, found " + raw.get(2).getClass().getSimpleName());
|
||||
if (raw.get(3) instanceof String)
|
||||
indicator = (String)raw.get(3);
|
||||
else if (raw.get(3) == null)
|
||||
if (raw.get(1) instanceof String) {
|
||||
name = (String) raw.get(F_STOP_NAME);
|
||||
} else {
|
||||
throw Model.typeErrorString(F_STOP_NAME, raw.get(F_STOP_NAME).getClass());
|
||||
}
|
||||
|
||||
if (raw.get(F_STOP_ID) instanceof String) {
|
||||
id = (String) raw.get(F_STOP_ID);
|
||||
} else {
|
||||
throw Model.typeErrorString(F_STOP_ID, raw.get(F_STOP_ID).getClass());
|
||||
}
|
||||
|
||||
if (raw.get(F_INDICATOR) instanceof String) {
|
||||
indicator = (String) raw.get(F_INDICATOR);
|
||||
} else if (raw.get(F_INDICATOR) == null) {
|
||||
indicator = null;
|
||||
else
|
||||
throw new IOException("Field 3 not of expected type String, found " + raw.get(3).getClass().getSimpleName());
|
||||
if (raw.get(4) instanceof Integer)
|
||||
state = (Integer)raw.get(4);
|
||||
else
|
||||
throw new IOException("Field 4 not of expected type Integer, found " + raw.get(4).getClass().getSimpleName());
|
||||
if (raw.get(5) instanceof Double)
|
||||
latitude = (Double)raw.get(5);
|
||||
else
|
||||
throw new IOException("Field 5 not of expected type Double, found " + raw.get(5).getClass().getSimpleName());
|
||||
if (raw.get(6) instanceof Double)
|
||||
longitude = (Double)raw.get(6);
|
||||
else
|
||||
throw new IOException("Field 6 not of expected type Double, found " + raw.get(6).getClass().getSimpleName());
|
||||
} else {
|
||||
throw Model.typeErrorString(F_INDICATOR, raw.get(F_INDICATOR).getClass());
|
||||
}
|
||||
|
||||
if (raw.get(F_STATE) instanceof Integer) {
|
||||
state = (Integer) raw.get(F_STATE);
|
||||
} else {
|
||||
throw Model.typeError(F_STATE, raw.get(F_STATE).getClass(), "Integer");
|
||||
}
|
||||
|
||||
if (raw.get(F_LATITUDE) instanceof Double) {
|
||||
latitude = (Double) raw.get(F_LATITUDE);
|
||||
} else {
|
||||
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() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The stop name.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The stop indicator.
|
||||
*/
|
||||
public String getIndicator() {
|
||||
return indicator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The stop indicator.
|
||||
*/
|
||||
public Integer getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The stop geoloaction latitude.
|
||||
*/
|
||||
public Double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The stop geolocation longitude.
|
||||
*/
|
||||
public Double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -24,7 +24,18 @@ import java.util.List;
|
||||
*
|
||||
* @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 String id;
|
||||
private final Integer visitID;
|
||||
@ -36,13 +47,81 @@ public class Trip {
|
||||
private final Long estimatedTime;
|
||||
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) {
|
||||
this(new Stop(stopID, stopName, stopIndicator, stopState, stopLatitude, stopLongitude),
|
||||
visitID, lineID, lineName, directionID, destinationName, destinationText, vehicleID, tripID, estimatedTime);
|
||||
/**
|
||||
* Construct Trip object from complete set of data.
|
||||
*
|
||||
* @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.visitID = visitID;
|
||||
this.lineID = lineID;
|
||||
@ -55,94 +134,159 @@ public class Trip {
|
||||
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);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
stop = new Stop(raw);
|
||||
|
||||
if (raw.get(7) instanceof Integer)
|
||||
visitID = (Integer) raw.get(7);
|
||||
else
|
||||
throw new IOException("Field 7 not of expected type Integer, found " + raw.get(7).getClass().getSimpleName());
|
||||
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);
|
||||
if (raw.get(VISIT_ID) instanceof Integer) {
|
||||
visitID = (Integer) raw.get(VISIT_ID);
|
||||
} else {
|
||||
throw Model.typeError(VISIT_ID, raw.get(VISIT_ID).getClass(), "Integer");
|
||||
}
|
||||
else
|
||||
throw new IOException("Field 10 not of expected type Integer, found " + raw.get(10).getClass().getSimpleName());
|
||||
if (raw.get(11) instanceof String)
|
||||
destinationName = (String)raw.get(11);
|
||||
else
|
||||
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);
|
||||
else
|
||||
throw new IOException("Field 12 not of expected type String, found " + raw.get(12).getClass().getSimpleName());
|
||||
|
||||
if (raw.get(LINE_ID) instanceof String) {
|
||||
lineID = (String) raw.get(LINE_ID);
|
||||
} else {
|
||||
throw Model.typeErrorString(LINE_ID, raw.get(LINE_ID).getClass());
|
||||
}
|
||||
|
||||
if (raw.get(LINE_NAME) instanceof String) {
|
||||
lineName = (String) raw.get(LINE_NAME);
|
||||
} 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 */
|
||||
if (raw.get(13) instanceof String || raw.get(13) instanceof Integer || raw.get(13) instanceof Long)
|
||||
vehicleID = raw.get(13).toString();
|
||||
else
|
||||
throw new IOException("Field 13 not of expected type String/Integer/Long, found " + raw.get(13).getClass().getSimpleName());
|
||||
if (raw.get(14) instanceof String || raw.get(14) instanceof Integer || raw.get(14) instanceof Long)
|
||||
id = raw.get(14).toString();
|
||||
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)
|
||||
estimatedTime = (Long)raw.get(15);
|
||||
else
|
||||
throw new IOException("Field 15 not of expected type Long, found " + raw.get(15).getClass().getSimpleName());
|
||||
if (raw.get(VEHICLE_ID) instanceof String
|
||||
|| raw.get(VEHICLE_ID) instanceof Integer
|
||||
|| raw.get(VEHICLE_ID) instanceof Long) {
|
||||
vehicleID = raw.get(VEHICLE_ID).toString();
|
||||
} else {
|
||||
throw Model.typeError(VEHICLE_ID, raw.get(VEHICLE_ID).getClass(), "String/Integer/Long");
|
||||
}
|
||||
|
||||
if (raw.get(TRIP_ID) instanceof String
|
||||
|| raw.get(TRIP_ID) instanceof Integer
|
||||
|| raw.get(TRIP_ID) instanceof Long) {
|
||||
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() {
|
||||
return stop;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The trip ID.
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The visit ID.
|
||||
*/
|
||||
public Integer getVisitID() {
|
||||
return visitID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The line ID.
|
||||
*/
|
||||
public String getLineID() {
|
||||
return lineID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The line name.
|
||||
*/
|
||||
public String getLineName() {
|
||||
return lineName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The direction ID.
|
||||
*/
|
||||
public Integer getDirectionID() {
|
||||
return directionID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination name.
|
||||
*/
|
||||
public String getDestinationName() {
|
||||
return destinationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination text.
|
||||
*/
|
||||
public String getDestinationText() {
|
||||
return destinationText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The estimated departure time.
|
||||
*/
|
||||
public Long getEstimatedTime() {
|
||||
return estimatedTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The vehicle ID.
|
||||
*/
|
||||
public String getVehicleID() {
|
||||
return vehicleID;
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* jURAclient model classes that represent responses returned by the URA interface.
|
||||
*/
|
||||
package de.stklcode.pubtrans.ura.model;
|
20
src/main/java/de/stklcode/pubtrans/ura/package-info.java
Normal file
20
src/main/java/de/stklcode/pubtrans/ura/package-info.java
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* jURAclient base package - see {@link de.stklcode.pubtrans.ura.UraClient} for usage.
|
||||
*/
|
||||
package de.stklcode.pubtrans.ura;
|
14
src/main/javadoc/overview.html
Normal file
14
src/main/javadoc/overview.html
Normal file
@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>API Overview</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Java client for URA based public transport APIs.</p>
|
||||
<p>
|
||||
This client allows to simply connect any Java application to the public transport API to implement a monitor for the
|
||||
local bus station or any other custom queries. API versions 1.x and 2.x are supported.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -18,151 +18,178 @@ package de.stklcode.pubtrans.ura;
|
||||
|
||||
import de.stklcode.pubtrans.ura.model.Stop;
|
||||
import de.stklcode.pubtrans.ura.model.Trip;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.agent.ByteBuddyAgent;
|
||||
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static net.bytebuddy.implementation.MethodDelegation.to;
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
|
||||
/**
|
||||
* Unit test for the URA Client.
|
||||
* Tests run against mocked data collected from ASEAG API (http://ivu.aseag.de) and
|
||||
* TFL API (http://http://countdown.api.tfl.gov.uk)
|
||||
* TFL API (http://http://countdown.api.tfl.gov.uk)
|
||||
*
|
||||
* @author Stefan Kalscheuer
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ UraClient.class, URL.class })
|
||||
public class UraClientTest {
|
||||
@Test
|
||||
public void getStopsTest() 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_V2_stops.txt"));
|
||||
// Mocked resource URL and exception message.
|
||||
private static String mockResource = null;
|
||||
private static String mockException = null;
|
||||
|
||||
/* List stops and verify some values */
|
||||
@BeforeAll
|
||||
public static void initByteBuddy() {
|
||||
// Install ByteBuddy Agent.
|
||||
ByteBuddyAgent.install();
|
||||
|
||||
new ByteBuddy().redefine(UraClient.class)
|
||||
.method(named("request"))
|
||||
.intercept(to(UraClientTest.class))
|
||||
.make()
|
||||
.load(UraClient.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStopsTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("instant_V2_stops.txt");
|
||||
|
||||
// List stops and verify some values.
|
||||
List<Stop> stops = new UraClient("mocked").getStops();
|
||||
assertThat(stops, hasSize(10));
|
||||
assertThat(stops.get(0).getId(), is("100210"));
|
||||
assertThat(stops.get(1).getName(), is("Brockenberg"));
|
||||
assertThat(stops.get(2).getState(), is(0));;
|
||||
assertThat(stops.get(2).getState(), is(0));
|
||||
assertThat(stops.get(3).getLatitude(), is(50.7578775));
|
||||
assertThat(stops.get(4).getLongitude(), is(6.0708663));
|
||||
|
||||
/* Test exception handling */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(new InputStream() {
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
throw new IOException("Provoked exception 1.");
|
||||
}
|
||||
});
|
||||
assertThat(new UraClient("mocked").getStops(), hasSize(0));
|
||||
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2."));
|
||||
assertThat(new UraClient("mocked").getStops(), hasSize(0));
|
||||
// Test Exception handling.
|
||||
mockHttpToException("Provoked Exception 1");
|
||||
|
||||
try {
|
||||
new UraClient("mocked").getStops();
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e, is(instanceOf(IllegalStateException.class)));
|
||||
assertThat(e.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(e.getCause().getMessage(), is("Provoked Exception 1"));
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStream request(String originalURL) throws IOException {
|
||||
if (mockResource == null && mockException != null) {
|
||||
IOException e = new IOException(mockException);
|
||||
mockException = null;
|
||||
throw e;
|
||||
}
|
||||
|
||||
InputStream res = UraClientTest.class.getResourceAsStream(mockResource);
|
||||
mockResource = null;
|
||||
return res;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStopsForLineTest() 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_V2_stops_line.txt"));
|
||||
public void getStopsForLineTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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();
|
||||
assertThat(stops, hasSize(47));
|
||||
assertThat(stops.get(0).getId(), is("100000"));
|
||||
assertThat(stops.get(1).getName(), is("Kuckelkorn"));
|
||||
assertThat(stops.get(2).getState(), is(0));;
|
||||
assertThat(stops.get(2).getState(), is(0));
|
||||
assertThat(stops.get(3).getLatitude(), is(50.7690688));
|
||||
assertThat(stops.get(4).getIndicator(), is("H.1"));
|
||||
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"));
|
||||
public void getStopsForPositionTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("instant_V1_stops_circle.txt");
|
||||
|
||||
/* List stops and verify some values */
|
||||
List<Stop> stops = new UraClient("mocked").forPosition(51.51009, -0.1345734, 200).getStops();
|
||||
// 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(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();
|
||||
mockHttpToFile("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"));
|
||||
public void getTripsForDestinationNamesTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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();
|
||||
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"));
|
||||
trips = new UraClient("mocked").forStops("156").forDestinationNames("Marble Arch").getTrips();
|
||||
mockHttpToFile("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()));
|
||||
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"));
|
||||
public void getTripsTowardsTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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"));
|
||||
mockHttpToFile("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 */
|
||||
URL mockURL = PowerMockito.mock(URL.class);
|
||||
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
|
||||
public void getTripsTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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();
|
||||
assertThat(trips, hasSize(10));
|
||||
assertThat(trips.get(0).getId(), is("27000165015001"));
|
||||
assertThat(trips.get(1).getLineID(), is("55"));
|
||||
assertThat(trips.get(2).getLineName(), is("28"));;
|
||||
assertThat(trips.get(2).getLineName(), is("28"));
|
||||
assertThat(trips.get(3).getDirectionID(), is(1));
|
||||
assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr."));
|
||||
assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof"));
|
||||
@ -171,14 +198,15 @@ public class UraClientTest {
|
||||
assertThat(trips.get(8).getVisitID(), is(30));
|
||||
assertThat(trips.get(9).getStop().getId(), is("100002"));
|
||||
|
||||
/* Repeat test for API V2 */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt"));
|
||||
/* Get trips without filters and verify some values */
|
||||
// Repeat test for API V2.
|
||||
mockHttpToFile("instant_V2_trips_all.txt");
|
||||
|
||||
// Get trips without filters and verify some values.
|
||||
trips = new UraClient("mocked").getTrips();
|
||||
assertThat(trips, hasSize(10));
|
||||
assertThat(trips.get(0).getId(), is("27000165015001"));
|
||||
assertThat(trips.get(1).getLineID(), is("55"));
|
||||
assertThat(trips.get(2).getLineName(), is("28"));;
|
||||
assertThat(trips.get(2).getLineName(), is("28"));
|
||||
assertThat(trips.get(3).getDirectionID(), is(1));
|
||||
assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr."));
|
||||
assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof"));
|
||||
@ -187,31 +215,28 @@ public class UraClientTest {
|
||||
assertThat(trips.get(8).getVisitID(), is(30));
|
||||
assertThat(trips.get(9).getStop().getId(), is("100002"));
|
||||
|
||||
/* Get limited number of trips */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
|
||||
// Get limited number of trips.
|
||||
mockHttpToFile("instant_V1_trips_all.txt");
|
||||
trips = new UraClient("mocked").getTrips(5);
|
||||
assertThat(trips, hasSize(5));
|
||||
|
||||
/* Test exception handling */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(new InputStream() {
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
throw new IOException("Provoked exception 1.");
|
||||
}
|
||||
});
|
||||
assertThat(new UraClient("mocked").getTrips(), hasSize(0));
|
||||
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2."));
|
||||
assertThat(new UraClient("mocked").getTrips(), hasSize(0));
|
||||
// Test mockException handling.
|
||||
mockHttpToException("Provoked mockException 2");
|
||||
try {
|
||||
new UraClient("mocked").getTrips();
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e, is(instanceOf(IllegalStateException.class)));
|
||||
assertThat(e.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(e.getCause().getMessage(), is("Provoked mockException 2"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTripsForStopTest() 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_stop.txt"));
|
||||
public void getTripsForStopTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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")
|
||||
.forStops("100000")
|
||||
.getTrips();
|
||||
@ -219,30 +244,29 @@ public class UraClientTest {
|
||||
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty()));
|
||||
assertThat(trips.get(0).getId(), is("27000158010001"));
|
||||
assertThat(trips.get(1).getLineID(), is("7"));
|
||||
assertThat(trips.get(2).getLineName(), is("25"));;
|
||||
assertThat(trips.get(2).getLineName(), is("25"));
|
||||
assertThat(trips.get(3).getStop().getIndicator(), is("H.15"));
|
||||
|
||||
/* Get trips for stop name "Uniklinik" and verify some values */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt"));
|
||||
// Get trips for stop name "Uniklinik" and verify some values.
|
||||
mockHttpToFile("instant_V1_trips_stop_name.txt");
|
||||
trips = new UraClient("mocked")
|
||||
.forStopsByName("Uniklinik")
|
||||
.getTrips();
|
||||
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(1).getLineID(), is("5"));
|
||||
assertThat(trips.get(2).getVehicleID(), is("317"));;
|
||||
assertThat(trips.get(2).getVehicleID(), is("317"));
|
||||
assertThat(trips.get(3).getDirectionID(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTripsForLine() 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_line.txt"));
|
||||
public void getTripsForLine() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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")
|
||||
.forLines("3")
|
||||
.getTrips();
|
||||
@ -250,11 +274,11 @@ public class UraClientTest {
|
||||
assertThat(trips.stream().filter(t -> !t.getLineID().equals("3")).findAny(), is(Optional.empty()));
|
||||
assertThat(trips.get(0).getId(), is("27000154004001"));
|
||||
assertThat(trips.get(1).getLineID(), is("3"));
|
||||
assertThat(trips.get(2).getLineName(), is("3.A"));;
|
||||
assertThat(trips.get(2).getLineName(), is("3.A"));
|
||||
assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)"));
|
||||
|
||||
/* Get trips for line name "3.A" and verify some values */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt"));
|
||||
// Get trips for line name "3.A" and verify some values.
|
||||
mockHttpToFile("instant_V1_trips_line_name.txt");
|
||||
trips = new UraClient("mocked")
|
||||
.forLinesByName("3.A")
|
||||
.getTrips();
|
||||
@ -262,11 +286,11 @@ public class UraClientTest {
|
||||
assertThat(trips.stream().filter(t -> !t.getLineName().equals("3.A")).findAny(), is(Optional.empty()));
|
||||
assertThat(trips.get(0).getId(), is("92000288014001"));
|
||||
assertThat(trips.get(1).getLineID(), is("3"));
|
||||
assertThat(trips.get(2).getLineName(), is("3.A"));;
|
||||
assertThat(trips.get(2).getLineName(), is("3.A"));
|
||||
assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße"));
|
||||
|
||||
/* 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"));
|
||||
// Get trips for line 3 with direction 1 and verify some values.
|
||||
mockHttpToFile("instant_V1_trips_line_direction.txt");
|
||||
trips = new UraClient("mocked")
|
||||
.forLines("412")
|
||||
.forDirection(2)
|
||||
@ -275,8 +299,8 @@ public class UraClientTest {
|
||||
assertThat(trips.stream().filter(t -> !t.getLineID().equals("412")).findAny(), is(Optional.empty()));
|
||||
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty()));
|
||||
|
||||
/* Test lineID and direction in different order */
|
||||
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
|
||||
// Test lineID and direction in different order.
|
||||
mockHttpToFile("instant_V1_trips_line_direction.txt");
|
||||
trips = new UraClient("mocked")
|
||||
.forDirection(2)
|
||||
.forLines("412")
|
||||
@ -287,23 +311,31 @@ public class UraClientTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTripsForStopAndLine() 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_stop_line.txt"));
|
||||
public void getTripsForStopAndLine() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("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")
|
||||
.forLines("25", "35")
|
||||
.forStops("100000")
|
||||
.getTrips();
|
||||
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.get(0).getId(), is("27000078014001"));
|
||||
assertThat(trips.get(1).getLineID(), is("25"));
|
||||
assertThat(trips.get(3).getLineName(), is("35"));;
|
||||
assertThat(trips.get(3).getLineName(), is("35"));
|
||||
assertThat(trips.get(5).getStop().getIndicator(), is("H.12"));
|
||||
}
|
||||
|
||||
|
||||
private static void mockHttpToFile(String newResourceFile) {
|
||||
mockResource = newResourceFile;
|
||||
}
|
||||
|
||||
private static void mockHttpToException(String newException) {
|
||||
mockException = newException;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -16,7 +16,7 @@
|
||||
|
||||
package de.stklcode.pubtrans.ura.model;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Unit test for the Stop metamodel.
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 Stefan Kalscheuer
|
||||
* 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.
|
||||
@ -16,7 +16,7 @@
|
||||
|
||||
package de.stklcode.pubtrans.ura.model;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Unit test for the Trip metamodel.
|
||||
@ -36,8 +36,21 @@ import static org.junit.Assert.fail;
|
||||
public class TripTest {
|
||||
@Test
|
||||
public void basicConstructorTest() {
|
||||
Trip trip = new Trip("sid", "name", "indicator", 1, 2.345, 6.789,
|
||||
123, "lineid", "linename", 0, "destination name", "destination text", "vehicle", "id", 123456789123456789L);
|
||||
Trip trip = new Trip("sid",
|
||||
"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().getName(), is("name"));
|
||||
assertThat(trip.getStop().getIndicator(), is("indicator"));
|
||||
|
Reference in New Issue
Block a user