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);
}
}
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)));
}
}
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);
}
}
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"));
}
}
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;
}
Aggregations