@Override public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) { if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final Method handlingMethod = resourceMethod.getHandlingMethod(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); final Method definitionMethod = resourceMethod.getDefinitionMethod(); if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result)); if (result instanceof Response) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity())); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "f") public void testIsReturnValueConstrainedForCascadedMethod() { MethodDescriptor descriptor = Executables.cascadedReturnValueMethod(); assertTrue( descriptor.hasConstrainedReturnValue(), "Should be constrained on return value" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "f") public void testIsReturnValueConstrainedForVoidMethod() { MethodDescriptor descriptor = Executables.crossParameterConstrainedMethod(); assertFalse( descriptor.hasConstrainedReturnValue(), "Should not be constrained on return value" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "f") public void testIsReturnValueConstrainedForConstrainedMethod() { MethodDescriptor descriptor = Executables.returnValueConstrainedMethod(); assertTrue( descriptor.hasConstrainedReturnValue(), "Should be constrained on return value" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "f") public void testIsReturnValueConstrainedForNotConstrainedMethod() { MethodDescriptor descriptor = Executables.cascadedParameterMethod(); assertFalse( descriptor.hasConstrainedReturnValue(), "Should not be constrained on return value" ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "c"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "f") }) public void testXmlMethodConfigurationApplied() throws Exception { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "listCustomers" ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertTrue( descriptor.hasConstrainedReturnValue() ); assertFalse( descriptor.hasConstrainedParameters() ); descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "findCustomer", String.class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertTrue( descriptor.hasConstrainedReturnValue() ); assertTrue( descriptor.hasConstrainedParameters() ); descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "isCustomer", String.class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertFalse( descriptor.hasConstrainedReturnValue() ); assertTrue( descriptor.hasConstrainedParameters() ); }
@Override public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) { if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final Method handlingMethod = resourceMethod.getHandlingMethod(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); final Method definitionMethod = resourceMethod.getDefinitionMethod(); if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result)); if (result instanceof Response) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity())); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } } }
@AroundInvoke public Object invoke(final InvocationContext context) throws Exception { final Method method = context.getMethod(); final Class<?> targetClass = Proxies.classFor(context.getTarget().getClass()); if (!isExecutableValidated(targetClass, method, this::computeIsMethodValidated)) { return context.proceed(); } final MethodDescriptor constraintsForMethod = validator.getConstraintsForClass(targetClass) .getConstraintsForMethod(method.getName(), method.getParameterTypes()); if (!DescriptorManager.isConstrained(constraintsForMethod)) { return context.proceed(); } initExecutableValidator(); if (constraintsForMethod.hasConstrainedParameters()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(context.getTarget(), method, context.getParameters()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } final Object result = context.proceed(); if (constraintsForMethod.hasConstrainedReturnValue()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateReturnValue(context.getTarget(), method, result); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } return result; }
@AroundInvoke public Object invoke(final InvocationContext context) throws Exception { final Method method = context.getMethod(); final Class<?> targetClass = Proxies.classFor(context.getTarget().getClass()); if (!isExecutableValidated(targetClass, method, this::computeIsMethodValidated)) { return context.proceed(); } final MethodDescriptor constraintsForMethod = validator.getConstraintsForClass(targetClass) .getConstraintsForMethod(method.getName(), method.getParameterTypes()); if (!DescriptorManager.isConstrained(constraintsForMethod)) { return context.proceed(); } initExecutableValidator(); if (constraintsForMethod.hasConstrainedParameters()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(context.getTarget(), method, context.getParameters()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } final Object result = context.proceed(); if (constraintsForMethod.hasConstrainedReturnValue()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateReturnValue(context.getTarget(), method, result); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } return result; }