Skip to content

Commit

Permalink
CIV-11882 Refactoring Generation of CSV schedular to modify elastic s…
Browse files Browse the repository at this point in the history
…earch query (#3884)

* CIV-11882 Error handling when mediation csv generation failed

* CIV-11882 updated ccd-api-gateway-web image to  pass through the pr

* CIV-11882 Modified code to refactor the elastic search code

* CIV-11882 Modified test cases

* CIV-11882 corrected assertion

* CIV-11882 checkstyle issue corrected

* CIV-11882 corrected code smells

* CIV-11882 Added units tests for uncovered code
  • Loading branch information
deepthidoppalapudihmcts authored Jan 24, 2024
1 parent 022816c commit 33cac21
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static java.time.LocalDate.now;

@Component
@RequiredArgsConstructor
Expand All @@ -42,43 +39,37 @@ public class GenerateCsvAndTransferTaskHandler implements BaseExternalTaskHandle

@Override
public void handleTask(ExternalTask externalTask) {
List<CaseDetails> cases = caseSearchService.getCases();

List<CaseData> inMediationCases;
LocalDate claimMovedDate;
if (externalTask.getVariable("claimMovedDate") != null) {
LocalDate erroredDate = LocalDate.parse(externalTask.getVariable("claimMovedDate").toString(), DATE_FORMATTER);
inMediationCases = cases.stream()
.map(caseDetailsConverter::toCaseData)
.filter(checkErrorCasesWithDate(erroredDate)).toList();
claimMovedDate = LocalDate.parse(externalTask.getVariable("claimMovedDate").toString(), DATE_FORMATTER);
} else {
inMediationCases = cases.stream()
.map(caseDetailsConverter::toCaseData)
.filter(checkMediationMovedDate).toList();
claimMovedDate = LocalDate.now().minusDays(1);
}
List<CaseDetails> cases = caseSearchService.getInMediationCases(claimMovedDate);
inMediationCases = cases.stream()
.map(caseDetailsConverter::toCaseData)
.toList();
log.info("Job '{}' found {} case(s)", externalTask.getTopicName(), inMediationCases.size());
String[] headers = {"SITE_ID", "CASE_NUMBER", "CASE_TYPE", "AMOUNT", "PARTY_TYPE", "COMPANY_NAME",
"CONTACT_NAME", "CONTACT_NUMBER", "CHECK_LIST", "PARTY_STATUS", "CONTACT_EMAIL", "PILOT"};
StringBuilder csvColContent = new StringBuilder();
if (!inMediationCases.isEmpty()) {
inMediationCases.forEach(caseData ->
csvColContent.append(generateCsvContent(caseData)));

String generateCsvData = generateCSVRow(headers) + csvColContent;
Optional<EmailData> emailData = prepareEmail(generateCsvData);

emailData.ifPresent(data -> sendGridClient.sendEmail(mediationCSVEmailConfiguration.getSender(), data));
try {
if (!inMediationCases.isEmpty()) {
inMediationCases.forEach(caseData ->
csvColContent.append(generateCsvContent(caseData)));

String generateCsvData = generateCSVRow(headers) + csvColContent;
Optional<EmailData> emailData = prepareEmail(generateCsvData);

emailData.ifPresent(data -> sendGridClient.sendEmail(mediationCSVEmailConfiguration.getSender(), data));
}
} catch (Exception e) {
log.error(e.getMessage());
}
}

private Predicate<CaseData> checkMediationMovedDate = caseData ->
caseData.getClaimMovedToMediationOn() != null
&& now().minusDays(1).equals(caseData.getClaimMovedToMediationOn());

private Predicate<CaseData> checkErrorCasesWithDate(LocalDate errorDate) {
return caseData ->
caseData.getClaimMovedToMediationOn() != null && errorDate != null
&& errorDate.equals(caseData.getClaimMovedToMediationOn());
}

private Optional<EmailData> prepareEmail(String generateCsvData) {
InputStreamSource inputSource = new ByteArrayResource(generateCsvData.getBytes(StandardCharsets.UTF_8));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand Down Expand Up @@ -40,6 +41,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

public BoolQueryBuilder beState(CaseState state) {
return boolQuery()
.must(matchQuery("state", state.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public Query query(int startIndex) {
startIndex
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand All @@ -25,4 +26,9 @@ public Query query(int startIndex) {
startIndex
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static java.util.Collections.emptyList;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand All @@ -18,19 +21,28 @@ public CaseStateSearchService(CoreCaseDataService coreCaseDataService) {
super(coreCaseDataService);
}

public Query query(int startIndex) {
private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
}

@Override
Query query(int startIndex) {
return null;
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
String targetDateString =
claimMovedDate.format(DateTimeFormatter.ISO_DATE);
return new Query(
boolQuery()
.minimumShouldMatch(1)
.should(boolQuery()
.must(beState(IN_MEDIATION))),
.must(beState(IN_MEDIATION))
.must(matchQuery("data.claimMovedToMediationOn", targetDateString))),
emptyList(),
startIndex
);
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand All @@ -32,6 +33,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -16,7 +17,6 @@
public abstract class ElasticSearchService {

private final CoreCaseDataService coreCaseDataService;

private static final int START_INDEX = 0;
private static final int ES_DEFAULT_SEARCH_LIMIT = 10;

Expand All @@ -33,8 +33,27 @@ public List<CaseDetails> getCases() {
return caseDetails;
}

public List<CaseDetails> getInMediationCases(LocalDate claimMovedDate) {

if (claimMovedDate == null) {
claimMovedDate = LocalDate.now().minusDays(1);
}
SearchResult searchResult = coreCaseDataService.searchCases(queryInMediationCases(START_INDEX, claimMovedDate));
int pages = calculatePages(searchResult);
List<CaseDetails> caseDetails = new ArrayList<>(searchResult.getCases());

for (int i = 1; i < pages; i++) {
SearchResult result = coreCaseDataService.searchCases(queryInMediationCases(i * ES_DEFAULT_SEARCH_LIMIT, claimMovedDate));
caseDetails.addAll(result.getCases());
}

return caseDetails;
}

abstract Query query(int startIndex);

abstract Query queryInMediationCases(int startIndex, LocalDate claimMovedDate);

private int calculatePages(SearchResult searchResult) {
return new BigDecimal(searchResult.getTotal()).divide(new BigDecimal(ES_DEFAULT_SEARCH_LIMIT), UP).intValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand Down Expand Up @@ -43,6 +44,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

public BoolQueryBuilder beState(CaseState state) {
return boolQuery()
.must(matchQuery("state", state.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.hmcts.reform.civil.model.search.Query;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.time.LocalDate;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
Expand Down Expand Up @@ -39,6 +40,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public Query query(int startIndex) {
);
}

@Override
Query queryInMediationCases(int startIndex, LocalDate claimMovedDate) {
return null;
}

private QueryBuilder beState(CaseState caseState) {
return boolQuery()
.must(matchQuery("state", caseState.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -85,39 +87,43 @@ void init() {

@Test
void shouldGenerateCsvAndSendEmailSuccessfully() {
when(searchService.getCases()).thenReturn(List.of(caseDetailsWithInMediationState, caseDetailsWithInMediationStateNotToProcess));
when(searchService.getInMediationCases(claimToBeProcessed)).thenReturn(List.of(caseDetailsWithInMediationState));
when(caseDetailsConverter.toCaseData(caseDetailsWithInMediationState)).thenReturn(caseDataInMediation);
when(caseDetailsConverter.toCaseData(caseDetailsWithInMediationStateNotToProcess)).thenReturn(caseDataInMediationNotToProcess);

inMediationCsvHandler.execute(externalTask, externalTaskService);
verify(searchService).getCases();
verify(searchService).getInMediationCases(claimToBeProcessed);
verify(sendGridClient).sendEmail(anyString(), any());
verify(sendGridClient, times(1)).sendEmail(anyString(), any());
verify(externalTaskService).complete(externalTask);
}

@Test
void shouldNotGenerateCsvAndSendEmail() {
when(searchService.getCases()).thenReturn(List.of(caseDetailsWithInMediationStateNotToProcess));
List<CaseDetails> cases = new ArrayList<>();
String date = (claimNotToBeProcessed.format(DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.UK))).toString();
when(externalTask.getVariable(any())).thenReturn(date);
when(searchService.getInMediationCases(any())).thenReturn(cases);
when(caseDetailsConverter.toCaseData(caseDetailsWithInMediationStateNotToProcess)).thenReturn(caseDataInMediationNotToProcess);

inMediationCsvHandler.execute(externalTask, externalTaskService);
verify(searchService).getCases();
verify(searchService).getInMediationCases(claimNotToBeProcessed);
verify(mediationCsvServiceFactory, times(0)).getMediationCSVService(any());
verify(sendGridClient, times(0)).sendEmail(anyString(), any());
verify(externalTaskService).complete(externalTask);
}

@Test
void should_handle_task_from_external_variable() {
String date = (claimToBeProcessed.format(DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.UK))).toString();

String date = (claimNotToBeProcessed.format(DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.UK))).toString();
when(externalTask.getVariable(any())).thenReturn(date);
when(searchService.getCases()).thenReturn(List.of(caseDetailsWithInMediationState, caseDetailsWithInMediationStateNotToProcess));
when(searchService.getInMediationCases(any())).thenReturn(List.of(caseDetailsWithInMediationState, caseDetailsWithInMediationStateNotToProcess));
when(caseDetailsConverter.toCaseData(caseDetailsWithInMediationState)).thenReturn(caseDataInMediation);
when(caseDetailsConverter.toCaseData(caseDetailsWithInMediationStateNotToProcess)).thenReturn(caseDataInMediationNotToProcess);

inMediationCsvHandler.execute(externalTask, externalTaskService);
verify(searchService).getCases();
verify(searchService).getInMediationCases(claimNotToBeProcessed);
verify(sendGridClient).sendEmail(anyString(), any());
verify(sendGridClient, times(1)).sendEmail(anyString(), any());
verify(externalTaskService).complete(externalTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ protected Query buildQuery(int fromValue) {
.must(boolQuery().must(matchQuery("state", "PREPARE_FOR_HEARING_CONDUCT_HEARING"))));
return new Query(query, List.of("reference"), fromValue);
}

@Override
protected Query buildQueryInMediation(int fromValue, LocalDate date) {
return null;
}
}
Loading

0 comments on commit 33cac21

Please sign in to comment.