@Test public void updateArticle_ArticleBodyWithNonSupportedContentTypeGiven_ShouldReturnUpdatedArticle() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles/1", "PUT", new ByteArrayInputStream("{\"text\": \"some updated article text\"}".getBytes())); req.getHeadersAsMultimap().add("Content-Type", MediaType.APPLICATION_JSON); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.UNSUPPORTED_MEDIA_TYPE), any(), emptyBaos()); }
@Test public void deleteApp_NoAdminUserGiven_ShouldForbidAccess() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); SecurityContext sc = mock(SecurityContext.class); Principal p = mock(Principal.class); when(sc.getUserPrincipal()).thenReturn(p); when(sc.isUserInRole("admin")).thenReturn(false); container.handleRequest(new TestRequest("/app", "DELETE"), responseWriter, sc); verify(responseWriter, times(1)).writeResponse(eq(Status.FORBIDDEN), any(), emptyBaos()); }
@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 deleteApp_AdminUserGiven_ShouldAllowAccess() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); SecurityContext sc = mock(SecurityContext.class); Principal p = mock(Principal.class); when(sc.getUserPrincipal()).thenReturn(p); when(sc.isUserInRole("admin")).thenReturn(true); container.handleRequest(new TestRequest("/app", "DELETE"), responseWriter, sc); verify(responseWriter, times(1)).writeResponse(eq(Status.NO_CONTENT), any(), emptyBaos()); }
@Test public void getArticle_NonSupportedMethodGiven_ShouldReturnInternalServerError() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "PATCH"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.METHOD_NOT_ALLOWED), any(), emptyBaos()); }
@Test public void getArticle_NonExistingIdGiven_ShouldReturnNotFound() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.NOT_FOUND), any(), emptyBaos()); }
@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()); }