use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForParameterProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test1", Optional.class, Map.class).parameter(0).containerElementType().constraint(new SizeDef().max(5)).parameter(1).containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
HashMap<String, Integer> fishCountByType = new HashMap<>();
fishCountByType.put("A", -1);
fishCountByType.put("BB", -2);
fishTank.test1(Optional.of("Too long"), fishCountByType);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Min.class).withMessage("must be greater than or equal to 1"), violationOf(Min.class).withMessage("must be greater than or equal to 1"));
}
}
use of jakarta.validation.ConstraintViolationException in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test3", List.class).parameter(0).containerElementType(0, 1, 0).constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
Set<String> bobsTags = CollectionHelper.asSet((String) null);
Map<String, Set<String>> januaryTags = new HashMap<>();
januaryTags.put("bob", bobsTags);
List<Map<String, Set<String>>> tagsOfFishOfTheMonth = new ArrayList<>();
tagsOfFishOfTheMonth.add(januaryTags);
fishTank.test3(tagsOfFishOfTheMonth);
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 ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareDeeplyNestedContainerElementConstraintsForReturnValueProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForReturnValueProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test3").returnValue().containerElementType(0, 1, 0).constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
fishTank.test3();
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 ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementCascadesForReturnValueProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementCascadesForReturnValueProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test4").returnValue().containerElementType().valid().type(Fish.class).field("name").constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
fishTank.test4();
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 AbstractMethodValidationTest method cascadingMapReturnValue.
@Test
public void cascadingMapReturnValue() {
try {
customerRepositoryValidatingProxy.cascadingMapReturnValue();
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
Customer customer = new Customer(null);
Map<String, Customer> expectedReturnValue = newHashMap();
expectedReturnValue.put("Bob", customer);
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withPropertyPath(pathWith().method("cascadingMapReturnValue").returnValue().property("name", true, "Bob", null, Map.class, 1)).withMessage(messagePrefix() + "must not be null").withRootBeanClass(CustomerRepositoryImpl.class).withInvalidValue(null));
ConstraintViolation<?> constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(constraintViolation.getMessage(), messagePrefix() + "must not be null");
assertMethod(constraintViolation, "cascadingMapReturnValue");
assertMethodValidationType(constraintViolation, ElementKind.RETURN_VALUE);
assertEquals(constraintViolation.getPropertyPath().toString(), "cascadingMapReturnValue.<return value>[Bob].name");
assertEquals(constraintViolation.getRootBeanClass(), CustomerRepositoryImpl.class);
assertEquals(constraintViolation.getRootBean(), customerRepositoryOriginalBean);
assertEquals(constraintViolation.getLeafBean(), customer);
assertEquals(constraintViolation.getInvalidValue(), null);
assertEquals(constraintViolation.getExecutableParameters(), null);
assertEquals(constraintViolation.getExecutableReturnValue(), expectedReturnValue);
}
}
Aggregations