@Test public void getRecentPublicBlogPosts_ShouldReturnRecentBlogPosts() throws Exception { Pageable pageable = new PageRequest(0, 4); when(blogPostRepositoryMock.findByPublishedIsTrueOrderByPublishedTimeDesc(any(Pageable.class))) .thenReturn(new PageImpl<BlogPost>(getTestPublishedBlogPostList(), pageable, 2)); mockMvc .perform(get(ApiUrls.API_ROOT + ApiUrls.URL_SITE_RECENT_BLOGS)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON)) .andExpect(jsonPath("$._embedded.blogPostList", hasSize(2))) .andExpect(jsonPath("$._embedded.blogPostList[0].id", is(BLOGS_1_ID))) .andExpect(jsonPath("$._embedded.blogPostList[0].authorId", is(BLOGS_1_AUTHOR_ID))) .andExpect(jsonPath("$._embedded.blogPostList[0].title", is(BLOGS_1_TITLE))) .andExpect(jsonPath("$._embedded.blogPostList[1].id", is(BLOGS_2_ID))) .andExpect(jsonPath("$._embedded.blogPostList[1].authorId", is(BLOGS_2_AUTHOR_ID))) .andExpect(jsonPath("$._embedded.blogPostList[1].title", is(BLOGS_2_TITLE))) // check links .andExpect(jsonPath("$._links.self.href", endsWith(ApiUrls.URL_SITE_RECENT_BLOGS))); verify(blogPostRepositoryMock, times(1)) .findByPublishedIsTrueOrderByPublishedTimeDesc(pageable); verifyNoMoreInteractions(blogPostRepositoryMock); }
@Test public void getLatestBlogPost_ShouldReturnBlogPost() throws Exception { BlogPost blog = getTestSinglePublishedBlogPost(); when(blogPostRepositoryMock.findByPublishedIsTrueOrderByPublishedTimeDesc(any(Pageable.class))) .thenReturn(new PageImpl<BlogPost>(Arrays.asList(blog), new PageRequest(0, 1), 1)); mockMvc .perform(get(ApiUrls.API_ROOT + ApiUrls.URL_SITE_LATEST_BLOG)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON)) .andExpect(jsonPath("$.id", is(BLOG_ID))) .andExpect(jsonPath("$.title", is(BLOG_TITLE))) .andExpect(jsonPath("$._links.self.href", endsWith(ApiUrls.URL_SITE_BLOGS + "/" + BLOG_ID))) .andExpect(jsonPath("$._links.comments.templated", is(true))) .andExpect( jsonPath( "$._links.comments.href", endsWith(ApiUrls.URL_SITE_BLOGS + "/" + BLOG_ID + "/comments{?page,size,sort}"))) .andExpect( jsonPath( "$._links.author.href", endsWith(ApiUrls.URL_SITE_PROFILES + "/" + BLOG_AUTHOR_ID))); }