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.api.extension.ExtendWith;
9 import org.junit.jupiter.params.ParameterizedTest;
10 import org.junit.jupiter.params.provider.ValueSource;
11 import org.mockito.ArgumentCaptor;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15 import org.springframework.boot.test.mock.mockito.MockBean;
16 import org.springframework.test.context.junit.jupiter.SpringExtension;
17 import org.springframework.ui.Model;
18 import org.springframework.web.reactive.function.client.WebClientResponseException;
19 import reactor.core.publisher.Mono;
20 import reactor.test.StepVerifier;
24 * Unit-Test for class {@link RemoteContentHtmlController}.
27 @ExtendWith(SpringExtension.class)
28 public class HtmlControllerTest
30 HtmlController controller;
33 RemoteContentService service;
41 controller = new HtmlController(service);
46 @DisplayName("Data successfully fetched from remote-server")
49 when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
51 String result = controller.fetch(model, "foo");
53 assertThat(result).isEqualTo("home");
54 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
55 verify(model).addAttribute(eq("text"), captor.capture());
57 .create(captor.getValue())
64 @DisplayName("Data not found on remote-server")
67 Mono<String> mono = Mono.error(WebClientResponseException.create(404, "", null, null, null));
68 when(service.getRemoteText("foo")).thenReturn(mono);
70 String result = controller.fetch(model, "foo");
72 assertThat(result).isEqualTo("home");
73 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
74 verify(model).addAttribute(eq("text"), captor.capture());
76 .create(captor.getValue())
83 * Only the behavior on the common errors, as defined in {@link
84 * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest)
85 * WebClientResponseException.create()} is tested.
87 @DisplayName("Other error while fetching data from remote-server")
88 @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}")
89 @ValueSource(ints = { 400, 401, 403, 405, 406, 409, 410, 415, 422, 429, 500, 501, 502, 503, 504 })
90 void testOtherError(int status)
92 Mono<String> mono = Mono.error(WebClientResponseException.create(status, "", null, null, null));
93 when(service.getRemoteText("foo")).thenReturn(mono);
95 String result = controller.fetch(model, "foo");
97 assertThat(result).isEqualTo("home");
98 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
99 verify(model).addAttribute(eq("text"), captor.capture());
101 .create(captor.getValue())
102 .expectNextMatches(message -> message.startsWith(Integer.toString(status)))