EnumListValidator implements ConstraintValidator<IsEnum, List<String>> { @Override public boolean isValid(List<String> value, ConstraintValidatorContext context) { if (value == null) { return allowNull; } else { List<String> invalidValues = value.stream() .filter(v -> !isValidEnum(v)) .collect(Collectors.toList()); if (!invalidValues.isEmpty()) { context.disableDefaultConstraintViolation(); String invalidValuesString = String.join(",", invalidValues); String messageTemplate = messageSource.getMessage(UNKNOWN_ENUMS_VALUE_MESSAGE_CODE, new Object[]{invalidValuesString}, Locale.getDefault()); context.buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation(); } return invalidValues.isEmpty(); } } EnumListValidator(@Qualifier("edisonValidationMessageSource") ResourceBundleMessageSource messageSource); @Override @SuppressWarnings("rawtypes") void initialize(IsEnum annotation); @Override boolean isValid(List<String> value, ConstraintValidatorContext context);  }
@Test public void shouldValidateSuccessfully() throws Exception { EnumListValidator enumListValidator = createAndInitializeValidator(false, false); boolean valid = enumListValidator.isValid(Arrays.asList("foo", "bar"), createContext()); assertThat(valid, is(true)); }
@Test public void shouldFailForInvalidValue() throws Exception { EnumListValidator enumListValidator = createAndInitializeValidator(false, false); boolean valid = enumListValidator.isValid(Arrays.asList("foo", "xxx"), createContext()); assertThat(valid, is(false)); }
@Test public void shouldFailWhenNotIgnoringCase() throws Exception { EnumListValidator enumListValidator = createAndInitializeValidator(false, false); ConstraintValidatorContext context = createContext(); boolean valid = enumListValidator.isValid(Collections.singletonList("Foo"), context); assertThat(valid, is(false)); }
@Test public void shouldSucceedWhenIgnoringCase() throws Exception { EnumListValidator enumListValidator = createAndInitializeValidator(true, false); boolean valid = enumListValidator.isValid(Collections.singletonList("Foo"), createContext()); assertThat(valid, is(true)); }
@Test public void shouldFailForNull() { EnumListValidator enumListValidator = createAndInitializeValidator(true, false); boolean valid = enumListValidator.isValid(null, createContext()); assertThat(valid, is(false)); }
@Test public void shouldFailForNullValue() { EnumListValidator enumListValidator = createAndInitializeValidator(true, false); boolean valid = enumListValidator.isValid(Collections.singletonList((String) null), createContext()); assertThat(valid, is(false)); }
@Test public void shouldAllowNullWhenFlagIsSet() { EnumListValidator enumListValidator = createAndInitializeValidator(true, true); boolean valid = enumListValidator.isValid(null, createContext()); assertThat(valid, is(true)); }
@Test public void shouldFailForEmptyString() { EnumListValidator enumListValidator = createAndInitializeValidator(true, false); boolean valid = enumListValidator.isValid(Collections.singletonList(""), createContext()); assertThat(valid, is(false)); }