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()));
}
}
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)));
}
}
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()));
}
}
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()));
}
}
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()));
}
}
Aggregations