View Javadoc
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.HttpData.SERVER_ERROR;
23  import static de.juplo.httpresources.TestUtil.*;
24  import static org.assertj.core.api.Assertions.assertThat;
25  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26  import static org.mockito.ArgumentMatchers.any;
27  import static org.mockito.Mockito.when;
28  
29  
30  @DisplayName(("HttpResource - Not Fetched - First Fetch: Server Error"))
31  @ExtendWith({ SpringExtension.class, MockitoExtension.class })
32  @MockitoSettings(strictness = Strictness.LENIENT)
33  public class HttpResourceNotFetchedFirstFetchServerErrorTest
34  {
35    private final static Logger LOG = LoggerFactory.getLogger(HttpResourceNotFetchedFirstFetchServerErrorTest.class);
36  
37    @Mock
38    HttpResourceFetcher fetcher;
39  
40    HttpResources resources;
41    URI uri;
42    HttpResource resource;
43  
44  
45    /************** SETUP */
46  
47    @BeforeEach
48    public void setUp()
49    {
50      Clock clock = Clock.fixed(NOW.toInstant(), ZoneId.of("GMT"));
51      resources = new HttpResources(fetcher, clock);
52      uri = URI.create("http://foo/bar");
53      resource = new HttpResource(resources, fetcher, clock, uri);
54  
55      // Everything is possible, nothing is necessary: Only defines behavior!
56  
57      HttpHeaders headers = new HttpHeaders();
58      headers.setContentType(MIME_TYPE_CONTENT_TYPE_HTML);
59  
60      when(fetcher.fetch(any(), any())).thenReturn(SERVER_ERROR);
61    }
62  
63  
64    /*************** Results for calls, that trigger a fetch */
65  
66    @Test
67    @DisplayName(("fetch() reports modifications"))
68    public void test_fetch_ReportsModifications()
69    {
70      LOG.info("<-- start of test-case");
71  
72      assertThat(resource.fetch()).isTrue();
73    }
74  
75    @Test
76    @DisplayName(("exists() is false"))
77    public void test_exists_IsFalse()
78    {
79      LOG.info("<-- start of test-case");
80  
81      assertThat(resource.exists()).isFalse();
82    }
83  
84    @Test
85    @DisplayName(("isReadable() is false"))
86    public void test_isReadable_IsFalse()
87    {
88      LOG.info("<-- start of test-case");
89  
90      assertThat(resource.isReadable()).isFalse();
91    }
92  
93    @Test
94    @DisplayName(("lastModified() returns the expected time"))
95    public void test_lastModified_ReturnsExpectedTime() throws Exception
96    {
97      LOG.info("<-- start of test-case");
98  
99      assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.lastModified());
100   }
101 
102   @Test
103   @DisplayName(("isModified() if true"))
104   public void test_isModified_IsTrue()
105   {
106     LOG.info("<-- start of test-case");
107 
108     assertThat(resource.isModified()).isTrue();
109   }
110 
111   @Test
112   @DisplayName(("getInputStream() throws FileNotFoundException"))
113   public void test_getInputStream_ThrowsFileNotFoundException() throws Exception
114   {
115     LOG.info("<-- start of test-case");
116 
117     assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.getInputStream());
118   }
119 
120   @Test
121   @DisplayName(("contentLength() throws FileNotFoundException"))
122   public void test_contentLength_ThrowsFileNotFoundException() throws Exception
123   {
124     LOG.info("<-- start of test-case");
125 
126     assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.contentLength());
127   }
128 
129 
130   /*************** Expected state after calls that trigger a fetch */
131 
132   @Test
133   @DisplayName(("call to fetch() updates data"))
134   public void test_fetch_UpdatesData()
135   {
136     LOG.info("<-- start of test-case");
137 
138     resource.fetch();
139 
140     assertThat(resource.data).isSameAs(SERVER_ERROR);
141   }
142 
143   @Test
144   @DisplayName(("call to exists() updates data"))
145   public void test_exists_UpdatesData()
146   {
147     LOG.info("<-- start of test-case");
148 
149     resource.exists();
150 
151     assertThat(resource.data).isSameAs(SERVER_ERROR);
152   }
153 
154   @Test
155   @DisplayName(("call to isReadable() updates data"))
156   public void test_isReadable_UpdatesData()
157   {
158     LOG.info("<-- start of test-case");
159 
160     resource.isReadable();
161 
162     assertThat(resource.data).isSameAs(SERVER_ERROR);
163   }
164 
165   @Test
166   @DisplayName(("call to lastModified() updates data"))
167   public void test_lastModified_UpdatesData() throws Exception
168   {
169     LOG.info("<-- start of test-case");
170 
171     try
172     {
173       resource.lastModified();
174     }
175     catch (FileNotFoundException e) {}
176 
177     assertThat(resource.data).isSameAs(SERVER_ERROR);
178   }
179 
180   @Test
181   @DisplayName(("call to isModified() updates data"))
182   public void test_isModified_UpdatesData()
183   {
184     LOG.info("<-- start of test-case");
185 
186     resource.isModified();
187 
188     assertThat(resource.data).isSameAs(SERVER_ERROR);
189   }
190 
191   @Test
192   @DisplayName(("call to getInputStream() updates data"))
193   public void test_getInputStream_UpdatesData() throws Exception
194   {
195     LOG.info("<-- start of test-case");
196 
197     try
198     {
199       resource.getInputStream();
200     }
201     catch (FileNotFoundException e) {}
202 
203     assertThat(resource.data).isSameAs(SERVER_ERROR);
204   }
205 
206   @Test
207   @DisplayName(("call to contentLength() updates data"))
208   public void test_contentLength_UpdatesData() throws Exception
209   {
210     LOG.info("<-- start of test-case");
211 
212     try
213     {
214       resource.contentLength();
215     }
216     catch (FileNotFoundException e) {}
217 
218     assertThat(resource.data).isSameAs(SERVER_ERROR);
219   }
220 }