DefaultMethodInvoker implements MethodInvoker { @Override public final Object invokeMethod(Object resource, GenericResourceMethod methodResource, ApplicationContext context) { Object[] params = makeMethodParameters(methodResource, context); beforeInvokeMethod(resource, methodResource, params, context); return invokeMethod(resource, methodResource, params, context); } DefaultMethodInvoker(ParameterResolverFactory parameterResolverFactory); @Override final Object invokeMethod(Object resource, GenericResourceMethod methodResource, ApplicationContext context); Object invokeMethod(Object resource, GenericResourceMethod methodResource, Object[] params, ApplicationContext context);  }
@Test public void notifiesMethodInvokerFiltersBeforeMethodInvocation() throws Exception { MethodInvokerFilter methodInvokerFilter = mock(MethodInvokerFilter.class); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); when(applicationContext.getProviders().getMethodInvokerFilters(RESOURCE_PATH)).thenReturn(newArrayList(methodInvokerFilter)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); verify(methodInvokerFilter).accept(eq(resourceMethod), aryEq(new Object[]{ARGUMENT_VALUE})); }
@Test public void invokesMethodWithEntityParameter() throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); Object invocationResult = methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); assertEquals(ARGUMENT_VALUE, invocationResult); }
@Test public void invokesMethodWithNullEntityParameterWhenEntityStreamDoesNotPresent() throws Exception { when(applicationContext.getContainerRequest().getEntityStream()).thenReturn(null); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); Object invocationResult = methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); assertEquals(null, invocationResult); }
@Test public void throwsWebApplicationExceptionWhenMessageBodyReaderForEntityParameterIsNotAvailable() throws Exception { when(applicationContext.getProviders() .getMessageBodyReader((Class)entityParameter.getParameterClass(), entityParameter.getGenericType(), entityParameter.getAnnotations(), applicationContext.getContainerRequest().getMediaType())) .thenReturn(null); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); thrown.expect(webApplicationExceptionWithStatusMatcher(UNSUPPORTED_MEDIA_TYPE)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@Test public void invokesMethodWithNullEntityParameterWhenMessageBodyReaderIsNotAvailableAndBothContentTypeAndContentLengthAreNotSet() throws Exception { when(applicationContext.getContainerRequest().getMediaType()).thenReturn(null); when(applicationContext.getProviders() .getMessageBodyReader((Class)entityParameter.getParameterClass(), entityParameter.getGenericType(), entityParameter.getAnnotations(), applicationContext.getContainerRequest().getMediaType())) .thenReturn(null); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.putSingle(CONTENT_LENGTH, "0"); when(applicationContext.getContainerRequest().getRequestHeaders()).thenReturn(headers); Object invocationResult = methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); assertEquals(null, invocationResult); }
@Test public void rethrowWebApplicationExceptionThatOccursWhenReadEntityParameter() throws Exception { WebApplicationException thrownByMessageBodyReader = new WebApplicationException(); when(messageBodyReader.readFrom((Class)entityParameter.getParameterClass(), entityParameter.getGenericType(), entityParameter.getAnnotations(), applicationContext.getContainerRequest().getMediaType(), applicationContext.getContainerRequest().getRequestHeaders(), applicationContext.getContainerRequest().getEntityStream())).thenThrow(thrownByMessageBodyReader); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); thrown.expect(exceptionSameInstanceMatcher(thrownByMessageBodyReader)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@Test public void rethrowInternalExceptionThatOccursWhenReadEntityParameter() throws Exception { InternalException thrownByMessageBodyReader = new InternalException(new Exception()); when(messageBodyReader.readFrom((Class)entityParameter.getParameterClass(), entityParameter.getGenericType(), entityParameter.getAnnotations(), applicationContext.getContainerRequest().getMediaType(), applicationContext.getContainerRequest().getRequestHeaders(), applicationContext.getContainerRequest().getEntityStream())).thenThrow(thrownByMessageBodyReader); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); thrown.expect(exceptionSameInstanceMatcher(thrownByMessageBodyReader)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@Test public void wrapsExceptionThatOccursWhenReadEntityParameterWithInternalException() throws Exception { Exception thrownByMessageBodyReader = new IOException(); when(messageBodyReader.readFrom((Class)entityParameter.getParameterClass(), entityParameter.getGenericType(), entityParameter.getAnnotations(), applicationContext.getContainerRequest().getMediaType(), applicationContext.getContainerRequest().getRequestHeaders(), applicationContext.getContainerRequest().getEntityStream())).thenThrow(thrownByMessageBodyReader); when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); thrown.expect(InternalException.class); thrown.expectCause(exceptionSameInstanceMatcher(thrownByMessageBodyReader)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@Test public void invokesMethodWithAnnotatedParameter() throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(annotatedParameter)); Object invocationResult = methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); assertEquals(ARGUMENT_VALUE, invocationResult); }
@UseDataProvider("annotationTypesWhenNeedToThrowWebApplicationExceptionWithStatusNotFound") @Test public <A extends Annotation> void throwsWebApplicationExceptionWithStatusNotFoundWhenFailResolveParameterAnnotatedWith(Class<A> annotationType) throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(annotatedParameter)); when(parameterAnnotation.annotationType()).thenReturn((Class)annotationType); Exception thrownByParameterResolver = new Exception(); when(parameterResolver.resolve(annotatedParameter, applicationContext)).thenThrow(thrownByParameterResolver); thrown.expect(webApplicationExceptionWithStatusMatcher(NOT_FOUND)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@UseDataProvider("annotationTypesWhenNeedToThrowWebApplicationExceptionWithStatusBadRequest") @Test public <A extends Annotation> void throwsWebApplicationExceptionWithStatusBadRequestWhenFailResolveParameterAnnotatedWith(Class<A> annotationType) throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(annotatedParameter)); when(parameterAnnotation.annotationType()).thenReturn((Class)annotationType); Exception thrownByParameterResolver = new Exception(); when(parameterResolver.resolve(annotatedParameter, applicationContext)).thenThrow(thrownByParameterResolver); thrown.expect(webApplicationExceptionWithStatusMatcher(BAD_REQUEST)); methodInvoker.invokeMethod(new EchoResource(), resourceMethod, applicationContext); }
@Test public void rethrowWebApplicationExceptionThatThrownByInvocatedMethod() throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); WebApplicationException thrownByMethod = new WebApplicationException(); thrown.expect(exceptionSameInstanceMatcher(thrownByMethod)); methodInvoker.invokeMethod(new EchoResource(thrownByMethod), resourceMethod, applicationContext); }
@Test public void rethrowInternalExceptionThatThrownByInvocatedMethod() throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); InternalException thrownByMethod = new InternalException(new Exception()); thrown.expect(exceptionSameInstanceMatcher(thrownByMethod)); methodInvoker.invokeMethod(new EchoResource(thrownByMethod), resourceMethod, applicationContext); }
@Test public void wrapsExceptionThatThrownByInvocatedMethodWithInternalException() throws Exception { when(resourceMethod.getMethodParameters()).thenReturn(newArrayList(entityParameter)); Exception thrownByMethod = new Exception(); thrown.expect(InternalException.class); thrown.expectCause(exceptionSameInstanceMatcher(thrownByMethod)); methodInvoker.invokeMethod(new EchoResource(thrownByMethod), resourceMethod, applicationContext); }