result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups); if (!result.isEmpty()) { throw new ConstraintViolationException(result);
result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups); if (!result.isEmpty()) { throw new ConstraintViolationException(result);
@Override public void validateResult(Object resource, Invocable invocable, Object returnValue) throws ConstraintViolationException { // If the Validated annotation is on a method, then validate the response with // the specified constraint group. final Class<?>[] groups; if (invocable.getHandlingMethod().isAnnotationPresent(Validated.class)) { groups = invocable.getHandlingMethod().getAnnotation(Validated.class).value(); } else { groups = new Class<?>[]{Default.class}; } final Set<ConstraintViolation<Object>> violations = forExecutables().validateReturnValue(resource, invocable.getHandlingMethod(), returnValue, groups); if (!violations.isEmpty()) { LOGGER.trace("Response validation failed: {}", ConstraintViolations.copyOf(violations)); throw new JerseyViolationException(violations, invocable); } }
@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); } } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Set<ConstraintViolation<Object>> constraintViolations = validator.forExecutables().validateParameters( wrapped, method, args == null ? EMPTY_ARGS : args, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } Object result = method.invoke( wrapped, args ); constraintViolations = validator.forExecutables().validateReturnValue( wrapped, method, result, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } return result; } }
violations = executableValidator.validateReturnValue( ctx.getTarget(), ctx.getMethod(),
@Override public void validateReturnValue(HttpRequest request, Object object, Method method, Object returnValue, Class<?>... groups) { Validator validator = getValidator(request); SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object); Set<ConstraintViolation<Object>> cvs = null; try { cvs = validator.forExecutables().validateReturnValue(object, method, returnValue, groups); } catch (Exception e) { violationsContainer.setException(e); throw toValidationException(e, violationsContainer); } violationsContainer.addViolations(cvs); if (violationsContainer.size() > 0) { throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes()); } }
private void validateReturnValue(final Object obj, final Method method, final Object value, final Class<?>[] groups) { final Set<ConstraintViolation<Object>> violations = methodValidator.validateReturnValue(obj, method, value, groups); if (!violations.isEmpty()) { final String message = "Invalid value returned by '" + method + "' was " + value; throw new ConstraintViolationException(message, violations); } } }
private void validateReturnValue(MethodInvocation invocation, ExecutableValidator executableValidator, Object returnValue) { Set<ConstraintViolation<Object>> constraintViolations = executableValidator.validateReturnValue( invocation.getThis(), invocation.getMethod(), returnValue /*, groups*/ ); if (!constraintViolations.isEmpty()) { throw new VerboseConstraintViolationException(constraintViolations); } } }
public< T > void validateReturnValue(final T instance, final Method method, final Object returnValue) { final ExecutableValidator methodValidator = getExecutableValidator(); final Set<ConstraintViolation< T > > violations = methodValidator.validateReturnValue(instance, method, returnValue); if (!violations.isEmpty()) { throw new ResponseConstraintViolationException(violations); } }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "f") public void testNullPassedForObjectCausesException() throws Exception { Object object = null; Method method = Customer.class.getMethod( "getAddress" ); Object returnValue = null; getExecutableValidator().validateReturnValue( object, method, returnValue ); }
public< T > void validateReturnValue(final T instance, final Method method, final Object returnValue) { final ExecutableValidator methodValidator = getExecutableValidator(); final Set<ConstraintViolation< T > > violations = methodValidator.validateReturnValue(instance, method, returnValue); if (!violations.isEmpty()) { throw new ResponseConstraintViolationException(violations); } }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnMethodReturnValue() throws Exception { Object object = new UserWithGroupConversionButWithoutValidAnnotationOnMethodReturnValue(); Method method = UserWithGroupConversionButWithoutValidAnnotationOnMethodReturnValue.class.getMethod( "retrieveAddresses" ); Object returnValue = null; getExecutableValidator().validateReturnValue( object, method, returnValue ); }
@Test(expectedExceptions = ValidationException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "d") public void testUnexpectedType() throws Exception { String methodName = "getValue"; Object object = new Email(); Method method = Email.class.getMethod( methodName ); Object returnValue = "S"; getExecutableValidator().validateReturnValue( object, method, returnValue ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "f") public void testNullPassedForMethodCausesException() throws Exception { Object object = new Customer(); Method method = null; Object returnValue = null; getExecutableValidator().validateReturnValue( object, method, returnValue ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "j") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "k") public void testGroupConversionGivenOnReturnValueInParallelInterfaces() throws Exception { Object object = new ImplementationOfParallelInterfacesWithGroupConversionOnReturnValue(); Method method = ImplementationOfParallelInterfacesWithGroupConversionOnReturnValue.class.getMethod( "getUsers" ); Object returnValue = null; getExecutableValidator().validateReturnValue( object, method, returnValue ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "j") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "k") public void testGroupConversionGivenOnReturnValueInImplementedInterfaceWithSuperClass() throws Exception { Object object = new ImplementationOfInterfaceWithGroupConversionOnReturnValueAndSuperClass(); Method method = ImplementationOfInterfaceWithGroupConversionOnReturnValueAndSuperClass.class .getMethod( "getUser" ); Object returnValue = null; getExecutableValidator().validateReturnValue( object, method, returnValue ); } }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "d") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "e") public void testReturnValueIsMarkedAsCascadedInInterfaceAndImplementationCausesException() throws Exception { Object object = new OrderServiceImplementation(); Method method = getPlaceOrderMethod( object ); Object returnValue = new Order(); getExecutableValidator().validateReturnValue( object, method, returnValue ); fail( "A method must not mark the return value as cascaded if the implemented interface method is cascaded, too. Expected exception wasn't thrown." ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "d") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "e") public void testReturnValueIsMarkedAsCascadedInSuperAndDerivedInterfaceCausesException() throws Exception { Object object = new ExtendedOrderServiceImplementation(); Method method = getPlaceOrderMethod( object ); Object returnValue = new Order(); getExecutableValidator().validateReturnValue( object, method, returnValue ); fail( "An interface method must not mark the return value as cascaded if the overridden superinterface method is cascaded, too. Expected exception wasn't thrown." ); }
@Test public void testPropertyPathCorrect() throws Exception { Set<ConstraintViolation<Foo>> violations = TestUtil.getValidatorUnderTest().forExecutables() .validateReturnValue( new Foo( null ), Foo.class.getDeclaredMethod( "bar" ), null ); assertThat( violations ).containsOnlyViolations( violationOf( NotNull.class ) .withPropertyPath( pathWith() .method( "bar" ) .returnValue() ) ); }