From ff2650e8c1069bbc6a4d583c2e77c5eb5dc639cc Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 29 Jun 2024 13:48:29 +0200 Subject: [PATCH] test: minor test code refactoring --- .../ura/UraClientConfigurationTest.java | 4 +- .../stklcode/pubtrans/ura/UraClientTest.java | 40 +++++++++---------- .../pubtrans/ura/model/MessageTest.java | 8 ++-- .../stklcode/pubtrans/ura/model/StopTest.java | 8 ++-- .../stklcode/pubtrans/ura/model/TripTest.java | 8 ++-- .../ura/reader/AsyncUraTripReaderTest.java | 18 ++++----- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/test/java/de/stklcode/pubtrans/ura/UraClientConfigurationTest.java b/src/test/java/de/stklcode/pubtrans/ura/UraClientConfigurationTest.java index 6a34ea8..36c0a67 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/UraClientConfigurationTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/UraClientConfigurationTest.java @@ -12,9 +12,9 @@ * * @author Stefan Kalscheuer */ -public class UraClientConfigurationTest { +class UraClientConfigurationTest { @Test - public void configBuilderTest() { + void configBuilderTest() { final String baseURL = "https://ura.example.com"; final String instantPath = "/path/to/instant"; final String streamPath = "/path/to/stream"; diff --git a/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java b/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java index 2b823fa..7f5fd3e 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java @@ -45,19 +45,19 @@ /** * Unit test for the URA Client. * Tests run against mocked data collected from ASEAG API (no longer available) and - * TFL API (http://countdown.api.tfl.gov.uk) + * TFL API (https://countdown.api.tfl.gov.uk) * * @author Stefan Kalscheuer */ -public class UraClientTest { +class UraClientTest { @RegisterExtension - public static WireMockExtension wireMock = WireMockExtension.newInstance() + static WireMockExtension wireMock = WireMockExtension.newInstance() .options(wireMockConfig().dynamicPort()) .build(); @Test - public void getStopsTest() throws UraClientException { + void getStopsTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(2, "instant_V2_stops.txt"); @@ -83,7 +83,7 @@ public void getStopsTest() throws UraClientException { } @Test - public void getStopsForLineTest() throws UraClientException { + void getStopsForLineTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(2, "instant_V2_stops_line.txt"); @@ -101,7 +101,7 @@ public void getStopsForLineTest() throws UraClientException { } @Test - public void getStopsForPositionTest() throws UraClientException { + void getStopsForPositionTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_stops_circle.txt"); @@ -127,7 +127,7 @@ public void getStopsForPositionTest() throws UraClientException { } @Test - public void getTripsForDestinationNamesTest() throws UraClientException { + void getTripsForDestinationNamesTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_destination.txt"); @@ -150,7 +150,7 @@ public void getTripsForDestinationNamesTest() throws UraClientException { } @Test - public void getTripsTowardsTest() throws UraClientException { + void getTripsTowardsTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_towards.txt"); @@ -165,7 +165,7 @@ public void getTripsTowardsTest() throws UraClientException { } @Test - public void getTripsTest() throws UraClientException { + void getTripsTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_all.txt"); @@ -229,11 +229,11 @@ public void getTripsTest() throws UraClientException { "Expected reader to raise an exception" ); 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 - public void getTripsForStopTest() throws UraClientException { + void getTripsForStopTest() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_stop.txt"); @@ -274,11 +274,11 @@ public void getTripsForStopTest() throws UraClientException { "Expected reader to raise an exception" ); 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 - public void getTripsForLine() throws UraClientException { + void getTripsForLine() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_line.txt"); @@ -327,7 +327,7 @@ public void getTripsForLine() throws UraClientException { } @Test - public void getTripsForStopAndLine() throws UraClientException { + void getTripsForStopAndLine() throws UraClientException { // Mock the HTTP call. mockHttpToFile(1, "instant_V1_trips_stop_line.txt"); @@ -348,7 +348,7 @@ public void getTripsForStopAndLine() throws UraClientException { @Test - public void getMessages() throws UraClientException { + void getMessages() throws UraClientException { UraClient uraClient = new UraClient(wireMock.baseUrl()); // Mock the HTTP call. @@ -379,11 +379,11 @@ public void getMessages() throws UraClientException { "Expected reader to raise an exception" ); 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 - public void getMessagesForStop() throws UraClientException { + void getMessagesForStop() throws UraClientException { UraClient uraClient = new UraClient(wireMock.baseUrl(), "/interfaces/ura/instant_V2", "/interfaces/ura/stream"); // Mock the HTTP call. @@ -406,7 +406,7 @@ public void getMessagesForStop() throws UraClientException { } @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. UraClientException exception = assertThrows( UraClientException.class, @@ -417,7 +417,7 @@ public void timeoutTest() { ).forDestinationNames("Piccadilly Circus").getTrips(), "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. wireMock.stubFor( @@ -444,7 +444,7 @@ public void timeoutTest() { ).forDestinationNames("Piccadilly Circus").getTrips(), "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( () -> new UraClient( diff --git a/src/test/java/de/stklcode/pubtrans/ura/model/MessageTest.java b/src/test/java/de/stklcode/pubtrans/ura/model/MessageTest.java index bdd6ff2..2e2e821 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/model/MessageTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/model/MessageTest.java @@ -30,13 +30,13 @@ 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 */ -public class MessageTest { +class MessageTest { @Test - public void basicConstructorTest() { + void basicConstructorTest() { Message message = new Message("sid", "name", "indicator", @@ -60,7 +60,7 @@ public void basicConstructorTest() { } @Test - public void listConstructorTest() { + void listConstructorTest() { /* Create valid raw data list */ List raw = new ArrayList<>(); raw.add(1); diff --git a/src/test/java/de/stklcode/pubtrans/ura/model/StopTest.java b/src/test/java/de/stklcode/pubtrans/ura/model/StopTest.java index 19d4aed..5562aa0 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/model/StopTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/model/StopTest.java @@ -30,13 +30,13 @@ 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 */ -public class StopTest { +class StopTest { @Test - public void basicConstructorTest() { + void basicConstructorTest() { Stop stop = new Stop("id", "name", "indicator", 1, 2.345, 6.789); assertThat(stop.getId(), is("id")); assertThat(stop.getName(), is("name")); @@ -47,7 +47,7 @@ public void basicConstructorTest() { } @Test - public void listConstructorTest() { + void listConstructorTest() { /* Create valid raw data list */ List raw = new ArrayList<>(); raw.add(1); diff --git a/src/test/java/de/stklcode/pubtrans/ura/model/TripTest.java b/src/test/java/de/stklcode/pubtrans/ura/model/TripTest.java index b042b7a..0fa1613 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/model/TripTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/model/TripTest.java @@ -30,13 +30,13 @@ 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 */ -public class TripTest { +class TripTest { @Test - public void basicConstructorTest() { + void basicConstructorTest() { Trip trip = new Trip("sid", "name", "indicator", @@ -70,7 +70,7 @@ public void basicConstructorTest() { } @Test - public void listConstructorTest() { + void listConstructorTest() { /* Create valid raw data list */ List raw = new ArrayList<>(); raw.add(1); diff --git a/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java b/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java index 53fbf13..c78e61a 100644 --- a/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java +++ b/src/test/java/de/stklcode/pubtrans/ura/reader/AsyncUraTripReaderTest.java @@ -18,13 +18,12 @@ import com.github.tomakehurst.wiremock.WireMockServer; 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.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.Request; 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.model.Trip; import org.junit.jupiter.api.AfterAll; @@ -54,7 +53,7 @@ * * @author Stefan Kalscheuer */ -public class AsyncUraTripReaderTest { +class AsyncUraTripReaderTest { private static WireMockServer httpMock; @BeforeAll @@ -82,7 +81,7 @@ public static void tearDown() { * @throws InterruptedException Thread interrupted. */ @Test - public void readerTest() throws InterruptedException { + void readerTest() throws InterruptedException { // Callback counter for some unhandy async mockery. final AtomicInteger counter = new AtomicInteger(0); @@ -161,7 +160,7 @@ public void readerTest() throws InterruptedException { * @throws InterruptedException Thread interrupted. */ @Test - public void streamClosedTest() throws InterruptedException { + void streamClosedTest() throws InterruptedException { // Callback counter for some unhandy async mockery. final AtomicInteger counter = new AtomicInteger(0); @@ -202,7 +201,7 @@ public void streamClosedTest() throws InterruptedException { } @Test - public void timeoutTest() throws InterruptedException { + void timeoutTest() throws InterruptedException { // Callback counter for some unhandy async mockery. final AtomicInteger counter = new AtomicInteger(0); @@ -289,9 +288,10 @@ private void readLinesToMock(int version, String resourceFile, int chunks) { ); } - public static class StreamTransformer extends ResponseTransformer { + public static class StreamTransformer implements ResponseTransformerV2 { @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); return Response.Builder.like(response) // Read source file to response.