Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LCAM-1401 #123

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crime-evidence/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jacoco {

def versions = [
pitest : "1.16.0",
crimeCommonsClasses : "3.28.0",
crimeCommonsClasses : "3.29.1",
commonsModSchemas : "1.8.0",
commonsRestClient : "3.4.0",
wmStubRunnerVersion : "4.1.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.gov.justice.laa.crime.evidence.service;

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.evidence.staticdata.enums.OtherEvidenceTypes;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class IncomeEvidenceValidationService {

public void checkEvidenceReceivedDate(Date incomeEvidenceReceivedDate, Date applicationReceivedDate) {
Date currentDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
muthusubra marked this conversation as resolved.
Show resolved Hide resolved
if (incomeEvidenceReceivedDate != null && incomeEvidenceReceivedDate.after(currentDate)) {
throw new IllegalArgumentException("Income evidence received date cannot be in the future");
}

if (incomeEvidenceReceivedDate != null && incomeEvidenceReceivedDate.before(applicationReceivedDate)) {
throw new IllegalArgumentException("Income evidence received date cannot be before application date received");
}
}

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.");
}
}


public void checkEvidenceDueDates(Date evidenceDueDate, Date firstReminderDate, Date secondReminderDate,
Date existingEvidenceDueDate) {
LocalDate localDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

if ((evidenceDueDate == null && existingEvidenceDueDate != null) && (
firstReminderDate != null || secondReminderDate != null)) {
throw new IllegalArgumentException("Evidence due date cannot be null");
}

if (evidenceDueDate != null && evidenceDueDate.before(date)
&& (!evidenceDueDate.equals(existingEvidenceDueDate))) {
throw new IllegalArgumentException("Cannot set due date in the past.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.gov.justice.laa.crime.evidence.staticdata.enums;

import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

public enum OtherEvidenceTypes {
OTHER("OTHER"),
OTHER_BUSINESS("OTHER BUSINESS"),
OTHER_ADHOC("OTHER_ADHOC");

private final String evidence;

OtherEvidenceTypes(String evidence) {
this.evidence = evidence;
}

public String getEvidence() {
return evidence;
}
muthusubra marked this conversation as resolved.
Show resolved Hide resolved

public static OtherEvidenceTypes getFrom(String type) {
if (StringUtils.isBlank(type)) return null;

return Stream.of(OtherEvidenceTypes.values())
.filter(ie -> ie.evidence.equals(type))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format("Evidence Type: %s does not exist.", type)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package uk.gov.justice.laa.crime.evidence.service;

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.util.DateUtil;

import java.time.LocalDate;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertThrows;

@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(new Date(), new Date());
}

@Test
void givenValidDates_whenCheckEvidenceReceivedDateIsInvokedWithIncomeEvidenceReceivedDateNull_noExceptionIsThrown() {
incomeEvidenceValidationService.checkEvidenceReceivedDate(null, new Date());
}

@Test
void givenValidDates_whenCheckEvidenceReceivedDateIsInvokedWithIncomeEvidenceReceivedDateAfterCurrentDate_thenExceptionIsThrown() {
LocalDate futureDate = LocalDate.now().plusMonths(2);
Date convertedFutureDate = DateUtil.asDate(futureDate);
Date currentDate = new Date();
assertThrows(IllegalArgumentException.class, () ->
incomeEvidenceValidationService.checkEvidenceReceivedDate(convertedFutureDate, currentDate)
);
}

@Test
void givenValidDates_whenCheckEvidenceReceivedDateIsInvokedWithIncomeEvidenceReceivedDateBeforeCurrentDate_ExceptionIsThrown() {
LocalDate pastDate = LocalDate.now().minusMonths(2);
Date pastDateConverted = DateUtil.asDate(pastDate);
Date currentDate = new Date();
assertThrows(IllegalArgumentException.class, () ->
incomeEvidenceValidationService.checkEvidenceReceivedDate(pastDateConverted, currentDate)
);
}

@Test
void givenIncomeExtraEvidenceReceivedDate_whenCheckIncomeEvidenceReceivedDateIsInvoked_thenNoExceptionIsThrown() {
incomeEvidenceValidationService.checkExtraEvidenceDescription("OTHER", "Some text");
}

@Test
void givenIncomeExtraEvidenceReceivedDate_whenCheckIncomeEvidenceReceivedDateIsInvokedWithIncomeExtraEvidenceTextNull_thenExceptionIsThrown() {
assertThrows(IllegalArgumentException.class, () -> {
incomeEvidenceValidationService.checkExtraEvidenceDescription("OTHER", null);
});
}

@Test
void givenIncomeExtraEvidenceReceivedDate_whenCheckIncomeEvidenceReceivedDateIsInvokedWithIncomeExtraEvidenceTextEmpty_thenExceptionIsThrown() {
assertThrows(IllegalArgumentException.class, () -> {
incomeEvidenceValidationService.checkExtraEvidenceDescription("OTHER", "");
});
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvoked_thenNoExceptionIsThrown() {
incomeEvidenceValidationService.checkEvidenceDueDates(new Date(), new Date(), new Date(), new Date());
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithEvidenceDueDateNull_thenExceptionIsThrown() {
Date currentDate = new Date();
assertThrows(IllegalArgumentException.class, () ->
incomeEvidenceValidationService.checkEvidenceDueDates(null, currentDate, currentDate, currentDate)
);
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithEvidenceDueDateAfterCurrentDate_thenNoExceptionIsThrown() {
LocalDate futureDate = LocalDate.now().plusMonths(2);
Date futureDateConverted = DateUtil.asDate(futureDate);
Date currentDate = new Date();
incomeEvidenceValidationService.checkEvidenceDueDates(futureDateConverted, currentDate, currentDate, currentDate);
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithEvidenceDueDateBeforeCurrentDate_thenExceptionIsThrown() {
LocalDate pastDate = LocalDate.now().minusMonths(2);
Date pastDateConverted = DateUtil.asDate(pastDate);
Date currentDate1 = new Date();
Date currentDate2 = new Date();
Date currentDate3 = new Date();
assertThrows(IllegalArgumentException.class, () ->
incomeEvidenceValidationService.checkEvidenceDueDates(pastDateConverted, currentDate1, currentDate2, currentDate3)
);
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithFirstReminderDateNull_thenNoExceptionIsThrown() {
incomeEvidenceValidationService.checkEvidenceDueDates(new Date(), null, new Date(), new Date());
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithFirstReminderDateAfterCurrentDate_thenNoExceptionIsThrown() {
LocalDate futureDate = LocalDate.now().plusMonths(2);
Date currentDate1 = new Date();
Date futureDateConverted = DateUtil.asDate(futureDate);
Date currentDate2 = new Date();
Date currentDate3 = new Date();
incomeEvidenceValidationService.checkEvidenceDueDates(currentDate1, futureDateConverted, currentDate2, currentDate3);
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithFirstReminderDateBeforeCurrentDate_thenNoExceptionIsThrown() {
LocalDate pastDate = LocalDate.now().minusMonths(2);
Date currentDate1 = new Date();
Date pastDateConverted = DateUtil.asDate(pastDate);
Date currentDate2 = new Date();
Date currentDate3 = new Date();
incomeEvidenceValidationService.checkEvidenceDueDates(currentDate1, pastDateConverted, currentDate2, currentDate3);
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithSecondReminderDateNull_thenExceptionIsThrown() {
incomeEvidenceValidationService.checkEvidenceDueDates(new Date(), new Date(), null, new Date());
}

@Test
void givenValidDates_whenCheckEvidenceDueDatesIsInvokedWithExistingEvidenceDueDateAsNullAndEvidenceDueDateBeforeDate_thenExceptionIsThrown() {
LocalDate pastDate = LocalDate.now().minusMonths(2);
Date pastDateConverted = DateUtil.asDate(pastDate);
Date currentDate1 = new Date();
Date currentDate2 = new Date();
assertThrows(IllegalArgumentException.class, () ->
incomeEvidenceValidationService.checkEvidenceDueDates(pastDateConverted, currentDate1, currentDate2, null)
);
}
}
Loading