|
3 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.github.tomakehurst.wiremock.client.WireMock; |
6 | | -import com.github.tomakehurst.wiremock.stubbing.ServeEvent; |
7 | 6 | import org.junit.After; |
8 | 7 | import org.junit.Before; |
9 | 8 | import org.junit.Test; |
|
37 | 36 | import java.time.LocalDateTime; |
38 | 37 | import java.time.ZoneOffset; |
39 | 38 | import java.time.ZonedDateTime; |
40 | | -import java.util.List; |
41 | 39 | import java.util.concurrent.TimeUnit; |
42 | 40 |
|
43 | 41 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
@@ -159,7 +157,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseSearch() throws JsonProcessi |
159 | 157 | .build(); |
160 | 158 |
|
161 | 159 | auditService.audit(auditContext); |
162 | | - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); |
| 160 | + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 1); |
163 | 161 |
|
164 | 162 | Mockito.verify(auditCaseRemoteOperation).postCaseSearch(captor.capture(), ArgumentMatchers.any()); |
165 | 163 | assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.SEARCH_CASE.getLabel()))); |
@@ -200,7 +198,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi |
200 | 198 | ArgumentCaptor<AuditEntry> captor = ArgumentCaptor.forClass(AuditEntry.class); |
201 | 199 |
|
202 | 200 | auditService.audit(auditContext); |
203 | | - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); |
| 201 | + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 1); |
204 | 202 |
|
205 | 203 | Mockito.verify(auditCaseRemoteOperation).postCaseAction(captor.capture(), ArgumentMatchers.any()); |
206 | 204 | assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.CASE_ACCESSED.getLabel()))); |
@@ -238,7 +236,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauIsDownAndRetry() |
238 | 236 | .willReturn(aResponse().withStatus(AUDIT_UNAUTHORISED_HTTP_STATUS))); |
239 | 237 |
|
240 | 238 | auditService.audit(auditContext); |
241 | | - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); |
| 239 | + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 3); |
242 | 240 |
|
243 | 241 | verifyWireMock(3, postRequestedFor(urlEqualTo(ACTION_AUDIT_ENDPOINT)) |
244 | 242 | .withRequestBody(equalToJson(EXPECTED_CASE_ACTION_LOG_JSON))); |
@@ -269,28 +267,30 @@ public void shouldNotThrowExceptionInAuditServiceIfLauSearchIsDownAndRetry() |
269 | 267 | .build(); |
270 | 268 |
|
271 | 269 | auditService.audit(auditContext); |
272 | | - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); |
| 270 | + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 3); |
273 | 271 |
|
274 | 272 | verifyWireMock(3, postRequestedFor(urlEqualTo(SEARCH_AUDIT_ENDPOINT)) |
275 | 273 | .withRequestBody(equalToJson(EXPECTED_CASE_SEARCH_LOG_JSON))); |
276 | 274 | } |
277 | 275 |
|
278 | 276 | private void waitForPossibleAuditResponse(String pathPrefix) throws InterruptedException { |
279 | | - List<ServeEvent> allServeEvents; |
280 | | - boolean found = false; |
281 | | - long finishTime = ZonedDateTime.now().toInstant().toEpochMilli() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; |
282 | | - |
283 | | - while (ZonedDateTime.now().toInstant().toEpochMilli() < finishTime && !found) { |
284 | | - allServeEvents = getAllServeEvents(); |
285 | | - for (ServeEvent serveEvent : allServeEvents) { |
286 | | - if (serveEvent.getRequest().getUrl().startsWith(pathPrefix)) { |
287 | | - found = true; |
288 | | - } |
289 | | - } |
290 | | - if (!found) { |
291 | | - TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); |
292 | | - } |
| 277 | + waitForPossibleAuditResponse(pathPrefix, 1); |
| 278 | + } |
| 279 | + |
| 280 | + private void waitForPossibleAuditResponse(String pathPrefix, int expectedCount) throws InterruptedException { |
| 281 | + long finishTime = System.currentTimeMillis() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; |
| 282 | + long currentCount = countServeEvents(pathPrefix); |
| 283 | + |
| 284 | + while (System.currentTimeMillis() < finishTime && currentCount < expectedCount) { |
| 285 | + TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); |
| 286 | + currentCount = countServeEvents(pathPrefix); |
293 | 287 | } |
294 | 288 | } |
295 | 289 |
|
| 290 | + private long countServeEvents(String pathPrefix) { |
| 291 | + return getAllServeEvents().stream() |
| 292 | + .filter(serveEvent -> serveEvent.getRequest().getUrl().startsWith(pathPrefix)) |
| 293 | + .count(); |
| 294 | + } |
| 295 | + |
296 | 296 | } |
0 commit comments