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.reactive.function.client.WebClientResponseException.InternalServerError;
15 import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound;
16 import org.springframework.web.reactive.function.client.WebClientResponseException.ServiceUnavailable;
17 import reactor.core.publisher.Mono;
18 import reactor.test.StepVerifier;
22 * Unit-Test for class {@link RestController}.
25 public class RestControllerTest
27 RestController controller;
28 RemoteContentService service;
34 service = Mockito.mock(RemoteContentService.class);
35 controller = new RestController(service);
40 @DisplayName("Data successfully fetched from remote-server")
43 when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
45 Mono<String> result = controller.fetch("foo");
55 @DisplayName("Data not found on remote-server")
56 void testResponseNotFoud()
58 when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404)));
60 Mono<String> result = controller.fetch("foo");
64 .expectError(NotFound.class)
69 * Only the behavior on the common errors, as defined in {@link
70 * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest)
71 * WebClientResponseException.create()} is tested.
73 @DisplayName("Other error while fetching data from remote-server")
74 @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}")
75 @EnumSource(value = HttpStatus.class, names = {
83 "UNSUPPORTED_MEDIA_TYPE",
85 "UNPROCESSABLE_ENTITY",
86 "INTERNAL_SERVER_ERROR",
89 "SERVICE_UNAVAILABLE",
92 void testResponseOtherErrors(HttpStatus status)
94 Mono<String> mono = Mono.error(exception(status.value()));
95 when(service.getRemoteText("foo")).thenReturn(mono);
97 Mono<String> result = controller.fetch("foo");
101 .expectErrorSatisfies((t) ->
103 assertThat(t).isInstanceOf(ServiceUnavailable.class);
104 assertThat(t.getMessage())
106 "503 Service Unavailable - Cause: " +
107 Integer.toString(status.value()));
113 @DisplayName("Internal error while fetching data from remote-server")
114 void testOtherErrors()
116 Mono<String> mono = Mono.error(new RuntimeException("Boom!"));
117 when(service.getRemoteText("foo")).thenReturn(mono);
119 Mono<String> result = controller.fetch("foo");
123 .expectErrorSatisfies((t) ->
125 assertThat(t).isInstanceOf(InternalServerError.class);
126 assertThat(t.getMessage()).isEqualTo("500 Internal Server Error - Cause: Boom!");
132 WebClientResponseException exception(int status)
134 return WebClientResponseException.create(status, "", null, null, null);