Search in sources :

Example 26 with ConstraintViolationException

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

the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForParameterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForParameterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test1", Optional.class, Map.class).parameter(0).containerElementType().constraint(new SizeDef().max(5)).parameter(1).containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        HashMap<String, Integer> fishCountByType = new HashMap<>();
        fishCountByType.put("A", -1);
        fishCountByType.put("BB", -2);
        fishTank.test1(Optional.of("Too long"), fishCountByType);
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Min.class).withMessage("must be greater than or equal to 1"), violationOf(Min.class).withMessage("must be greater than or equal to 1"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) HashMap(java.util.HashMap) Size(jakarta.validation.constraints.Size) MinDef(org.hibernate.validator.cfg.defs.MinDef) Min(jakarta.validation.constraints.Min) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 27 with ConstraintViolationException

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

the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test3", List.class).parameter(0).containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        Set<String> bobsTags = CollectionHelper.asSet((String) null);
        Map<String, Set<String>> januaryTags = new HashMap<>();
        januaryTags.put("bob", bobsTags);
        List<Map<String, Set<String>>> tagsOfFishOfTheMonth = new ArrayList<>();
        tagsOfFishOfTheMonth.add(januaryTags);
        fishTank.test3(tagsOfFishOfTheMonth);
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 28 with ConstraintViolationException

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

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareDeeplyNestedContainerElementConstraintsForReturnValueProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test3").returnValue().containerElementType(0, 1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test3();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 29 with ConstraintViolationException

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

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementCascadesForReturnValueProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test4").returnValue().containerElementType().valid().type(Fish.class).field("name").constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test4();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 30 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException 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)

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