From e4369be44c0011e768c23326d4fa6a04a440e05c Mon Sep 17 00:00:00 2001 From: RebeccaBaker <38425793+RebeccaBaker@users.noreply.github.com> Date: Tue, 17 Jan 2023 09:42:59 +0000 Subject: [PATCH] HMAN-445 update logging (#80) * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update logging * HMAN-445 update test * HMAN-445 updated logging * HMAN-445 Remove plus 1 on size * HMAN-445 update log message for downstream calls * HMAN-445 suppressions.xml * HMAN-445 checkstyle * HMAN-445 checkstyle --- .../updater/exceptions/ErrorMessages.java | 3 +++ .../repository/CcdCaseEventRepository.java | 20 +++++++++++++++---- .../repository/ElasticSearchRepository.java | 1 - .../updater/service/CcdCaseEventService.java | 13 ++++++++---- .../date/updater/service/CsvService.java | 13 ++++++++++-- .../CcdCaseEventRepositoryTest.java | 12 ++++++----- .../date/updater/service/CsvServiceTest.java | 2 +- .../NextHearingDateUpdaterServiceTest.java | 5 +++-- 8 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/exceptions/ErrorMessages.java b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/exceptions/ErrorMessages.java index cf2b677b..339f9fc9 100644 --- a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/exceptions/ErrorMessages.java +++ b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/exceptions/ErrorMessages.java @@ -9,10 +9,13 @@ public final class ErrorMessages { public static final String MAX_CSV_ENTRIES_EXCEEDED_ERROR = "001 More than %d references in CSV"; public static final String INVALID_CASE_REF_ERROR = "002 Invalid Case Reference number '{}' in CSV"; + public static final String ERROR_DOWNSTREAM = + "006 ERROR occurred downstream for endpoint %s, for case id %s, (%s of %s)"; public static final String INVALID_DATA_SOURCE_CONFIGURATION = "Invalid Configuration: CSV file and Case Types are both specified"; public static final String NO_REFERENCES_TO_PROCESS = "No Case References found to be processed"; + public static final String NO_REFERENCES_TO_VALIDATE = "No Case References found to be validated"; public static final String HEARING_DATE_TIME_IN_PAST = "003 hearingDateTime set is in the past '{}'"; diff --git a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepository.java b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepository.java index 8087371f..8a77d61a 100644 --- a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepository.java +++ b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepository.java @@ -14,6 +14,8 @@ import java.time.LocalDateTime; +import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.ERROR_DOWNSTREAM; + /** * HMAN-322. */ @@ -26,6 +28,12 @@ public class CcdCaseEventRepository { "Call to following downstream CCD endpoint failed: /cases/%s/events"; public static final String START_EVENT_ERROR = "Call to following downstream CCD endpoint failed: /cases/%s/event-triggers/%s"; + + public static final String SUBMIT_EVENT = + "/cases/%s/events"; + public static final String START_EVENT = + "/cases/%s/event-triggers/%s"; + private final CoreCaseDataApi datastoreClient; private final SecurityUtils securityUtils; @@ -35,7 +43,7 @@ public CcdCaseEventRepository(CoreCaseDataApi datastoreClient, SecurityUtils sec this.securityUtils = securityUtils; } - public StartEventResponse triggerAboutToStartEvent(String caseReference) { + public StartEventResponse triggerAboutToStartEvent(String caseReference, int index, int size) { StartEventResponse startEventResponse = null; try { String nextHearingDateAdminAccessToken = securityUtils.getNextHearingDateAdminAccessToken(); @@ -43,15 +51,17 @@ public StartEventResponse triggerAboutToStartEvent(String caseReference) { startEventResponse = datastoreClient.startEvent(nextHearingDateAdminAccessToken, s2SToken, caseReference, CaseEventConfig.EVENT_ID); } catch (FeignException feignException) { - log.error(String.format(START_EVENT_ERROR, caseReference, CaseEventConfig.EVENT_ID), feignException); + log.error(String.format(ERROR_DOWNSTREAM, + String.format(START_EVENT, caseReference, CaseEventConfig.EVENT_ID), caseReference, index, size)); + log.error(String.format(START_EVENT_ERROR, caseReference, CaseEventConfig.EVENT_ID), + feignException); log.error(String.format("Error, failed to set next hearing date for %s at %s", caseReference, LocalDateTime.now())); } - return startEventResponse; } - public CaseResource createCaseEvent(StartEventResponse startEventResponse) { + public CaseResource createCaseEvent(StartEventResponse startEventResponse, int index, int size) { String caseReference = startEventResponse.getCaseDetails().getId().toString(); CaseDataContent caseDataContent = CaseDataContent.builder() @@ -69,6 +79,8 @@ public CaseResource createCaseEvent(StartEventResponse startEventResponse) { caseDataContent ); } catch (FeignException feignException) { + log.error(String.format(ERROR_DOWNSTREAM, String.format(SUBMIT_EVENT, caseReference), + caseReference, index, size)); log.error(String.format(SUBMIT_EVENT_ERROR, caseReference), feignException); log.error(String.format("Error, failed to set next hearing date for %s at %s", caseReference, LocalDateTime.now())); diff --git a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/ElasticSearchRepository.java b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/ElasticSearchRepository.java index d1397374..5a2f49f1 100644 --- a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/ElasticSearchRepository.java +++ b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/ElasticSearchRepository.java @@ -42,7 +42,6 @@ public List findCasesWithOutOfDateNextHearingDate(String caseType) caseType, elasticSearchQuery.getQuery()); List caseDetails = new ArrayList<>(); - if (searchResult.getTotal() > 0) { List searchResultCases = searchResult.getCases(); caseDetails.addAll(searchResultCases); diff --git a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CcdCaseEventService.java b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CcdCaseEventService.java index 03a81570..9090a389 100644 --- a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CcdCaseEventService.java +++ b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CcdCaseEventService.java @@ -27,11 +27,15 @@ public CcdCaseEventService(CcdCaseEventRepository ccdCaseEventRepository) { } public void createCaseEvents(List caseReferences) { - caseReferences.forEach(this::createCaseEvent); + caseReferences.forEach(caseReference -> createCaseEvent(caseReference, caseReferences)); } - private void createCaseEvent(String caseReference) { - StartEventResponse startEventResult = ccdCaseEventRepository.triggerAboutToStartEvent(caseReference); + private void createCaseEvent(String caseReference, List caseReferences) { + int index = caseReferences.indexOf(caseReference) + 1; + int size = caseReferences.size(); + log.info(String.format("Processing case %s (%s of %s)", caseReference, index, size)); + StartEventResponse startEventResult = ccdCaseEventRepository.triggerAboutToStartEvent(caseReference, + index, size); if (startEventResult != null) { @SuppressWarnings("unchecked") @@ -49,9 +53,10 @@ private void createCaseEvent(String caseReference) { .build(); if (nextHearingDetails.isValid()) { - ccdCaseEventRepository.createCaseEvent(startEventResult); + ccdCaseEventRepository.createCaseEvent(startEventResult, index, size); } } + log.info(String.format("Process complete for case %s (%s of %s)", caseReference, index, size)); } private String getStringFromJsonMap(Map jsonMap, String key) { diff --git a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvService.java b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvService.java index 38fd9f38..7a508d77 100644 --- a/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvService.java +++ b/src/main/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvService.java @@ -21,7 +21,9 @@ import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.CSV_FILE_READ_ERROR; import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.INVALID_CASE_REF_ERROR; +import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.MAX_CSV_ENTRIES_EXCEEDED_ERROR; import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.NO_CSV_FILE; +import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.NO_REFERENCES_TO_VALIDATE; @Service @Slf4j @@ -36,6 +38,8 @@ public List getCaseReferences() { try { List caseReferences = getCaseReferencesFromCsvFile(); validateCaseRefsFile(caseReferences); + log.info("The Next-Hearing-Date-Updater has processed csv and found the following " + + "number of case references {}.", caseReferences.size()); return caseReferences; } catch (TooManyCsvRecordsException | CsvFileException exception) { throw new InvalidConfigurationError(CSV_FILE_READ_ERROR, exception); @@ -60,12 +64,17 @@ private List getCaseReferencesFromCsvFile() throws CsvFileException { } private void validateCaseRefsFile(List caseReferences) throws TooManyCsvRecordsException { - validateCsvCaseSizeLessThanMaximum(caseReferences); - logInvalidCaseReferences(caseReferences); + if (caseReferences.isEmpty()) { + log.info(NO_REFERENCES_TO_VALIDATE); + } else { + validateCsvCaseSizeLessThanMaximum(caseReferences); + logInvalidCaseReferences(caseReferences); + } } private void validateCsvCaseSizeLessThanMaximum(List caseReferences) throws TooManyCsvRecordsException { if (caseReferences.size() > maxNumCaseReferences) { + log.error(String.format(MAX_CSV_ENTRIES_EXCEEDED_ERROR, maxNumCaseReferences)); throw new TooManyCsvRecordsException(maxNumCaseReferences); } } diff --git a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepositoryTest.java b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepositoryTest.java index 3abcbc91..f5055d6a 100644 --- a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepositoryTest.java +++ b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/repository/CcdCaseEventRepositoryTest.java @@ -63,7 +63,8 @@ void testTriggerAboutToStartEvent() { when(datastoreClient.startEvent(ADMIN_TOKEN, S2S_TOKEN, CASE_REFERENCE, EVENT_ID)) .thenReturn(START_EVENT_RESPONSE); - StartEventResponse returnedStartEventResponse = ccdCaseEventRepository.triggerAboutToStartEvent(CASE_REFERENCE); + StartEventResponse returnedStartEventResponse = + ccdCaseEventRepository.triggerAboutToStartEvent(CASE_REFERENCE, 1, 4); assertNotNull(returnedStartEventResponse); verify(securityUtils).getNextHearingDateAdminAccessToken(); @@ -75,7 +76,8 @@ void testTriggerAboutToStartEventThrowsFeignException() { when(datastoreClient.startEvent(any(), any(), any(), any())) .thenThrow(FeignException.class); - StartEventResponse returnedStartEventResponse = ccdCaseEventRepository.triggerAboutToStartEvent(CASE_REFERENCE); + StartEventResponse returnedStartEventResponse = + ccdCaseEventRepository.triggerAboutToStartEvent(CASE_REFERENCE, 1, 4); assertNull(returnedStartEventResponse); verify(securityUtils).getNextHearingDateAdminAccessToken(); @@ -88,7 +90,7 @@ void testCreateCaseEventPopulatesCaseDataContent() { when(securityUtils.getNextHearingDateAdminAccessToken()).thenReturn(ADMIN_TOKEN); - ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE); + ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE, 1, 3); verify(datastoreClient).createEvent(any(), any(), any(), caseDataContentArgumentCaptor.capture()); @@ -109,7 +111,7 @@ void testCreateCaseEvent() { when(securityUtils.getNextHearingDateAdminAccessToken()).thenReturn(ADMIN_TOKEN); when(datastoreClient.createEvent(any(), any(), any(), any())).thenReturn(new CaseResource()); - CaseResource caseEvent = ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE); + CaseResource caseEvent = ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE, 1, 3); assertNotNull(caseEvent); verify(securityUtils).getNextHearingDateAdminAccessToken(); @@ -121,7 +123,7 @@ void testCreateCaseEventThrowsFeignException() { when(datastoreClient.createEvent(any(), any(), any(), any())) .thenThrow(FeignException.class); - CaseResource caseEvent = ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE); + CaseResource caseEvent = ccdCaseEventRepository.createCaseEvent(START_EVENT_RESPONSE, 1, 3); assertNull(caseEvent); verify(securityUtils).getNextHearingDateAdminAccessToken(); diff --git a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvServiceTest.java b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvServiceTest.java index ffb1cc60..3bc79626 100644 --- a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/CsvServiceTest.java @@ -87,7 +87,7 @@ void getCaseReferencesShouldRemoveAndLogInvalidCaseReferences() { List formattedMessages = logsList.stream() .map(ILoggingEvent::getFormattedMessage) .toList(); - assertEquals(12, formattedMessages.size()); + assertEquals(13, formattedMessages.size()); boolean anyMatch = IntStream.rangeClosed(1, 6) .allMatch(i -> formattedMessages.contains( diff --git a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/NextHearingDateUpdaterServiceTest.java b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/NextHearingDateUpdaterServiceTest.java index 25a9fe55..74c7bc63 100644 --- a/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/NextHearingDateUpdaterServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/next/hearing/date/updater/service/NextHearingDateUpdaterServiceTest.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -97,7 +98,7 @@ void executeThrowsErrorIfCaseRefsFoundFromCsvAndCaseTypes() { assertThrows(InvalidConfigurationError.class, () -> nextHearingDateUpdaterService.execute()); // THEN - verify(ccdCaseEventRepository, never()).createCaseEvent(any()); + verify(ccdCaseEventRepository, never()).createCaseEvent(any(), anyInt(), anyInt()); } @Test @@ -110,7 +111,7 @@ void executeDoesNotProcessIfNoCaseReferencesExist() { nextHearingDateUpdaterService.execute(); // THEN - verify(ccdCaseEventRepository, never()).createCaseEvent(any()); + verify(ccdCaseEventRepository, never()).createCaseEvent(any(), anyInt(), anyInt()); } @Test