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.EnumSource;
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.http.HttpStatus;
17 import org.springframework.test.context.junit.jupiter.SpringExtension;
18 import org.springframework.ui.Model;
19 import org.springframework.web.reactive.function.client.WebClientResponseException;
20 import reactor.core.publisher.Mono;
21 import reactor.test.StepVerifier;
25 * Unit-Test for class {@link RemoteContentHtmlController}.
28 @ExtendWith(SpringExtension.class)
29 public class HtmlControllerTest
31 HtmlController controller;
34 RemoteContentService service;
42 controller = new HtmlController(service);
47 @DisplayName("Data successfully fetched from remote-server")
50 when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
52 String result = controller.fetch(model, "foo");
54 assertThat(result).isEqualTo("home");
55 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
56 verify(model).addAttribute("path", "foo");
57 verify(model).addAttribute(eq("text"), captor.capture());
59 .create(captor.getValue())
66 @DisplayName("Data not found on remote-server")
69 Mono<String> mono = Mono.error(WebClientResponseException.create(404, "", null, null, null));
70 when(service.getRemoteText("foo")).thenReturn(mono);
72 String result = controller.fetch(model, "foo");
74 assertThat(result).isEqualTo("home");
75 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
76 verify(model).addAttribute("path", "foo");
77 verify(model).addAttribute(eq("text"), captor.capture());
79 .create(captor.getValue())
86 * Only the behavior on the common errors, as defined in {@link
87 * WebClientResponseException#create(int, String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset, org.springframework.http.HttpRequest)
88 * WebClientResponseException.create()} is tested.
90 @DisplayName("Other error while fetching data from remote-server")
91 @ParameterizedTest(name = "{arguments} ==> HTTP-status={0}")
92 @EnumSource(value = HttpStatus.class, names = {
100 "UNSUPPORTED_MEDIA_TYPE",
102 "UNPROCESSABLE_ENTITY",
103 "INTERNAL_SERVER_ERROR",
106 "SERVICE_UNAVAILABLE",
109 void testOtherError(HttpStatus status)
111 Mono<String> mono = Mono.error(WebClientResponseException.create(status.value(), "", null, null, null));
112 when(service.getRemoteText("foo")).thenReturn(mono);
114 String result = controller.fetch(model, "foo");
116 assertThat(result).isEqualTo("home");
117 ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
118 verify(model).addAttribute(eq("text"), captor.capture());
120 .create(captor.getValue())
121 .expectNextMatches(message -> message.startsWith(Integer.toString(status.value())))