Skip to content

Commit

Permalink
Validate extra evidence descriptions in the updateEvidence process
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat980 authored and karlbaker02 committed Oct 25, 2024
1 parent 114b4bd commit 38b088d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -163,8 +167,7 @@ protected boolean isCalcRequired(CrimeEvidenceDTO crimeEvidenceDTO) {
(crimeEvidenceDTO.getEvidenceFee() == null || crimeEvidenceDTO.getEvidenceFee().getFeeLevel() == null);
}

private List<ApiIncomeEvidence> getUpdatedEvidenceItems(
ApiMeansAssessmentResponse meansAssessmentResponse,
private List<ApiIncomeEvidence> getUpdatedEvidenceItems(ApiMeansAssessmentResponse meansAssessmentResponse,
int personId) {
return meansAssessmentResponse.getIncomeEvidence()
.stream()
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IncomeEvidenceType> 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(),
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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<ApiIncomeEvidence> incomeEvidences) {
incomeEvidences.stream()
.filter(apiIncomeEvidence -> EXTRA_EVIDENCES.contains(apiIncomeEvidence.getEvidenceType()))
.forEach(apiIncomeEvidence -> {
if (StringUtils.isBlank(apiIncomeEvidence.getDescription())) {
throw new IllegalArgumentException(MISSING_OTHER_EVIDENCE_DESCRIPTION);
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
Expand Down Expand Up @@ -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<ApiIncomeEvidence> 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<ApiIncomeEvidence> 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));
}
}

0 comments on commit 38b088d

Please sign in to comment.