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.junit.jupiter.api.Assertions.assertNull;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.*;
27
28
29 @DisplayName(("HttpResource - Not Fetched"))
30 @ExtendWith({ SpringExtension.class, MockitoExtension.class })
31 @MockitoSettings(strictness = Strictness.LENIENT)
32 public class HttpResourceNotFetchedTest
33 {
34 private final static Logger LOG = LoggerFactory.getLogger(HttpResourceNotFetchedTest.class);
35
36 @Mock
37 HttpResourceFetcher fetcher;
38
39 HttpResources resources;
40 URI uri;
41 HttpResource resource;
42
43
44
45
46 @BeforeEach
47 public void setUp()
48 {
49 Clock clock = Clock.fixed(NOW.toInstant(), ZoneId.of("GMT"));
50 resources = new HttpResources(fetcher, clock);
51 uri = URI.create("http://foo/bar");
52 resource = new HttpResource(resources, fetcher, clock, uri);
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);
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 lastModified() triggers fetch"))
103 public void test_lastModified_TriggersFetch() throws Exception
104 {
105 LOG.info("<-- start of test-case");
106
107 resource.lastModified();
108
109
110 verify(fetcher).fetch(eq(uri), any());
111 }
112
113 @Test
114 @DisplayName(("call to isModified() triggers fetch"))
115 public void test_isModified_TriggersFetch()
116 {
117 LOG.info("<-- start of test-case");
118
119 resource.isModified();
120
121
122 verify(fetcher).fetch(eq(uri), any());
123 }
124
125 @Test
126 @DisplayName(("call to getInputStream() triggers fetch"))
127 public void test_getInputStream_TriggersFetch() throws Exception
128 {
129 LOG.info("<-- start of test-case");
130
131 resource.getInputStream();
132
133
134 verify(fetcher).fetch(eq(uri), any());
135 }
136
137 @Test
138 @DisplayName(("call to contentLength() does trigger fetch"))
139 public void test_contentLength_TriggersFetch() throws Exception
140 {
141 LOG.info("<-- start of test-case");
142
143 resource.contentLength();
144
145
146 verify(fetcher).fetch(eq(uri), any());
147 }
148
149
150
151
152 @Test
153 @DisplayName(("call to fetched() does not trigger fetch"))
154 public void test_fetched_DoesNotTriggerFetch() throws Exception
155 {
156 LOG.info("<-- start of test-case");
157
158 resource.fetched();
159
160
161 verify(fetcher, never()).fetch(eq(uri), any());
162 }
163
164 @Test
165 @DisplayName(("call to expires() does not trigger fetch"))
166 public void test_expires_DoesNotTriggerFetch() throws Exception
167 {
168 LOG.info("<-- start of test-case");
169
170 resource.expires();
171
172
173 verify(fetcher, never()).fetch(eq(uri), any());
174 }
175
176 @Test
177 @DisplayName(("call to isExpired() does not trigger fetch"))
178 public void test_isExpired_DoesNotTriggerFetch() throws Exception
179 {
180 LOG.info("<-- start of test-case");
181
182 resource.isExpired();
183
184
185 verify(fetcher, never()).fetch(eq(uri), any());
186 }
187
188 @Test
189 @DisplayName(("call to eTag() does not trigger fetch"))
190 public void test_eTag_DoesNotTriggerFetch() throws Exception
191 {
192 LOG.info("<-- start of test-case");
193
194 resource.eTag();
195
196
197 verify(fetcher, never()).fetch(eq(uri), any());
198 }
199
200 @Test
201 @DisplayName(("call to contentType() does not trigger fetch"))
202 public void test_contentType_DoesNotTriggerFetch() throws Exception
203 {
204 LOG.info("<-- start of test-case");
205
206 resource.contentType();
207
208
209 verify(fetcher, never()).fetch(eq(uri), any());
210 }
211
212
213
214
215 @Test
216 @DisplayName(("is not fetched"))
217 public void test_isNotFetched() throws Exception
218 {
219 LOG.info("<-- start of test-case");
220
221 assertThat(resource.fetched()).isFalse();
222 }
223
224 @Test
225 @DisplayName(("has Long.MIN_VALUE as initial expiration value"))
226 public void test_hasInitialExpirationValue() throws Exception
227 {
228 LOG.info("<-- start of test-case");
229
230 assertThat(resource.expires()).isEqualTo(Long.MIN_VALUE);
231 }
232
233 @Test
234 @DisplayName(("is expired"))
235 public void test_isExpired() throws Exception
236 {
237 LOG.info("<-- start of test-case");
238
239 assertThat(resource.isExpired()).isTrue();
240 }
241
242 @Test
243 @DisplayName(("has no eTag"))
244 public void test_hasNoETag() throws Exception
245 {
246 LOG.info("<-- start of test-case");
247
248 assertThat(resource.eTag()).isNull();
249 }
250
251 @Test
252 @DisplayName(("has no content-type"))
253 public void test_hasNoContentType() throws Exception
254 {
255 LOG.info("<-- start of test-case");
256
257 assertThat(resource.contentType()).isNull();
258 }
259
260
261
262
263
264
265
266 }