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.beans.factory.annotation.Autowired;
14  import org.springframework.cache.Cache;
15  import org.springframework.cache.support.NoOpCache;
16  import org.springframework.http.HttpHeaders;
17  import org.springframework.test.context.junit.jupiter.SpringExtension;
18  
19  import java.net.URI;
20  import java.time.Clock;
21  import java.time.ZoneId;
22  
23  import static de.juplo.httpresources.TestUtil.*;
24  import static org.assertj.core.api.Assertions.assertThat;
25  import static org.mockito.ArgumentMatchers.any;
26  import static org.mockito.Mockito.when;
27  
28  
29  @DisplayName(("HttpResource - Expired - Fetch: Modified"))
30  @ExtendWith({ SpringExtension.class, MockitoExtension.class })
31  @MockitoSettings(strictness = Strictness.LENIENT)
32  public class HttpResourceExpiredFetchModifiedTest
33  {
34    private final static Logger LOG = LoggerFactory.getLogger(HttpResourceExpiredFetchModifiedTest.class);
35  
36    @Mock
37    HttpResourceFetcher fetcher;
38  
39    HttpResources resources;
40    URI uri;
41    HttpResource resource;
42  
43  
44    /************** SETUP */
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      resource.data = DATA_EXPIRED;
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(DATA_UPDATED);
61    }
62  
63  
64    /*************** For calls, that do not trigger a fetch see: HttpResourceNotExpiredTest */
65  
66  
67    /*************** Results for calls, that did trigger a fetch */
68  
69    @Test
70    @DisplayName(("fetch() reports modifications"))
71    public void test_fetch_ReportsModifications()
72    {
73      LOG.info("<-- start of test-case");
74  
75      assertThat(resource.fetch()).isTrue();
76    }
77  
78    @Test
79    @DisplayName(("exists() is true"))
80    public void test_exists_IsTrue()
81    {
82      LOG.info("<-- start of test-case");
83  
84      assertThat(resource.exists()).isTrue();
85    }
86  
87    @Test
88    @DisplayName(("readable() is true"))
89    public void test_isReadable_IsTrue()
90    {
91      LOG.info("<-- start of test-case");
92  
93      assertThat(resource.isReadable()).isTrue();
94    }
95  
96    @Test
97    @DisplayName(("isModified() reports modifications"))
98    public void test_isModified_ReportsModifications()
99    {
100     LOG.info("<-- start of test-case");
101 
102     assertThat(resource.isModified()).isTrue();
103   }
104 
105   @Test
106   @DisplayName(("getInputStream() returns expected content"))
107   public void test_hasExpectedContent() throws Exception
108   {
109     LOG.info("<-- start of test-case");
110 
111     assertThat(resource.getInputStream()).hasContent(STR_CONTENT_MODIFIED);
112   }
113 
114 
115   /*************** Expected state after calls, that did trigger a fetch */
116 
117   @Test
118   @DisplayName(("fetch() updates data"))
119   public void test_fetch_UpdatesData()
120   {
121     LOG.info("<-- start of test-case");
122 
123     resource.fetch();
124 
125     assertThat(resource.data).isSameAs(DATA_UPDATED);
126   }
127 
128   @Test
129   @DisplayName(("exists() updates data"))
130   public void test_exists_UpdatesData()
131   {
132     LOG.info("<-- start of test-case");
133 
134     resource.exists();
135 
136     assertThat(resource.data).isSameAs(DATA_UPDATED);
137   }
138 
139   @Test
140   @DisplayName(("isReadable() updates data"))
141   public void test_isReadable_UpdatesData()
142   {
143     LOG.info("<-- start of test-case");
144 
145     resource.isReadable();
146 
147     assertThat(resource.data).isSameAs(DATA_UPDATED);
148   }
149 
150   @Test
151   @DisplayName(("isModified() updates data"))
152   public void test_isModified_UpdatesData()
153   {
154     LOG.info("<-- start of test-case");
155 
156     resource.isModified();
157 
158     assertThat(resource.data).isSameAs(DATA_UPDATED);
159   }
160 
161   @Test
162   @DisplayName(("getInputStream() updates data"))
163   public void test_getInputStream_UpdatesData() throws Exception
164   {
165     LOG.info("<-- start of test-case");
166 
167     resource.getInputStream();
168 
169     assertThat(resource.data).isSameAs(DATA_UPDATED);
170   }
171 }