Search in sources :

Example 86 with ConstraintViolationException

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

the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareNestedContainerElementConstraintsForReturnValueProgrammatically.

@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareNestedContainerElementConstraintsForReturnValueProgrammatically() {
    ConstraintMapping newMapping = config.createConstraintMapping();
    newMapping.type(IFishTank.class).method("test2").returnValue().containerElementType(1, 0).constraint(new NotNullDef());
    config.addMapping(newMapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test2();
        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 87 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testConstraintAtCascadedParameter.

@Test
public void testConstraintAtCascadedParameter() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", User.class).parameter(0).constraint(new NotNullDef()).valid();
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    try {
        service.greet((User) null);
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("greet").parameter("user", 0)));
    }
    try {
        service.greet(new User(null));
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().method("greet").parameter("user", 0).property("name")));
    }
}
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 88 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testCascadingMethodReturnDefinitionWithGroupConversion.

@Test
public void testCascadingMethodReturnDefinitionWithGroupConversion() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", User.class).returnValue().valid().convertGroup(Default.class).to(TestGroup.class).type(Message.class).field("message").constraint(new NotNullDef().message("message must not be null").groups(TestGroup.class));
    config.addMapping(mapping);
    GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
    try {
        service.greet(new User("foo"));
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("message must not be null").withPropertyPath(pathWith().method("greet").returnValue().property("message")));
    }
}
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 89 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testProgrammaticAndAnnotationParameterConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationParameterConstraintsAddUp() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("sayHello", String.class).parameter(0).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.sayHello("");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("sayHello").parameter("name", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("sayHello").parameter("name", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 90 with ConstraintViolationException

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

the class MethodConstraintMappingTest method shouldDetermineConstraintTargetForReturnValueConstraint.

@Test
@TestForIssue(jiraKey = "HV-769")
public void shouldDetermineConstraintTargetForReturnValueConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class, String.class).returnValue().constraint(new GenericConstraintDef<>(GenericAndCrossParameterConstraint.class));
    config.addMapping(mapping);
    Validator validator = config.buildValidatorFactory().getValidator();
    GreetingService service = getValidatingProxy(wrappedObject, validator);
    try {
        service.greet(null, null);
        fail("Expected exception wasn't thrown");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().method("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

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