Search in sources :

Example 81 with ConstraintViolation

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

the class GroupTest method driveAway.

@Test
public void driveAway() {
    // tag::driveAway[]
    // create a car and check that everything is ok with it.
    Car car = new Car("Morris", "DD-AB-123", 2);
    Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
    assertEquals(0, constraintViolations.size());
    // but has it passed the vehicle inspection?
    constraintViolations = validator.validate(car, CarChecks.class);
    assertEquals(1, constraintViolations.size());
    assertEquals("The car has to pass the vehicle inspection first", constraintViolations.iterator().next().getMessage());
    // let's go to the vehicle inspection
    car.setPassedVehicleInspection(true);
    assertEquals(0, validator.validate(car, CarChecks.class).size());
    // now let's add a driver. He is 18, but has not passed the driving test yet
    Driver john = new Driver("John Doe");
    john.setAge(18);
    car.setDriver(john);
    constraintViolations = validator.validate(car, DriverChecks.class);
    assertEquals(1, constraintViolations.size());
    assertEquals("You first have to pass the driving test", constraintViolations.iterator().next().getMessage());
    // ok, John passes the test
    john.passedDrivingTest(true);
    assertEquals(0, validator.validate(car, DriverChecks.class).size());
    // just checking that everything is in order now
    assertEquals(0, validator.validate(car, Default.class, CarChecks.class, DriverChecks.class).size());
// end::driveAway[]
}
Also used : RaceCarChecks(org.hibernate.validator.referenceguide.chapter05.groupinheritance.RaceCarChecks) SuperCar(org.hibernate.validator.referenceguide.chapter05.groupinheritance.SuperCar) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.junit.Test)

Example 82 with ConstraintViolation

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

the class CarTest method returnValueConstraintsAddUp.

@Test
public void returnValueConstraintsAddUp() throws Exception {
    Car object = new Car();
    Method method = Car.class.getMethod("getPassengers");
    Object returnValue = Collections.<Person>emptyList();
    Set<ConstraintViolation<Car>> violations = executableValidator.validateReturnValue(object, method, returnValue);
    assertEquals(1, violations.size());
    assertEquals(Size.class, violations.iterator().next().getConstraintDescriptor().getAnnotation().annotationType());
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 83 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testCascadingConstructorParameterDefinitionWithGroupConversion.

@Test
public void testCascadingConstructorParameterDefinitionWithGroupConversion() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(User.class).parameter(0).valid().convertGroup(Default.class).to(TestGroup.class).type(User.class).field("name").constraint(new NotNullDef().message("name must not be null").groups(TestGroup.class));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
    Object[] parameterValues = new Object[] { new User(null) };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("name must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0).property("name")));
}
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 84 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testGenericParameterConstraint.

@Test
public void testGenericParameterConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class).parameter(0).constraint(new GenericConstraintDef<>(Size.class).param("min", 1).param("max", 10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.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)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Example 85 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.

@Test
public void testMultipleParameterConstraintsAtDifferentParameters() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class, String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).parameter(1).constraint(new SizeDef().min(1).max(10));
    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(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 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("anotherMessage", 1)));
}
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)

Aggregations

ConstraintViolation (jakarta.validation.ConstraintViolation)735 Test (org.testng.annotations.Test)618 Validator (jakarta.validation.Validator)434 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)59 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