Search in sources :

Example 6 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project hibernate-validator by hibernate.

the class CarTest method testGroupConversionDescriptor.

@Test
public void testGroupConversionDescriptor() {
    BeanDescriptor carDescriptor = validator.getConstraintsForClass(Car.class);
    // tag::testGroupConversionDescriptor[]
    PropertyDescriptor driverDescriptor = carDescriptor.getConstraintsForProperty("driver");
    Set<GroupConversionDescriptor> groupConversions = driverDescriptor.getGroupConversions();
    assertEquals(1, groupConversions.size());
    GroupConversionDescriptor groupConversionDescriptor = groupConversions.iterator().next();
    assertEquals(Default.class, groupConversionDescriptor.getFrom());
    assertEquals(Person.Basic.class, groupConversionDescriptor.getTo());
// end::testGroupConversionDescriptor[]
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) GroupConversionDescriptor(jakarta.validation.metadata.GroupConversionDescriptor) Test(org.junit.Test)

Example 7 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project hibernate-validator by hibernate.

the class LibraryTest method testContainerElementTypeDescriptor.

@Test
public void testContainerElementTypeDescriptor() {
    BeanDescriptor libraryDescriptor = validator.getConstraintsForClass(Library.class);
    // tag::testContainerElementTypeDescriptor[]
    PropertyDescriptor booksDescriptor = libraryDescriptor.getConstraintsForProperty("books");
    Set<ContainerElementTypeDescriptor> booksContainerElementTypeDescriptors = booksDescriptor.getConstrainedContainerElementTypes();
    ContainerElementTypeDescriptor booksContainerElementTypeDescriptor = booksContainerElementTypeDescriptors.iterator().next();
    assertTrue(booksContainerElementTypeDescriptor.hasConstraints());
    assertTrue(booksContainerElementTypeDescriptor.isCascaded());
    assertEquals(0, booksContainerElementTypeDescriptor.getTypeArgumentIndex().intValue());
    assertEquals(List.class, booksContainerElementTypeDescriptor.getContainerClass());
    Set<ConstraintDescriptor<?>> constraintDescriptors = booksContainerElementTypeDescriptor.getConstraintDescriptors();
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(NotNull.class, constraintDescriptor.getAnnotation().annotationType());
// end::testContainerElementTypeDescriptor[]
}
Also used : ContainerElementTypeDescriptor(jakarta.validation.metadata.ContainerElementTypeDescriptor) PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.junit.Test)

Example 8 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project hibernate-validator by hibernate.

the class BeanDescriptorTest method testGetConstrainedPropertiesForTypeWithClassLevelConstraint.

@Test
@TestForIssue(jiraKey = "HV-660")
public void testGetConstrainedPropertiesForTypeWithClassLevelConstraint() {
    BeanDescriptor descriptor = getBeanDescriptor(ClassLevelConstrainedType.class);
    Set<PropertyDescriptor> constrainedProperties = descriptor.getConstrainedProperties();
    assertThat(constrainedProperties).isEmpty();
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) ValidatorUtil.getBeanDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 9 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project hibernate-validator by hibernate.

the class ConstraintDescriptorTest method testBasicDescriptorAttributes.

@Test
public void testBasicDescriptorAttributes() {
    BeanDescriptor beanDescriptor = getBeanDescriptor(Foo.class);
    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty("fubar");
    Set<ConstraintDescriptor<?>> constraintDescriptors = propertyDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 1);
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(constraintDescriptor.getMessageTemplate(), "bar", "Wrong message");
    Set<Class<?>> groups = newHashSet();
    groups.add(SnafuGroup.class);
    assertEquals(constraintDescriptor.getGroups(), groups, "Wrong groups");
    Set<Class<?>> payloads = newHashSet();
    payloads.add(Payload22.class);
    assertEquals(constraintDescriptor.getPayload(), payloads, "Wrong payload");
    assertNull(constraintDescriptor.getValidationAppliesTo(), "There is no validationAppliedTo attribute");
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) ValidatorUtil.getBeanDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.testng.annotations.Test)

Example 10 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project beanvalidation-tck by eclipse-ee4j.

the class FieldLevelOverridingTest method testIncludeAnnotations.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "d") })
public void testIncludeAnnotations() {
    Validator validator = TestUtil.getValidatorUnderTest();
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(User.class);
    assertNotNull(beanDescriptor);
    PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty("lastname");
    assertNotNull(propDescriptor);
    Set<ConstraintDescriptor<?>> constraintDescriptors = propDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 2, "There should be two constraints");
    boolean foundNotNullConstraint = false;
    boolean foundPatternConstraint = false;
    for (ConstraintDescriptor<?> descriptor : constraintDescriptors) {
        if (descriptor.getAnnotation() instanceof NotNull) {
            foundNotNullConstraint = true;
        } else if (descriptor.getAnnotation() instanceof Pattern) {
            foundPatternConstraint = true;
        } else {
            fail("Invalid constraint for property.");
        }
    }
    if (!(foundNotNullConstraint && foundPatternConstraint)) {
        fail("Not all configured constraints discovered.");
    }
}
Also used : Pattern(jakarta.validation.constraints.Pattern) PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Assert.assertNotNull(org.testng.Assert.assertNotNull) NotNull(jakarta.validation.constraints.NotNull) Validator(jakarta.validation.Validator) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Aggregations

PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)66 Test (org.testng.annotations.Test)55 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)46 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)39 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)31 Validator (jakarta.validation.Validator)22 TestUtil.getPropertyDescriptor (org.hibernate.beanvalidation.tck.util.TestUtil.getPropertyDescriptor)15 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)15 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)12 GroupConversionDescriptor (jakarta.validation.metadata.GroupConversionDescriptor)12 ContainerElementTypeDescriptor (jakarta.validation.metadata.ContainerElementTypeDescriptor)8 NotNull (jakarta.validation.constraints.NotNull)6 Test (org.junit.Test)5 Annotation (java.lang.annotation.Annotation)4 Assert.assertNotNull (org.testng.Assert.assertNotNull)4 ConstraintValidator (jakarta.validation.ConstraintValidator)3 Default (jakarta.validation.groups.Default)3 MinijaxPropertyDescriptor (org.minijax.validation.metadata.MinijaxPropertyDescriptor)3 ConstraintViolation (jakarta.validation.ConstraintViolation)2 DecimalMin (jakarta.validation.constraints.DecimalMin)2