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