@Test public void findAllReplies() { int pageNumber = 1; int pageSize = 3; Pageable pageable = PageRequest.of(pageNumber, pageSize); Long parentCommentId = 1L; EcoNewsComment ecoNewsCommentChild = getEcoNewsComment(); ecoNewsCommentChild.setParentComment(getEcoNewsComment()); ecoNewsCommentChild.setUsersLiked(new HashSet<>()); Page<EcoNewsComment> pages = new PageImpl<>(Collections.singletonList(ecoNewsCommentChild), pageable, 1); when(ecoNewsCommentRepo.findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId)) .thenReturn(pages); when(modelMapper.map(ecoNewsCommentChild, EcoNewsCommentDto.class)).thenReturn(getEcoNewsCommentDto()); PageableDto<EcoNewsCommentDto> allReplies = ecoNewsCommentService.findAllReplies(pageable, parentCommentId, getUser()); assertEquals(getEcoNewsCommentDto().getId(), allReplies.getPage().get(0).getId()); assertEquals(4, allReplies.getTotalElements()); assertEquals(1, allReplies.getCurrentPage()); assertEquals(1, allReplies.getPage().size()); }
@Override public PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user) { Page<EcoNewsComment> pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user) { Page<EcoNewsComment> pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); } }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user) { Page<EcoNewsComment> pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user) { Page<EcoNewsComment> pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .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> findAllReplies(Pageable pageable, Long parentCommentId, User user) { Page<EcoNewsComment> pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateAsc(pageable, parentCommentId); List<EcoNewsCommentDto> ecoNewsCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(ecoNewsComment -> modelMapper.map(ecoNewsComment, EcoNewsCommentDto.class)) .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);  }