From 686995aeb6ae345584edd485e9e0ca4ed574ddf1 Mon Sep 17 00:00:00 2001 From: ASak1104 Date: Sun, 4 Aug 2024 18:14:44 +0900 Subject: [PATCH] test: add post deleting service test --- .../masil/post/service/PostServiceTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/team/silvertown/masil/post/service/PostServiceTest.java b/src/test/java/team/silvertown/masil/post/service/PostServiceTest.java index 5d44fdf0..e0e8a7e2 100644 --- a/src/test/java/team/silvertown/masil/post/service/PostServiceTest.java +++ b/src/test/java/team/silvertown/masil/post/service/PostServiceTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -21,6 +22,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; import team.silvertown.masil.common.exception.DataNotFoundException; +import team.silvertown.masil.common.exception.ForbiddenException; import team.silvertown.masil.common.map.KakaoPoint; import team.silvertown.masil.common.scroll.OrderType; import team.silvertown.masil.common.scroll.dto.NormalListRequest; @@ -172,6 +174,56 @@ void cleanUp() { assertThat(actual.pins()).hasSize(pinSize); } + @Test + void 산책로_포스트를_삭제한다() { + // given + Post post = postRepository.save(PostTexture.createDependentPost(user, 3)); + + entityManager.clear(); + + // when + postService.deleteById(user.getId(), post.getId()); + + // then + Optional actual = postRepository.findById(post.getId()); + + assertThat(actual).isEmpty(); + } + + @Test + void 존재하지_않는_산책로_포스트는_삭제에_실패한다() { + // given + Post post = postRepository.save(PostTexture.createDependentPost(user, 3)); + + entityManager.clear(); + + // when + ThrowingCallable deleteNotExistPost = () -> postService.deleteById(user.getId(), post.getId() + 1); + + // then + + assertThatExceptionOfType(DataNotFoundException.class) + .isThrownBy(deleteNotExistPost) + .withMessage(PostErrorCode.POST_NOT_FOUND.getMessage()); + } + + @Test + void 로그인한_사용자와_산책로_포스트_작성자가_다를_경우_삭제에_실패한다() { + // given + Post post = postRepository.save(PostTexture.createDependentPost(user, 3)); + + entityManager.clear(); + + // when + ThrowingCallable deleteByNotMatchingUser = () -> postService.deleteById(user.getId() + 1, post.getId()); + + // then + + assertThatExceptionOfType(ForbiddenException.class) + .isThrownBy(deleteByNotMatchingUser) + .withMessage(PostErrorCode.AUTHOR_NOT_MATCHING.getMessage()); + } + @Test void 같은_IP로_산책로_포스트_단일_조회_시_조회수가_한_번만_증가한다() { // given