@GET @Path("/articles/{articleId}") public Response getArticle(@PathParam("articleId") int articleId) { String article = testService.getArticle(articleId); if (article == null) { return Response.status(Status.NOT_FOUND).build(); } else { return Response.ok(article).build(); } }
@Test public void getArticle_ServiceExceptionGiven_ShouldReturnInternalServerError() throws IOException { when(testService.getArticle(1)).thenThrow(new RuntimeException()); JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.INTERNAL_SERVER_ERROR), any(), emptyBaos()); }
@Test public void getArticle_OneSupportedAcceptGiven_ShouldReturnArticle() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles/1", "GET"); req.getHeadersAsMultimap().add("Accept", MediaType.APPLICATION_XML + "," + MediaType.TEXT_PLAIN); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("some article")); }
@Test public void getArticle_ExistingIdGiven_ShouldReturnArticle() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("some article")); }
@Test public void getArticle_NonSupportedAccept_ShouldReturnNotAcceptable() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles/1", "GET"); req.getHeadersAsMultimap().add("Accept", MediaType.APPLICATION_XML); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.NOT_ACCEPTABLE), any(), emptyBaos()); }