@Test public void deleteCommentUserHasNoPermissionThrowException() { User user = ModelUtils.getUser(); user.setId(2L); Long commentId = 1L; EcoNewsComment ecoNewsComment = getEcoNewsComment(); ecoNewsComment.setUser(user); when(ecoNewsCommentRepo.findById(commentId)).thenReturn(java.util.Optional.ofNullable(ecoNewsComment)); BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> ecoNewsCommentService.deleteById(commentId, ModelUtils.getUser())); assertEquals(ErrorMessage.USER_HAS_NO_PERMISSION, badRequestException.getMessage()); }
@Override public void deleteById(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } comment.setDeleted(true); ecoNewsCommentRepo.save(comment); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public void deleteById(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } comment.setDeleted(true); ecoNewsCommentRepo.save(comment); } }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public void deleteById(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } comment.setDeleted(true); ecoNewsCommentRepo.save(comment); }  }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { @Override public void deleteById(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } comment.setDeleted(true); ecoNewsCommentRepo.save(comment); }  @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 void deleteById(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } comment.setDeleted(true); ecoNewsCommentRepo.save(comment); }  @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);  }