Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())));
Expand Down Expand Up @@ -204,7 +202,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi
ArgumentCaptor<AuditEntry> 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())));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -375,21 +373,23 @@ public void shouldRetryIfResponseStatus504()
}

private void waitForPossibleAuditResponse(String pathPrefix) throws InterruptedException {
List<ServeEvent> 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();
}

}