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 - Not Expired"))
29 @ExtendWith({ SpringExtension.class, MockitoExtension.class })
30 @MockitoSettings(strictness = Strictness.LENIENT)
31 public class HttpResourceNotExpiredTest
32 {
33 private final static Logger LOG = LoggerFactory.getLogger(HttpResourceNotExpiredTest.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_NOT_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_NOT_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
78
79
80 @Test
81 @DisplayName(("call to fetched() does not trigger fetch"))
82 public void test_fetched_DoesNotTriggerFetch() throws Exception
83 {
84 LOG.info("<-- start of test-case");
85
86 resource.fetched();
87
88
89 verify(fetcher, never()).fetch(eq(uri), any());
90 }
91
92 @Test
93 @DisplayName(("call to lastModified() does not trigger fetch"))
94 public void test_lastModified_DoesNotTriggerFetch() throws Exception
95 {
96 LOG.info("<-- start of test-case");
97
98 resource.lastModified();
99
100
101 verify(fetcher, never()).fetch(eq(uri), any());
102 }
103
104 @Test
105 @DisplayName(("call to isModified() does not trigger fetch"))
106 public void test_isModified_DoesNotTriggerFetch() throws Exception
107 {
108 LOG.info("<-- start of test-case");
109
110 resource.isModified();
111
112
113 verify(fetcher, never()).fetch(eq(uri), any());
114 }
115
116 @Test
117 @DisplayName(("call to getInputStream() does not trigger fetch"))
118 public void test_getInputStream_DoesNotTriggerFetch() throws Exception
119 {
120 LOG.info("<-- start of test-case");
121
122 resource.getInputStream();
123
124
125 verify(fetcher, never()).fetch(eq(uri), any());
126 }
127
128 @Test
129 @DisplayName(("call to contentLength() does not trigger fetch"))
130 public void test_contentLength_DoesNotTriggerFetch() throws Exception
131 {
132 LOG.info("<-- start of test-case");
133
134 resource.contentLength();
135
136
137 verify(fetcher, never()).fetch(eq(uri), any());
138 }
139
140 @Test
141 @DisplayName(("call to expires() does not trigger fetch"))
142 public void test_expires_DoesNotTriggerFetch() throws Exception
143 {
144 LOG.info("<-- start of test-case");
145
146 resource.expires();
147
148
149 verify(fetcher, never()).fetch(eq(uri), any());
150 }
151
152 @Test
153 @DisplayName(("call to isExpired() does not trigger fetch"))
154 public void test_isExpired_DoesNotTriggerFetch() throws Exception
155 {
156 LOG.info("<-- start of test-case");
157
158 resource.isExpired();
159
160
161 verify(fetcher, never()).fetch(eq(uri), any());
162 }
163
164 @Test
165 @DisplayName(("call to eTag() does not trigger fetch"))
166 public void test_eTag_DoesNotTriggerFetch() throws Exception
167 {
168 LOG.info("<-- start of test-case");
169
170 resource.eTag();
171
172
173 verify(fetcher, never()).fetch(eq(uri), any());
174 }
175
176 @Test
177 @DisplayName(("call to contentType() does not trigger fetch"))
178 public void test_contentType_DoesNotTriggerFetch() throws Exception
179 {
180 LOG.info("<-- start of test-case");
181
182 resource.contentType();
183
184
185 verify(fetcher, never()).fetch(eq(uri), any());
186 }
187
188
189
190
191 @Test
192 @DisplayName(("is fetched"))
193 public void test_isNotFetched() throws Exception
194 {
195 LOG.info("<-- start of test-case");
196
197 assertThat(resource.fetched()).isTrue();
198 }
199
200 @Test
201 @DisplayName(("exists"))
202 public void test_exists_IsTrue()
203 {
204 LOG.info("<-- start of test-case");
205
206 assertThat(resource.exists()).isTrue();
207 }
208
209 @Test
210 @DisplayName(("is readable"))
211 public void test_isReadable_IsTrue()
212 {
213 LOG.info("<-- start of test-case");
214
215 assertThat(resource.isReadable()).isTrue();
216 }
217
218 @Test
219 @DisplayName(("has expected value for last modification"))
220 public void test_hasExpectedLastModification() throws Exception
221 {
222 LOG.info("<-- start of test-case");
223
224 assertThat(resource.lastModified()).isEqualTo(LONG_THEN );
225 }
226
227 @Test
228 @DisplayName(("is not modified"))
229 public void test_isNotModified()
230 {
231 LOG.info("<-- start of test-case");
232
233 assertThat(resource.isModified()).isFalse();
234 }
235
236 @Test
237 @DisplayName(("has expected content"))
238 public void test_hasExpectedContent() throws Exception
239 {
240 LOG.info("<-- start of test-case");
241
242 assertThat(resource.getInputStream()).hasContent(STR_CONTENT);
243 }
244
245 @Test
246 @DisplayName(("has expected content-length"))
247 public void test_hasExpectedContentLength() throws Exception
248 {
249 LOG.info("<-- start of test-case");
250
251 assertThat(resource.contentLength()).isEqualTo(STR_CONTENT.getBytes().length);
252 }
253
254 @Test
255 @DisplayName(("has expected expiration value"))
256 public void test_hasInitialExpirationValue() throws Exception
257 {
258 LOG.info("<-- start of test-case");
259
260 assertThat(resource.expires()).isEqualTo(DATA_NOT_EXPIRED.expires);
261 }
262
263 @Test
264 @DisplayName(("is not expired"))
265 public void test_isNotExpired() throws Exception
266 {
267 LOG.info("<-- start of test-case");
268
269 assertThat(resource.isExpired()).isFalse();
270 }
271
272 @Test
273 @DisplayName(("has expected eTag"))
274 public void test_hasExpectedETag() throws Exception
275 {
276 LOG.info("<-- start of test-case");
277
278 assertThat(resource.eTag()).isEqualTo(STR_ETAG);
279 }
280
281 @Test
282 @DisplayName(("has expected content-type"))
283 public void test_hasExpectedContentType() throws Exception
284 {
285 LOG.info("<-- start of test-case");
286
287 assertThat(resource.contentType()).isEqualTo(MIME_TYPE_CONTENT_TYPE_HTML);
288 }
289
290
291
292
293
294
295
296
297 }