Search in sources :

Example 6 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareNestedContainerElementConstraintsForParameterProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareNestedContainerElementConstraintsForParameterProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test2", Map.class).parameter(0).containerElementType(1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        Map<String, List<Fish>> fishOfTheMonth = new HashMap<>();
        List<Fish> january = Arrays.asList(null, new Fish());
        fishOfTheMonth.put("january", january);
        fishTank.test2(fishOfTheMonth);
        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) HashMap(java.util.HashMap) 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 7 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef 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 8 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef 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 9 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef 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 10 with NotNullDef

use of org.hibernate.validator.cfg.defs.NotNullDef in project hibernate-validator by hibernate.

the class ProgrammaticContainerElementConstraintsForReturnValueTest method configuringSameContainerElementTwiceCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000214.*")
@TestForIssue(jiraKey = "HV-1239")
public void configuringSameContainerElementTwiceCausesException() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).method("test3").returnValue().containerElementType(0, 1, 0).constraint(new NotNullDef()).containerElementType(0, 1, 0);
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)47 Test (org.testng.annotations.Test)42 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)37 HibernateValidator (org.hibernate.validator.HibernateValidator)31 Validator (jakarta.validation.Validator)28 TestForIssue (org.hibernate.validator.testutil.TestForIssue)22 ConstraintViolation (jakarta.validation.ConstraintViolation)19 ConstraintViolationException (jakarta.validation.ConstraintViolationException)10 NotNull (jakarta.validation.constraints.NotNull)9 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 NotEmptyDef (org.hibernate.validator.cfg.defs.NotEmptyDef)4 Test (org.junit.Test)4 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)3 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 MinDef (org.hibernate.validator.cfg.defs.MinDef)2