Search in sources :

Example 1 with Customer

use of org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer in project hibernate-validator by hibernate.

the class AbstractConstructorValidationTest method cascadedConstructorParameterValidationYieldsConstraintViolation.

@Test
public void cascadedConstructorParameterValidationYieldsConstraintViolation() throws Exception {
    Set<ConstraintViolation<CustomerRepositoryImpl>> violations = executableValidator.validateConstructorParameters(CustomerRepositoryImpl.class.getConstructor(Customer.class), new Customer[] { new Customer(null) });
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage(messagePrefix() + "must not be null").withInvalidValue(null).withRootBeanClass(CustomerRepositoryImpl.class).withPropertyPath(pathWith().constructor(CustomerRepositoryImpl.class).parameter("customer", 0).property("name")));
}
Also used : Customer(org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer) ConstraintViolation(jakarta.validation.ConstraintViolation) CustomerRepositoryImpl(org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl) NotNull(jakarta.validation.constraints.NotNull) Test(org.testng.annotations.Test)

Example 2 with Customer

use of org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer in project hibernate-validator by hibernate.

the class AbstractMethodValidationTest method cascadingMapReturnValue.

@Test
public void cascadingMapReturnValue() {
    try {
        customerRepositoryValidatingProxy.cascadingMapReturnValue();
        fail("Expected ConstraintViolationException wasn't thrown.");
    } catch (ConstraintViolationException e) {
        Customer customer = new Customer(null);
        Map<String, Customer> expectedReturnValue = newHashMap();
        expectedReturnValue.put("Bob", customer);
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingMapReturnValue").returnValue().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, "cascadingMapReturnValue");
        assertMethodValidationType(constraintViolation, ElementKind.RETURN_VALUE);
        assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingMapReturnValue.<return value>[Bob].name");
        assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
        assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
        assertEquals(constraintViolation.getLeafBean(), customer);
        assertEquals(constraintViolation.getInvalidValue(), null);
        assertEquals(constraintViolation.getExecutableParameters(), null);
        assertEquals(constraintViolation.getExecutableReturnValue(), expectedReturnValue);
    }
}
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 3 with Customer

use of org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer in project hibernate-validator by hibernate.

the class AbstractMethodValidationTest method cascadingArrayParameter.

// HV-1428 Container element support is disabled for arrays
@Test(enabled = false)
public void cascadingArrayParameter() {
    Customer customer = new Customer(null);
    try {
        customerRepositoryValidatingProxy.cascadingArrayParameter(null, customer);
        fail("Expected ConstraintViolationException wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingArrayParameter").parameter("customer", 0).property("name", true, null, 1, Object[].class, null)).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, "cascadingArrayParameter", Customer[].class);
        assertParameterIndex(constraintViolation, 0);
        assertMethodValidationType(constraintViolation, ElementKind.PARAMETER);
        assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingArrayParameter.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[] { new Object[] { null, 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 4 with Customer

use of org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer in project hibernate-validator by hibernate.

the class CustomerRepositoryImpl method cascadingMapReturnValue.

@Override
public Map<String, Customer> cascadingMapReturnValue() {
    Map<String, Customer> theValue = newHashMap();
    theValue.put("Bob", new Customer(null));
    return theValue;
}
Also used : Customer(org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)

Example 5 with Customer

use of org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer 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)

Aggregations

Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)11 ConstraintViolation (jakarta.validation.ConstraintViolation)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 Test (org.testng.annotations.Test)10 ConstraintViolationException (jakarta.validation.ConstraintViolationException)9 List (java.util.List)2 Map (java.util.Map)2 CollectionHelper.newHashMap (org.hibernate.validator.internal.util.CollectionHelper.newHashMap)2 NotNull (jakarta.validation.constraints.NotNull)1 Address (org.hibernate.validator.test.internal.engine.methodvalidation.model.Address)1