Search in sources :

Example 6 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method testMultipleReturnValueConstraints.

@Test
public void testMultipleReturnValueConstraints() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class).returnValue().constraint(new GenericConstraintDef<>(ValidGreetingService.class).message("invalid 1")).constraint(new GenericConstraintDef<>(ValidGreetingService.class).message("invalid 2"));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class);
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService(""));
    assertThat(violations).containsOnlyViolations(violationOf(ValidGreetingService.class).withMessage("invalid 1").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()), violationOf(ValidGreetingService.class).withMessage("invalid 2").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Example 7 with GenericConstraintDef

use of org.hibernate.validator.cfg.GenericConstraintDef in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method testReturnValueConstraint.

@Test
public void testReturnValueConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class).returnValue().constraint(new GenericConstraintDef<>(ValidGreetingService.class).message("invalid"));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class);
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService(""));
    assertThat(violations).containsOnlyViolations(violationOf(ValidGreetingService.class).withMessage("invalid").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
Also used : GenericConstraintDef(org.hibernate.validator.cfg.GenericConstraintDef) ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Aggregations

GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)7 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)5 Test (org.testng.annotations.Test)5 ConstraintViolation (jakarta.validation.ConstraintViolation)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)3 HibernateValidator (org.hibernate.validator.HibernateValidator)3 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)2 Test (org.junit.Test)2 Validator (jakarta.validation.Validator)1 MaxDef (org.hibernate.validator.cfg.defs.MaxDef)1 MinDef (org.hibernate.validator.cfg.defs.MinDef)1 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)1