RequestDispatcher { public void dispatch(GenericContainerRequest request, GenericContainerResponse response) { ApplicationContext context = ApplicationContext.getCurrent(); String requestPath = getRequestPathWithoutMatrixParameters(context); List<String> parameterValues = context.getParameterValues(); ObjectFactory<ResourceDescriptor> resourceFactory = getRootResource(parameterValues, requestPath); if (resourceFactory == null) { LOG.debug("Root resource not found for {}", requestPath); response.setResponse(Response.status(NOT_FOUND) .entity(String.format("There is no any resources matched to request path %s", requestPath)) .type(TEXT_PLAIN) .build()); return; } String newRequestPath = getPathTail(parameterValues); context.addMatchedURI(requestPath.substring(0, requestPath.lastIndexOf(newRequestPath))); context.setParameterNames(resourceFactory.getObjectModel().getUriPattern().getParameterNames()); Object resource = resourceFactory.getInstance(context); dispatch(request, response, context, resourceFactory.getObjectModel(), resource, newRequestPath); } RequestDispatcher(ResourceBinder resourceBinder); void dispatch(GenericContainerRequest request, GenericContainerResponse response); ResourceBinder getResources();  }
@Test public void returnsResponseWithStatus_NOT_FOUND_WhenRootResourceNotFound() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(NOT_FOUND, argumentCaptor.getValue().getStatusInfo()); }
@Test public void callsResourceMethod() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(resourceMethod), same(applicationContext))).thenReturn("foo"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void returnsResponseWithStatus_NO_CONTENT_WhenResourceMethodReturnsNull() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(resourceMethod), same(applicationContext))).thenReturn(null); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(NO_CONTENT, argumentCaptor.getValue().getStatusInfo()); }
@Test public void returnsResponseWithStatus_ACCEPTED_WhenResourceMethodReturnsAsynchronousJob() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); AsynchronousJob asynchronousJob = mock(AsynchronousJob.class); when(asynchronousJob.getJobURI()).thenReturn("async/1"); when(methodInvoker.invokeMethod(same(resource), same(resourceMethod), same(applicationContext))).thenReturn(asynchronousJob); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(ACCEPTED, argumentCaptor.getValue().getStatusInfo()); assertEquals("http: }
@Test public void returnsResponseProducedByResourceMethod() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); Response methodResponse = mock(Response.class); when(methodResponse.getMetadata()).thenReturn(new MultivaluedHashMap<>()); when(methodInvoker.invokeMethod(same(resource), same(resourceMethod), same(applicationContext))).thenReturn(methodResponse); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertSame(methodResponse, argumentCaptor.getValue()); }
@Test public void callsResourceMethodRespectsConsumedMediaTypes() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); when(request.getMediaType()).thenReturn(APPLICATION_XML_TYPE); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethodOne = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(TEXT_XML_TYPE, APPLICATION_XML_TYPE), newArrayList(WILDCARD_TYPE)); ResourceMethodDescriptor resourceMethodTwo = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(APPLICATION_JSON_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethodOne, resourceMethodTwo), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(resourceMethodOne), same(applicationContext))).thenReturn("foo"); when(methodInvoker.invokeMethod(same(resource), same(resourceMethodTwo), same(applicationContext))).thenReturn("bar"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void returnsResponseWithStatus_NOT_FOUND_WhenResourceMethodNotFound() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(NOT_FOUND, argumentCaptor.getValue().getStatusInfo()); }
@Test public void returnsResponseWithStatus_METHOD_NOT_ALLOWED_WhenNotFoundResourceMethodWithRequestedHttpMethod() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "GET", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(METHOD_NOT_ALLOWED, argumentCaptor.getValue().getStatusInfo()); String allowHeader = argumentCaptor.getValue().getHeaderString(ALLOW); assertEquals("GET", allowHeader); }
@Test public void returnsResponseWithStatus_UNSUPPORTED_MEDIA_TYPE_WhenResourceMethodDoesNotSupportContentTypeOfRequest() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); when(request.getMediaType()).thenReturn(APPLICATION_JSON_TYPE); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(TEXT_PLAIN_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(UNSUPPORTED_MEDIA_TYPE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void callsResourceMethodRespectsProducedMediaTypes() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_XML_TYPE, 0.7f), new AcceptMediaType(TEXT_PLAIN_TYPE))); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethodOne = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_XML_TYPE, APPLICATION_XML_TYPE)); ResourceMethodDescriptor resourceMethodTwo = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_PLAIN_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethodOne, resourceMethodTwo), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(resourceMethodOne), same(applicationContext))).thenReturn("<bar/>"); when(methodInvoker.invokeMethod(same(resource), same(resourceMethodTwo), same(applicationContext))).thenReturn("foo"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void returnsResponseWithStatus_NOT_ACCEPTABLE_WhenResourceMethodDoesNotProduceContentAcceptableByCaller() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b;x=y")); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_PLAIN_TYPE))); Resource resource = new Resource(); ResourceMethodDescriptor resourceMethod = mockResourceMethod(Resource.class.getMethod("echo", String.class), "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_XML_TYPE, APPLICATION_XML_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(resourceMethod), newArrayList(), newArrayList()); matchRequestPath(); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(NOT_ACCEPTABLE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void callsSubResourceMethod() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethod = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethod), newArrayList()); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(subResourceMethod), same(applicationContext))).thenReturn("foo"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void callsSubResourceMethodRespectsProducedMediaTypes() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_XML_TYPE, 0.7f), new AcceptMediaType(TEXT_PLAIN_TYPE))); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethodOne = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_PLAIN_TYPE)); SubResourceMethodDescriptor subResourceMethodTwo = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_XML_TYPE, APPLICATION_XML_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethodOne, subResourceMethodTwo), newArrayList()); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(subResourceMethodOne), same(applicationContext))).thenReturn("foo"); when(methodInvoker.invokeMethod(same(resource), same(subResourceMethodTwo), same(applicationContext))).thenReturn("<bar/>"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void findsSubResourceWithRequestedHttpMethodWhenPathMatchesToMoreThanOneSubResourcesPaths() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b")); when(request.getMethod()).thenReturn("DELETE"); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_PLAIN_TYPE))); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethodOne = mockSubResourceMethod(Resource.class.getMethod("echo", String.class),"(.*)", "GET", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_PLAIN_TYPE)); SubResourceMethodDescriptor subResourceMethodTwo = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), ".*", "DELETE", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_PLAIN_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethodOne, subResourceMethodTwo), newArrayList()); matchRequestPath("b"); when(resources.getMatchedResource(eq("/a/b"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(resource), same(subResourceMethodOne), same(applicationContext))).thenReturn("get"); when(methodInvoker.invokeMethod(same(resource), same(subResourceMethodTwo), same(applicationContext))).thenReturn("delete"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("delete", argumentCaptor.getValue().getEntity()); }
@Test public void returnsResponseWithStatus_METHOD_NOT_ALLOWED_WhenNotFoundSubResourceMethodWithRequestedHttpMethod() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethod = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "GET", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethod), newArrayList()); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(METHOD_NOT_ALLOWED, argumentCaptor.getValue().getStatusInfo()); String allowHeader = argumentCaptor.getValue().getHeaderString(ALLOW); assertEquals("GET", allowHeader); }
@Test public void returnsResponseWithStatus_UNSUPPORTED_MEDIA_TYPE_WhenSubResourceMethodDoesNotSupportContentTypeOfRequest() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); when(request.getMediaType()).thenReturn(APPLICATION_JSON_TYPE); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethod = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(TEXT_PLAIN_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethod), newArrayList()); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(UNSUPPORTED_MEDIA_TYPE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void returnsResponseWithStatus_NOT_ACCEPTABLE_WhenSubResourceMethodDoesNotProduceContentAcceptableByCaller() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_PLAIN_TYPE))); Resource resource = new Resource(); SubResourceMethodDescriptor subResourceMethod = mockSubResourceMethod(Resource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(WILDCARD_TYPE), newArrayList(TEXT_XML_TYPE, APPLICATION_XML_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(resource, newArrayList(), newArrayList(subResourceMethod), newArrayList()); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(NOT_ACCEPTABLE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void callsSubResourceLocator() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); LocatorResource locatorResource = new LocatorResource(); SubResourceLocatorDescriptor subResourceLocator = mockSubResourceLocator(LocatorResource.class.getMethod("resource"), "c/d"); ObjectFactory resourceFactory = mockResourceFactory(locatorResource, newArrayList(), newArrayList(), newArrayList(subResourceLocator)); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); SubResource subResource = new SubResource(); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceLocator), same(applicationContext))).thenReturn(subResource); when(methodInvoker.invokeMethod(same(subResource), isA(ResourceMethodDescriptor.class), same(applicationContext))).thenReturn("foo"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void returnsResponseWithStatus_METHOD_NOT_ALLOWED_WhenSubResourceLocatorProducesResourceThatDoesNotSupportRequestedHttpMethod() throws Exception { when(request.getMethod()).thenReturn("GET"); when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); LocatorResource locatorResource = new LocatorResource(); SubResourceLocatorDescriptor subResourceLocator = mockSubResourceLocator(LocatorResource.class.getMethod("resource"), "c/d"); ObjectFactory resourceFactory = mockResourceFactory(locatorResource, newArrayList(), newArrayList(), newArrayList(subResourceLocator)); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceLocator), same(applicationContext))).thenReturn(new SubResource()); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(METHOD_NOT_ALLOWED, argumentCaptor.getValue().getStatusInfo()); String allowHeader = argumentCaptor.getValue().getHeaderString(ALLOW); assertEquals("POST,OPTIONS", allowHeader); }
@Test public void returnsResponseWithStatus_UNSUPPORTED_MEDIA_TYPE_WhenSubResourceLocatorProducesResourceThatDoesNotSupportContentTypeOfRequest() throws Exception { when(request.getMediaType()).thenReturn(APPLICATION_JSON_TYPE); when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); LocatorResource locatorResource = new LocatorResource(); SubResourceLocatorDescriptor subResourceLocator = mockSubResourceLocator(LocatorResource.class.getMethod("resource"), "c/d"); ObjectFactory resourceFactory = mockResourceFactory(locatorResource, newArrayList(), newArrayList(), newArrayList(subResourceLocator)); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceLocator), same(applicationContext))).thenReturn(new SubResource()); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(UNSUPPORTED_MEDIA_TYPE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void returnsResponseWithStatus_NOT_ACCEPTABLE_WhenSubResourceLocatorProducesResourceThatDoesNotProduceContentAcceptableByCaller() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); when(request.getAcceptMediaTypeList()).thenReturn(newArrayList(new AcceptMediaType(TEXT_PLAIN_TYPE))); LocatorResource locatorResource = new LocatorResource(); SubResourceLocatorDescriptor subResourceLocator = mockSubResourceLocator(LocatorResource.class.getMethod("resource"), "c/d"); ObjectFactory resourceFactory = mockResourceFactory(locatorResource, newArrayList(), newArrayList(), newArrayList(subResourceLocator)); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceLocator), same(applicationContext))).thenReturn(new SubResource()); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(NOT_ACCEPTABLE, argumentCaptor.getValue().getStatusInfo()); }
@Test public void subResourceMethodTakesPrecedenceOverSubResourceLocatorWithTheSamePath() throws Exception { when(applicationContext.getPathSegments(false)).thenReturn(createPathSegments("a", "b", "c", "d;x=y")); LocatorResource locatorResource = new LocatorResource(); SubResourceLocatorDescriptor subResourceLocator = mockSubResourceLocator(LocatorResource.class.getMethod("resource"), "c/d"); SubResourceMethodDescriptor subResourceMethod = mockSubResourceMethod(LocatorResource.class.getMethod("echo", String.class), "c/d", "POST", newArrayList(WILDCARD_TYPE), newArrayList(WILDCARD_TYPE)); ObjectFactory resourceFactory = mockResourceFactory(locatorResource, newArrayList(), newArrayList(subResourceMethod), newArrayList(subResourceLocator)); matchRequestPath("c/d"); when(resources.getMatchedResource(eq("/a/b/c/d"), anyList())).thenReturn(resourceFactory); SubResource subResource = new SubResource(); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceMethod), same(applicationContext))).thenReturn("foo"); when(methodInvoker.invokeMethod(same(locatorResource), same(subResourceLocator), same(applicationContext))).thenReturn(subResource); when(methodInvoker.invokeMethod(same(subResource), isA(ResourceMethodDescriptor.class), same(applicationContext))).thenReturn("bar"); requestDispatcher.dispatch(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response, atLeastOnce()).setResponse(argumentCaptor.capture()); assertEquals(OK, argumentCaptor.getValue().getStatusInfo()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }