Test coverage 100%

This commit is contained in:
Stefan Kalscheuer 2017-01-02 15:15:53 +01:00
parent 060fe2f6b9
commit ef7423e4ca
9 changed files with 436 additions and 13 deletions

View File

@ -1,6 +1,7 @@
jURAclient
jURAclient [![Build status](https://travis-ci.org/stklcode/juraclient.svg?branch=master)](https://travis-ci.org/stklcode/juraclient)
==========
Java client for URA based public transport APIs.
This client allows to simply connect any Java application to the public transport API to implement a monitor for the
@ -40,12 +41,6 @@ List<Trip> trips = ura.forStop("100000")
* More refined query parameters
* Stream API with asynchronous consumer
**Build status:**
* master [![Build status](https://travis-ci.org/stklcode/juraclient.svg?branch=master)](https://travis-ci.org/stklcode/juraclient)
* develop [![Build status](https://travis-ci.org/stklcode/juraclient.svg?branch=develop)](https://travis-ci.org/stklcode/juraclient)
**License**
The project is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).

View File

@ -197,7 +197,7 @@ public class UraClient {
*
* @return the list
*/
public List<Stop> listStops() {
public List<Stop> getStops() {
List<Stop> stops = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_STOP, null, null, null, null, null);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {

View File

@ -69,7 +69,6 @@ public class Stop {
longitude = (Double)raw.get(6);
else
throw new IOException("Field 6 not of expected type Double, found " + raw.get(6).getClass().getSimpleName());
}
public String getId() {

View File

@ -24,6 +24,8 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Optional;
@ -40,23 +42,32 @@ import static org.hamcrest.core.Is.is;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ UraClient.class, URL.class })
public class UraClientTest {
private static final String BASE_ASEAG = "http://ivu.aseag.de";
@Test
public void listStopsTest() throws Exception {
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_stops.txt"));
/* List stops and verify some values */
List<Stop> stops = new UraClient("mocked").listStops();
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(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
@ -84,6 +95,17 @@ public class UraClientTest {
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_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
@ -103,6 +125,18 @@ public class UraClientTest {
assertThat(trips.get(1).getLineID(), is("7"));
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_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.get(0).getId(), is("92000043013001"));
assertThat(trips.get(1).getLineID(), is("5"));
assertThat(trips.get(2).getVehicleID(), is("317"));;
assertThat(trips.get(3).getDirectionID(), is(1));
}
@Test
@ -122,6 +156,38 @@ public class UraClientTest {
assertThat(trips.get(1).getLineID(), is("3"));
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_trips_line_name.txt"));
trips = new UraClient("mocked")
.forLinesByName("3.A")
.getTrips();
assertThat(trips, hasSize(10));
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(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_trips_line_direction.txt"));
trips = new UraClient("mocked")
.forLines("3")
.forDirection(1)
.getTrips();
assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getLineID().equals("3")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(1)).findAny(), is(Optional.empty()));
/* Test lineID and direction in different order */
PowerMockito.when(mockURL.openStream()).thenReturn(getClass().getResourceAsStream("instant_trips_line_direction.txt"));
trips = new UraClient("mocked")
.forDirection(1)
.forLines("3")
.getTrips();
assertThat(trips, hasSize(10));
assertThat(trips.stream().filter(t -> !t.getLineID().equals("3")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(1)).findAny(), is(Optional.empty()));
}
@Test

View File

@ -0,0 +1,136 @@
package de.stklcode.pubtrans.ura.model;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
/**
* Unit test for the Stop metamodel.
*
* @author Stefan Kalscheuer <stefan@stklcode.de>
*/
public class StopTest {
@Test
public void basicConstructorTest() {
Stop stop = new Stop("id", "name", "indicator", 1, 2.345, 6.789);
assertThat(stop.getId(), is("id"));
assertThat(stop.getName(), is("name"));
assertThat(stop.getIndicator(), is("indicator"));
assertThat(stop.getState(), is(1));
assertThat(stop.getLatitude(), is(2.345));
assertThat(stop.getLongitude(), is(6.789));
}
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
raw.add("stopIndicator");
raw.add(9);
raw.add(8.765);
raw.add(4.321);
try {
Stop stop = new Stop(raw);
assertThat(stop.getId(), is("stopId"));
assertThat(stop.getName(), is("stopName"));
assertThat(stop.getIndicator(), is("stopIndicator"));
assertThat(stop.getState(), is(9));
assertThat(stop.getLatitude(), is(8.765));
assertThat(stop.getLongitude(), is(4.321));
} catch (IOException e) {
fail("Creation of Stop from valid list failed: " + e.getMessage());
}
/* Excess elements should be ignored */
raw.add("foo");
try {
Stop stop = new Stop(raw);
assertThat(stop, is(notNullValue()));
raw.remove(7);
} catch (IOException e) {
fail("Creation of Stop from valid list failed: " + e.getMessage());
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
invalid.remove(1);
invalid.add(1, 5);
try {
new Stop(invalid);
fail("Creation of Stop with invalid name field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(2);
invalid.add(2, 0);
try {
new Stop(invalid);
fail("Creation of Stop with invalid id field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(3);
invalid.add(3, -1.23);
try {
new Stop(invalid);
fail("Creation of Stop with invalid indicator field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(4);
invalid.add(4, "foo");
try {
new Stop(invalid);
fail("Creation of Stop with invalid state field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(5);
invalid.add(5, "123");
try {
new Stop(invalid);
fail("Creation of Stop with invalid latitude field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(6);
invalid.add(6, 456);
try {
new Stop(invalid);
fail("Creation of Stop with invalid longitude field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(6);
try {
new Stop(invalid);
fail("Creation of Stop with too short list successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
}
}

View File

@ -0,0 +1,194 @@
package de.stklcode.pubtrans.ura.model;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
/**
* Unit test for the Trip metamodel.
*
* @author Stefan Kalscheuer <stefan@stklcode.de>
*/
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);
assertThat(trip.getStop().getId(), is("sid"));
assertThat(trip.getStop().getName(), is("name"));
assertThat(trip.getStop().getIndicator(), is("indicator"));
assertThat(trip.getStop().getState(), is(1));
assertThat(trip.getStop().getLatitude(), is(2.345));
assertThat(trip.getStop().getLongitude(), is(6.789));
assertThat(trip.getVisitID(), is(123));
assertThat(trip.getLineID(), is("lineid"));
assertThat(trip.getLineName(), is("linename"));
assertThat(trip.getDirectionID(), is(0));
assertThat(trip.getDestinationName(), is("destination name"));
assertThat(trip.getDestinationText(), is("destination text"));
assertThat(trip.getVehicleID(), is("vehicle"));
assertThat(trip.getId(), is("id"));
assertThat(trip.getEstimatedTime(), is(123456789123456789L));
}
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
raw.add("stopIndicator");
raw.add(9);
raw.add(8.765);
raw.add(43.21);
raw.add(123);
raw.add("lineid");
raw.add("linename");
raw.add(0);
raw.add("destination name");
raw.add("destination text");
raw.add("vehicle");
raw.add("id");
raw.add(123456789123456789L);
try {
Trip trip = new Trip(raw);
assertThat(trip.getStop().getId(), is("stopId"));
assertThat(trip.getStop().getName(), is("stopName"));
assertThat(trip.getStop().getIndicator(), is("stopIndicator"));
assertThat(trip.getStop().getState(), is(9));
assertThat(trip.getStop().getLatitude(), is(8.765));
assertThat(trip.getStop().getLongitude(), is(43.21));
assertThat(trip.getVisitID(), is(123));
assertThat(trip.getLineID(), is("lineid"));
assertThat(trip.getLineName(), is("linename"));
assertThat(trip.getDirectionID(), is(0));
assertThat(trip.getDestinationName(), is("destination name"));
assertThat(trip.getDestinationText(), is("destination text"));
assertThat(trip.getVehicleID(), is("vehicle"));
assertThat(trip.getId(), is("id"));
assertThat(trip.getEstimatedTime(), is(123456789123456789L));
} catch (IOException e) {
fail("Creation of Trip from valid list failed: " + e.getMessage());
}
/* Excess elements should be ignored */
raw.add("foo");
try {
Trip trip = new Trip(raw);
assertThat(trip, is(notNullValue()));
raw.remove(16);
} catch (IOException e) {
fail("Creation of Trip from valid list failed: " + e.getMessage());
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
invalid.remove(7);
invalid.add(7, "123");
try {
new Trip(invalid);
fail("Creation of Trip with invalid visitID field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(8);
invalid.add(8, 25);
try {
new Trip(invalid);
fail("Creation of Trip with invalid lineID field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(9);
invalid.add(9, 234L);
try {
new Trip(invalid);
fail("Creation of Trip with invalid line name field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(10);
invalid.add(10, "1");
try {
new Trip(invalid);
fail("Creation of Trip with invalid directionID field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(11);
invalid.add(11, 987);
try {
new Trip(invalid);
fail("Creation of Trip with invalid destinationName field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(12);
invalid.add(12, 456.78);
try {
new Trip(invalid);
fail("Creation of Trip with invalid destinationText field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(13);
invalid.add(13, 'x');
try {
new Trip(invalid);
fail("Creation of Trip with invalid vehicleID field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(14);
invalid.add(14, 123);
try {
new Trip(invalid);
fail("Creation of Trip with invalid id field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(15);
invalid.add(15, 456);
try {
new Trip(invalid);
fail("Creation of Trip with invalid estimatedTime field successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(15);
try {
new Trip(invalid);
fail("Creation of Trip with too short list successfull");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
}
}

View File

@ -0,0 +1,11 @@
[4,"2.0",1483363349461]
[1,"Westbahnhof","100641","H.2",0,50.7805372,6.0719672,17,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","325","92000288014001",1483367880000]
[1,"Aachen Gartenstraße","100601","",0,50.7704169,6.0695841,4,"3","3.A",1,"Schanz-Hbf.-Ponttor","Schanz-Hbf.-Ponttor","229","92000058014001",1483368360000]
[1,"Schanz","100012","H.2 (Boxgraben)",0,50.76891,6.074498,5,"3","3.A",1,"Hbf.-Ponttor-Uniklinik","Hbf.-Ponttor-Uniklinik","0","92000288015001",1483369380000]
[1,"Kastanienweg","100631","",0,50.7854908,6.0547602,21,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","229","92000058014001",1483369860000]
[1,"Eurogress","100007","",0,50.7807852,6.0900738,13,"3","3.A",1,"Ponttor-Unikl.-Schanz","Ponttor-Unikl.-Schanz","327","92000289012001",1483363996000]
[1,"Augustastraße","100008","",0,50.7731333,6.0959027,10,"3","3.A",1,"Ponttor-Unikl.-Schanz","Ponttor-Unikl.-Schanz","325","92000288014001",1483367280000]
[1,"Hörn Brücke","100627","",0,50.7871194,6.051875,22,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000279013001",1483369020000]
[1,"Audimax","100029","",0,50.7802655,6.0752138,16,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000288015001",1483370460000]
[1,"Misereor","100010","",0,50.7685583,6.0833027,7,"3","3.A",1,"Hbf.-Ponttor-Uniklinik","Hbf.-Ponttor-Uniklinik","0","92000154003001",1483370460000]
[1,"Audimax","100029","",0,50.7802655,6.0752138,16,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","229","92000058014001",1483369560000]

View File

@ -0,0 +1,11 @@
[4,"2.0",1483362794677]
[1,"Westbahnhof","100641","H.2",0,50.7805372,6.0719672,17,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000288014001",1483367880000]
[1,"Aachen Gartenstraße","100601","",0,50.7704169,6.0695841,4,"3","3.A",1,"Schanz-Hbf.-Ponttor","Schanz-Hbf.-Ponttor","0","92000058014001",1483368360000]
[1,"Schanz","100012","H.2 (Boxgraben)",0,50.76891,6.074498,5,"3","3.A",1,"Hbf.-Ponttor-Uniklinik","Hbf.-Ponttor-Uniklinik","0","92000288015001",1483369380000]
[1,"Aachen Gartenstraße","100601","",0,50.7704169,6.0695841,4,"3","3.A",1,"Schanz-Hbf.-Ponttor","Schanz-Hbf.-Ponttor","327","92000289012001",1483363045000]
[1,"Kastanienweg","100631","",0,50.7854908,6.0547602,21,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000058014001",1483369860000]
[1,"Eurogress","100007","",0,50.7807852,6.0900738,13,"3","3.A",1,"Ponttor-Unikl.-Schanz","Ponttor-Unikl.-Schanz","327","92000289012001",1483363994000]
[1,"Augustastraße","100008","",0,50.7731333,6.0959027,10,"3","3.A",1,"Ponttor-Unikl.-Schanz","Ponttor-Unikl.-Schanz","0","92000288014001",1483367280000]
[1,"Hörn Brücke","100627","",0,50.7871194,6.051875,22,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000279013001",1483369020000]
[1,"Audimax","100029","",0,50.7802655,6.0752138,16,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","0","92000058014001",1483369560000]
[1,"Aachen Hauptbahnhof","100004","H.1",0,50.7687027,6.0906277,8,"3","3.A",1,"Ponttor-Unikl.-Schanz","Ponttor-Unikl.-Schanz","0","92000288014001",1483367040000]

View File

@ -0,0 +1,11 @@
[4,"2.0",1483362959788]
[1,"Uniklinik","100600","H.2",0,50.7756388,6.04425,11,"33","33",1,"Aachen Fuchserde","Aachen Fuchserde","318","92000043013001",1483362935000]
[1,"Uniklinik","100600","H.1",0,50.7756388,6.04425,1,"5","5",1,"Driescher Hof-Brand","Driescher Hof-Brand","312","92000282009001",1483362936000]
[1,"Uniklinik","100600","H.4",0,50.7756388,6.04425,33,"45","45",1,"Uniklinik","Uniklinik","317","92000285009001",1483363294000]
[1,"Uniklinik","100600","H.3",0,50.7756388,6.04425,28,"10","3.B",1,"Uniklinik-Ponttor-Hbf.","Uniklinik-Ponttor-Hbf.","347","92000053015001",1483363039000]
[1,"Uniklinik","100600","H.3",0,50.7756388,6.04425,29,"33","33",1,"Uniklinik","Uniklinik","529","92000209014001",1483363288000]
[1,"Uniklinik","100600","H.2",0,50.7756388,6.04425,1,"73","73",1,"Aachen Bf.Rothe Erde","Aachen Bf.Rothe Erde","315","92000291016001",1483363080000]
[1,"Uniklinik","100600","H.3",0,50.7756388,6.04425,29,"10","3.B",1,"Uniklinik-Ponttor-Hbf.","Uniklinik-Ponttor-Hbf.","347","92000053015001",1483363099000]
[1,"Uniklinik","100600","H.1",0,50.7756388,6.04425,28,"3","3.A",1,"Uniklinik-Schanz-Hbf.","Uniklinik-Schanz-Hbf.","325","92000288012001",1483363080000]
[1,"Uniklinik","100600","H.2",0,50.7756388,6.04425,6,"70","70",1,"Aachen Adenauerallee","Aachen Adenauerallee","588","92000225009001",1483363346000]
[1,"Uniklinik","100600","H.1",0,50.7756388,6.04425,1,"3","3.A",1,"Schanz-Hbf.-Ponttor","Schanz-Hbf.-Ponttor","325","92000288013001",1483363380000]