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()));
}
}
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);
}
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());
}
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());
}
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());
}
Aggregations