Search in sources :

Example 21 with ConstraintViolationException

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

the class MethodConstraintMappingTest method crossParameterConstraint.

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

Example 22 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testGenericParameterConstraint.

@Test
public void testGenericParameterConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).parameter(0).constraint(new GenericConstraintDef<>(Size.class).param("min", 1).param("max", 10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("");
        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("greet").parameter("string", 0)));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Test(org.testng.annotations.Test)

Example 23 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testReturnValueConstraint.

@Test
public void testReturnValueConstraint() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("Hello");
        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("greet").returnValue()));
    }
}
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 24 with ConstraintViolationException

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

the class MethodConstraintMappingTest method testMultipleReturnValueConstraints.

@Test
public void testMultipleReturnValueConstraints() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("greet", String.class).returnValue().constraint(new SizeDef().min(1).max(10)).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.greet("Hello");
        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("greet").returnValue()), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().method("greet").returnValue()));
    }
}
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 25 with ConstraintViolationException

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

the class MethodConstraintMappingTest method crossParameterConstraintOnMethodReturningVoid.

@Test
@TestForIssue(jiraKey = "HV-1220")
public void crossParameterConstraintOnMethodReturningVoid() {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).method("sayNothing", String.class).crossParameter().constraint(new GenericConstraintDef<GenericAndCrossParameterConstraint>(GenericAndCrossParameterConstraint.class));
    config.addMapping(mapping);
    try {
        GreetingService service = getValidatingProxy(wrappedObject, config.buildValidatorFactory().getValidator());
        service.sayNothing("");
        fail("Expected exception wasn't thrown.");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().method("sayNothing").crossParameter()));
    }
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

ConstraintViolationException (jakarta.validation.ConstraintViolationException)135 Test (org.testng.annotations.Test)64 Validator (jakarta.validation.Validator)40 ConstraintViolation (jakarta.validation.ConstraintViolation)39 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)18 Test (org.junit.Test)18 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Set (java.util.Set)10 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 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)9 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)9 ArrayList (java.util.ArrayList)8 FacesMessage (javax.faces.application.FacesMessage)8