@Test void saveReplyWrongIdTest() { TipsAndTricksComment tipsAndTricksComment = ModelUtils.getTipsAndTricksComment(); tipsAndTricksComment.setTipsAndTricks(ModelUtils.getTipsAndTricks()); tipsAndTricksComment.getTipsAndTricks().setId(2L); when(tipsAndTricksService.findById(anyLong())).thenReturn(ModelUtils.getTipsAndTricks()); when(modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class)) .thenReturn(tipsAndTricksComment); when(tipsAndTricksCommentRepo.findById(anyLong())).thenReturn(Optional.of(tipsAndTricksComment)); when(modelMapper.map(tipsAndTricksComment, AddTipsAndTricksCommentDtoResponse.class)) .thenReturn(addTipsAndTricksCommentDtoResponse); when(tipsAndTricksCommentRepo.save(tipsAndTricksComment)).thenReturn(tipsAndTricksComment); try { tipsAndTricksCommentService.save(2L, addTipsAndTricksCommentDtoRequest, ModelUtils.getUser()); } catch (BadRequestException ex) { assertEquals(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID, ex.getMessage()); } }
@Override public AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId, AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest, User user) { TipsAndTricks tipsAndTricks = tipsAndTricksService.findById(tipsandtricksId); TipsAndTricksComment tipsAndTricksComment = modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class); tipsAndTricksComment.setUser(user); tipsAndTricksComment.setTipsAndTricks(tipsAndTricks); if (addTipsAndTricksCommentDtoRequest.getParentCommentId() != 0) { TipsAndTricksComment parentComment = tipsAndTricksCommentRepo.findById(addTipsAndTricksCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.isDeleted()) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_TO_DELETED_COMMENT); } if (parentComment.getParentComment() == null && parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { tipsAndTricksComment.setParentComment(parentComment); } else { if (parentComment.getParentComment() != null) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } else if (!parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID); } } } return modelMapper .map(tipsAndTricksCommentRepo.save(tipsAndTricksComment), AddTipsAndTricksCommentDtoResponse.class); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId, AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest, User user) { TipsAndTricks tipsAndTricks = tipsAndTricksService.findById(tipsandtricksId); TipsAndTricksComment tipsAndTricksComment = modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class); tipsAndTricksComment.setUser(user); tipsAndTricksComment.setTipsAndTricks(tipsAndTricks); if (addTipsAndTricksCommentDtoRequest.getParentCommentId() != 0) { TipsAndTricksComment parentComment = tipsAndTricksCommentRepo.findById(addTipsAndTricksCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.isDeleted()) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_TO_DELETED_COMMENT); } if (parentComment.getParentComment() == null && parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { tipsAndTricksComment.setParentComment(parentComment); } else { if (parentComment.getParentComment() != null) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } else if (!parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID); } } } return modelMapper .map(tipsAndTricksCommentRepo.save(tipsAndTricksComment), AddTipsAndTricksCommentDtoResponse.class); } }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId, AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest, User user) { TipsAndTricks tipsAndTricks = tipsAndTricksService.findById(tipsandtricksId); TipsAndTricksComment tipsAndTricksComment = modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class); tipsAndTricksComment.setUser(user); tipsAndTricksComment.setTipsAndTricks(tipsAndTricks); if (addTipsAndTricksCommentDtoRequest.getParentCommentId() != 0) { TipsAndTricksComment parentComment = tipsAndTricksCommentRepo.findById(addTipsAndTricksCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.isDeleted()) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_TO_DELETED_COMMENT); } if (parentComment.getParentComment() == null && parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { tipsAndTricksComment.setParentComment(parentComment); } else { if (parentComment.getParentComment() != null) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } else if (!parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID); } } } return modelMapper .map(tipsAndTricksCommentRepo.save(tipsAndTricksComment), AddTipsAndTricksCommentDtoResponse.class); }  }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId, AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest, User user) { TipsAndTricks tipsAndTricks = tipsAndTricksService.findById(tipsandtricksId); TipsAndTricksComment tipsAndTricksComment = modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class); tipsAndTricksComment.setUser(user); tipsAndTricksComment.setTipsAndTricks(tipsAndTricks); if (addTipsAndTricksCommentDtoRequest.getParentCommentId() != 0) { TipsAndTricksComment parentComment = tipsAndTricksCommentRepo.findById(addTipsAndTricksCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.isDeleted()) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_TO_DELETED_COMMENT); } if (parentComment.getParentComment() == null && parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { tipsAndTricksComment.setParentComment(parentComment); } else { if (parentComment.getParentComment() != null) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } else if (!parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID); } } } return modelMapper .map(tipsAndTricksCommentRepo.save(tipsAndTricksComment), AddTipsAndTricksCommentDtoResponse.class); }  @Override AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId,
                                                   AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest,
                                                   User user); @Override PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId); @Override List<TipsAndTricksCommentDto> findAllReplies(Long parentCommentId); @Override void deleteById(Long id, User user); @Override void update(String text, Long id, User user); @Override void like(Long id, User user); @Override int countLikes(Long id); @Override int countReplies(Long id); @Override int countComments(Long tipAndTricksId); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId, AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest, User user) { TipsAndTricks tipsAndTricks = tipsAndTricksService.findById(tipsandtricksId); TipsAndTricksComment tipsAndTricksComment = modelMapper.map(addTipsAndTricksCommentDtoRequest, TipsAndTricksComment.class); tipsAndTricksComment.setUser(user); tipsAndTricksComment.setTipsAndTricks(tipsAndTricks); if (addTipsAndTricksCommentDtoRequest.getParentCommentId() != 0) { TipsAndTricksComment parentComment = tipsAndTricksCommentRepo.findById(addTipsAndTricksCommentDtoRequest.getParentCommentId()).orElseThrow( () -> new BadRequestException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (parentComment.isDeleted()) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_TO_DELETED_COMMENT); } if (parentComment.getParentComment() == null && parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { tipsAndTricksComment.setParentComment(parentComment); } else { if (parentComment.getParentComment() != null) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_THE_REPLY); } else if (!parentComment.getTipsAndTricks().getId().equals(tipsandtricksId)) { throw new BadRequestException(ErrorMessage.CANNOT_REPLY_WITH_OTHER_DIFFERENT_TIPSANDTRICKS_ID); } } } return modelMapper .map(tipsAndTricksCommentRepo.save(tipsAndTricksComment), AddTipsAndTricksCommentDtoResponse.class); }  @Override AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId,
                                                   AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest,
                                                   User user); @Override PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId); @Override List<TipsAndTricksCommentDto> findAllReplies(Long parentCommentId); @Override void deleteById(Long id, User user); @Override void update(String text, Long id, User user); @Override void like(Long id, User user); @Override int countLikes(Long id); @Override int countReplies(Long id); @Override int countComments(Long tipAndTricksId);  }