Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 6, 2023
1 parent a5958c5 commit bb33b82
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/test/java/de/malkusch/km200/KM200Test.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -125,6 +127,15 @@ public void shouldFailOnUnknownError() throws Exception {
assertThrows(KM200Exception.class, () -> km200.queryString("/unknown-error"));
}

@ParameterizedTest
@EnumSource(Fault.class)
public void shouldFailOnBadResponses(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)));
Expand All @@ -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)
Expand Down

0 comments on commit bb33b82

Please sign in to comment.