From 6d9d77ae7d95b4b503fec1475632d5c7b1d11690 Mon Sep 17 00:00:00 2001 From: Markus Malkusch Date: Mon, 6 Nov 2023 11:17:39 +0100 Subject: [PATCH] Add Tests --- .../java/de/malkusch/km200/KM200Test.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/malkusch/km200/KM200Test.java b/src/test/java/de/malkusch/km200/KM200Test.java index 40dd249..31aadba 100644 --- a/src/test/java/de/malkusch/km200/KM200Test.java +++ b/src/test/java/de/malkusch/km200/KM200Test.java @@ -1,5 +1,6 @@ package de.malkusch.km200; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.get; @@ -17,7 +18,6 @@ import static de.malkusch.km200.KM200.USER_AGENT; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.apache.commons.io.IOUtils.resourceToString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -32,8 +32,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.ValueSource; +import com.github.tomakehurst.wiremock.http.Fault; import com.github.tomakehurst.wiremock.junit5.WireMockTest; @WireMockTest(httpPort = KM200Test.PORT) @@ -125,6 +127,15 @@ public void shouldFailOnUnknownError() throws Exception { assertThrows(KM200Exception.class, () -> km200.queryString("/unknown-error")); } + @ParameterizedTest + @EnumSource(Fault.class) + public void shouldFailOnBadResponse(Fault fault) throws Exception { + stubFor(get("/bad-response").willReturn(aResponse().withFault(fault))); + var km200 = new KM200(URI, TIMEOUT, GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT); + + assertThrows(IOException.class, () -> km200.queryString("/bad-response")); + } + @Test public void shouldFailOnLocked() throws Exception { stubFor(post("/locked").willReturn(status(423))); @@ -141,6 +152,14 @@ public void shouldTimeout() throws Exception { assertThrows(HttpTimeoutException.class, () -> km200.query("/timeout")); } + @Test + public void shouldTimeoutResponseBody() throws Exception { + stubFor(get("/timeout-body").willReturn(ok(loadBody("gateway.DateTime")).withChunkedDribbleDelay(5, 20000))); + var km200 = new KM200(URI, Duration.ofMillis(50), GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT); + + assertThrows(HttpTimeoutException.class, () -> km200.query("/timeout-body")); + } + @Test public void shouldRetryOnTimeout() throws Exception { stubFor(get("/retry").inScenario("retry").whenScenarioStateIs(STARTED)