use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class SequenceOfSequencesTest method groupSequenceOfGroupSequences.
/**
* A sequence of sequences is passed to the validate() call.
*/
@Test
public void groupSequenceOfGroupSequences() {
Validator validator = ValidatorUtil.getValidator();
PlushAlligator alligator = new PlushAlligator();
Set<ConstraintViolation<PlushAlligator>> violations = validator.validate(alligator, AllConstraints.class);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withProperty("name"));
alligator.name = "Ruben";
violations = validator.validate(alligator, AllConstraints.class);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withProperty("highestEducationalDegree"));
alligator.highestEducationalDegree = "PhD";
violations = validator.validate(alligator, AllConstraints.class);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withProperty("length"));
alligator.length = 540;
violations = validator.validate(alligator, AllConstraints.class);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withProperty("age"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class GroupInheritanceTest method testGroupInheritanceWithinGroupSequence.
/**
* HV-288, HV-1057.
*/
@Test
public void testGroupInheritanceWithinGroupSequence() {
Validator validator = ValidatorUtil.getValidator();
Try tryMe = new Try();
tryMe.field3 = "bar";
Set<ConstraintViolation<Try>> violations = validator.validate(tryMe, Try.GlobalCheck.class);
assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withProperty("field1").withMessage("field1"), violationOf(NotNull.class).withProperty("field2").withMessage("field2"));
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class NodeImplTest method testContainerElementNodeGetValueForMap.
@Test
@TestForIssue(jiraKey = "HV-1419")
public void testContainerElementNodeGetValueForMap() {
Validator validator = ValidatorUtil.getConfiguration().addValueExtractor(new CustomContainerValueExtractor()).buildValidatorFactory().getValidator();
BuildingWithContainerElementConstraints building = new BuildingWithContainerElementConstraints();
building.managers.put("main", "Ron");
building.managers.put("stand-in", "Ronnie");
Set<ConstraintViolation<BuildingWithContainerElementConstraints>> constraintViolations = validator.validate(building);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().property("managers").containerElement("<map value>", true, "main", null, Map.class, 1)));
Path path = constraintViolations.iterator().next().getPropertyPath();
Iterator<Path.Node> nodeIterator = path.iterator();
Path.Node node = nodeIterator.next();
assertEquals(node.getKind(), ElementKind.PROPERTY, "unexpected node kind");
node = nodeIterator.next();
assertEquals(node.getKind(), ElementKind.CONTAINER_ELEMENT, "unexpected node kind");
assertEquals(node.as(ContainerElementNode.class).getValue(), "Ron");
assertFalse(nodeIterator.hasNext());
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class NodeImplTest method testContainerElementNodeGetValueForNestedContainer.
@Test
@TestForIssue(jiraKey = "HV-1419")
public void testContainerElementNodeGetValueForNestedContainer() {
Validator validator = ValidatorUtil.getConfiguration().addValueExtractor(new CustomContainerValueExtractor()).buildValidatorFactory().getValidator();
BuildingWithContainerElementConstraints building = new BuildingWithContainerElementConstraints();
List<String> floor1Inhabitants = Arrays.asList("Pa", "Paul");
building.inhabitantsPerFloor.put(1, floor1Inhabitants);
Set<ConstraintViolation<BuildingWithContainerElementConstraints>> constraintViolations = validator.validate(building);
assertThat(constraintViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().property("inhabitantsPerFloor").containerElement("<map value>", true, 1, null, Map.class, 1).containerElement("<list element>", true, null, 0, List.class, 0)));
Path path = constraintViolations.iterator().next().getPropertyPath();
Iterator<Path.Node> nodeIterator = path.iterator();
Path.Node node = nodeIterator.next();
assertEquals(node.getKind(), ElementKind.PROPERTY, "unexpected node kind");
node = nodeIterator.next();
assertEquals(node.getKind(), ElementKind.CONTAINER_ELEMENT, "unexpected node kind");
assertEquals(node.as(ContainerElementNode.class).getValue(), floor1Inhabitants);
node = nodeIterator.next();
assertEquals(node.getKind(), ElementKind.CONTAINER_ELEMENT, "unexpected node kind");
assertEquals(node.as(ContainerElementNode.class).getValue(), "Pa");
assertFalse(nodeIterator.hasNext());
}
use of jakarta.validation.Validator in project hibernate-validator by hibernate.
the class RelaxedMethodParameterConstraintsTest method disallowValidAddedInSubType.
@Test(expectedExceptions = ConstraintDeclarationException.class, expectedExceptionsMessageRegExp = "HV000131.*")
public void disallowValidAddedInSubType() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
validator.forExecutables().validateParameters(new SubRealizationWithValidConstraintOnMethodParameter(), SubRealizationWithValidConstraintOnMethodParameter.class.getDeclaredMethods()[0], new Object[] {});
}
Aggregations