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.io.FileNotFoundException;
18 import java.net.URI;
19 import java.time.Clock;
20 import java.time.ZoneId;
21
22 import static de.juplo.httpresources.TestUtil.*;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27
28
29 @DisplayName(("HttpResource - Not Fetched - First Fetch: Not Found"))
30 @ExtendWith({ SpringExtension.class, MockitoExtension.class })
31 @MockitoSettings(strictness = Strictness.LENIENT)
32 public class HttpResourceNotFetchedFirstFetchNotFoundTest
33 {
34 private final static Logger LOG = LoggerFactory.getLogger(HttpResourceNotFetchedFirstFetchNotFoundTest.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_FOUND);
60 }
61
62
63
64
65 @Test
66 @DisplayName(("fetch() reports modifications"))
67 public void test_fetch_ReportsModifications()
68 {
69 LOG.info("<-- start of test-case");
70
71 assertThat(resource.fetch()).isTrue();
72 }
73
74 @Test
75 @DisplayName(("exists() is false"))
76 public void test_exists_IsFalse()
77 {
78 LOG.info("<-- start of test-case");
79
80 assertThat(resource.exists()).isFalse();
81 }
82
83 @Test
84 @DisplayName(("isReadable() is false"))
85 public void test_isReadable_IsFalse()
86 {
87 LOG.info("<-- start of test-case");
88
89 assertThat(resource.isReadable()).isFalse();
90 }
91
92 @Test
93 @DisplayName(("lastModified() returns the expected time"))
94 public void test_lastModified_ReturnsExpectedTime() throws Exception
95 {
96 LOG.info("<-- start of test-case");
97
98 assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.lastModified());
99 }
100
101 @Test
102 @DisplayName(("isModified() if true"))
103 public void test_isModified_IsTrue()
104 {
105 LOG.info("<-- start of test-case");
106
107 assertThat(resource.isModified()).isTrue();
108 }
109
110 @Test
111 @DisplayName(("getInputStream() throws FileNotFoundException"))
112 public void test_getInputStream_ThrowsFileNotFoundException() throws Exception
113 {
114 LOG.info("<-- start of test-case");
115
116 assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.getInputStream());
117 }
118
119 @Test
120 @DisplayName(("contentLength() throws FileNotFoundException"))
121 public void test_contentLength_ThrowsFileNotFoundException() throws Exception
122 {
123 LOG.info("<-- start of test-case");
124
125 assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.contentLength());
126 }
127
128
129
130
131 @Test
132 @DisplayName(("call to fetch() updates data"))
133 public void test_fetch_UpdatesData()
134 {
135 LOG.info("<-- start of test-case");
136
137 resource.fetch();
138
139 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
140 }
141
142 @Test
143 @DisplayName(("call to exists() updates data"))
144 public void test_exists_UpdatesData()
145 {
146 LOG.info("<-- start of test-case");
147
148 resource.exists();
149
150 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
151 }
152
153 @Test
154 @DisplayName(("call to isReadable() updates data"))
155 public void test_isReadable_UpdatesData()
156 {
157 LOG.info("<-- start of test-case");
158
159 resource.isReadable();
160
161 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
162 }
163
164 @Test
165 @DisplayName(("call to lastModified() updates data"))
166 public void test_lastModified_UpdatesData() throws Exception
167 {
168 LOG.info("<-- start of test-case");
169
170 try
171 {
172 resource.lastModified();
173 }
174 catch (FileNotFoundException e) {}
175
176 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
177 }
178
179 @Test
180 @DisplayName(("call to isModified() updates data"))
181 public void test_isModified_UpdatesData()
182 {
183 LOG.info("<-- start of test-case");
184
185 resource.isModified();
186
187 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
188 }
189
190 @Test
191 @DisplayName(("call to getInputStream() updates data"))
192 public void test_getInputStream_UpdatesData() throws Exception
193 {
194 LOG.info("<-- start of test-case");
195
196 try
197 {
198 resource.getInputStream();
199 }
200 catch (FileNotFoundException e) {}
201
202 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
203 }
204
205 @Test
206 @DisplayName(("call to contentLength() updates data"))
207 public void test_contentLength_UpdatesData() throws Exception
208 {
209 LOG.info("<-- start of test-case");
210
211 try
212 {
213 resource.contentLength();
214 }
215 catch (FileNotFoundException e) {}
216
217 assertThat(resource.data).isSameAs(DATA_NOT_FOUND);
218 }
219 }