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 - Expired - Fetch: Server Error"))
31  @ExtendWith({ SpringExtension.class, MockitoExtension.class })
32  @MockitoSettings(strictness = Strictness.LENIENT)
33  public class HttpResourceExpiredFetchServerErrorTest
34  {
35    private final static Logger LOG = LoggerFactory.getLogger(HttpResourceExpiredFetchServerErrorTest.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      resource.data = DATA_EXPIRED;
55  
56      // Everything is possible, nothing is necessary: Only defines behavior!
57  
58      HttpHeaders headers = new HttpHeaders();
59      headers.setContentType(MIME_TYPE_CONTENT_TYPE_HTML);
60  
61      when(fetcher.fetch(any(), any())).thenReturn(SERVER_ERROR);
62    }
63  
64  
65    /*************** Results for calls, that did trigger a fetch */
66  
67    @Test
68    @DisplayName(("fetch() reports modifications"))
69    public void test_fetch_ReportsModifications()
70    {
71      LOG.info("<-- start of test-case");
72  
73      assertThat(resource.fetch()).isTrue();
74    }
75  
76    @Test
77    @DisplayName(("exists() is false"))
78    public void test_exists_ReportsFalse()
79    {
80      LOG.info("<-- start of test-case");
81  
82      assertThat(resource.exists()).isFalse();
83    }
84  
85    @Test
86    @DisplayName(("isReadable() is false"))
87    public void test_isReadable_ReportsFalse()
88    {
89      LOG.info("<-- start of test-case");
90  
91      assertThat(resource.isReadable()).isFalse();
92    }
93  
94    @Test
95    @DisplayName(("isModified() reports modifications"))
96    public void test_isModified_ReportsModifications()
97    {
98      LOG.info("<-- start of test-case");
99  
100     assertThat(resource.isModified()).isTrue();
101   }
102 
103   @Test
104   @DisplayName(("getInputStream() throws FileNotFoundException"))
105   public void test_HasNoContent()
106   {
107     LOG.info("<-- start of test-case");
108 
109     assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resource.getInputStream());
110   }
111 
112 
113   /*************** Expected state after calls, that did trigger a fetch */
114 
115   @Test
116   @DisplayName(("fetch() updates data"))
117   public void test_fetch_UpdatesData()
118   {
119     LOG.info("<-- start of test-case");
120 
121     resource.fetch();
122 
123     assertThat(resource.data).isSameAs(SERVER_ERROR);
124   }
125 
126   @Test
127   @DisplayName(("exists() updates data"))
128   public void test_exists_UpdatesData()
129   {
130     LOG.info("<-- start of test-case");
131 
132     resource.exists();
133 
134     assertThat(resource.data).isSameAs(SERVER_ERROR);
135   }
136 
137   @Test
138   @DisplayName(("isReadable() updates data"))
139   public void test_isReadable_UpdatesData()
140   {
141     LOG.info("<-- start of test-case");
142 
143     resource.isReadable();
144 
145     assertThat(resource.data).isSameAs(SERVER_ERROR);
146   }
147 
148   @Test
149   @DisplayName(("isModified() updates data"))
150   public void test_isModified_UpdatesData()
151   {
152     LOG.info("<-- start of test-case");
153 
154     resource.isModified();
155 
156     assertThat(resource.data).isSameAs(SERVER_ERROR);
157   }
158 
159   @Test
160   @DisplayName(("getInputStream() updates data"))
161   public void test_getInputStream_UpdatesData()
162   {
163     LOG.info("<-- start of test-case");
164 
165     try
166     {
167       resource.getInputStream();
168     }
169     catch(Exception e) {}
170 
171     assertThat(resource.data).isSameAs(SERVER_ERROR);
172   }
173 }