@Test public void saveCommentWithNoParentCommentId() { EcoNews ecoNews = ModelUtils.getEcoNews(); AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest = ModelUtils.getAddEcoNewsCommentDtoRequest(); EcoNewsComment ecoNewsComment = ModelUtils.getEcoNewsComment(); when(ecoNewsService.findById(anyLong())).thenReturn(ecoNews); when(ecoNewsCommentRepo.save(any(EcoNewsComment.class))).then(AdditionalAnswers.returnsFirstArg()); when(modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class)).thenReturn(ecoNewsComment); when(modelMapper.map(any(EcoNewsComment.class), eq(AddEcoNewsCommentDtoResponse.class))) .thenReturn(ModelUtils.getAddEcoNewsCommentDtoResponse()); ecoNewsCommentService.save(1L, addEcoNewsCommentDtoRequest, getUser()); verify(ecoNewsCommentRepo).save(any(EcoNewsComment.class)); }
@Override public AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest, User user) { EcoNews ecoNews = ecoNewsService.findById(econewsId); EcoNewsComment ecoNewsComment = modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class); ecoNewsComment.setUser(user); ecoNewsComment.setEcoNews(ecoNews); if (addEcoNewsCommentDtoRequest.getParentCommentId() != 0) { EcoNewsComment parentComment = ecoNewsCommentRepo.findById(addEcoNewsCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.getParentComment() == null) { ecoNewsComment.setParentComment(parentComment); } else { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } } return modelMapper.map(ecoNewsCommentRepo.save(ecoNewsComment), AddEcoNewsCommentDtoResponse.class); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest, User user) { EcoNews ecoNews = ecoNewsService.findById(econewsId); EcoNewsComment ecoNewsComment = modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class); ecoNewsComment.setUser(user); ecoNewsComment.setEcoNews(ecoNews); if (addEcoNewsCommentDtoRequest.getParentCommentId() != 0) { EcoNewsComment parentComment = ecoNewsCommentRepo.findById(addEcoNewsCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.getParentComment() == null) { ecoNewsComment.setParentComment(parentComment); } else { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } } return modelMapper.map(ecoNewsCommentRepo.save(ecoNewsComment), AddEcoNewsCommentDtoResponse.class); } }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest, User user) { EcoNews ecoNews = ecoNewsService.findById(econewsId); EcoNewsComment ecoNewsComment = modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class); ecoNewsComment.setUser(user); ecoNewsComment.setEcoNews(ecoNews); if (addEcoNewsCommentDtoRequest.getParentCommentId() != 0) { EcoNewsComment parentComment = ecoNewsCommentRepo.findById(addEcoNewsCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.getParentComment() == null) { ecoNewsComment.setParentComment(parentComment); } else { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } } return modelMapper.map(ecoNewsCommentRepo.save(ecoNewsComment), AddEcoNewsCommentDtoResponse.class); }  }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest, User user) { EcoNews ecoNews = ecoNewsService.findById(econewsId); EcoNewsComment ecoNewsComment = modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class); ecoNewsComment.setUser(user); ecoNewsComment.setEcoNews(ecoNews); if (addEcoNewsCommentDtoRequest.getParentCommentId() != 0) { EcoNewsComment parentComment = ecoNewsCommentRepo.findById(addEcoNewsCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.getParentComment() == null) { ecoNewsComment.setParentComment(parentComment); } else { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } } return modelMapper.map(ecoNewsCommentRepo.save(ecoNewsComment), AddEcoNewsCommentDtoResponse.class); }  @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 AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest, User user) { EcoNews ecoNews = ecoNewsService.findById(econewsId); EcoNewsComment ecoNewsComment = modelMapper.map(addEcoNewsCommentDtoRequest, EcoNewsComment.class); ecoNewsComment.setUser(user); ecoNewsComment.setEcoNews(ecoNews); if (addEcoNewsCommentDtoRequest.getParentCommentId() != 0) { EcoNewsComment parentComment = ecoNewsCommentRepo.findById(addEcoNewsCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.getParentComment() == null) { ecoNewsComment.setParentComment(parentComment); } else { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } } return modelMapper.map(ecoNewsCommentRepo.save(ecoNewsComment), AddEcoNewsCommentDtoResponse.class); }  @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);  }