Search in sources :

Example 66 with ConstraintViolationException

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

the class AbstractMethodValidationTest method methodValidationWithCascadingParameter.

@Test
public void methodValidationWithCascadingParameter() {
    Customer customer = new Customer(null, null);
    try {
        customerRepositoryValidatingProxy.persistCustomer(customer);
        fail("Expected ConstraintViolationException wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("persistCustomer").parameter("customer", 0).property("name")).withMessage(messagePrefix() + "must not be null").withRootBeanClass(CustomerRepositoryImpl.class).withInvalidValue(null));
        ConstraintViolation<?> constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(constraintViolation.getMessage(), messagePrefix() + "must not be null");
        assertMethod(constraintViolation, "persistCustomer", Customer.class);
        assertParameterIndex(constraintViolation, 0);
        assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
        assertEquals(constraintViolation.getPropertyPath().toString(), "persistCustomer.customer.name");
        assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
        assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
        assertEquals(constraintViolation.getLeafBean(), customer);
        assertEquals(constraintViolation.getInvalidValue(), null);
        assertEquals(constraintViolation.getExecutableParameters(), new Object[] { customer });
        assertEquals(constraintViolation.getExecutableReturnValue(), null);
    }
}
Also used : Customer(org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) CustomerRepositoryImpl(org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl) Test(org.testng.annotations.Test)

Example 67 with ConstraintViolationException

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

the class AbstractMethodValidationTest method cascadingIterableReturnValue.

@Test
public void cascadingIterableReturnValue() {
    try {
        customerRepositoryValidatingProxy.cascadingIterableReturnValue();
        fail("Expected ConstraintViolationException wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingIterableReturnValue").returnValue().property("name", true, null, 1, List.class, 0)).withMessage(messagePrefix() + "must not be null").withRootBeanClass(CustomerRepositoryImpl.class).withInvalidValue(null));
        ConstraintViolation<?> constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(constraintViolation.getMessage(), messagePrefix() + "must not be null");
        assertMethod(constraintViolation, "cascadingIterableReturnValue");
        assertMethodValidationType(constraintViolation, ElementKind.RETURN_VALUE);
        assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingIterableReturnValue.<return value>[1].name");
        assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
        assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
        assertEquals(constraintViolation.getLeafBean(), new Customer(null));
        assertEquals(constraintViolation.getInvalidValue(), null);
        assertEquals(constraintViolation.getExecutableParameters(), null);
        assertEquals(constraintViolation.getExecutableReturnValue(), Arrays.asList(null, new Customer(null)));
    }
}
Also used : Customer(org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) CustomerRepositoryImpl(org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl) Test(org.testng.annotations.Test)

Example 68 with ConstraintViolationException

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

the class AnnotationBasedMethodValidationTest method iterableParameterWithCascadingTypeParameter.

// TODO Move up once XML support is there for type level cascades
@Test
public void iterableParameterWithCascadingTypeParameter() {
    Customer customer = new Customer(null);
    List<Customer> customers = Arrays.asList(null, customer);
    try {
        customerRepositoryValidatingProxy.iterableParameterWithCascadingTypeParameter(customers);
        fail("Expected ConstraintViolationException wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertEquals(e.getConstraintViolations().size(), 1);
        ConstraintViolation<?> constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(constraintViolation.getMessage(), messagePrefix() + "must not be null");
        assertMethod(constraintViolation, "iterableParameterWithCascadingTypeParameter", List.class);
        assertParameterIndex(constraintViolation, 0);
        assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
        assertEquals(constraintViolation.getPropertyPath().toString(), "iterableParameterWithCascadingTypeParameter.customer[1].name");
        assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
        assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
        assertEquals(constraintViolation.getLeafBean(), customer);
        assertEquals(constraintViolation.getInvalidValue(), null);
        assertEquals(constraintViolation.getExecutableParameters(), new Object[] { customers });
        assertEquals(constraintViolation.getExecutableReturnValue(), null);
    }
}
Also used : Customer(org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) List(java.util.List) CustomerRepositoryImpl(org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl) Test(org.testng.annotations.Test)

Example 69 with ConstraintViolationException

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

the class ParameterScriptAssertValidatorTest method shouldValidateParameterScriptAssertConstraint.

@Test
public void shouldValidateParameterScriptAssertConstraint() {
    CalendarService calendar = getValidatingProxy(new CalendarServiceImpl(), getValidator());
    Date startDate = new GregorianCalendar(2009, 8, 20).getTime();
    Date endDate = new GregorianCalendar(2009, 8, 21).getTime();
    try {
        calendar.createEvent(endDate, startDate);
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(ParameterScriptAssert.class).withMessage("script expression \"start < end\" didn't evaluate to true"));
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ParameterScriptAssert(org.hibernate.validator.constraints.ParameterScriptAssert) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 70 with ConstraintViolationException

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

the class ValidationInterceptor method validateMethodInvocation.

/**
 * Validates the Bean Validation constraints specified at the parameters and/or return value of the intercepted method.
 *
 * @param ctx The context of the intercepted method invocation.
 *
 * @return The result of the method invocation.
 *
 * @throws Exception Any exception caused by the intercepted method invocation. A {@link ConstraintViolationException}
 * in case at least one constraint violation occurred either during parameter or return value validation.
 */
@AroundInvoke
public Object validateMethodInvocation(InvocationContext ctx) throws Exception {
    ExecutableValidator executableValidator = validator.forExecutables();
    Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(ctx.getTarget(), ctx.getMethod(), ctx.getParameters());
    if (!violations.isEmpty()) {
        throw new ConstraintViolationException(getMessage(ctx.getMethod(), ctx.getParameters(), violations), violations);
    }
    Object result = ctx.proceed();
    violations = executableValidator.validateReturnValue(ctx.getTarget(), ctx.getMethod(), result);
    if (!violations.isEmpty()) {
        throw new ConstraintViolationException(getMessage(ctx.getMethod(), ctx.getParameters(), violations), violations);
    }
    return result;
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) AroundInvoke(jakarta.interceptor.AroundInvoke)

Aggregations

ConstraintViolationException (jakarta.validation.ConstraintViolationException)114 Test (org.testng.annotations.Test)55 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 ConstraintViolation (jakarta.validation.ConstraintViolation)32 Validator (jakarta.validation.Validator)29 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)16 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Test (org.junit.Test)14 Session (org.hibernate.Session)10 Transaction (org.hibernate.Transaction)10 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 FacesMessage (javax.faces.application.FacesMessage)8 NotNull (jakarta.validation.constraints.NotNull)7 BigDecimal (java.math.BigDecimal)7 Set (java.util.Set)7 EntityManager (jakarta.persistence.EntityManager)6