EligibleEmailValidator implements ConstraintValidator<EligibleEmail, String> { @Override public boolean isValid(String email, ConstraintValidatorContext context) { email = email.toLowerCase(); final String extractedDomain = extractDomain(email); if (extractedDomain != null && extractedDomainIsEligible(extractedDomain) && doesNotMatchBlacklistPatterns(email)) { return true; } context.buildConstraintViolationWithTemplate("eligible").addConstraintViolation(); return false; }  @Override void initialize(EligibleEmail constraintAnnotation); @Override boolean isValid(String email, ConstraintValidatorContext context);  }
@Test public void isValid_shouldReturnTrueOnValidEmailAddress() { assertTrue(eligibleEmailValidator.isValid("test@asideas.de", ValidatorTestUtil.constraintValidatorContext())); }
@Test public void isValid_shouldReturnFalseOnNotWhitelistedEmailAddress() { assertFalse(eligibleEmailValidator.isValid("test@someHost.de", ValidatorTestUtil.constraintValidatorContext())); }
@Test public void isValid_shouldReturnFalseOnBlacklistedAddressPattern() { assertFalse(eligibleEmailValidator.isValid("test_extern@asideas.de", ValidatorTestUtil.constraintValidatorContext())); }
@Test public void isValid_shouldReturnTrueForAnyMailAddressOn_NonExistingList() { Whitebox.setInternalState(eligibleEmailValidator, "allowedEmailDomains", null); assertTrue(eligibleEmailValidator.isValid("test@someHost.de", ValidatorTestUtil.constraintValidatorContext())); }