Skip to content

Commit

Permalink
Merge branch 'master' into FCFIELDS-47
Browse files Browse the repository at this point in the history
  • Loading branch information
alb3rtino authored Jan 18, 2024
2 parents a4eed68 + 69d8736 commit 7af6117
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ramls/customFieldTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"SINGLE_SELECT_DROPDOWN",
"MULTI_SELECT_DROPDOWN",
"TEXTBOX_SHORT",
"TEXTBOX_LONG"
"TEXTBOX_LONG",
"DATE_PICKER"
]
}
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());
}
}
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);
}
}
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 src/test/java/org/folio/validate/DatePickerValueValidatorTest.java
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());
}
}
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"));
}
}

0 comments on commit 7af6117

Please sign in to comment.