use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class AbstractMethodValidationTest method methodValidationWithCascadingParameterAndCascadingConstraint.
@Test
public void methodValidationWithCascadingParameterAndCascadingConstraint() {
Address address = new Address(null);
Customer customer = new Customer("Bob", address);
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("address").property("city")).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.address.city");
assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
assertEquals(constraintViolation.getLeafBean(), address);
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 cascadingIterableParameter.
@Test
public void cascadingIterableParameter() {
Customer customer = new Customer(null);
List<Customer> customers = Arrays.asList(null, customer);
try {
customerRepositoryValidatingProxy.cascadingIterableParameter(customers);
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingIterableParameter").parameter("customer", 0).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, "cascadingIterableParameter", List.class);
assertParameterIndex(constraintViolation, 0);
assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingIterableParameter.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 AbstractMethodValidationTest method cascadingMapParameter.
@Test
public void cascadingMapParameter() {
Map<String, Customer> customers = newHashMap();
Customer bob = new Customer(null);
customers.put("Bob", bob);
try {
customerRepositoryValidatingProxy.cascadingMapParameter(customers);
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingMapParameter").parameter("customer", 0).property("name", true, "Bob", null, Map.class, 1)).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, "cascadingMapParameter", Map.class);
assertParameterIndex(constraintViolation, 0);
assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingMapParameter.customer[Bob].name");
assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
assertEquals(constraintViolation.getLeafBean(), bob);
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 AbstractMethodValidationTest method validFromOverriddenMethodIsEvaluated.
@Test
public void validFromOverriddenMethodIsEvaluated() {
try {
customerRepositoryValidatingProxy.bar(new Customer(null, null));
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("bar").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, "bar", Customer.class);
assertParameterIndex(constraintViolation, 0);
assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
assertEquals(constraintViolation.getPropertyPath().toString(), "bar.customer.name");
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class AbstractMethodValidationTest method validationOfCrossParameterConstraint.
@Test
public void validationOfCrossParameterConstraint() {
// given
LocalDate startDate = LocalDate.of(2012, 11, 5);
LocalDate endDate = LocalDate.of(2012, 11, 4);
try {
// when
customerRepositoryValidatingProxy.methodWithCrossParameterConstraint(startDate, endDate);
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
// then
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(ConsistentDateParameters.class).withPropertyPath(pathWith().method("methodWithCrossParameterConstraint").crossParameter()).withMessage(messagePrefix() + "{ConsistentDateParameters.message}").withRootBeanClass(CustomerRepositoryImpl.class));
ConstraintViolation<?> constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(constraintViolation.getConstraintDescriptor().getAnnotation().annotationType(), ConsistentDateParameters.class);
assertEquals(constraintViolation.getInvalidValue(), new Object[] { startDate, endDate });
assertEquals(constraintViolation.getLeafBean(), customerRepositoryOriginalBean);
assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
assertEquals(constraintViolation.getExecutableParameters(), new Object[] { startDate, endDate });
assertEquals(constraintViolation.getExecutableReturnValue(), null);
assertMethod(constraintViolation, "methodWithCrossParameterConstraint", LocalDate.class, LocalDate.class);
}
}
Aggregations