Search in sources :

Example 86 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationParameterConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationParameterConstraintsAddUp() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(CharSequence.class).parameter(0).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
    Object[] parameterValues = new Object[] { "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 87 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method shouldDetermineConstraintTargetForReturnValueConstraint.

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

Example 88 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method testParameterConstraint.

@Test
public void testParameterConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(User.class).parameter(0).constraint(new NotNullDef());
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
    Object[] parameterValues = new Object[] { null };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Test(org.testng.annotations.Test)

Example 89 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.

the class ConstructorConstraintMappingTest method crossParameterConstraint.

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

Example 90 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-validator by hibernate.

the class ConstraintMappingTest method testSingleConstraint.

@Test
public void testSingleConstraint() {
    mapping.type(Marathon.class).getter("name").constraint(new NotNullDef());
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<Marathon>> violations = validator.validate(new Marathon());
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
Also used : 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

ConstraintViolation (jakarta.validation.ConstraintViolation)741 Test (org.testng.annotations.Test)621 Validator (jakarta.validation.Validator)437 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)309 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)265 Method (java.lang.reflect.Method)152 TestForIssue (org.hibernate.validator.testutil.TestForIssue)134 HibernateValidator (org.hibernate.validator.HibernateValidator)125 NotNull (jakarta.validation.constraints.NotNull)82 ConstraintValidator (jakarta.validation.ConstraintValidator)74 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)70 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)70 ValidatorFactory (jakarta.validation.ValidatorFactory)62 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)54 Test (org.junit.Test)52 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)44 ConstraintViolationException (jakarta.validation.ConstraintViolationException)42 Path (jakarta.validation.Path)35 HashMap (java.util.HashMap)29 Size (jakarta.validation.constraints.Size)28