test: minor test code refactoring

This commit is contained in:
Stefan Kalscheuer 2024-06-29 13:48:29 +02:00
parent 14f4266cb1
commit ff2650e8c1
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
6 changed files with 43 additions and 43 deletions

View File

@ -12,9 +12,9 @@ import static org.junit.jupiter.api.Assertions.assertNull;
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class UraClientConfigurationTest { class UraClientConfigurationTest {
@Test @Test
public void configBuilderTest() { void configBuilderTest() {
final String baseURL = "https://ura.example.com"; final String baseURL = "https://ura.example.com";
final String instantPath = "/path/to/instant"; final String instantPath = "/path/to/instant";
final String streamPath = "/path/to/stream"; final String streamPath = "/path/to/stream";

View File

@ -45,19 +45,19 @@ import static org.junit.jupiter.api.Assertions.*;
/** /**
* Unit test for the URA Client. * Unit test for the URA Client.
* Tests run against mocked data collected from ASEAG API (no longer available) and * Tests run against mocked data collected from ASEAG API (no longer available) and
* TFL API (http://countdown.api.tfl.gov.uk) * TFL API (<a href="https://countdown.api.tfl.gov.uk">https://countdown.api.tfl.gov.uk</a>)
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class UraClientTest { class UraClientTest {
@RegisterExtension @RegisterExtension
public static WireMockExtension wireMock = WireMockExtension.newInstance() static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort()) .options(wireMockConfig().dynamicPort())
.build(); .build();
@Test @Test
public void getStopsTest() throws UraClientException { void getStopsTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(2, "instant_V2_stops.txt"); mockHttpToFile(2, "instant_V2_stops.txt");
@ -83,7 +83,7 @@ public class UraClientTest {
} }
@Test @Test
public void getStopsForLineTest() throws UraClientException { void getStopsForLineTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(2, "instant_V2_stops_line.txt"); mockHttpToFile(2, "instant_V2_stops_line.txt");
@ -101,7 +101,7 @@ public class UraClientTest {
} }
@Test @Test
public void getStopsForPositionTest() throws UraClientException { void getStopsForPositionTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_stops_circle.txt"); mockHttpToFile(1, "instant_V1_stops_circle.txt");
@ -127,7 +127,7 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsForDestinationNamesTest() throws UraClientException { void getTripsForDestinationNamesTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_destination.txt"); mockHttpToFile(1, "instant_V1_trips_destination.txt");
@ -150,7 +150,7 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsTowardsTest() throws UraClientException { void getTripsTowardsTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_towards.txt"); mockHttpToFile(1, "instant_V1_trips_towards.txt");
@ -165,7 +165,7 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsTest() throws UraClientException { void getTripsTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_all.txt"); mockHttpToFile(1, "instant_V1_trips_all.txt");
@ -229,11 +229,11 @@ public class UraClientTest {
"Expected reader to raise an exception" "Expected reader to raise an exception"
); );
assertEquals("Failed to read trips from API", exception.getMessage(), "Unexpected error message"); assertEquals("Failed to read trips from API", exception.getMessage(), "Unexpected error message");
assertTrue(exception.getCause() instanceof IOException, "Unexpected error cause"); assertInstanceOf(IOException.class, exception.getCause(), "Unexpected error cause");
} }
@Test @Test
public void getTripsForStopTest() throws UraClientException { void getTripsForStopTest() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_stop.txt"); mockHttpToFile(1, "instant_V1_trips_stop.txt");
@ -274,11 +274,11 @@ public class UraClientTest {
"Expected reader to raise an exception" "Expected reader to raise an exception"
); );
assertEquals("Failed to read stops from API", exception.getMessage(), "Unexpected error message"); assertEquals("Failed to read stops from API", exception.getMessage(), "Unexpected error message");
assertTrue(exception.getCause() instanceof IOException, "Unexpected error cause"); assertInstanceOf(IOException.class, exception.getCause(), "Unexpected error cause");
} }
@Test @Test
public void getTripsForLine() throws UraClientException { void getTripsForLine() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_line.txt"); mockHttpToFile(1, "instant_V1_trips_line.txt");
@ -327,7 +327,7 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsForStopAndLine() throws UraClientException { void getTripsForStopAndLine() throws UraClientException {
// Mock the HTTP call. // Mock the HTTP call.
mockHttpToFile(1, "instant_V1_trips_stop_line.txt"); mockHttpToFile(1, "instant_V1_trips_stop_line.txt");
@ -348,7 +348,7 @@ public class UraClientTest {
@Test @Test
public void getMessages() throws UraClientException { void getMessages() throws UraClientException {
UraClient uraClient = new UraClient(wireMock.baseUrl()); UraClient uraClient = new UraClient(wireMock.baseUrl());
// Mock the HTTP call. // Mock the HTTP call.
@ -379,11 +379,11 @@ public class UraClientTest {
"Expected reader to raise an exception" "Expected reader to raise an exception"
); );
assertEquals("Failed to read messages from API", exception.getMessage(), "Unexpected error message"); assertEquals("Failed to read messages from API", exception.getMessage(), "Unexpected error message");
assertTrue(exception.getCause() instanceof IOException, "Unexpected error cause"); assertInstanceOf(IOException.class, exception.getCause(), "Unexpected error cause");
} }
@Test @Test
public void getMessagesForStop() throws UraClientException { void getMessagesForStop() throws UraClientException {
UraClient uraClient = new UraClient(wireMock.baseUrl(), "/interfaces/ura/instant_V2", "/interfaces/ura/stream"); UraClient uraClient = new UraClient(wireMock.baseUrl(), "/interfaces/ura/instant_V2", "/interfaces/ura/stream");
// Mock the HTTP call. // Mock the HTTP call.
@ -406,7 +406,7 @@ public class UraClientTest {
} }
@Test @Test
public void timeoutTest() { void timeoutTest() {
// Try to read trips from TEST-NET-1 IP that is not routed (hopefully) and will not connect within 100ms. // Try to read trips from TEST-NET-1 IP that is not routed (hopefully) and will not connect within 100ms.
UraClientException exception = assertThrows( UraClientException exception = assertThrows(
UraClientException.class, UraClientException.class,
@ -417,7 +417,7 @@ public class UraClientTest {
).forDestinationNames("Piccadilly Circus").getTrips(), ).forDestinationNames("Piccadilly Circus").getTrips(),
"Connection to TEST-NET-1 address should fail" "Connection to TEST-NET-1 address should fail"
); );
assertTrue(exception.getCause() instanceof HttpConnectTimeoutException, "Exception cause is not HttpConnectionTimeoutException"); assertInstanceOf(HttpConnectTimeoutException.class, exception.getCause(), "Exception cause is not HttpConnectionTimeoutException");
// Mock the HTTP call with delay of 200ms, but immediate connection. // Mock the HTTP call with delay of 200ms, but immediate connection.
wireMock.stubFor( wireMock.stubFor(
@ -444,7 +444,7 @@ public class UraClientTest {
).forDestinationNames("Piccadilly Circus").getTrips(), ).forDestinationNames("Piccadilly Circus").getTrips(),
"Response timeout did not raise an exception" "Response timeout did not raise an exception"
); );
assertTrue(exception.getCause() instanceof HttpTimeoutException, "Exception cause is not HttpTimeoutException"); assertInstanceOf(HttpTimeoutException.class, exception.getCause(), "Exception cause is not HttpTimeoutException");
assertDoesNotThrow( assertDoesNotThrow(
() -> new UraClient( () -> new UraClient(

View File

@ -30,13 +30,13 @@ import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Unit test for the {@link Message} meta model. * Unit test for the {@link Message} model.
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class MessageTest { class MessageTest {
@Test @Test
public void basicConstructorTest() { void basicConstructorTest() {
Message message = new Message("sid", Message message = new Message("sid",
"name", "name",
"indicator", "indicator",
@ -60,7 +60,7 @@ public class MessageTest {
} }
@Test @Test
public void listConstructorTest() { void listConstructorTest() {
/* Create valid raw data list */ /* Create valid raw data list */
List<Serializable> raw = new ArrayList<>(); List<Serializable> raw = new ArrayList<>();
raw.add(1); raw.add(1);

View File

@ -30,13 +30,13 @@ import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Unit test for the {@link Stop} meta model. * Unit test for the {@link Stop} model.
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class StopTest { class StopTest {
@Test @Test
public void basicConstructorTest() { void basicConstructorTest() {
Stop stop = new Stop("id", "name", "indicator", 1, 2.345, 6.789); Stop stop = new Stop("id", "name", "indicator", 1, 2.345, 6.789);
assertThat(stop.getId(), is("id")); assertThat(stop.getId(), is("id"));
assertThat(stop.getName(), is("name")); assertThat(stop.getName(), is("name"));
@ -47,7 +47,7 @@ public class StopTest {
} }
@Test @Test
public void listConstructorTest() { void listConstructorTest() {
/* Create valid raw data list */ /* Create valid raw data list */
List<Serializable> raw = new ArrayList<>(); List<Serializable> raw = new ArrayList<>();
raw.add(1); raw.add(1);

View File

@ -30,13 +30,13 @@ import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Unit test for the {@link Trip} meta model. * Unit test for the {@link Trip} model.
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class TripTest { class TripTest {
@Test @Test
public void basicConstructorTest() { void basicConstructorTest() {
Trip trip = new Trip("sid", Trip trip = new Trip("sid",
"name", "name",
"indicator", "indicator",
@ -70,7 +70,7 @@ public class TripTest {
} }
@Test @Test
public void listConstructorTest() { void listConstructorTest() {
/* Create valid raw data list */ /* Create valid raw data list */
List<Serializable> raw = new ArrayList<>(); List<Serializable> raw = new ArrayList<>();
raw.add(1); raw.add(1);

View File

@ -18,13 +18,12 @@ package de.stklcode.pubtrans.ura.reader;
import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.Parameters; import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer; import com.github.tomakehurst.wiremock.extension.ResponseTransformerV2;
import com.github.tomakehurst.wiremock.http.ChunkedDribbleDelay; import com.github.tomakehurst.wiremock.http.ChunkedDribbleDelay;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response; import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import de.stklcode.pubtrans.ura.UraClientConfiguration; import de.stklcode.pubtrans.ura.UraClientConfiguration;
import de.stklcode.pubtrans.ura.model.Trip; import de.stklcode.pubtrans.ura.model.Trip;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
@ -54,7 +53,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
public class AsyncUraTripReaderTest { class AsyncUraTripReaderTest {
private static WireMockServer httpMock; private static WireMockServer httpMock;
@BeforeAll @BeforeAll
@ -82,7 +81,7 @@ public class AsyncUraTripReaderTest {
* @throws InterruptedException Thread interrupted. * @throws InterruptedException Thread interrupted.
*/ */
@Test @Test
public void readerTest() throws InterruptedException { void readerTest() throws InterruptedException {
// Callback counter for some unhandy async mockery. // Callback counter for some unhandy async mockery.
final AtomicInteger counter = new AtomicInteger(0); final AtomicInteger counter = new AtomicInteger(0);
@ -161,7 +160,7 @@ public class AsyncUraTripReaderTest {
* @throws InterruptedException Thread interrupted. * @throws InterruptedException Thread interrupted.
*/ */
@Test @Test
public void streamClosedTest() throws InterruptedException { void streamClosedTest() throws InterruptedException {
// Callback counter for some unhandy async mockery. // Callback counter for some unhandy async mockery.
final AtomicInteger counter = new AtomicInteger(0); final AtomicInteger counter = new AtomicInteger(0);
@ -202,7 +201,7 @@ public class AsyncUraTripReaderTest {
} }
@Test @Test
public void timeoutTest() throws InterruptedException { void timeoutTest() throws InterruptedException {
// Callback counter for some unhandy async mockery. // Callback counter for some unhandy async mockery.
final AtomicInteger counter = new AtomicInteger(0); final AtomicInteger counter = new AtomicInteger(0);
@ -289,9 +288,10 @@ public class AsyncUraTripReaderTest {
); );
} }
public static class StreamTransformer extends ResponseTransformer { public static class StreamTransformer implements ResponseTransformerV2 {
@Override @Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) { public Response transform(Response response, ServeEvent serveEvent) {
Parameters parameters = serveEvent.getTransformerParameters();
int chunks = parameters.getInt("chunks", 1); int chunks = parameters.getInt("chunks", 1);
return Response.Builder.like(response) return Response.Builder.like(response)
// Read source file to response. // Read source file to response.