Search in sources :

Example 21 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForParameterTest method configuringConstraintsOnGenericTypeArgumentOfListThrowsException.

@Test(expectedExceptions = UnexpectedTypeException.class, expectedExceptionsMessageRegExp = "HV000030:.*")
@TestForIssue(jiraKey = "HV-1279")
public void configuringConstraintsOnGenericTypeArgumentOfListThrowsException() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test8", List.class).parameter(0).containerElementType(0).constraint(new SizeDef().max(5));
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    fishTank.test8(Arrays.asList("Too long"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ArrayList(java.util.ArrayList) List(java.util.List) 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 22 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue 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 23 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue 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 24 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class UniqueElementsDefTest method testUniqueElementsDef.

@Test
@TestForIssue(jiraKey = "HV-1466")
public void testUniqueElementsDef() {
    final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
    final ConstraintMapping programmaticMapping = configuration.createConstraintMapping();
    programmaticMapping.type(Library.class).field("books").constraint(new UniqueElementsDef());
    configuration.addMapping(programmaticMapping);
    Validator validator = configuration.buildValidatorFactory().getValidator();
    Set<ConstraintViolation<Library>> violations = validator.validate(new Library(Arrays.asList("A Prayer for Owen Meany", "The Cider House Rules", "The Cider House Rules")));
    assertThat(violations).containsOnlyViolations(violationOf(UniqueElements.class).withProperty("books"));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolation(jakarta.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) UniqueElementsDef(org.hibernate.validator.cfg.defs.UniqueElementsDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 25 with TestForIssue

use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.

the class ConstraintValidatorContextTest method testAddParameterNodeUsingCustomParameterNameProvider.

@Test
@TestForIssue(jiraKey = "HV-709")
public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception {
    ExecutableValidator executableValidator = ValidatorUtil.getConfiguration().parameterNameProvider(new PrefixableParameterNameProvider("param")).buildValidatorFactory().getValidator().forExecutables();
    Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters(new User(), User.class.getMethod("setAddresses", Map.class), new java.lang.Object[] { new HashMap<>() });
    assertThat(constraintViolations).containsOnlyPaths(pathWith().method("setAddresses").parameter("param0", 0), pathWith().method("setAddresses").parameter("param0", 0).bean(), pathWith().method("setAddresses").parameter("param0", 0).property("myNode1", true, null, 23).bean());
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) PrefixableParameterNameProvider(org.hibernate.validator.testutil.PrefixableParameterNameProvider) ConstraintViolation(jakarta.validation.ConstraintViolation) HashMap(java.util.HashMap) Map(java.util.Map) CollectionHelper.newHashMap(org.hibernate.validator.internal.util.CollectionHelper.newHashMap) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

TestForIssue (org.hibernate.validator.testutil.TestForIssue)286 Test (org.testng.annotations.Test)286 Validator (jakarta.validation.Validator)180 ConstraintViolation (jakarta.validation.ConstraintViolation)134 HibernateValidator (org.hibernate.validator.HibernateValidator)80 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)80 ConstraintValidator (jakarta.validation.ConstraintValidator)64 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)55 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)33 ValidatorFactory (jakarta.validation.ValidatorFactory)29 ConstraintViolationException (jakarta.validation.ConstraintViolationException)26 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)22 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)21 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)21 NotNull (jakarta.validation.constraints.NotNull)17 Method (java.lang.reflect.Method)17 Size (jakarta.validation.constraints.Size)15 CountValidationCallsValidator (org.hibernate.validator.testutil.CountValidationCallsValidator)13 File (java.io.File)12 HibernateConstraintViolation (org.hibernate.validator.engine.HibernateConstraintViolation)12