### Question:
PostHibernateDao extends GenericDao<Post> implements PostDao { @Override public Page<Post> getPosts(Topic topic, PageRequest pageRequest) { Number totalCount = (Number) session() .getNamedQuery("getCountPostsInTopic") .setParameter(TOPIC_PARAMETER_NAME, topic) .uniqueResult(); Query query = session() .getNamedQuery("getPostsInTopic") .setParameter(TOPIC_PARAMETER_NAME, topic); pageRequest.adjustPageNumber(totalCount.intValue()); query.setFirstResult(pageRequest.getOffset()); query.setMaxResults(pageRequest.getPageSize()); @SuppressWarnings("unchecked") List<Post> posts = (List<Post>) query.list(); return new PageImpl<>(posts, pageRequest, totalCount.intValue()); } PostHibernateDao(SessionFactory sessionFactory); Page<Post> getUserPosts(JCUser author, PageRequest pageRequest, List<Long> allowedBranchesIds); @Override Page<Post> getPosts(Topic topic, PageRequest pageRequest); @Override Post getLastPostFor(Branch branch); @Override List<Post> getLastPostsFor(List<Long> branchIds, int postCount); @Override void changeRating(Long postId, int changes);  }### Answer:
@Test public void testGetPostsWithEnabledPaging() { int totalSize = 50; int pageCount = 2; int pageSize = totalSize / pageCount; PageRequest pageRequest = new PageRequest("1", pageSize); List<Post> posts = PersistedObjectsFactory.createAndSavePostList(totalSize); Topic topic = posts.get(0).getTopic(); Page<Post> postsPage = dao.getPosts(topic, pageRequest); assertEquals(postsPage.getContent().size(), pageSize, "Incorrect count of posts in one page."); assertEquals(postsPage.getTotalElements(), totalSize, "Incorrect total count."); assertEquals(postsPage.getTotalPages(), pageCount, "Incorrect count of pages."); }
@Test public void testGetPostsWithEnabledPagingPageTooLow() { int totalSize = 50; int pageCount = 2; int pageSize = totalSize / pageCount; PageRequest pageRequest = new PageRequest(PAGE_NUMBER_TOO_LOW, pageSize); List<Post> posts = PersistedObjectsFactory.createAndSavePostList(totalSize); Topic topic = posts.get(0).getTopic(); Page<Post> postsPage = dao.getPosts(topic, pageRequest); assertEquals(postsPage.getContent().size(), pageSize, "Incorrect count of posts in one page."); assertEquals(postsPage.getTotalElements(), totalSize, "Incorrect total count."); assertEquals(postsPage.getTotalPages(), pageCount, "Incorrect count of pages."); assertEquals(postsPage.getNumber(), 1, "Incorrect number of page"); }
@Test public void testGetPostsWithEnabledPagingPageTooBig() { int totalSize = 50; int pageCount = 2; int pageSize = totalSize / pageCount; PageRequest pageRequest = new PageRequest(PAGE_NUMBER_TOO_BIG, pageSize); List<Post> posts = PersistedObjectsFactory.createAndSavePostList(totalSize); Topic topic = posts.get(0).getTopic(); Page<Post> postsPage = dao.getPosts(topic, pageRequest); assertEquals(postsPage.getContent().size(), pageSize, "Incorrect count of posts in one page."); assertEquals(postsPage.getTotalElements(), totalSize, "Incorrect total count."); assertEquals(postsPage.getTotalPages(), pageCount, "Incorrect count of pages."); assertEquals(postsPage.getNumber(), pageCount, "Incorrect number of page"); }