public Object invoke(MethodInvocation invocation) throws Throwable { Class[] groups = determineValidationGroups(invocation); Set<MethodConstraintViolation<Object>> result = this.validator.validateAllParameters( invocation.getThis(), invocation.getMethod(), invocation.getArguments(), groups); if (!result.isEmpty()) { throw new MethodConstraintViolationException(result); } Object returnValue = invocation.proceed(); result = this.validator.validateReturnValue( invocation.getThis(), invocation.getMethod(), returnValue, groups); if (!result.isEmpty()) { throw new MethodConstraintViolationException(result); } return returnValue; }
@Test public void testGetJsonString() throws Exception { MethodConstraintViolationException oopsie = mock(MethodConstraintViolationException.class); Set<MethodConstraintViolation<?>> violations = new HashSet<MethodConstraintViolation<?>>( (List<MethodConstraintViolation<?>>) (List) Arrays.asList( withFields(MethodConstraintViolation.class, "message", "A problem." ), withFields(MethodConstraintViolation.class, "message", "Something else." ) )); when(oopsie.getConstraintViolations()).thenReturn(violations); Response res = mapper.toResponse(oopsie); assertThat(res.getStatus()).isEqualTo(Response.Status.PRECONDITION_FAILED.getStatusCode()); // beide volgende resultaten zijn mogelijk, dus test moet een beetje anders //assertThat(res.getEntity()).isEqualTo("{ \"error\" : {\"\":[\"Something else.\",\"A problem.\"]}}"); //assertThat(res.getEntity()).isEqualTo("{ \"error\" : {\"\":[\"A problem.\",\"Something else.\"]}}"); assertThat((String) res.getEntity()).startsWith("{ \"error\" : {\"\":["); assertThat((String) res.getEntity()).endsWith("]}}"); assertThat((String) res.getEntity()).containsOnlyOnce("\"A problem.\""); assertThat((String) res.getEntity()).containsOnlyOnce("\"Something else.\""); verify(preProcessLoggingInterceptor).postProcessError(oopsie, "Applicatie keerde terug met een (verwachtte) ConstraintViolation:"); }
@Override public Response toResponse(DomainValidationException exception) { ResourceBase.ResourceBaseBuilder<?> resourseBaseBuilder = ResourceBase.resourceBaseBuilder(); for (MethodConstraintViolation<Object> violation : exception.getConstraintViolations()) { resourseBaseBuilder.error(null, violation.getParameterName(), violation.getMessage()); } return Response .status(400) .entity(resourseBaseBuilder.build()) .type(MediaType.APPLICATION_JSON_TYPE) .build(); }
@AroundInvoke public Object interceptAndValidate(InvocationContext invocationContext) throws Exception { Set<MethodConstraintViolation<Object>> constraintViolations = methodValidator.validateAllParameters(invocationContext.getTarget(), invocationContext.getMethod(), invocationContext.getParameters()); if(constraintViolations.isEmpty()){ return invocationContext.proceed(); } throw new DomainValidationException(invocationContext.getMethod().getDeclaringClass(), invocationContext.getMethod(), constraintViolations); }
public DomainValidationException(Class<?> targetClass, Method targetMethod, Set<MethodConstraintViolation<Object>> constraintViolations) { this.targetClass = targetClass; this.targetMethod = targetMethod; this.constraintViolations = constraintViolations; }
public Set<MethodConstraintViolation<Object>> getConstraintViolations() { return constraintViolations; }