From 69d8736614d1cec66c0c37cf379b71263904484f Mon Sep 17 00:00:00 2001 From: alb3rtino Date: Tue, 16 Jan 2024 10:23:57 +0100 Subject: [PATCH] [FCFIELDS-46] - Add custom field type DATE_PICKER (#42) --- ramls/customFieldTypes.json | 3 +- .../DatePickerDefinitionValidator.java | 21 ++++++++ .../value/DatePickerValueValidator.java | 27 ++++++++++ .../value/format/DateFormatValidator.java | 18 +++++++ .../DatePickerValueValidatorTest.java | 49 +++++++++++++++++++ .../DatePickerDefinitionValidatorTest.java | 42 ++++++++++++++++ 6 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/folio/validate/definition/DatePickerDefinitionValidator.java create mode 100644 src/main/java/org/folio/validate/value/DatePickerValueValidator.java create mode 100644 src/main/java/org/folio/validate/value/format/DateFormatValidator.java create mode 100644 src/test/java/org/folio/validate/DatePickerValueValidatorTest.java create mode 100644 src/test/java/org/folio/validate/definition/DatePickerDefinitionValidatorTest.java diff --git a/ramls/customFieldTypes.json b/ramls/customFieldTypes.json index 238379a..8f14e52 100644 --- a/ramls/customFieldTypes.json +++ b/ramls/customFieldTypes.json @@ -10,6 +10,7 @@ "SINGLE_SELECT_DROPDOWN", "MULTI_SELECT_DROPDOWN", "TEXTBOX_SHORT", - "TEXTBOX_LONG" + "TEXTBOX_LONG", + "DATE_PICKER" ] } diff --git a/src/main/java/org/folio/validate/definition/DatePickerDefinitionValidator.java b/src/main/java/org/folio/validate/definition/DatePickerDefinitionValidator.java new file mode 100644 index 0000000..0fd67bf --- /dev/null +++ b/src/main/java/org/folio/validate/definition/DatePickerDefinitionValidator.java @@ -0,0 +1,21 @@ +package org.folio.validate.definition; + +import static org.folio.validate.definition.AllowedFieldsConstants.COMMON_ALLOWED_FIELDS; + +import org.folio.rest.jaxrs.model.CustomField; +import org.folio.rest.jaxrs.model.CustomField.Type; +import org.springframework.stereotype.Component; + +@Component +public class DatePickerDefinitionValidator implements Validatable { + + @Override + public void validateDefinition(CustomField fieldDefinition) { + CustomDefinitionValidationUtil.onlyHasAllowedFields(fieldDefinition, COMMON_ALLOWED_FIELDS); + } + + @Override + public boolean isApplicable(CustomField fieldDefinition) { + return Type.DATE_PICKER.equals(fieldDefinition.getType()); + } +} diff --git a/src/main/java/org/folio/validate/value/DatePickerValueValidator.java b/src/main/java/org/folio/validate/value/DatePickerValueValidator.java new file mode 100644 index 0000000..96ab7a4 --- /dev/null +++ b/src/main/java/org/folio/validate/value/DatePickerValueValidator.java @@ -0,0 +1,27 @@ +package org.folio.validate.value; + +import static org.apache.commons.lang3.Validate.isInstanceOf; +import static org.folio.validate.value.CustomFieldValueValidatorConstants.EXPECT_STRING_MESSAGE; + +import java.util.List; +import org.folio.rest.jaxrs.model.CustomField; +import org.folio.rest.jaxrs.model.CustomField.Type; +import org.folio.validate.value.format.DateFormatValidator; +import org.springframework.stereotype.Component; + +@Component +public class DatePickerValueValidator implements CustomFieldValueValidator { + + private static final DateFormatValidator dateFormatValidator = new DateFormatValidator(); + + @Override + public void validate(Object fieldValue, CustomField fieldDefinition) { + isInstanceOf(String.class, fieldValue, EXPECT_STRING_MESSAGE, fieldDefinition.getType()); + dateFormatValidator.validate(fieldValue.toString()); + } + + @Override + public List supportedTypes() { + return List.of(Type.DATE_PICKER); + } +} diff --git a/src/main/java/org/folio/validate/value/format/DateFormatValidator.java b/src/main/java/org/folio/validate/value/format/DateFormatValidator.java new file mode 100644 index 0000000..682cb90 --- /dev/null +++ b/src/main/java/org/folio/validate/value/format/DateFormatValidator.java @@ -0,0 +1,18 @@ +package org.folio.validate.value.format; + +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class DateFormatValidator implements FormatValidator { + + private static final String INVALID_DATE_FORMAT_MESSAGE = "Invalid date format: %s"; + + @Override + public void validate(String value) { + try { + DateTimeFormatter.ISO_LOCAL_DATE.parse(value); + } catch (DateTimeParseException e) { + throw new IllegalArgumentException(String.format(INVALID_DATE_FORMAT_MESSAGE, value)); + } + } +} diff --git a/src/test/java/org/folio/validate/DatePickerValueValidatorTest.java b/src/test/java/org/folio/validate/DatePickerValueValidatorTest.java new file mode 100644 index 0000000..ec58467 --- /dev/null +++ b/src/test/java/org/folio/validate/DatePickerValueValidatorTest.java @@ -0,0 +1,49 @@ +package org.folio.validate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import java.util.List; +import org.folio.rest.jaxrs.model.CustomField; +import org.folio.rest.jaxrs.model.CustomField.Type; +import org.folio.spring.TestConfiguration; +import org.folio.validate.value.DatePickerValueValidator; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = TestConfiguration.class) +public class DatePickerValueValidatorTest { + + private static final CustomField customField = + new CustomField().withType(Type.DATE_PICKER).withEntityType("user").withName("Date"); + @Autowired private DatePickerValueValidator validator; + + @Test + public void shouldThrowIfInvalidType() { + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> validator.validate(true, customField)); + assertEquals("Field with type DATE_PICKER must be a string", e.getMessage()); + } + + @Test + public void shouldThrowIfInvalidDateFormat() { + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> validator.validate("20231231", customField)); + assertEquals("Invalid date format: 20231231", e.getMessage()); + } + + @Test + public void testValidDate() { + validator.validate("2023-12-31", customField); + } + + @Test + public void testSupportedTypes() { + assertEquals(List.of(Type.DATE_PICKER), validator.supportedTypes()); + } +} diff --git a/src/test/java/org/folio/validate/definition/DatePickerDefinitionValidatorTest.java b/src/test/java/org/folio/validate/definition/DatePickerDefinitionValidatorTest.java new file mode 100644 index 0000000..9102cdc --- /dev/null +++ b/src/test/java/org/folio/validate/definition/DatePickerDefinitionValidatorTest.java @@ -0,0 +1,42 @@ +package org.folio.validate.definition; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import org.folio.rest.jaxrs.model.CustomField; +import org.folio.rest.jaxrs.model.CustomField.Type; +import org.folio.rest.jaxrs.model.SelectField; +import org.folio.spring.TestConfiguration; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = TestConfiguration.class) +public class DatePickerDefinitionValidatorTest { + + private static final CustomField customField = + new CustomField() + .withName("Date field") + .withType(Type.DATE_PICKER) + .withEntityType("user") + .withSelectField(new SelectField()); + @Autowired private DatePickerDefinitionValidator validator; + + @Test + public void shouldBeApplicableForDatePicker() { + assertTrue(validator.isApplicable(customField)); + } + + @Test + public void shouldReturnErrorIfContainsNotAllowedFields() { + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> validator.validateDefinition(customField)); + assertThat(e.getMessage(), containsString("Attribute selectField is not allowed")); + } +}