Skip to content

Commit

Permalink
Add test coverage for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
karlbaker02 committed Oct 25, 2024
1 parent 62d5220 commit f2cdf2b
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package uk.gov.justice.laa.crime.evidence.builder;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceRequest;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;

@Component
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UpdateEvidenceDTOBuilder {

public static UpdateEvidenceDTO build(final ApiUpdateIncomeEvidenceRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import uk.gov.justice.laa.crime.evidence.staticdata.entity.IncomeEvidenceRequiredItemEntity;
import uk.gov.justice.laa.crime.evidence.staticdata.projection.IncomeEvidenceRequiredItemProjection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.laa.crime.evidence.repository;

import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.laa.crime.evidence.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,8 +14,6 @@
import uk.gov.justice.laa.crime.common.model.meansassessment.ApiIncomeEvidenceSummary;
import uk.gov.justice.laa.crime.common.model.meansassessment.ApiMeansAssessmentResponse;
import uk.gov.justice.laa.crime.common.model.meansassessment.ApiUpdateMeansAssessmentRequest;
import uk.gov.justice.laa.crime.enums.EmploymentStatus;
import uk.gov.justice.laa.crime.enums.MagCourtOutcome;
import uk.gov.justice.laa.crime.enums.evidence.IncomeEvidenceType;
import uk.gov.justice.laa.crime.evidence.builder.EvidenceFeeRulesDTOBuilder;
import uk.gov.justice.laa.crime.evidence.common.Constants;
Expand All @@ -25,7 +22,6 @@
import uk.gov.justice.laa.crime.common.model.evidence.ApiCalculateEvidenceFeeResponse;
import uk.gov.justice.laa.crime.common.model.evidence.ApiEvidenceFee;
import uk.gov.justice.laa.crime.enums.EvidenceFeeLevel;
import uk.gov.justice.laa.crime.evidence.dto.EvidenceReceivedResultDTO;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;
import uk.gov.justice.laa.crime.evidence.staticdata.enums.ApplicantType;
import uk.gov.justice.laa.crime.evidence.staticdata.enums.EvidenceFeeRules;
Expand Down Expand Up @@ -107,90 +103,64 @@ public ApiUpdateIncomeEvidenceResponse updateEvidence(UpdateEvidenceDTO updateEv
DateUtil.parseLocalDate(incomeEvidenceSummary.getSecondReminderDate()),
DateUtil.parseLocalDate(incomeEvidenceSummary.getEvidenceDueDate()));

boolean evidenceReceived = checkEvidenceReceived(
applicantEvidenceItems,
partnerEvidenceItems,
updateEvidenceDTO.getMagCourtOutcome(),
updateEvidenceDTO.getApplicantDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerDetails().getEmploymentStatus(),
updateEvidenceDTO.getApplicantPensionAmount(),
updateEvidenceDTO.getPartnerPensionAmount());

updateEvidenceReceivedDate(incomeEvidenceSummary, evidenceReceived, updateEvidenceDTO.getEvidenceReceivedDate());
boolean applicantEvidenceItemsReceived = true;
boolean partnerEvidenceItemsReceived = true;

if (!applicantEvidenceItems.isEmpty()) {
applicantEvidenceItemsReceived = incomeEvidenceService.checkEvidenceReceived(
applicantEvidenceItems,
updateEvidenceDTO.getMagCourtOutcome(),
updateEvidenceDTO.getApplicantDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerDetails() != null
? updateEvidenceDTO.getPartnerDetails().getEmploymentStatus() : null,
updateEvidenceDTO.getApplicantPensionAmount(),
ApplicantType.APPLICANT);
}

if (!partnerEvidenceItems.isEmpty()) {
partnerEvidenceItemsReceived = incomeEvidenceService.checkEvidenceReceived(
partnerEvidenceItems,
updateEvidenceDTO.getMagCourtOutcome(),
updateEvidenceDTO.getApplicantDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerPensionAmount(),
ApplicantType.PARTNER);
}

boolean allEvidenceItemsReceived = applicantEvidenceItemsReceived && partnerEvidenceItemsReceived;

updateEvidenceReceivedDate(incomeEvidenceSummary, allEvidenceItemsReceived, updateEvidenceDTO.getEvidenceReceivedDate());
updateEvidenceDueDate(incomeEvidenceSummary, updateEvidenceDTO.getEvidenceDueDate());

ApiMeansAssessmentResponse updateAssessmentResponse = updateMeansAssessment(
incomeEvidenceSummary,
updateEvidenceDTO.getFinancialAssessmentId(),
updateEvidenceDTO.getApplicantDetails().getId(),
updateEvidenceDTO.getPartnerDetails().getId(),
updateEvidenceDTO.getPartnerDetails() != null ? updateEvidenceDTO.getPartnerDetails().getId() : null,
applicantEvidenceItems,
partnerEvidenceItems
);

applicantEvidenceItems = getUpdatedEvidenceItems(updateAssessmentResponse, updateEvidenceDTO.getApplicantDetails().getId());
partnerEvidenceItems = getUpdatedEvidenceItems(updateAssessmentResponse, updateEvidenceDTO.getPartnerDetails().getId());

return new ApiUpdateIncomeEvidenceResponse()

ApiUpdateIncomeEvidenceResponse response = new ApiUpdateIncomeEvidenceResponse()
.withApplicantEvidenceItems(new ApiIncomeEvidenceItems(updateEvidenceDTO.getApplicantDetails(), applicantEvidenceItems))
.withPartnerEvidenceItems(new ApiIncomeEvidenceItems(updateEvidenceDTO.getPartnerDetails(), partnerEvidenceItems))
.withDueDate(DateUtil.parseLocalDate(incomeEvidenceSummary.getEvidenceDueDate()))
.withAllEvidenceReceivedDate(DateUtil.parseLocalDate(incomeEvidenceSummary.getEvidenceReceivedDate()));
}

protected boolean isCalcRequired(CrimeEvidenceDTO crimeEvidenceDTO) {
return (crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.SENT_FOR_TRIAL)
|| crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.COMMITTED_FOR_TRIAL)) &&
(crimeEvidenceDTO.getEvidenceFee() == null || crimeEvidenceDTO.getEvidenceFee().getFeeLevel() == null);
}

private boolean checkEvidenceReceived(
List<ApiIncomeEvidence> applicantEvidenceItems,
List<ApiIncomeEvidence> partnerEvidenceItems,
MagCourtOutcome magCourtOutcome,
EmploymentStatus applicantEmploymentStatus,
EmploymentStatus partnerEmploymentStatus,
BigDecimal applicantPensionAmount,
BigDecimal partnerPensionAmount) {
EvidenceReceivedResultDTO applicantEvidenceReceivedResult = incomeEvidenceService.checkMinimumEvidenceItemsReceived(
applicantEvidenceItems,
ApplicantType.APPLICANT,
magCourtOutcome,
applicantEmploymentStatus,
partnerEmploymentStatus,
applicantPensionAmount
);

if (!applicantEvidenceReceivedResult.isEvidenceReceived()) {
return false;
}

if (!partnerEvidenceItems.isEmpty()) {
EvidenceReceivedResultDTO partnerEvidenceReceivedResult = incomeEvidenceService.checkMinimumEvidenceItemsReceived(
partnerEvidenceItems,
ApplicantType.PARTNER,
magCourtOutcome,
applicantEmploymentStatus,
partnerEmploymentStatus,
partnerPensionAmount
);
partnerEvidenceItems = getUpdatedEvidenceItems(updateAssessmentResponse, updateEvidenceDTO.getPartnerDetails().getId());

if (!partnerEvidenceReceivedResult.isEvidenceReceived()) {
return false;
}

boolean partnerRequiredEvidenceOutstanding = incomeEvidenceService.isRequiredEvidenceOutstanding(
partnerEvidenceReceivedResult.getIncomeEvidenceRequiredId(), partnerEvidenceItems);

if (partnerRequiredEvidenceOutstanding) {
return false;
}
response.setPartnerEvidenceItems(new ApiIncomeEvidenceItems(updateEvidenceDTO.getPartnerDetails(), partnerEvidenceItems));
}

boolean applicantRequiredEvidenceOutstanding = incomeEvidenceService.isRequiredEvidenceOutstanding(
applicantEvidenceReceivedResult.getIncomeEvidenceRequiredId(), applicantEvidenceItems);
return response;
}

return !applicantRequiredEvidenceOutstanding;
protected boolean isCalcRequired(CrimeEvidenceDTO crimeEvidenceDTO) {
return (crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.SENT_FOR_TRIAL)
|| crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.COMMITTED_FOR_TRIAL)) &&
(crimeEvidenceDTO.getEvidenceFee() == null || crimeEvidenceDTO.getEvidenceFee().getFeeLevel() == null);
}

private List<ApiIncomeEvidence> getUpdatedEvidenceItems(
Expand All @@ -207,7 +177,7 @@ private ApiMeansAssessmentResponse updateMeansAssessment(
ApiIncomeEvidenceSummary currentIncomeEvidenceSummary,
int financialAssessmentId,
int applicantId,
int partnerId,
Integer partnerId,
List<ApiIncomeEvidence> applicantEvidenceItems,
List<ApiIncomeEvidence> partnerEvidenceItems) {
// Note: the ApiIncomeEvidenceSummary is being passed only to update the evidence due and
Expand All @@ -225,7 +195,7 @@ private ApiMeansAssessmentResponse updateMeansAssessment(
return meansAssessmentApiService.update(request);
}

private uk.gov.justice.laa.crime.common.model.meansassessment.ApiIncomeEvidence mapApiIncomeEvidence(ApiIncomeEvidence apiIncomeEvidence, int applicantId) {
private uk.gov.justice.laa.crime.common.model.meansassessment.ApiIncomeEvidence mapApiIncomeEvidence(ApiIncomeEvidence apiIncomeEvidence, Integer applicantId) {
return new uk.gov.justice.laa.crime.common.model.meansassessment.ApiIncomeEvidence()
.withId(apiIncomeEvidence.getId())
.withApplicantId(applicantId)
Expand All @@ -247,17 +217,15 @@ private void updateEvidenceDueDate(ApiIncomeEvidenceSummary incomeEvidenceSummar

if (evidenceDueDate == null && previousEvidenceDueDate != null) {
incomeEvidenceSummary.setEvidenceDueDate(previousEvidenceDueDate);
} else if (LocalDateTime.now().isAfter(evidenceDueDate) && evidenceDueDate != previousEvidenceDueDate) {
incomeEvidenceSummary.setEvidenceDueDate(previousEvidenceDueDate);
}
}

private void updateEvidenceReceivedDate(ApiIncomeEvidenceSummary incomeEvidenceSummary, boolean evidenceReceived, LocalDateTime evidenceReceivedDate) {
if (evidenceReceivedDate == null) {
evidenceReceivedDate = evidenceReceived ? LocalDateTime.now() : null;
}

if (evidenceReceived && incomeEvidenceSummary.getEvidenceReceivedDate() == null) {
if (evidenceReceivedDate == null) {
evidenceReceivedDate = LocalDateTime.now();
}

incomeEvidenceSummary.setEvidenceReceivedDate(evidenceReceivedDate);
} else if (!evidenceReceived && incomeEvidenceSummary.getEvidenceReceivedDate() != null) {
incomeEvidenceSummary.setEvidenceReceivedDate(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ public class IncomeEvidenceService {
private final IncomeEvidenceRequiredRepository incomeEvidenceRequiredRepository;
private final IncomeEvidenceRequiredItemRepository incomeEvidenceRequiredItemRepository;

public boolean checkEvidenceReceived(
List<ApiIncomeEvidence> evidenceItems,
MagCourtOutcome magCourtOutcome,
EmploymentStatus applicantEmploymentStatus,
EmploymentStatus partnerEmploymentStatus,
BigDecimal pensionAmount,
ApplicantType applicantType
) {
EvidenceReceivedResultDTO evidenceReceivedResult = checkMinimumEvidenceItemsReceived(
evidenceItems,
applicantType,
magCourtOutcome,
applicantEmploymentStatus,
partnerEmploymentStatus,
pensionAmount
);

if (!evidenceReceivedResult.isEvidenceReceived()) {
return false;
}

boolean requiredEvidenceOutstanding = isRequiredEvidenceOutstanding(
evidenceReceivedResult.getIncomeEvidenceRequiredId(), evidenceItems);

return !requiredEvidenceOutstanding;
}

public boolean isRequiredEvidenceOutstanding(int incomeEvidenceRequiredId, List<ApiIncomeEvidence> providedEvidenceItems) {
// Note: The income evidence items passed in are only those items provided. There may be
// many more evidence items required than are passed in, therefore we need to call out to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import uk.gov.justice.laa.crime.evidence.staticdata.enums.OtherEvidenceTypes;
import uk.gov.justice.laa.crime.util.DateUtil;

import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@ public class MeansAssessmentApiService {
@Qualifier("cmaApiClient")
private final RestAPIClient cmaApiClient;
private final ServicesConfiguration configuration;
private static final String REQUEST_STRING = "Request to Means Assessment Api: {}";
private static final String RESPONSE_STRING = "Response from Means Assessment Api: {}";

public ApiGetMeansAssessmentResponse find(Integer assessmentId) {
log.debug(REQUEST_STRING, assessmentId);
ApiGetMeansAssessmentResponse response = cmaApiClient.get(
new ParameterizedTypeReference<>() {
},
configuration.getCmaApi().getEndpoints().getFindUrl(),
assessmentId
);
log.debug(RESPONSE_STRING, response);

return response;
}

public ApiMeansAssessmentResponse update(ApiUpdateMeansAssessmentRequest request) {
log.debug(REQUEST_STRING, request);
ApiMeansAssessmentResponse response = cmaApiClient.put(
request,
new ParameterizedTypeReference<>() {
},
configuration.getCmaApi().getEndpoints().getUpdateUrl(),
Map.of()
);
log.debug(RESPONSE_STRING, response);

return response;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package uk.gov.justice.laa.crime.evidence.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -16,8 +17,6 @@
import uk.gov.justice.laa.crime.evidence.service.EvidenceService;
import uk.gov.justice.laa.crime.util.RequestBuilderUtils;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(IncomeEvidenceController.class)
@AutoConfigureMockMvc(addFilters = false)
class IncomeEvidenceControllerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.UUID;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;
import uk.gov.justice.laa.crime.util.DateUtil;

@Component
public class TestModelDataBuilder {
Expand Down Expand Up @@ -166,4 +167,27 @@ public static UpdateEvidenceDTO getUpdateEvidenceRequest() {
.financialAssessmentId(FINANCIAL_ASSESSMENT_ID)
.build();
}

public static UpdateEvidenceDTO getUpdateEvidenceRequest(
LocalDate applicationReceivedDate,
ApiApplicantDetails applicantDetails,
List<ApiIncomeEvidence> applicantEvidenceItems,
LocalDate evidenceDueDate,
LocalDate evidenceReceivedDate
) {
if (applicantEvidenceItems == null) {
applicantEvidenceItems = Collections.emptyList();
}

return UpdateEvidenceDTO.builder()
.magCourtOutcome(MagCourtOutcome.SENT_FOR_TRIAL)
.applicantIncomeEvidenceItems(applicantEvidenceItems)
.applicantDetails(applicantDetails)
.applicationReceivedDate(applicationReceivedDate)
.evidenceDueDate(DateUtil.convertDateToDateTime(evidenceDueDate))
.evidenceReceivedDate(DateUtil.convertDateToDateTime(evidenceReceivedDate))
.partnerIncomeEvidenceItems(Collections.emptyList())
.financialAssessmentId(FINANCIAL_ASSESSMENT_ID)
.build();
}
}
Loading

0 comments on commit f2cdf2b

Please sign in to comment.