Skip to content

Commit

Permalink
test: minor test code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Jun 29, 2024
1 parent 14f4266 commit ff2650e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
40 changes: 20 additions & 20 deletions src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<a href="https://countdown.api.tfl.gov.uk">https://countdown.api.tfl.gov.uk</a>)
*
* @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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/de/stklcode/pubtrans/ura/model/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -60,7 +60,7 @@ public void basicConstructorTest() {
}

@Test
public void listConstructorTest() {
void listConstructorTest() {
/* Create valid raw data list */
List<Serializable> raw = new ArrayList<>();
raw.add(1);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/de/stklcode/pubtrans/ura/model/StopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -47,7 +47,7 @@ public void basicConstructorTest() {
}

@Test
public void listConstructorTest() {
void listConstructorTest() {
/* Create valid raw data list */
List<Serializable> raw = new ArrayList<>();
raw.add(1);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/de/stklcode/pubtrans/ura/model/TripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -70,7 +70,7 @@ public void basicConstructorTest() {
}

@Test
public void listConstructorTest() {
void listConstructorTest() {
/* Create valid raw data list */
List<Serializable> raw = new ArrayList<>();
raw.add(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +53,7 @@
*
* @author Stefan Kalscheuer
*/
public class AsyncUraTripReaderTest {
class AsyncUraTripReaderTest {
private static WireMockServer httpMock;

@BeforeAll
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ff2650e

Please sign in to comment.