@@ -159,7 +159,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseSearch() throws JsonProcessi
159159 .build ();
160160
161161 auditService .audit (auditContext );
162- waitForPossibleAuditResponse (SEARCH_AUDIT_ENDPOINT );
162+ waitForPossibleAuditResponse (SEARCH_AUDIT_ENDPOINT , 1 );
163163
164164 Mockito .verify (auditCaseRemoteOperation ).postCaseSearch (captor .capture (), ArgumentMatchers .any ());
165165 assertThat (captor .getValue ().getOperationType (), is (equalTo (AuditOperationType .SEARCH_CASE .getLabel ())));
@@ -200,7 +200,7 @@ public void shouldMakeAuditRequestWhenPerformingCaseAction() throws JsonProcessi
200200 ArgumentCaptor <AuditEntry > captor = ArgumentCaptor .forClass (AuditEntry .class );
201201
202202 auditService .audit (auditContext );
203- waitForPossibleAuditResponse (ACTION_AUDIT_ENDPOINT );
203+ waitForPossibleAuditResponse (ACTION_AUDIT_ENDPOINT , 1 );
204204
205205 Mockito .verify (auditCaseRemoteOperation ).postCaseAction (captor .capture (), ArgumentMatchers .any ());
206206 assertThat (captor .getValue ().getOperationType (), is (equalTo (AuditOperationType .CASE_ACCESSED .getLabel ())));
@@ -238,7 +238,7 @@ public void shouldNotThrowExceptionInAuditServiceIfLauIsDownAndRetry()
238238 .willReturn (aResponse ().withStatus (AUDIT_UNAUTHORISED_HTTP_STATUS )));
239239
240240 auditService .audit (auditContext );
241- waitForPossibleAuditResponse (ACTION_AUDIT_ENDPOINT );
241+ waitForPossibleAuditResponse (ACTION_AUDIT_ENDPOINT , 3 );
242242
243243 verifyWireMock (3 , postRequestedFor (urlEqualTo (ACTION_AUDIT_ENDPOINT ))
244244 .withRequestBody (equalToJson (EXPECTED_CASE_ACTION_LOG_JSON )));
@@ -269,28 +269,30 @@ public void shouldNotThrowExceptionInAuditServiceIfLauSearchIsDownAndRetry()
269269 .build ();
270270
271271 auditService .audit (auditContext );
272- waitForPossibleAuditResponse (SEARCH_AUDIT_ENDPOINT );
272+ waitForPossibleAuditResponse (SEARCH_AUDIT_ENDPOINT , 3 );
273273
274274 verifyWireMock (3 , postRequestedFor (urlEqualTo (SEARCH_AUDIT_ENDPOINT ))
275275 .withRequestBody (equalToJson (EXPECTED_CASE_SEARCH_LOG_JSON )));
276276 }
277277
278278 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- }
279+ waitForPossibleAuditResponse (pathPrefix , 1 );
280+ }
281+
282+ private void waitForPossibleAuditResponse (String pathPrefix , int expectedCount ) throws InterruptedException {
283+ long finishTime = System .currentTimeMillis () + ASYNC_DELAY_TIMEOUT_MILLISECONDS ;
284+ long currentCount = countServeEvents (pathPrefix );
285+
286+ while (System .currentTimeMillis () < finishTime && currentCount < expectedCount ) {
287+ TimeUnit .MILLISECONDS .sleep (ASYNC_DELAY_INTERVAL_MILLISECONDS );
288+ currentCount = countServeEvents (pathPrefix );
293289 }
294290 }
295291
292+ private long countServeEvents (String pathPrefix ) {
293+ return getAllServeEvents ().stream ()
294+ .filter (serveEvent -> serveEvent .getRequest ().getUrl ().startsWith (pathPrefix ))
295+ .count ();
296+ }
297+
296298}
0 commit comments