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.Mockito.*;
25
26
27 @DisplayName(("HttpResource - Not Fetched - First Fetch: Successfull"))
28 @ExtendWith({ SpringExtension.class, MockitoExtension.class })
29 @MockitoSettings(strictness = Strictness.LENIENT)
30 public class HttpResourceNotFetchedFirstFetchSuccessfulTest
31 {
32 private final static Logger LOG = LoggerFactory.getLogger(HttpResourceNotFetchedFirstFetchSuccessfulTest.class);
33
34 @Mock
35 HttpResourceFetcher fetcher;
36
37 HttpResources resources;
38 URI uri;
39 HttpResource resource;
40
41
42
43
44 @BeforeEach
45 public void setUp()
46 {
47 Clock clock = Clock.fixed(NOW.toInstant(), ZoneId.of("GMT"));
48 resources = new HttpResources(fetcher, clock);
49 uri = URI.create("http://foo/bar");
50 resource = new HttpResource(resources, fetcher, clock, uri);
51
52
53
54 HttpHeaders headers = new HttpHeaders();
55 headers.setContentType(MIME_TYPE_CONTENT_TYPE_HTML);
56
57 when(fetcher.fetch(any(), any())).thenReturn(DATA_NOT_EXPIRED);
58 }
59
60
61
62
63 @Test
64 @DisplayName(("fetch() reports modifications"))
65 public void test_fetch_ReportsModifications()
66 {
67 LOG.info("<-- start of test-case");
68
69 assertThat(resource.fetch()).isTrue();
70 }
71
72 @Test
73 @DisplayName(("exists() is true"))
74 public void test_exists_IsTrue()
75 {
76 LOG.info("<-- start of test-case");
77
78 assertThat(resource.exists()).isTrue();
79 }
80
81 @Test
82 @DisplayName(("isReadable() is true"))
83 public void test_isReadable_IsTrue()
84 {
85 LOG.info("<-- start of test-case");
86
87 assertThat(resource.isReadable()).isTrue();
88 }
89
90 @Test
91 @DisplayName(("lastModified() returns the expected time"))
92 public void test_lastModified_ReturnsExpectedTime() throws Exception
93 {
94 LOG.info("<-- start of test-case");
95
96 assertThat(resource.lastModified()).isEqualTo(DATA_NOT_EXPIRED.lastModified);
97 }
98
99 @Test
100 @DisplayName(("isModified() is true"))
101 public void test_isModified_IsTrue()
102 {
103 LOG.info("<-- start of test-case");
104
105 assertThat(resource.isModified()).isTrue();
106 }
107
108 @Test
109 @DisplayName(("getInputStream() returns expected content"))
110 public void test_getInputStream_TriggersFetch() throws Exception
111 {
112 LOG.info("<-- start of test-case");
113
114 assertThat(resource.getInputStream()).hasContent(STR_CONTENT);
115 }
116
117 @Test
118 @DisplayName(("contentLength() returns expected content-length"))
119 public void test_contentLength_ReturnsExpectedLength() throws Exception
120 {
121 LOG.info("<-- start of test-case");
122
123 assertThat(resource.contentLength()).isSameAs((long)STR_CONTENT.getBytes().length);
124 }
125
126
127
128
129 @Test
130 @DisplayName(("call to fetch() updates data"))
131 public void test_fetch_UpdatesData()
132 {
133 LOG.info("<-- start of test-case");
134
135 resource.fetch();
136
137 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
138 }
139
140 @Test
141 @DisplayName(("call to exists() updates data"))
142 public void test_exists_UpdatesData()
143 {
144 LOG.info("<-- start of test-case");
145
146 resource.exists();
147
148 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
149 }
150
151 @Test
152 @DisplayName(("call to isReadable() updates data"))
153 public void test_isReadable_UpdatesData()
154 {
155 LOG.info("<-- start of test-case");
156
157 resource.isReadable();
158
159 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
160 }
161
162 @Test
163 @DisplayName(("call to lastModified() updates data"))
164 public void test_lastModified_UpdatesData() throws Exception
165 {
166 LOG.info("<-- start of test-case");
167
168 resource.lastModified();
169
170 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
171 }
172
173 @Test
174 @DisplayName(("call to isModified() updates data"))
175 public void test_isModified_UpdatesData()
176 {
177 LOG.info("<-- start of test-case");
178
179 resource.isModified();
180
181 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
182 }
183
184 @Test
185 @DisplayName(("call to getInputStream() updates data"))
186 public void test_getInputStream_UpdatesData() throws Exception
187 {
188 LOG.info("<-- start of test-case");
189
190 resource.getInputStream();
191
192 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
193 }
194
195 @Test
196 @DisplayName(("call to contentLength() updates data"))
197 public void test_contentLength_UpdatesData() throws Exception
198 {
199 LOG.info("<-- start of test-case");
200
201 resource.contentLength();
202
203 assertThat(resource.data).isSameAs(DATA_NOT_EXPIRED);
204 }
205 }