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.NotFound;
15 import org.springframework.web.reactive.function.client.WebClientResponseException.ServiceUnavailable;
16 import reactor.core.publisher.Mono;
17 import reactor.test.StepVerifier;
21 * Unit-Test for class {@link RestController}.
24 public class RestControllerTest
26 RestController controller;
27 RemoteContentService service;
33 service = Mockito.mock(RemoteContentService.class);
34 controller = new RestController(service);
39 @DisplayName("Data successfully fetched from remote-server")
42 when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
44 Mono<String> result = controller.fetch("foo");
54 @DisplayName("Data not found on remote-server")
55 void testResponseNotFoud()
57 when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404)));
59 Mono<String> result = controller.fetch("foo");
63 .expectError(NotFound.class)
68 * Only the behavior on the common errors, as defined in {@link
69 * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest)
70 * WebClientResponseException.create()} is tested.
72 @DisplayName("Other error while fetching data from remote-server")
73 @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}")
74 @EnumSource(value = HttpStatus.class, names = {
82 "UNSUPPORTED_MEDIA_TYPE",
84 "UNPROCESSABLE_ENTITY",
85 "INTERNAL_SERVER_ERROR",
88 "SERVICE_UNAVAILABLE",
91 void testResponseOtherErrors(HttpStatus status)
93 Mono<String> mono = Mono.error(exception(status.value()));
94 when(service.getRemoteText("foo")).thenReturn(mono);
96 Mono<String> result = controller.fetch("foo");
100 .expectErrorSatisfies((t) ->
102 assertThat(t).isInstanceOf(ServiceUnavailable.class);
103 assertThat(t.getMessage())
105 "503 Service Unavailable - Cause: " +
106 Integer.toString(status.value()));
112 WebClientResponseException exception(int status)
114 return WebClientResponseException.create(status, "", null, null, null);