ac1488e546fa116e815f6eaa242222b03a282fed
[demos/testing] / src / test / java / de / juplo / demo / RemoteContentServiceTest.java
1 package de.juplo.demo;
2
3
4 import java.net.URI;
5 import java.nio.charset.Charset;
6 import java.time.ZonedDateTime;
7 import java.util.Map;
8 import java.util.function.Consumer;
9 import java.util.function.Function;
10 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
11 import org.junit.jupiter.api.BeforeEach;
12 import org.junit.jupiter.api.Test;
13 import org.junit.jupiter.api.extension.ExtendWith;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.when;
16 import org.springframework.boot.test.mock.mockito.MockBean;
17 import org.springframework.http.HttpMethod;
18 import org.springframework.http.MediaType;
19 import org.springframework.test.context.junit.jupiter.SpringExtension;
20 import org.springframework.web.reactive.function.client.ClientResponse;
21 import org.springframework.web.reactive.function.client.WebClient;
22 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
23 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
24 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
25 import reactor.core.publisher.Mono;
26
27
28 /**
29  * Unit-Test for class {@link RemoteContentService}.
30  * @author Kai Moritz
31  */
32 @ExtendWith(SpringExtension.class)
33 public class RemoteContentServiceTest
34 {
35   RemoteContentService service;
36
37   @MockBean
38   WebClient webClient;
39   @MockBean
40   RequestHeadersUriSpec headersSpec;
41   @MockBean
42   RequestBodyUriSpec bodySpec;
43   @MockBean
44   ResponseSpec responseSpec;
45   @MockBean
46   ClientResponse clientResponse;
47
48
49   @BeforeEach
50   void setUp()
51   {
52     service = new RemoteContentService(webClient);
53   }
54
55
56   @Test
57   void test()
58   {
59     Mono<String> mono = Mono.just("bar");
60
61     when(webClient.options()).thenReturn(headersSpec);
62     when(webClient.head()).thenReturn(headersSpec);
63     when(webClient.get()).thenReturn(headersSpec);
64     when(webClient.method(any(HttpMethod.class))).thenReturn(bodySpec);
65     when(headersSpec.uri(any(URI.class))).thenReturn(headersSpec);
66     when(headersSpec.uri(any(String.class), any(Function.class))).thenReturn(headersSpec);
67     when(headersSpec.uri(any(String.class), any(Map.class))).thenReturn(headersSpec);
68     when(headersSpec.uri(any(String.class), (Object[])any())).thenReturn(headersSpec);
69     when(headersSpec.uri(any(Function.class))).thenReturn(headersSpec);
70     when(headersSpec.accept((MediaType[])any())).thenReturn(headersSpec);
71     when(headersSpec.acceptCharset((Charset[])any())).thenReturn(headersSpec);
72     when(headersSpec.attribute(any(String.class), any(Object.class))).thenReturn(headersSpec);
73     when(headersSpec.cookie(any(String.class), any(String.class))).thenReturn(headersSpec);
74     when(headersSpec.cookies(any(Consumer.class))).thenReturn(headersSpec);
75     when(headersSpec.exchange()).thenReturn(mono);
76     when(headersSpec.header(any(String.class), (String[])any())).thenReturn(headersSpec);
77     when(headersSpec.headers(any(Consumer.class))).thenReturn(headersSpec);
78     when(headersSpec.ifModifiedSince(any(ZonedDateTime.class))).thenReturn(headersSpec);
79     when(headersSpec.ifNoneMatch((String[])any())).thenReturn(headersSpec);
80     when(headersSpec.retrieve()).thenReturn(responseSpec);
81     when(bodySpec.uri(any(URI.class))).thenReturn(bodySpec);
82     when(bodySpec.uri(any(String.class), any(Function.class))).thenReturn(bodySpec);
83     when(bodySpec.uri(any(String.class), any(Map.class))).thenReturn(bodySpec);
84     when(bodySpec.uri(any(String.class), (Object[])any())).thenReturn(bodySpec);
85     when(bodySpec.uri(any(Function.class))).thenReturn(bodySpec);
86     when(bodySpec.accept((MediaType[])any())).thenReturn(bodySpec);
87     when(bodySpec.acceptCharset((Charset[])any())).thenReturn(bodySpec);
88     when(bodySpec.attribute(any(String.class), any(Object.class))).thenReturn(bodySpec);
89     when(bodySpec.cookie(any(String.class), any(String.class))).thenReturn(bodySpec);
90     when(bodySpec.cookies(any(Consumer.class))).thenReturn(bodySpec);
91     when(bodySpec.exchange()).thenReturn(Mono.just(clientResponse));
92     when(bodySpec.header(any(String.class), (String[])any())).thenReturn(bodySpec);
93     when(bodySpec.headers(any(Consumer.class))).thenReturn(bodySpec);
94     when(bodySpec.ifModifiedSince(any(ZonedDateTime.class))).thenReturn(bodySpec);
95     when(bodySpec.ifNoneMatch((String[])any())).thenReturn(bodySpec);
96     when(bodySpec.contentLength(any(Long.class))).thenReturn(bodySpec);
97     when(bodySpec.contentType(any(MediaType.class))).thenReturn(bodySpec);
98     when(bodySpec.retrieve()).thenReturn(responseSpec);
99     when(responseSpec.bodyToMono(String.class)).thenReturn(mono);
100     when(clientResponse.bodyToMono(String.class)).thenReturn(mono);
101
102     Mono<String> result = service.getRemoteText("/foo");
103
104     assertThat(result).isSameAs(mono);
105   }
106 }