@Test public void saveTest() { AddCommentDto addCommentDto = ModelUtils.getAddCommentDto(); Comment comment = ModelUtils.getComment(); when(placeService.findById(anyLong())).thenReturn(ModelUtils.getPlace()); when(userService.findByEmail(anyString())).thenReturn(ModelUtils.getUser()); when(modelMapper.map(addCommentDto, Comment.class)).thenReturn(comment); when(modelMapper.map(comment, CommentReturnDto.class)) .thenReturn(ModelUtils.getCommentReturnDto()); when(placeCommentRepo.save(any())).thenReturn(comment); placeCommentService.save(1L, addCommentDto, "email"); verify(placeCommentRepo, times(1)).save(comment); }
@Override public CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email) { Place place = placeService.findById(placeId); User user = userService.findByEmail(email); Comment comment = modelMapper.map(addCommentDto, Comment.class); comment.setPlace(place); comment.setUser(user); if (comment.getEstimate() != null) { comment.getEstimate().setUser(user); comment.getEstimate().setPlace(place); } comment.getPhotos().forEach(photo -> { if (photoService.findByName(photo.getName()).isPresent()) { throw new BadRequestException(ErrorMessage.PHOTO_IS_PRESENT); } photo.setUser(user); photo.setComment(comment); }); return modelMapper.map(placeCommentRepo.save(comment), CommentReturnDto.class); }
PlaceCommentServiceImpl implements PlaceCommentService { @Override public CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email) { Place place = placeService.findById(placeId); User user = userService.findByEmail(email); Comment comment = modelMapper.map(addCommentDto, Comment.class); comment.setPlace(place); comment.setUser(user); if (comment.getEstimate() != null) { comment.getEstimate().setUser(user); comment.getEstimate().setPlace(place); } comment.getPhotos().forEach(photo -> { if (photoService.findByName(photo.getName()).isPresent()) { throw new BadRequestException(ErrorMessage.PHOTO_IS_PRESENT); } photo.setUser(user); photo.setComment(comment); }); return modelMapper.map(placeCommentRepo.save(comment), CommentReturnDto.class); } }
PlaceCommentServiceImpl implements PlaceCommentService { @Override public CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email) { Place place = placeService.findById(placeId); User user = userService.findByEmail(email); Comment comment = modelMapper.map(addCommentDto, Comment.class); comment.setPlace(place); comment.setUser(user); if (comment.getEstimate() != null) { comment.getEstimate().setUser(user); comment.getEstimate().setPlace(place); } comment.getPhotos().forEach(photo -> { if (photoService.findByName(photo.getName()).isPresent()) { throw new BadRequestException(ErrorMessage.PHOTO_IS_PRESENT); } photo.setUser(user); photo.setComment(comment); }); return modelMapper.map(placeCommentRepo.save(comment), CommentReturnDto.class); }  }
PlaceCommentServiceImpl implements PlaceCommentService { @Override public CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email) { Place place = placeService.findById(placeId); User user = userService.findByEmail(email); Comment comment = modelMapper.map(addCommentDto, Comment.class); comment.setPlace(place); comment.setUser(user); if (comment.getEstimate() != null) { comment.getEstimate().setUser(user); comment.getEstimate().setPlace(place); } comment.getPhotos().forEach(photo -> { if (photoService.findByName(photo.getName()).isPresent()) { throw new BadRequestException(ErrorMessage.PHOTO_IS_PRESENT); } photo.setUser(user); photo.setComment(comment); }); return modelMapper.map(placeCommentRepo.save(comment), CommentReturnDto.class); }  @Override CommentReturnDto findById(Long id); @Override CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email); @Override void deleteById(Long id); @Override PageableDto getAllComments(Pageable pageable); }
PlaceCommentServiceImpl implements PlaceCommentService { @Override public CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email) { Place place = placeService.findById(placeId); User user = userService.findByEmail(email); Comment comment = modelMapper.map(addCommentDto, Comment.class); comment.setPlace(place); comment.setUser(user); if (comment.getEstimate() != null) { comment.getEstimate().setUser(user); comment.getEstimate().setPlace(place); } comment.getPhotos().forEach(photo -> { if (photoService.findByName(photo.getName()).isPresent()) { throw new BadRequestException(ErrorMessage.PHOTO_IS_PRESENT); } photo.setUser(user); photo.setComment(comment); }); return modelMapper.map(placeCommentRepo.save(comment), CommentReturnDto.class); }  @Override CommentReturnDto findById(Long id); @Override CommentReturnDto save(Long placeId, AddCommentDto addCommentDto, String email); @Override void deleteById(Long id); @Override PageableDto getAllComments(Pageable pageable);  }