Search in sources :

Example 41 with Validator

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"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test)

Example 42 with Validator

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"));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test)

Example 43 with Validator

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());
}
Also used : Path(jakarta.validation.Path) ConstraintViolation(jakarta.validation.ConstraintViolation) PropertyNode(org.hibernate.validator.path.PropertyNode) ContainerElementNode(org.hibernate.validator.path.ContainerElementNode) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 44 with Validator

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());
}
Also used : Path(jakarta.validation.Path) ConstraintViolation(jakarta.validation.ConstraintViolation) PropertyNode(org.hibernate.validator.path.PropertyNode) ContainerElementNode(org.hibernate.validator.path.ContainerElementNode) HashMap(java.util.HashMap) Map(java.util.Map) Validator(jakarta.validation.Validator) ConstraintValidator(jakarta.validation.ConstraintValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 45 with Validator

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[] {});
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(jakarta.validation.ValidatorFactory) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(jakarta.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.testng.annotations.Test)

Aggregations

Validator (jakarta.validation.Validator)699 Test (org.testng.annotations.Test)600 ConstraintViolation (jakarta.validation.ConstraintViolation)436 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)255 HibernateValidator (org.hibernate.validator.HibernateValidator)217 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)216 TestForIssue (org.hibernate.validator.testutil.TestForIssue)181 ConstraintValidator (jakarta.validation.ConstraintValidator)145 ValidatorFactory (jakarta.validation.ValidatorFactory)109 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)88 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)71 NotNull (jakarta.validation.constraints.NotNull)48 Test (org.junit.Test)45 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)44 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)43 ConstraintViolationException (jakarta.validation.ConstraintViolationException)42 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)40 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)35 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)28 Size (jakarta.validation.constraints.Size)27