@@ -47,20 +47,16 @@ public class TestAsyncHttpCacheServiceImpl {
47
47
private static final String SERVICE_NAME = "Service Name" ;
48
48
49
49
private static long CACHING_TTL = 60 ;
50
- @ Rule
51
- public WireMockRule wireMockRule = new WireMockRule (8089 );
50
+ @ Rule public WireMockRule wireMockRule = new WireMockRule (8089 );
52
51
53
- @ Spy
54
- private AsyncHttpClient asyncHttpClient = new AsyncHttpClient ();
52
+ @ Spy private AsyncHttpClient asyncHttpClient = new AsyncHttpClient ();
55
53
56
54
@ InjectMocks
57
55
private AsyncHttpCacheService asyncHttpCacheService =
58
56
new AsyncHttpCacheServiceImpl (SERVICE_NAME , asyncHttpClient , CACHING_TTL );
59
57
60
- @ Mock
61
- private CachedResponseService cachedResponseService ;
62
- @ Mock
63
- private Request request ;
58
+ @ Mock private CachedResponseService cachedResponseService ;
59
+ @ Mock private Request request ;
64
60
65
61
@ Test
66
62
public void executeRequest_whenWasCached_getResponseFromCacheAndCallOnComplete ()
@@ -154,6 +150,44 @@ public void executeRequest_withTtl_whenWasNotCached_executeHttpRequestAndCacheNe
154
150
assertThat (savedCachedResponses .get (0 ).getId ()).isNotEmpty ();
155
151
}
156
152
153
+ @ Test
154
+ public void
155
+ executeRequest_withTtlAndCacheKey_whenWasNotCached_executeHttpRequestAndCacheNewResponse ()
156
+ throws Exception {
157
+ final Request request =
158
+ new RequestBuilder ().setMethod ("GET" ).setUrl ("http://localhost:8089/resources/" ).build ();
159
+ final List <CachedResponse > savedCachedResponses = Lists .newArrayList ();
160
+ final long customTtl = 10 ;
161
+
162
+ stubFor (
163
+ get (urlEqualTo ("/resources/" ))
164
+ .willReturn (
165
+ aResponse ()
166
+ .withStatus (200 )
167
+ .withHeader ("Content-Type" , "text/json" )
168
+ .withBody ("This is body" )));
169
+
170
+ when (cachedResponseService .findById (anyString ())).thenReturn (Optional .empty ());
171
+ when (cachedResponseService .save (any (), eq (customTtl )))
172
+ .thenAnswer (
173
+ invocation -> {
174
+ CachedResponse cr = invocation .getArgumentAt (0 , CachedResponse .class );
175
+ savedCachedResponses .add (cr );
176
+ return Optional .of (cr );
177
+ });
178
+
179
+ Optional <ListenableFuture <Response >> responseListenableFuture =
180
+ asyncHttpCacheService .executeRequest (
181
+ request , new AsyncCompletionHandlerBase (), customTtl , "test" );
182
+
183
+ responseListenableFuture .get ().get ();
184
+
185
+ verify (asyncHttpClient ).executeRequest (any (), any ());
186
+ assertThat (savedCachedResponses .size ()).isEqualTo (1 );
187
+ assertThat (savedCachedResponses .get (0 ).getId ()).isEqualTo ("test" );
188
+ assertThat (savedCachedResponses .get (0 ).getId ()).isNotEmpty ();
189
+ }
190
+
157
191
@ Test
158
192
public void buildResponseId_returnsDifferentIdsForDifferentRequest () throws Exception {
159
193
@@ -198,32 +232,30 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce
198
232
"POST" , "http://localhost:8089/resources/" , "param" , "test2" );
199
233
200
234
Request requestWithBody =
201
- RequestFixture .createWithBody (
202
- "POST" , "http://localhost:8089/resources/" , "test1" );
235
+ RequestFixture .createWithBody ("POST" , "http://localhost:8089/resources/" , "test1" );
203
236
204
237
Request requestWithDifferentBody =
205
- RequestFixture .createWithBody (
206
- "POST" , "http://localhost:8089/resources/" , "test2" );
238
+ RequestFixture .createWithBody ("POST" , "http://localhost:8089/resources/" , "test2" );
207
239
208
240
Method method =
209
241
AsyncHttpCacheServiceImpl .class .getDeclaredMethod ("buildResponseId" , Request .class );
210
242
method .setAccessible (true );
211
243
212
244
final List <String > responseIds =
213
245
Stream .of (
214
- request ,
215
- requestWithDifferentUrl ,
216
- requestWithDifferentMethod ,
217
- requestWithParams ,
218
- requestWithDifferentParams ,
219
- requestWithCookie ,
220
- requestWithDifferentCookie ,
221
- requestWithHeader ,
222
- requestWithDifferentHeader ,
223
- requestWithQueryParams ,
224
- requestWithDifferentQueryParams ,
225
- requestWithBody ,
226
- requestWithDifferentBody )
246
+ request ,
247
+ requestWithDifferentUrl ,
248
+ requestWithDifferentMethod ,
249
+ requestWithParams ,
250
+ requestWithDifferentParams ,
251
+ requestWithCookie ,
252
+ requestWithDifferentCookie ,
253
+ requestWithHeader ,
254
+ requestWithDifferentHeader ,
255
+ requestWithQueryParams ,
256
+ requestWithDifferentQueryParams ,
257
+ requestWithBody ,
258
+ requestWithDifferentBody )
227
259
.map (
228
260
rq -> {
229
261
try {
0 commit comments