Skip to content

Commit

Permalink
HMAN-445 update logging (#80)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RebeccaBaker authored Jan 17, 2023
1 parent 4ed3aef commit e4369be
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 '{}'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.time.LocalDateTime;

import static uk.gov.hmcts.reform.next.hearing.date.updater.exceptions.ErrorMessages.ERROR_DOWNSTREAM;

/**
* HMAN-322.
*/
Expand All @@ -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;

Expand All @@ -35,23 +43,25 @@ 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();
String s2SToken = securityUtils.getS2SToken();
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()
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public List<CaseDetails> findCasesWithOutOfDateNextHearingDate(String caseType)
caseType, elasticSearchQuery.getQuery());

List<CaseDetails> caseDetails = new ArrayList<>();

if (searchResult.getTotal() > 0) {
List<CaseDetails> searchResultCases = searchResult.getCases();
caseDetails.addAll(searchResultCases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ public CcdCaseEventService(CcdCaseEventRepository ccdCaseEventRepository) {
}

public void createCaseEvents(List<String> 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<String> 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")
Expand All @@ -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<String, Object> jsonMap, String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +38,8 @@ public List<String> getCaseReferences() {
try {
List<String> 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);
Expand All @@ -60,12 +64,17 @@ private List<String> getCaseReferencesFromCsvFile() throws CsvFileException {
}

private void validateCaseRefsFile(List<String> 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<String> caseReferences) throws TooManyCsvRecordsException {
if (caseReferences.size() > maxNumCaseReferences) {
log.error(String.format(MAX_CSV_ENTRIES_EXCEEDED_ERROR, maxNumCaseReferences));
throw new TooManyCsvRecordsException(maxNumCaseReferences);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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());

Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void getCaseReferencesShouldRemoveAndLogInvalidCaseReferences() {
List<String> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -110,7 +111,7 @@ void executeDoesNotProcessIfNoCaseReferencesExist() {
nextHearingDateUpdaterService.execute();

// THEN
verify(ccdCaseEventRepository, never()).createCaseEvent(any());
verify(ccdCaseEventRepository, never()).createCaseEvent(any(), anyInt(), anyInt());
}

@Test
Expand Down

0 comments on commit e4369be

Please sign in to comment.