@Test public void createCommentEntryExistingCommentary() throws Exception { Commentary commentary = new Commentary(); commentary.setId(1L); CommentEntry entry = new CommentEntry(); entry.setTitle("Test Title"); entry.setId(1L); when(commentaryService.createCommentEntry(eq(1L), any(CommentEntry.class))).thenReturn(entry); mockMvc .perform( post("/rest/commentaries/1/comment-entries") .content("{\"content\":\"Generic content\"}") .contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.title", is(entry.getTitle()))) .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("rest/comment-entries/1")))) .andExpect(header().string("Location", endsWith("rest/comment-entries/1"))) .andExpect(status().isCreated()); }
@Test public void findAllCommentaries() throws Exception { List<Commentary> list = new ArrayList<Commentary>(); Commentary commentaryA = new Commentary(); commentaryA.setId(1L); commentaryA.setContent("content A"); list.add(commentaryA); Commentary commentaryB = new Commentary(); commentaryB.setId(2L); commentaryB.setContent("content B"); list.add(commentaryB); CommentaryList allCommentaries = new CommentaryList(list); when(commentaryService.findAllCommentaries()).thenReturn(allCommentaries); mockMvc .perform(get("/rest/commentaries")) .andExpect( jsonPath( "$.commentaries[*].content", hasItems(endsWith("content A"), endsWith("content B")))) .andExpect(status().isOk()); }
@Test public void getCommentary() throws Exception { Commentary commentary = new Commentary(); commentary.setContent("Test content"); commentary.setId(1L); Account account = new Account(); account.setId(1L); commentary.setOwner(account); when(commentaryService.findCommentary(1L)).thenReturn(commentary); mockMvc .perform(get("/rest/commentaries/1")) .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/commentaries/1")))) .andExpect( jsonPath("$.links[*].href", hasItem(endsWith("/commentaries/1/comment-entries")))) .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/accounts/1")))) .andExpect(jsonPath("$.links[*].rel", hasItems(is("self"), is("owner"), is("entries")))) .andExpect(jsonPath("$.content", is("Test content"))) .andExpect(status().isOk()); }