X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FRestControllerTest.java;h=4fcb8c91db3f63174c162692ab25010acefe56ec;hb=refs%2Fheads%2Fmaster;hp=8b248ce2e5c7779bdd225be70ebcef6ac0042c54;hpb=1d6b6b053ce2ef80739039c8abfa21bd7212a55e;p=demos%2Ftesting diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java index 8b248ce..4fcb8c9 100644 --- a/src/test/java/de/juplo/demo/RestControllerTest.java +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -1,11 +1,17 @@ package de.juplo.demo; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.mockito.Mockito; import static org.mockito.Mockito.when; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClientResponseException; +import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -42,4 +48,98 @@ public class RestControllerTest .expectComplete() .verify(); } + + @Test + @DisplayName("Data not found on remote-server") + void testResponseNotFoud() + { + when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404))); + + Mono result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectErrorSatisfies((t) -> + { + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException status = (ResponseStatusException)t; + assertThat(status.getStatus()).isEqualTo(HttpStatus.NOT_FOUND); + assertThat(t.getMessage()).startsWith("404 NOT_FOUND \"Cause: 404 MESSAGE\";"); + }) + .verify(); + } + + /** + * Only the behavior on the common errors, as defined in {@link + * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest) + * WebClientResponseException.create()} is tested. + */ + @DisplayName("Other error while fetching data from remote-server") + @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}") + @EnumSource(value = HttpStatus.class, names = { + "BAD_REQUEST", + "UNAUTHORIZED", + "FORBIDDEN", + "METHOD_NOT_ALLOWED", + "NOT_ACCEPTABLE", + "CONFLICT", + "GONE", + "UNSUPPORTED_MEDIA_TYPE", + "TOO_MANY_REQUESTS", + "UNPROCESSABLE_ENTITY", + "INTERNAL_SERVER_ERROR", + "NOT_IMPLEMENTED", + "BAD_GATEWAY", + "SERVICE_UNAVAILABLE", + "GATEWAY_TIMEOUT" + }) + void testResponseOtherErrors(HttpStatus status) + { + Mono mono = Mono.error(exception(status.value())); + when(service.getRemoteText("foo")).thenReturn(mono); + + Mono result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectErrorSatisfies((t) -> + { + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException e = (ResponseStatusException)t; + assertThat(e.getStatus()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); + assertThat(t.getMessage()) + .startsWith( + "503 SERVICE_UNAVAILABLE \"Cause: " + + Integer.toString(status.value()) + + " MESSAGE\";"); + }) + .verify(); + } + + @Test + @DisplayName("Internal error while fetching data from remote-server") + void testOtherErrors() + { + Mono mono = Mono.error(new RuntimeException("Boom!")); + when(service.getRemoteText("foo")).thenReturn(mono); + + Mono result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectErrorSatisfies((t) -> + { + assertThat(t).isInstanceOf(ResponseStatusException.class); + ResponseStatusException e = (ResponseStatusException)t; + assertThat(e.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); + assertThat(t.getMessage()).startsWith("500 INTERNAL_SERVER_ERROR \"Cause: Boom!\";"); + }) + .verify(); + } + + + WebClientResponseException exception(int status) + { + return WebClientResponseException.create(status, "MESSAGE", null, null, null); + } }