1 package de.juplo.httpresources;
2
3 import org.junit.jupiter.api.BeforeEach;
4 import org.junit.jupiter.api.DisplayName;
5 import org.junit.jupiter.api.Test;
6 import org.junit.jupiter.api.extension.ExtendWith;
7 import org.mockito.Mock;
8 import org.mockito.junit.jupiter.MockitoExtension;
9 import org.mockito.junit.jupiter.MockitoSettings;
10 import org.mockito.quality.Strictness;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.cache.support.NoOpCache;
14 import org.springframework.http.HttpHeaders;
15 import org.springframework.test.context.junit.jupiter.SpringExtension;
16
17 import java.net.URI;
18 import java.time.Clock;
19 import java.time.ZoneId;
20
21 import static de.juplo.httpresources.TestUtil.*;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.*;
26
27
28 @DisplayName(("HttpResource - Expired"))
29 @ExtendWith({ SpringExtension.class, MockitoExtension.class })
30 @MockitoSettings(strictness = Strictness.LENIENT)
31 public class HttpResourceExpiredTest
32 {
33 private final static Logger LOG = LoggerFactory.getLogger(HttpResourceExpiredTest.class);
34
35 @Mock
36 HttpResourceFetcher fetcher;
37
38 HttpResources resources;
39 URI uri;
40 HttpResource resource;
41
42
43
44
45 @BeforeEach
46 public void setUp()
47 {
48 Clock clock = Clock.fixed(NOW.toInstant(), ZoneId.of("GMT"));
49 resources = new HttpResources(fetcher, clock);
50 uri = URI.create("http://foo/bar");
51 resource = new HttpResource(resources, fetcher, clock, uri);
52 resource.data = DATA_EXPIRED;
53
54
55
56 HttpHeaders headers = new HttpHeaders();
57 headers.setContentType(MIME_TYPE_CONTENT_TYPE_HTML);
58
59 when(fetcher.fetch(any(), any())).thenReturn(DATA_EXPIRED_DUMMY);
60 }
61
62
63
64
65 @Test
66 @DisplayName(("call to fetch() triggers fetch"))
67 public void test_fetch_TriggersFetch()
68 {
69 LOG.info("<-- start of test-case");
70
71 resource.fetch();
72
73
74 verify(fetcher).fetch(eq(uri), any());
75 }
76
77 @Test
78 @DisplayName(("call to exists() triggers fetch"))
79 public void test_exists_TriggersFetch()
80 {
81 LOG.info("<-- start of test-case");
82
83 resource.exists();
84
85
86 verify(fetcher).fetch(eq(uri), any());
87 }
88
89 @Test
90 @DisplayName(("call to isReadable() triggers fetch"))
91 public void test_isReadable_TriggersFetch()
92 {
93 LOG.info("<-- start of test-case");
94
95 resource.isReadable();
96
97
98 verify(fetcher).fetch(eq(uri), any());
99 }
100
101 @Test
102 @DisplayName(("call to isModified() triggers fetch"))
103 public void test_isModified_TriggersFetch()
104 {
105 LOG.info("<-- start of test-case");
106
107 resource.isModified();
108
109
110 verify(fetcher).fetch(eq(uri), any());
111 }
112
113 @Test
114 @DisplayName(("call to getInputStream() triggers fetch"))
115 public void test_getInputStream_TriggersFetch() throws Exception
116 {
117 LOG.info("<-- start of test-case");
118
119 resource.getInputStream();
120
121
122 verify(fetcher).fetch(eq(uri), any());
123 }
124
125
126
127 @Test
128 @DisplayName(("call to fetched() does not trigger fetch"))
129 public void test_fetched_DoesNotTriggerFetch() throws Exception
130 {
131 LOG.info("<-- start of test-case");
132
133 resource.fetched();
134
135
136 verify(fetcher, never()).fetch(eq(uri), any());
137 }
138
139 @Test
140 @DisplayName(("call to lastModified() does not trigger fetch"))
141 public void test_lastModified_DoesNotTriggerFetch() throws Exception
142 {
143 LOG.info("<-- start of test-case");
144
145 resource.lastModified();
146
147
148 verify(fetcher, never()).fetch(eq(uri), any());
149 }
150
151 @Test
152 @DisplayName(("call to contentLength() does not trigger fetch"))
153 public void test_contentLength_DoesNotTriggerFetch() throws Exception
154 {
155 LOG.info("<-- start of test-case");
156
157 resource.contentLength();
158
159
160 verify(fetcher, never()).fetch(eq(uri), any());
161 }
162
163 @Test
164 @DisplayName(("call to expires() does not trigger fetch"))
165 public void test_expires_DoesNotTriggerFetch() throws Exception
166 {
167 LOG.info("<-- start of test-case");
168
169 resource.expires();
170
171
172 verify(fetcher, never()).fetch(eq(uri), any());
173 }
174
175 @Test
176 @DisplayName(("call to isExpired() does not trigger fetch"))
177 public void test_isExpired_DoesNotTriggerFetch() throws Exception
178 {
179 LOG.info("<-- start of test-case");
180
181 resource.isExpired();
182
183
184 verify(fetcher, never()).fetch(eq(uri), any());
185 }
186
187 @Test
188 @DisplayName(("call to eTag() does not trigger fetch"))
189 public void test_eTag_DoesNotTriggerFetch() throws Exception
190 {
191 LOG.info("<-- start of test-case");
192
193 resource.eTag();
194
195
196 verify(fetcher, never()).fetch(eq(uri), any());
197 }
198
199 @Test
200 @DisplayName(("call to contentType() does not trigger fetch"))
201 public void test_contentType_DoesNotTriggerFetch() throws Exception
202 {
203 LOG.info("<-- start of test-case");
204
205 resource.contentType();
206
207
208 verify(fetcher, never()).fetch(eq(uri), any());
209 }
210
211
212
213
214 @Test
215 @DisplayName(("is fetched"))
216 public void test_isNotFetched() throws Exception
217 {
218 LOG.info("<-- start of test-case");
219
220 assertThat(resource.fetched()).isTrue();
221 }
222
223 @Test
224 @DisplayName(("has expected value for last modification"))
225 public void test_hasExpectedLastModification() throws Exception
226 {
227 LOG.info("<-- start of test-case");
228
229 assertThat(resource.lastModified()).isEqualTo(LONG_THEN );
230 }
231
232 @Test
233 @DisplayName(("is not modified"))
234 public void test_isNotModified()
235 {
236 LOG.info("<-- start of test-case");
237
238 assertThat(resource.isModified()).isFalse();
239 }
240
241 @Test
242 @DisplayName(("has expected content-length"))
243 public void test_hasExpectedContentLength() throws Exception
244 {
245 LOG.info("<-- start of test-case");
246
247 assertThat(resource.contentLength()).isEqualTo(STR_CONTENT.getBytes().length);
248 }
249
250 @Test
251 @DisplayName(("has expected expiration value"))
252 public void test_hasInitialExpirationValue() throws Exception
253 {
254 LOG.info("<-- start of test-case");
255
256 assertThat(resource.expires()).isEqualTo(LONG_EXPIRED);
257 }
258
259 @Test
260 @DisplayName(("is expired"))
261 public void test_isNotExpired() throws Exception
262 {
263 LOG.info("<-- start of test-case");
264
265 assertThat(resource.isExpired()).isTrue();
266 }
267
268 @Test
269 @DisplayName(("has expected eTag"))
270 public void test_hasExpectedETag() throws Exception
271 {
272 LOG.info("<-- start of test-case");
273
274 assertThat(resource.eTag()).isEqualTo(STR_ETAG);
275 }
276
277 @Test
278 @DisplayName(("has expected content-type"))
279 public void test_hasExpectedContentType() throws Exception
280 {
281 LOG.info("<-- start of test-case");
282
283 assertThat(resource.contentType()).isEqualTo(MIME_TYPE_CONTENT_TYPE_HTML);
284 }
285
286
287
288
289
290
291
292
293 }