@Test public void findAllComments() { int pageNumber = 1; int pageSize = 3; Pageable pageable = PageRequest.of(pageNumber, pageSize); Long ecoNewsId = 1L; EcoNewsComment ecoNewsComment = ModelUtils.getEcoNewsComment(); ecoNewsComment.setUsersLiked(new HashSet<>()); Page<EcoNewsComment> pages = new PageImpl<>(Collections.singletonList(ecoNewsComment), pageable, 1); EcoNewsCommentDto ecoNewsCommentDto = ModelUtils.getEcoNewsCommentDto(); when(ecoNewsService.findById(1L)).thenReturn(ModelUtils.getEcoNews()); when(ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc(pageable, ecoNewsId)) .thenReturn(pages); when(modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)).thenReturn(ecoNewsCommentDto); when(ecoNewsCommentRepo.countByParentCommentId(any())).thenReturn(0); PageableDto<EcoNewsCommentDto> allComments = ecoNewsCommentService.findAllComments(pageable, getUser(), ecoNewsId); assertEquals(ecoNewsCommentDto, allComments.getPage().get(0)); assertEquals(4, allComments.getTotalElements()); assertEquals(1, allComments.getCurrentPage()); assertEquals(1, allComments.getPage().size()); }
@Override public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId) { ecoNewsService.findById(ecoNewsId); Page<EcoNewsComment> pages = ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc( pageable, ecoNewsId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .map(comment -> { comment.setReplies(ecoNewsCommentRepo.countByParentCommentId(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId) { ecoNewsService.findById(ecoNewsId); Page<EcoNewsComment> pages = ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc( pageable, ecoNewsId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .map(comment -> { comment.setReplies(ecoNewsCommentRepo.countByParentCommentId(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); } }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId) { ecoNewsService.findById(ecoNewsId); Page<EcoNewsComment> pages = ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc( pageable, ecoNewsId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .map(comment -> { comment.setReplies(ecoNewsCommentRepo.countByParentCommentId(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId) { ecoNewsService.findById(ecoNewsId); Page<EcoNewsComment> pages = ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc( pageable, ecoNewsId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .map(comment -> { comment.setReplies(ecoNewsCommentRepo.countByParentCommentId(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @Override AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest,
                                             User user); @Override PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user); @Override void deleteById(Long id, User user); @Override @Transactional void update(String text, Long id, User user); void like(Long id, User user); @Override @Transactional int countLikes(Long id); @Override int countReplies(Long id); @Override int countOfComments(Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> getAllActiveComments(Pageable pageable, User user, Long ecoNewsId); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId) { ecoNewsService.findById(ecoNewsId); Page<EcoNewsComment> pages = ecoNewsCommentRepo.findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateAsc( pageable, ecoNewsId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .map(comment -> { comment.setReplies(ecoNewsCommentRepo.countByParentCommentId(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @Override AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest,
                                             User user); @Override PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user); @Override void deleteById(Long id, User user); @Override @Transactional void update(String text, Long id, User user); void like(Long id, User user); @Override @Transactional int countLikes(Long id); @Override int countReplies(Long id); @Override int countOfComments(Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> getAllActiveComments(Pageable pageable, User user, Long ecoNewsId);  }