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=dae89298bf7af0ffbb6bb1662ff88d5565d81cbd;hpb=b7dc861910bd43020ff91c8f9f51fe4c32a66aec;p=demos%2Ftesting diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java index dae8929..4fcb8c9 100644 --- a/src/test/java/de/juplo/demo/RestControllerTest.java +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -1,13 +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.reactive.function.client.WebClientResponseException.NotFound; +import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -55,13 +59,87 @@ public class RestControllerTest StepVerifier .create(result) - .expectError(NotFound.class) + .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, "", null, null, null); + return WebClientResponseException.create(status, "MESSAGE", null, null, null); } }