Search in sources :

Example 11 with NotNullDef

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

the class MethodConstraintMappingTest method constraintConfiguredOnPropertyIsEvaluatedByMethodValidation.

@Test
public void constraintConfiguredOnPropertyIsEvaluatedByMethodValidation() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).getter("hello").constraint(new NotNullDef());
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.getHello();
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("getHello").returnValue()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Test(org.testng.annotations.Test)

Example 12 with NotNullDef

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

the class MultipleConstraintMappingsTest method testMultipleConstraintMappings.

@Test
@TestForIssue(jiraKey = "HV-500")
public void testMultipleConstraintMappings() {
    ConstraintMapping marathonMapping = config.createConstraintMapping();
    marathonMapping.type(Marathon.class).getter("name").constraint(new NotNullDef());
    ConstraintMapping runnerMapping = config.createConstraintMapping();
    runnerMapping.type(Runner.class).getter("name").constraint(new NotNullDef());
    config.addMapping(marathonMapping);
    config.addMapping(runnerMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(Marathon.class);
    assertTrue(beanDescriptor.isBeanConstrained(), "There should be constraints defined on the Marathon class");
    assertEquals(beanDescriptor.getConstrainedProperties().iterator().next().getPropertyName(), "name", "The property name should be constrained");
    beanDescriptor = validator.getConstraintsForClass(Runner.class);
    assertTrue(beanDescriptor.isBeanConstrained(), "There should be constraints defined on the Runner class");
    assertEquals(beanDescriptor.getConstrainedProperties().iterator().next().getPropertyName(), "name", "The property name should be constrained");
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 13 with NotNullDef

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

the class ProgrammaticContainerElementConstraintsForFieldTest method configuringSameContainerElementTwiceCausesException.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000214.*")
@TestForIssue(jiraKey = "HV-1239")
public void configuringSameContainerElementTwiceCausesException() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(FishTank.class).field("tagsOfFishOfTheMonth").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)

Example 14 with NotNullDef

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

the class ConstructorConstraintMappingTest method testCascadingConstructorParameterDefinitionWithGroupConversion.

@Test
public void testCascadingConstructorParameterDefinitionWithGroupConversion() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(User.class).parameter(0).valid().convertGroup(Default.class).to(TestGroup.class).type(User.class).field("name").constraint(new NotNullDef().message("name must not be null").groups(TestGroup.class));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
    Object[] parameterValues = new Object[] { new User(null) };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("name must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0).property("name")));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Test(org.testng.annotations.Test)

Example 15 with NotNullDef

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

the class ConstructorConstraintMappingTest method testParameterConstraint.

@Test
public void testParameterConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(User.class).parameter(0).constraint(new NotNullDef());
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
    Object[] parameterValues = new Object[] { null };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Test(org.testng.annotations.Test)

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