Search in sources :

Example 1 with Node

use of jakarta.validation.Path.Node in project resteasy by resteasy.

the class ConstraintTypeUtilImpl method getConstraintType.

@Override
public ConstraintType.Type getConstraintType(Object o) {
    if (!(o instanceof ConstraintViolation)) {
        throw new RuntimeException(Messages.MESSAGES.unknownObjectPassedAsConstraintViolation(o));
    }
    ConstraintViolation<?> v = ConstraintViolation.class.cast(o);
    Iterator<Node> nodes = v.getPropertyPath().iterator();
    Node firstNode = nodes.next();
    switch(firstNode.getKind()) {
        case BEAN:
            return ConstraintType.Type.CLASS;
        case CONSTRUCTOR:
        case METHOD:
            Node secondNode = nodes.next();
            if (secondNode.getKind() == ElementKind.PARAMETER || secondNode.getKind() == ElementKind.CROSS_PARAMETER) {
                return ConstraintType.Type.PARAMETER;
            } else if (secondNode.getKind() == ElementKind.RETURN_VALUE) {
                return ConstraintType.Type.RETURN_VALUE;
            } else {
                throw new RuntimeException(Messages.MESSAGES.unexpectedPathNodeViolation(secondNode.getKind()));
            }
        case PROPERTY:
            return ConstraintType.Type.PROPERTY;
        case CROSS_PARAMETER:
        case PARAMETER:
        case RETURN_VALUE:
        // we shouldn't encounter these element types at the root
        case CONTAINER_ELEMENT:
        default:
            throw new RuntimeException(Messages.MESSAGES.unexpectedPathNode(firstNode.getKind()));
    }
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Node(jakarta.validation.Path.Node)

Example 2 with Node

use of jakarta.validation.Path.Node in project beanvalidation-tck by eclipse-ee4j.

the class PropertyPathTest method testPassingWrongTypeToAsOnConstructorNodeCausesClassCastException.

@Test(expectedExceptions = ClassCastException.class)
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "s")
public void testPassingWrongTypeToAsOnConstructorNodeCausesClassCastException() throws Exception {
    // given
    Constructor<MovieStudio> constructor = MovieStudio.class.getConstructor(String.class, Person.class);
    Object[] parameterValues = new Object[] { null, null };
    // when
    Set<ConstraintViolation<MovieStudio>> constraintViolations = getExecutableValidator().validateConstructorParameters(constructor, parameterValues);
    // then
    assertThat(constraintViolations).containsOnlyViolations(violationOf(NotNull.class), violationOf(NotNull.class));
    Iterator<Path.Node> nodeIter = getConstraintViolationForParameter(constraintViolations, "name").getPropertyPath().iterator();
    // parameter 0
    assertTrue(nodeIter.hasNext());
    Node nextNode = nodeIter.next();
    assertNode(nextNode, "MovieStudio", ElementKind.CONSTRUCTOR, false, null, null);
    nextNode.as(PropertyNode.class);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ReturnValueNode(jakarta.validation.Path.ReturnValueNode) MethodNode(jakarta.validation.Path.MethodNode) ContainerElementNode(jakarta.validation.Path.ContainerElementNode) CrossParameterNode(jakarta.validation.Path.CrossParameterNode) ParameterNode(jakarta.validation.Path.ParameterNode) PropertyNode(jakarta.validation.Path.PropertyNode) ConstructorNode(jakarta.validation.Path.ConstructorNode) Node(jakarta.validation.Path.Node) BeanNode(jakarta.validation.Path.BeanNode) Assert.assertNotNull(org.testng.Assert.assertNotNull) NotNull(jakarta.validation.constraints.NotNull) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Example 3 with Node

use of jakarta.validation.Path.Node in project beanvalidation-tck by eclipse-ee4j.

the class PropertyPathTest method testGetContainerClassGetTypeArgumentIndex.

@Test
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "v")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "av")
public void testGetContainerClassGetTypeArgumentIndex() {
    // container element node
    Set<ConstraintViolation<MovieProduction>> constraintViolations = getValidator().validate(MovieProduction.invalidMapKey());
    assertThat(constraintViolations).containsOnlyViolations(violationOf(NotBlank.class));
    ConstraintViolation<MovieProduction> constraintViolation = constraintViolations.iterator().next();
    Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator();
    assertTrue(nodeIter.hasNext());
    Node node = nodeIter.next();
    assertNode(node, "locationsByScene", ElementKind.PROPERTY, false, null, null);
    PropertyNode propertyNode = node.as(PropertyNode.class);
    assertNotNull(propertyNode);
    assertNull(propertyNode.getContainerClass());
    assertNull(propertyNode.getTypeArgumentIndex());
    assertTrue(nodeIter.hasNext());
    node = nodeIter.next();
    assertNode(node, "<map key>", ElementKind.CONTAINER_ELEMENT, true, null, "");
    ContainerElementNode containerElementNode = node.as(ContainerElementNode.class);
    assertNotNull(containerElementNode);
    assertEquals(containerElementNode.getContainerClass(), Map.class);
    assertEquals(containerElementNode.getTypeArgumentIndex(), Integer.valueOf(0));
    assertFalse(nodeIter.hasNext());
    // property node
    constraintViolations = getValidator().validate(MovieProduction.invalidCascading());
    assertThat(constraintViolations).containsOnlyViolations(violationOf(NotBlank.class));
    constraintViolation = constraintViolations.iterator().next();
    nodeIter = constraintViolation.getPropertyPath().iterator();
    assertTrue(nodeIter.hasNext());
    node = nodeIter.next();
    assertNode(node, "locationsByScene", ElementKind.PROPERTY, false, null, null);
    propertyNode = node.as(PropertyNode.class);
    assertNotNull(propertyNode);
    assertNull(propertyNode.getContainerClass());
    assertNull(propertyNode.getTypeArgumentIndex());
    assertTrue(nodeIter.hasNext());
    node = nodeIter.next();
    assertNode(node, "zipCode", ElementKind.PROPERTY, true, null, "Scene 1");
    propertyNode = node.as(PropertyNode.class);
    assertNotNull(propertyNode);
    assertEquals(propertyNode.getContainerClass(), Map.class);
    assertEquals(propertyNode.getTypeArgumentIndex(), Integer.valueOf(1));
    assertFalse(nodeIter.hasNext());
    // bean node
    constraintViolations = getValidator().validate(MovieProduction.invalidExecutiveProducer());
    assertThat(constraintViolations).containsOnlyViolations(violationOf(ValidExecutiveProducer.class));
    constraintViolation = constraintViolations.iterator().next();
    nodeIter = constraintViolation.getPropertyPath().iterator();
    assertTrue(nodeIter.hasNext());
    node = nodeIter.next();
    assertNode(node, "executiveProducers", ElementKind.PROPERTY, false, null, null);
    propertyNode = node.as(PropertyNode.class);
    assertNotNull(propertyNode);
    assertNull(propertyNode.getContainerClass());
    assertNull(propertyNode.getTypeArgumentIndex());
    assertTrue(nodeIter.hasNext());
    node = nodeIter.next();
    assertNode(node, null, ElementKind.BEAN, true, 0, null);
    BeanNode beanNode = node.as(BeanNode.class);
    assertNotNull(beanNode);
    assertEquals(beanNode.getContainerClass(), List.class);
    assertEquals(beanNode.getTypeArgumentIndex(), Integer.valueOf(0));
    assertFalse(nodeIter.hasNext());
}
Also used : PropertyNode(jakarta.validation.Path.PropertyNode) ConstraintViolation(jakarta.validation.ConstraintViolation) NotBlank(jakarta.validation.constraints.NotBlank) ReturnValueNode(jakarta.validation.Path.ReturnValueNode) MethodNode(jakarta.validation.Path.MethodNode) ContainerElementNode(jakarta.validation.Path.ContainerElementNode) CrossParameterNode(jakarta.validation.Path.CrossParameterNode) ParameterNode(jakarta.validation.Path.ParameterNode) PropertyNode(jakarta.validation.Path.PropertyNode) ConstructorNode(jakarta.validation.Path.ConstructorNode) Node(jakarta.validation.Path.Node) BeanNode(jakarta.validation.Path.BeanNode) BeanNode(jakarta.validation.Path.BeanNode) ContainerElementNode(jakarta.validation.Path.ContainerElementNode) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Example 4 with Node

use of jakarta.validation.Path.Node in project beanvalidation-tck by eclipse-ee4j.

the class PropertyPathTest method testPropertyPathWithConstraintViolationForRootObject.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "m"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "n"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "o"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "p"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "q"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "r"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "s"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "x"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "aj") })
public void testPropertyPathWithConstraintViolationForRootObject() {
    Set<ConstraintViolation<VerySpecialClass>> constraintViolations = getValidator().validate(new VerySpecialClass());
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Special.class));
    ConstraintViolation<VerySpecialClass> constraintViolation = constraintViolations.iterator().next();
    Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator();
    assertTrue(nodeIter.hasNext());
    Node node = nodeIter.next();
    assertNode(node, BEAN_NODE_NAME, ElementKind.BEAN, false, null, null);
    BeanNode beanNode = node.as(BeanNode.class);
    assertNotNull(beanNode);
    assertFalse(nodeIter.hasNext());
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ReturnValueNode(jakarta.validation.Path.ReturnValueNode) MethodNode(jakarta.validation.Path.MethodNode) ContainerElementNode(jakarta.validation.Path.ContainerElementNode) CrossParameterNode(jakarta.validation.Path.CrossParameterNode) ParameterNode(jakarta.validation.Path.ParameterNode) PropertyNode(jakarta.validation.Path.PropertyNode) ConstructorNode(jakarta.validation.Path.ConstructorNode) Node(jakarta.validation.Path.Node) BeanNode(jakarta.validation.Path.BeanNode) BeanNode(jakarta.validation.Path.BeanNode) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Example 5 with Node

use of jakarta.validation.Path.Node in project beanvalidation-tck by eclipse-ee4j.

the class PropertyPathTest method testPropertyPathTraversedObject.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "m"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "n"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "o"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "p"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "q"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "r"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "s"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "y"), @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ai") })
public void testPropertyPathTraversedObject() {
    Engine engine = new Engine();
    engine.setSerialNumber("ABCDEFGH1234");
    Set<ConstraintViolation<Engine>> constraintViolations = getValidator().validate(engine);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Pattern.class));
    ConstraintViolation<Engine> constraintViolation = constraintViolations.iterator().next();
    Iterator<Path.Node> nodeIter = constraintViolation.getPropertyPath().iterator();
    assertTrue(nodeIter.hasNext());
    Node node = nodeIter.next();
    assertNode(node, "serialNumber", ElementKind.PROPERTY, false, null, null);
    PropertyNode propertyNode = node.as(PropertyNode.class);
    assertNotNull(propertyNode);
    assertFalse(nodeIter.hasNext());
}
Also used : Pattern(jakarta.validation.constraints.Pattern) PropertyNode(jakarta.validation.Path.PropertyNode) ConstraintViolation(jakarta.validation.ConstraintViolation) ReturnValueNode(jakarta.validation.Path.ReturnValueNode) MethodNode(jakarta.validation.Path.MethodNode) ContainerElementNode(jakarta.validation.Path.ContainerElementNode) CrossParameterNode(jakarta.validation.Path.CrossParameterNode) ParameterNode(jakarta.validation.Path.ParameterNode) PropertyNode(jakarta.validation.Path.PropertyNode) ConstructorNode(jakarta.validation.Path.ConstructorNode) Node(jakarta.validation.Path.Node) BeanNode(jakarta.validation.Path.BeanNode) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Aggregations

Node (jakarta.validation.Path.Node)14 ConstraintViolation (jakarta.validation.ConstraintViolation)12 PropertyNode (jakarta.validation.Path.PropertyNode)12 MethodNode (jakarta.validation.Path.MethodNode)11 ParameterNode (jakarta.validation.Path.ParameterNode)11 BeanNode (jakarta.validation.Path.BeanNode)10 ConstructorNode (jakarta.validation.Path.ConstructorNode)10 ContainerElementNode (jakarta.validation.Path.ContainerElementNode)10 CrossParameterNode (jakarta.validation.Path.CrossParameterNode)10 ReturnValueNode (jakarta.validation.Path.ReturnValueNode)10 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)10 Test (org.testng.annotations.Test)10 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)6 NotNull (jakarta.validation.constraints.NotNull)5 Assert.assertNotNull (org.testng.Assert.assertNotNull)5 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)4 Method (java.lang.reflect.Method)3 Test (org.junit.jupiter.api.Test)2 MinijaxPropertyNode (org.minijax.validation.MinijaxPath.MinijaxPropertyNode)2 NotBlank (jakarta.validation.constraints.NotBlank)1