Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
ee91f05898 | |||
83e6e446ba | |||
e8ca106037 | |||
5808365777 | |||
9e7a2a92ee | |||
4de8f3f073 | |||
8631a43ed8 | |||
c9444bbc97 | |||
0d0859bd3a | |||
d5e09392ef | |||
d12c585083 | |||
ffa8af2d5d | |||
eb400b52ca | |||
0e21c0d64c | |||
7e3af36e22 |
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
|
||||
|
@ -1,6 +1,7 @@
|
||||
language: java
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
- oraclejdk11
|
||||
install: true
|
||||
addons:
|
||||
sonarcloud:
|
||||
|
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,3 +1,11 @@
|
||||
## 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
|
||||
@ -9,4 +17,4 @@
|
||||
* [test] Test coverage 100% (line); tested against ASEAG and TFL APIs
|
||||
|
||||
## 1.0.0 [2017-01-02]
|
||||
* Initial release
|
||||
* Initial release
|
||||
|
@ -1,8 +1,9 @@
|
||||
# jURAclient
|
||||
[](https://travis-ci.org/stklcode/juraclient)
|
||||
[](https://sonarcloud.io/dashboard?id=de.stklcode.pubtrans%3Ajuraclient)
|
||||
[](https://github.com/stklcode/juraclient/blob/master/LICENSE.txt)
|
||||
[](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.
|
||||
|
||||
@ -54,7 +55,7 @@ List<Trip> trips = ura.forStopByName("Piccadilly Circus")
|
||||
<dependency>
|
||||
<groupId>de.stklcode.pubtrans</groupId>
|
||||
<artifactId>juraclient</artifactId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.3</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
101
pom.xml
101
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>de.stklcode.pubtrans</groupId>
|
||||
<artifactId>juraclient</artifactId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.3</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -50,17 +50,17 @@
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.9.4</version>
|
||||
<version>2.9.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.4</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>
|
||||
@ -70,15 +70,15 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>2.0.0-beta.5</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-mockito2</artifactId>
|
||||
<version>2.0.0-beta.5</version>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>1.9.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -88,12 +88,29 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.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>
|
||||
@ -103,7 +120,7 @@
|
||||
<dependency>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>org.jacoco.agent</artifactId>
|
||||
<version>0.8.0</version>
|
||||
<version>0.8.2</version>
|
||||
<classifier>runtime</classifier>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -112,7 +129,7 @@
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.0</version>
|
||||
<version>0.8.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-instrument</id>
|
||||
@ -141,7 +158,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20.1</version>
|
||||
<version>2.22.1</version>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
|
||||
@ -151,5 +168,57 @@
|
||||
</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>
|
||||
|
@ -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";
|
||||
|
||||
@ -308,9 +307,18 @@ public class UraClient {
|
||||
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
|
||||
}
|
||||
|
||||
URL url = new URL(urlStr);
|
||||
return request(urlStr);
|
||||
}
|
||||
|
||||
return url.openStream();
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,12 +17,15 @@
|
||||
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 {
|
||||
interface Model extends Serializable {
|
||||
/**
|
||||
* Generate exception for unmatched type when String is expected.
|
||||
*
|
||||
|
@ -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>
|
@ -18,108 +18,121 @@ 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.instanceOf;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
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.");
|
||||
}
|
||||
});
|
||||
// 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."));
|
||||
assertThat(e.getCause().getMessage(), is("Provoked Exception 1"));
|
||||
}
|
||||
}
|
||||
|
||||
@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 static InputStream request(String originalURL) throws IOException {
|
||||
if (mockResource == null && mockException != null) {
|
||||
IOException e = new IOException(mockException);
|
||||
mockException = null;
|
||||
throw e;
|
||||
}
|
||||
|
||||
/* List stops and verify some values */
|
||||
InputStream res = UraClientTest.class.getResourceAsStream(mockResource);
|
||||
mockResource = null;
|
||||
return res;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getStopsForLineTest() {
|
||||
// Mock the HTTP call.
|
||||
mockHttpToFile("instant_V2_stops_line.txt");
|
||||
|
||||
// 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 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"));
|
||||
mockHttpToFile("instant_V1_stops_circle_name.txt");
|
||||
stops = new UraClient("mocked")
|
||||
.forStopsByName("Piccadilly Circus")
|
||||
.forPosition(51.51009, -0.1345734, 200)
|
||||
@ -129,21 +142,17 @@ public class UraClientTest {
|
||||
}
|
||||
|
||||
@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()));
|
||||
|
||||
PowerMockito.when(mockURL.openStream())
|
||||
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_destination.txt"));
|
||||
mockHttpToFile("instant_V1_trips_stop_destination.txt");
|
||||
trips = new UraClient("mocked")
|
||||
.forStops("156")
|
||||
.forDestinationNames("Marble Arch")
|
||||
@ -156,38 +165,31 @@ public class UraClientTest {
|
||||
}
|
||||
|
||||
@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"));
|
||||
@ -196,15 +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"));
|
||||
@ -213,37 +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.");
|
||||
}
|
||||
});
|
||||
// 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 exception 1."));
|
||||
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();
|
||||
@ -251,12 +244,11 @@ 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();
|
||||
@ -265,19 +257,16 @@ public class UraClientTest {
|
||||
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();
|
||||
@ -285,12 +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();
|
||||
@ -298,12 +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)
|
||||
@ -312,9 +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")
|
||||
@ -325,14 +311,11 @@ 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")
|
||||
@ -343,7 +326,16 @@ public class UraClientTest {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
Reference in New Issue
Block a user