nclazz-mail-relay/src/test/java/de/nclazz/service/mailrelay/adapter/web/AccountFormValidationTest.java

88 lines
2.5 KiB
Java

package de.nclazz.service.mailrelay.adapter.web;
import org.hibernate.validator.HibernateValidator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class AccountFormValidationTest {
private LocalValidatorFactoryBean localValidatorFactory;
@BeforeEach
public void setup() {
localValidatorFactory = new LocalValidatorFactoryBean();
localValidatorFactory.setProviderClass(HibernateValidator.class);
localValidatorFactory.afterPropertiesSet();
}
@Test
void nameAsEmptyStringShouldBeNotValid() {
AccountForm form = new AccountForm("", List.of());
assertThat(localValidatorFactory.validate(form))
.isNotEmpty();
}
@Test
void nameIsMissingShouldBeNotValid() {
AccountForm form = new AccountForm(null, List.of());
assertThat(localValidatorFactory.validate(form))
.isNotEmpty();
}
@Test
void nameWithSingleCharacterShouldBeValid() {
AccountForm form = new AccountForm("a", List.of());
assertThat(localValidatorFactory.validate(form))
.hasSize(0);
}
@Test
void nameContainingNonAlphaNumericCharsShouldBeNotValid() {
AccountForm form = new AccountForm("a_.+a", List.of());
assertThat(localValidatorFactory.validate(form))
.hasSize(1);
}
@Test
void nameStartingWithHyphenShouldBeNotValid() {
AccountForm form = new AccountForm("-name", List.of());
assertThat(localValidatorFactory.validate(form))
.hasSize(1);
}
@Test
void nameContainingWithHyphenShouldBeValid() {
AccountForm form = new AccountForm("prefix-name", List.of());
assertThat(localValidatorFactory.validate(form))
.hasSize(0);
}
@Test
void receiversWithEmailAddressesShouldBeValid() {
AccountForm form = new AccountForm("legal-name", List.of("vip@company.com", "mail@example.com"));
assertThat(localValidatorFactory.validate(form))
.hasSize(0);
}
@Test
void receiversWithInvalidEmailAddressesShouldBeVNotalid() {
AccountForm form = new AccountForm("legal-name", List.of("vip @ company.com", "--mail@example.com"));
assertThat(localValidatorFactory.validate(form))
.hasSize(1);
}
}