-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/org/folio/validate/definition/DatePickerDefinitionValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/folio/validate/value/DatePickerValueValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CustomField.Type> supportedTypes() { | ||
return List.of(Type.DATE_PICKER); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/folio/validate/value/format/DateFormatValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/org/folio/validate/DatePickerValueValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/folio/validate/definition/DatePickerDefinitionValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |