4 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
5 import org.junit.jupiter.api.BeforeEach;
6 import org.junit.jupiter.api.DisplayName;
7 import org.junit.jupiter.api.Test;
8 import org.junit.jupiter.params.ParameterizedTest;
9 import org.junit.jupiter.params.provider.EnumSource;
10 import org.mockito.Mockito;
11 import static org.mockito.Mockito.when;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.web.reactive.function.client.WebClientResponseException;
14 import org.springframework.web.server.ResponseStatusException;
15 import reactor.core.publisher.Mono;
16 import reactor.test.StepVerifier;
20 * Unit-Test for class {@link RestController}.
23 public class RestControllerTest
25 RestController controller;
26 RemoteContentService service;
32 service = Mockito.mock(RemoteContentService.class);
33 controller = new RestController(service);
38 @DisplayName("Data successfully fetched from remote-server")
41 when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
43 Mono<String> result = controller.fetch("foo");
53 @DisplayName("Data not found on remote-server")
54 void testResponseNotFoud()
56 when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404)));
58 Mono<String> result = controller.fetch("foo");
62 .expectErrorSatisfies((t) ->
64 assertThat(t).isInstanceOf(ResponseStatusException.class);
65 ResponseStatusException status = (ResponseStatusException)t;
66 assertThat(status.getStatus()).isEqualTo(HttpStatus.NOT_FOUND);
67 assertThat(t.getMessage()).startsWith("404 NOT_FOUND \"Cause: 404 MESSAGE\";");
73 * Only the behavior on the common errors, as defined in {@link
74 * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest)
75 * WebClientResponseException.create()} is tested.
77 @DisplayName("Other error while fetching data from remote-server")
78 @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}")
79 @EnumSource(value = HttpStatus.class, names = {
87 "UNSUPPORTED_MEDIA_TYPE",
89 "UNPROCESSABLE_ENTITY",
90 "INTERNAL_SERVER_ERROR",
93 "SERVICE_UNAVAILABLE",
96 void testResponseOtherErrors(HttpStatus status)
98 Mono<String> mono = Mono.error(exception(status.value()));
99 when(service.getRemoteText("foo")).thenReturn(mono);
101 Mono<String> result = controller.fetch("foo");
105 .expectErrorSatisfies((t) ->
107 assertThat(t).isInstanceOf(ResponseStatusException.class);
108 ResponseStatusException e = (ResponseStatusException)t;
109 assertThat(e.getStatus()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
110 assertThat(t.getMessage())
112 "503 SERVICE_UNAVAILABLE \"Cause: " +
113 Integer.toString(status.value()) +
120 @DisplayName("Internal error while fetching data from remote-server")
121 void testOtherErrors()
123 Mono<String> mono = Mono.error(new RuntimeException("Boom!"));
124 when(service.getRemoteText("foo")).thenReturn(mono);
126 Mono<String> result = controller.fetch("foo");
130 .expectErrorSatisfies((t) ->
132 assertThat(t).isInstanceOf(ResponseStatusException.class);
133 ResponseStatusException e = (ResponseStatusException)t;
134 assertThat(e.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
135 assertThat(t.getMessage()).startsWith("500 INTERNAL_SERVER_ERROR \"Cause: Boom!\";");
141 WebClientResponseException exception(int status)
143 return WebClientResponseException.create(status, "MESSAGE", null, null, null);