Search in sources :

Example 61 with ConstraintViolationException

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);
    }
}
Also used : Address(org.hibernate.validator.test.internal.engine.methodvalidation.model.Address) 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 62 with ConstraintViolationException

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);
    }
}
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 63 with ConstraintViolationException

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);
    }
}
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) Map(java.util.Map) CollectionHelper.newHashMap(org.hibernate.validator.internal.util.CollectionHelper.newHashMap) Test(org.testng.annotations.Test)

Example 64 with ConstraintViolationException

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");
    }
}
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 65 with ConstraintViolationException

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);
    }
}
Also used : ConsistentDateParameters(org.hibernate.validator.test.internal.engine.methodvalidation.service.ConsistentDateParameters) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) CustomerRepositoryImpl(org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

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