Search in sources :

Example 1 with NotEmptyDef

use of org.hibernate.validator.cfg.defs.NotEmptyDef in project hibernate-validator by hibernate.

the class ConstraintMappingTest method testDefaultGroupSequenceProvider.

@Test
public void testDefaultGroupSequenceProvider() {
    mapping.type(Marathon.class).defaultGroupSequenceProviderClass(MarathonDefaultGroupSequenceProvider.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 2 with NotEmptyDef

use of org.hibernate.validator.cfg.defs.NotEmptyDef in project hibernate-validator by hibernate.

the class ConstraintMappingTest method testDefaultGroupSequence.

@Test
public void testDefaultGroupSequence() {
    mapping.type(Marathon.class).defaultGroupSequence(Foo.class, Marathon.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Marathon marathon = new Marathon();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    marathon.setName("Stockholm Marathon");
    violations = validator.validate(marathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 3 with NotEmptyDef

use of org.hibernate.validator.cfg.defs.NotEmptyDef in project hibernate-validator by hibernate.

the class ConstraintMappingTest method testProgrammaticDefaultGroupSequenceAndDefaultGroupSequenceProviderDefinedOnSameClass.

@Test(expectedExceptions = GroupDefinitionException.class, expectedExceptionsMessageRegExp = "HV[0-9]*: Default group sequence and default group sequence provider cannot be defined at the same time.")
public void testProgrammaticDefaultGroupSequenceAndDefaultGroupSequenceProviderDefinedOnSameClass() {
    mapping.type(Marathon.class).defaultGroupSequence(Foo.class, Marathon.class).defaultGroupSequenceProviderClass(MarathonDefaultGroupSequenceProvider.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    validator.validate(new Marathon());
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Example 4 with NotEmptyDef

use of org.hibernate.validator.cfg.defs.NotEmptyDef in project hibernate-validator by hibernate.

the class ConstraintMappingTest method testDefaultGroupSequenceDefinedOnClassWithNoConstraints.

@Test(description = "HV-444")
public void testDefaultGroupSequenceDefinedOnClassWithNoConstraints() {
    mapping.type(Marathon.class).getter("name").constraint(new NotNullDef().groups(Foo.class)).getter("runners").constraint(new NotEmptyDef()).type(ExtendedMarathon.class).defaultGroupSequence(Foo.class, ExtendedMarathon.class);
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    ExtendedMarathon extendedMarathon = new ExtendedMarathon();
    Set<ConstraintViolation<ExtendedMarathon>> violations = validator.validate(extendedMarathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    extendedMarathon.setName("Stockholm Marathon");
    violations = validator.validate(extendedMarathon);
    assertThat(violations).containsOnlyViolations(violationOf(NotEmpty.class).withMessage("must not be empty"));
}
Also used : NotEmptyDef(org.hibernate.validator.cfg.defs.NotEmptyDef) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Aggregations

Validator (jakarta.validation.Validator)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4 NotEmptyDef (org.hibernate.validator.cfg.defs.NotEmptyDef)4 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)4 Test (org.testng.annotations.Test)4 ConstraintViolation (jakarta.validation.ConstraintViolation)3