Skip to content

Commit

Permalink
fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Mar 8, 2024
1 parent 4beb858 commit ca69b60
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
36 changes: 30 additions & 6 deletions src/main/java/alfio/util/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.time.Period;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -66,6 +67,7 @@ public final class Validator {
private static final String ADDITIONAL_PREFIX = "additional[";
private static final String ADDITIONAL_SERVICES = "additionalServices";
private static final String ERROR_RESTRICTED_VALUE = "error.restrictedValue";
public static final Supplier<LocalDate> LOCAL_DATE_SUPPLIER = () -> LocalDate.now(ClockProvider.clock());

private Validator() {
}
Expand Down Expand Up @@ -436,7 +438,7 @@ private static void fieldValueBasicValidation(Errors errors, String prefixForLam
}

if (!errors.hasFieldErrors() && StringUtils.isNotBlank(formValue) && fieldConf.isDateOfBirth()) {
int age = calculateAge(formValue, true);
int age = calculateAge(formValue, true, LOCAL_DATE_SUPPLIER);
if (age < 0) {
// age was not provided in the right format
errors.rejectValue(prefixForLambda + ADDITIONAL_PREFIX + fieldConf.getName()+"]["+ i +"]", ErrorsCode.EMPTY_FIELD);
Expand Down Expand Up @@ -492,9 +494,22 @@ private static void validateMinLength(String value, String fieldName, String err
}
}

static void validateMinAge(String value, String fieldName, String errorCode, PurchaseContextFieldConfiguration fieldConfiguration, Errors errors) {
static void validateMinAge(String value,
String fieldName,
String errorCode,
PurchaseContextFieldConfiguration fieldConfiguration,
Errors errors) {
validateMinAge(value, fieldName, errorCode, fieldConfiguration, errors, LOCAL_DATE_SUPPLIER);
}

static void validateMinAge(String value,
String fieldName,
String errorCode,
PurchaseContextFieldConfiguration fieldConfiguration,
Errors errors,
Supplier<LocalDate> currentDateSupplier) {
int minAge = fieldConfiguration.getMinLength();
int age = calculateAge(value, false);
int age = calculateAge(value, false, currentDateSupplier);
if (age >= 0 && age < minAge) {
errors.rejectValue(fieldName, errorCode, new Object[] { minAge }, null);
}
Expand Down Expand Up @@ -524,16 +539,25 @@ private static void validateMaxLength(String value, String fieldName, String err
}

static void validateMaxAge(String value, String fieldName, String errorCode, PurchaseContextFieldConfiguration fieldConfiguration, Errors errors) {
validateMaxAge(value, fieldName, errorCode, fieldConfiguration, errors, LOCAL_DATE_SUPPLIER);
}

static void validateMaxAge(String value,
String fieldName,
String errorCode,
PurchaseContextFieldConfiguration fieldConfiguration,
Errors errors,
Supplier<LocalDate> localDateSupplier) {
int maxAge = fieldConfiguration.getMaxLength();
int age = calculateAge(value, true);
int age = calculateAge(value, true, localDateSupplier);
if (age > maxAge) {
errors.rejectValue(fieldName, errorCode, new Object[] { maxAge }, null);
}
}

private static int calculateAge(String value, boolean addRemainder) {
private static int calculateAge(String value, boolean addRemainder, Supplier<LocalDate> currentDateSupplier) {
try {
var period = Period.between(LocalDate.parse(value), LocalDate.now(ClockProvider.clock()));
var period = Period.between(LocalDate.parse(value), currentDateSupplier.get());
int years = period.getYears();
if (addRemainder && (period.getMonths() > 0 || period.getDays() > 0)) {
years += 1;
Expand Down
63 changes: 41 additions & 22 deletions src/test/java/alfio/util/ValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.validation.Errors;
import org.springframework.validation.MapBindingResult;
Expand Down Expand Up @@ -221,41 +222,59 @@ void invalidEmails(String address) {
}

@ParameterizedTest
@ValueSource(ints = {
1, 2, 3, 4, 5
@CsvSource({
// minAge, birtDate, today
"1,2023-02-28,2024-02-28",
"1,2023-03-01,2024-03-01",
"2,2022-02-28,2024-02-28",
"2,2022-03-01,2024-03-01",
"3,2021-02-28,2024-02-28",
"3,2021-03-01,2024-03-01",
"4,2020-02-28,2024-02-28",
"4,2020-03-01,2024-03-01",
"5,2019-02-28,2024-02-28",
"5,2019-03-01,2024-03-01",
"5,2018-03-01,2023-03-01"
})
void minAgeValidator(int minAge) {
void minAgeValidator(String minAgeAsString, String birthDateAsString, String todayAsString) {
int minAge = Integer.parseInt(minAgeAsString);
var ticketFieldConfiguration = mock(PurchaseContextFieldConfiguration.class);
var today = LocalDate.parse(todayAsString);
when(ticketFieldConfiguration.getMinLength()).thenReturn(minAge);
var birth = LocalDate.now(ClockProvider.clock()).minusYears(minAge);
var birth = LocalDate.parse(birthDateAsString);
var date = birth.format(DateTimeFormatter.ISO_LOCAL_DATE);
Validator.validateMinAge(date, "fieldName", "error", ticketFieldConfiguration, errors);
Validator.validateMinAge(date, "fieldName", "error", ticketFieldConfiguration, errors, () -> today);
assertFalse(errors.hasFieldErrors());
date = birth.plusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
Validator.validateMinAge(date, "fieldName", "error", ticketFieldConfiguration, errors);
Validator.validateMinAge(date, "fieldName", "error", ticketFieldConfiguration, errors, () -> today);
assertTrue(errors.hasFieldErrors());
}

@ParameterizedTest
@ValueSource(ints = {
1, 2, 3, 4, 5
@CsvSource({
// maxAge, birtDate, today
"1,2023-02-28,2024-02-27",
"1,2023-03-01,2024-02-29",
"2,2022-02-28,2024-02-27",
"2,2022-03-01,2024-02-29",
"3,2021-02-28,2024-02-27",
"3,2021-03-01,2024-02-29",
"4,2020-02-28,2024-02-27",
"4,2020-03-01,2024-02-29",
"5,2019-02-28,2024-02-27",
"5,2019-03-01,2024-02-29",
"5,2018-03-01,2023-02-28"
})
void maxAgeValidator(int maxAge) {
void maxAgeValidator(String maxAgeAsString, String birthDateAsString, String todayAsString) {
int maxAge = Integer.parseInt(maxAgeAsString);
var today = LocalDate.parse(todayAsString);
var ticketFieldConfiguration = mock(PurchaseContextFieldConfiguration.class);
when(ticketFieldConfiguration.getMaxLength()).thenReturn(maxAge);
var todayLeapYear = LocalDate.now(ClockProvider.clock()).isLeapYear();
// today -> 29.02.2024 -> birth 28.02.2023, but when -4 years, birth is also leap year
var birth = LocalDate.now(ClockProvider.clock()).minusYears(maxAge);
var birthLeapYear = birth.isLeapYear();
var date = birth.format(DateTimeFormatter.ISO_LOCAL_DATE);
Validator.validateMaxAge(date, "fieldName", "error", ticketFieldConfiguration, errors);
if (todayLeapYear && !birthLeapYear) {
assertTrue(errors.hasFieldErrors()); // as today is 29 and not 28, the person will have x year + 1 day, thus being over max age
errors = new MapBindingResult(new HashMap<>(), "test");
} else {
assertFalse(errors.hasFieldErrors());
}
date = birth.minusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE);

Validator.validateMaxAge(birthDateAsString, "fieldName", "error", ticketFieldConfiguration, errors, () -> today);
assertFalse(errors.hasFieldErrors());

var date = LocalDate.parse(birthDateAsString).minusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
Validator.validateMaxAge(date, "fieldName", "error", ticketFieldConfiguration, errors);
assertTrue(errors.hasFieldErrors());
}
Expand Down

0 comments on commit ca69b60

Please sign in to comment.