diff --git a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/EvidenceService.java b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/EvidenceService.java index e7e80ce..5ea1a8d 100644 --- a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/EvidenceService.java +++ b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/EvidenceService.java @@ -1,11 +1,10 @@ package uk.gov.justice.laa.crime.evidence.service; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +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.common.model.evidence.ApiIncomeEvidence; import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidenceItems; import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceResponse; @@ -14,19 +13,21 @@ 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.EvidenceFeeLevel; 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; import uk.gov.justice.laa.crime.evidence.dto.CrimeEvidenceDTO; import uk.gov.justice.laa.crime.evidence.dto.EvidenceFeeRulesDTO; -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.UpdateEvidenceDTO; import uk.gov.justice.laa.crime.evidence.staticdata.enums.ApplicantType; import uk.gov.justice.laa.crime.evidence.staticdata.enums.EvidenceFeeRules; import uk.gov.justice.laa.crime.util.DateUtil; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + @Slf4j @Service @RequiredArgsConstructor @@ -94,6 +95,9 @@ public ApiUpdateIncomeEvidenceResponse updateEvidence(UpdateEvidenceDTO updateEv DateUtil.parseLocalDate(updateEvidenceDTO.getEvidenceReceivedDate()), updateEvidenceDTO.getApplicationReceivedDate()); + incomeEvidenceValidationService.checkExtraEvidenceDescriptions(applicantEvidenceItems); + incomeEvidenceValidationService.checkExtraEvidenceDescriptions(partnerEvidenceItems); + ApiGetMeansAssessmentResponse oldMeansAssessmentResponse = meansAssessmentApiService.find(updateEvidenceDTO.getFinancialAssessmentId()); ApiIncomeEvidenceSummary incomeEvidenceSummary = oldMeansAssessmentResponse.getIncomeEvidenceSummary(); @@ -163,8 +167,7 @@ protected boolean isCalcRequired(CrimeEvidenceDTO crimeEvidenceDTO) { (crimeEvidenceDTO.getEvidenceFee() == null || crimeEvidenceDTO.getEvidenceFee().getFeeLevel() == null); } - private List getUpdatedEvidenceItems( - ApiMeansAssessmentResponse meansAssessmentResponse, + private List getUpdatedEvidenceItems(ApiMeansAssessmentResponse meansAssessmentResponse, int personId) { return meansAssessmentResponse.getIncomeEvidence() .stream() @@ -222,11 +225,7 @@ private void updateEvidenceDueDate(ApiIncomeEvidenceSummary incomeEvidenceSummar private void updateEvidenceReceivedDate(ApiIncomeEvidenceSummary incomeEvidenceSummary, boolean evidenceReceived, LocalDateTime evidenceReceivedDate) { if (evidenceReceived && incomeEvidenceSummary.getEvidenceReceivedDate() == null) { - if (evidenceReceivedDate == null) { - evidenceReceivedDate = LocalDateTime.now(); - } - - incomeEvidenceSummary.setEvidenceReceivedDate(evidenceReceivedDate); + incomeEvidenceSummary.setEvidenceReceivedDate(evidenceReceivedDate != null ? evidenceReceivedDate : LocalDateTime.now()); } else if (!evidenceReceived && incomeEvidenceSummary.getEvidenceReceivedDate() != null) { incomeEvidenceSummary.setEvidenceReceivedDate(null); } diff --git a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationService.java b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationService.java index 022185b..a724be8 100644 --- a/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationService.java +++ b/crime-evidence/src/main/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationService.java @@ -1,18 +1,26 @@ package uk.gov.justice.laa.crime.evidence.service; -import java.time.LocalDate; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidence; +import uk.gov.justice.laa.crime.enums.evidence.IncomeEvidenceType; import uk.gov.justice.laa.crime.evidence.dto.EvidenceDTO; import uk.gov.justice.laa.crime.evidence.staticdata.enums.OtherEvidenceTypes; import uk.gov.justice.laa.crime.util.DateUtil; +import java.time.LocalDate; +import java.util.List; + @Slf4j @Service @RequiredArgsConstructor public class IncomeEvidenceValidationService { + private static final List EXTRA_EVIDENCES = List.of(IncomeEvidenceType.OTHER, + IncomeEvidenceType.OTHER_ADHOC, + IncomeEvidenceType.OTHER_BUSINESS); + public static final String MISSING_OTHER_EVIDENCE_DESCRIPTION = "When other evidence is requested, you must provide descriptive text."; public void validate(EvidenceDTO evidenceDTO){ checkExtraEvidenceDescription(evidenceDTO.getIncomeExtraEvidence(), @@ -41,7 +49,7 @@ public void checkEvidenceReceivedDate(LocalDate incomeEvidenceReceivedDate, Loca public void checkExtraEvidenceDescription(String incomeExtraEvidence, String incomeExtraEvidenceText) { if (OtherEvidenceTypes.getFrom(incomeExtraEvidence) != null && StringUtils.isBlank(incomeExtraEvidenceText)) { - throw new IllegalArgumentException("When other evidence is requested, you must provide descriptive text."); + throw new IllegalArgumentException(MISSING_OTHER_EVIDENCE_DESCRIPTION); } } @@ -58,4 +66,15 @@ public void checkEvidenceDueDates(LocalDate evidenceDueDate, LocalDate firstRemi throw new IllegalArgumentException("Cannot set due date in the past."); } } + + public void checkExtraEvidenceDescriptions(List incomeEvidences) { + incomeEvidences.stream() + .filter(apiIncomeEvidence -> EXTRA_EVIDENCES.contains(apiIncomeEvidence.getEvidenceType())) + .forEach(apiIncomeEvidence -> { + if (StringUtils.isBlank(apiIncomeEvidence.getDescription())) { + throw new IllegalArgumentException(MISSING_OTHER_EVIDENCE_DESCRIPTION); + } + } + ); + } } diff --git a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/data/builder/TestModelDataBuilder.java b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/data/builder/TestModelDataBuilder.java index eb6c26f..863d788 100644 --- a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/data/builder/TestModelDataBuilder.java +++ b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/data/builder/TestModelDataBuilder.java @@ -190,4 +190,13 @@ public static UpdateEvidenceDTO getUpdateEvidenceRequest( .financialAssessmentId(FINANCIAL_ASSESSMENT_ID) .build(); } + + public static ApiIncomeEvidence getIncomeEvidence(IncomeEvidenceType incomeEvidenceType) { + return new ApiIncomeEvidence() + .withId(9315) + .withDescription("mock evidence item") + .withMandatory(true) + .withEvidenceType(incomeEvidenceType) + .withDateReceived(EVIDENCE_RECEIVED_DATE); + } } \ No newline at end of file diff --git a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationServiceTest.java b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationServiceTest.java index 886f614..8802678 100644 --- a/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationServiceTest.java +++ b/crime-evidence/src/test/java/uk/gov/justice/laa/crime/evidence/service/IncomeEvidenceValidationServiceTest.java @@ -1,32 +1,29 @@ package uk.gov.justice.laa.crime.evidence.service; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.time.LocalDate; -import org.assertj.core.api.SoftAssertions; -import org.assertj.core.api.junit.jupiter.InjectSoftAssertions; import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidence; +import uk.gov.justice.laa.crime.enums.evidence.IncomeEvidenceType; +import uk.gov.justice.laa.crime.evidence.data.builder.TestModelDataBuilder; import uk.gov.justice.laa.crime.evidence.dto.EvidenceDTO; import uk.gov.justice.laa.crime.util.DateUtil; +import java.time.LocalDate; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static uk.gov.justice.laa.crime.evidence.service.IncomeEvidenceValidationService.MISSING_OTHER_EVIDENCE_DESCRIPTION; + @ExtendWith(MockitoExtension.class) @ExtendWith(SoftAssertionsExtension.class) class IncomeEvidenceValidationServiceTest { - @Mock - private MaatCourtDataService maatCourtDataService; - @InjectMocks private IncomeEvidenceValidationService incomeEvidenceValidationService; - @InjectSoftAssertions - private SoftAssertions softly; - @Test void givenValidDates_whenCheckEvidenceReceivedDateIsInvoked_noExceptionIsThrown() { incomeEvidenceValidationService.checkEvidenceReceivedDate(LocalDate.now(), LocalDate.now()); @@ -217,4 +214,37 @@ void givenValidDates_whenValidateIsInvoked_thenNoExceptionIsThrown() { .build(); incomeEvidenceValidationService.validate(evidenceDTO); } + + @Test + void givenExtraEvidenceWithDescription_whenCheckExtraEvidenceDescriptionsIsInvoked_thenNoExceptionIsThrown() { + incomeEvidenceValidationService.checkExtraEvidenceDescriptions( + List.of(TestModelDataBuilder.getIncomeEvidence(IncomeEvidenceType.OTHER))); + } + + @Test + void givenIncomeExtraEvidenceWithNoDescription_whenCheckExtraEvidenceDescriptionsIsInvoked_thenExceptionIsThrown() { + ApiIncomeEvidence extraIncomeEvidence = TestModelDataBuilder.getIncomeEvidence(IncomeEvidenceType.OTHER_ADHOC); + extraIncomeEvidence.setDescription(null); + List evidenceList = List.of(extraIncomeEvidence); + assertThrows(IllegalArgumentException.class, + () -> incomeEvidenceValidationService.checkExtraEvidenceDescriptions(evidenceList), + MISSING_OTHER_EVIDENCE_DESCRIPTION); + } + + @Test + void givenIncomeExtraEvidenceWithEmptyDescription_whenCheckExtraEvidenceDescriptionsIsInvoked_thenExceptionIsThrown() { + ApiIncomeEvidence extraIncomeEvidence = TestModelDataBuilder.getIncomeEvidence(IncomeEvidenceType.OTHER_BUSINESS); + extraIncomeEvidence.setDescription(""); + List evidenceList = List.of(extraIncomeEvidence); + assertThrows(IllegalArgumentException.class, + () -> incomeEvidenceValidationService.checkExtraEvidenceDescriptions(evidenceList), + MISSING_OTHER_EVIDENCE_DESCRIPTION); + } + + @Test + void givenIncomeEvidence_whenCheckExtraEvidenceDescriptionsIsInvoked_thenNoExceptionIsThrown() { + ApiIncomeEvidence incomeEvidence = TestModelDataBuilder.getIncomeEvidence(IncomeEvidenceType.NINO); + incomeEvidence.setDescription(""); + incomeEvidenceValidationService.checkExtraEvidenceDescriptions(List.of(incomeEvidence)); + } } \ No newline at end of file