diff --git a/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java b/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java index 8da7837a95..9f97234649 100644 --- a/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java +++ b/src/test/java/uk/gov/hmcts/ccd/domain/service/lau/AuditCaseRemoteOperationIT.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.stubbing.ServeEvent; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -37,7 +36,6 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.util.List; import java.util.concurrent.TimeUnit; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; @@ -163,7 +161,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseSearch() throws JsonProcessi .build(); auditService.audit(auditContext); - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 1); Mockito.verify(auditCaseRemoteOperation).postCaseSearch(captor.capture(), ArgumentMatchers.any()); assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.SEARCH_CASE.getLabel()))); @@ -204,7 +202,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi ArgumentCaptor captor = ArgumentCaptor.forClass(AuditEntry.class); auditService.audit(auditContext); - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 1); Mockito.verify(auditCaseRemoteOperation).postCaseAction(captor.capture(), ArgumentMatchers.any()); assertThat(captor.getValue().getOperationType(), is(equalTo(AuditOperationType.CASE_ACCESSED.getLabel()))); @@ -242,7 +240,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauIsDownAndRetry() .willReturn(aResponse().withStatus(AUDIT_UNAUTHORISED_HTTP_STATUS))); auditService.audit(auditContext); - waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(ACTION_AUDIT_ENDPOINT, 3); verifyWireMock(3, postRequestedFor(urlEqualTo(ACTION_AUDIT_ENDPOINT)) .withRequestBody(equalToJson(EXPECTED_CASE_ACTION_LOG_JSON))); @@ -272,7 +270,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauSearchIsDownAndRetry() .build(); auditService.audit(auditContext); - waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT); + waitForPossibleAuditResponse(SEARCH_AUDIT_ENDPOINT, 3); verifyWireMock(3, postRequestedFor(urlEqualTo(SEARCH_AUDIT_ENDPOINT)) .withRequestBody(equalToJson(EXPECTED_CASE_SEARCH_LOG_JSON))); @@ -375,21 +373,23 @@ public void shouldRetryIfResponseStatus504() } private void waitForPossibleAuditResponse(String pathPrefix) throws InterruptedException { - List allServeEvents; - boolean found = false; - long finishTime = ZonedDateTime.now().toInstant().toEpochMilli() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; - - while (ZonedDateTime.now().toInstant().toEpochMilli() < finishTime && !found) { - allServeEvents = getAllServeEvents(); - for (ServeEvent serveEvent : allServeEvents) { - if (serveEvent.getRequest().getUrl().startsWith(pathPrefix)) { - found = true; - } - } - if (!found) { - TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); - } + waitForPossibleAuditResponse(pathPrefix, 1); + } + + private void waitForPossibleAuditResponse(String pathPrefix, int expectedCount) throws InterruptedException { + long finishTime = System.currentTimeMillis() + ASYNC_DELAY_TIMEOUT_MILLISECONDS; + long currentCount = countServeEvents(pathPrefix); + + while (System.currentTimeMillis() < finishTime && currentCount < expectedCount) { + TimeUnit.MILLISECONDS.sleep(ASYNC_DELAY_INTERVAL_MILLISECONDS); + currentCount = countServeEvents(pathPrefix); } } + private long countServeEvents(String pathPrefix) { + return getAllServeEvents().stream() + .filter(serveEvent -> serveEvent.getRequest().getUrl().startsWith(pathPrefix)) + .count(); + } + }