From 4706e8c79c0dae9d36d2318f717e25a08d0dcbfb Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 15:48:39 +0900 Subject: [PATCH 1/6] =?UTF-8?q?test(community):=20communityBoard=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeleteCommunityBoardServiceTest.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java diff --git a/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java new file mode 100644 index 000000000..adc4f0530 --- /dev/null +++ b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java @@ -0,0 +1,103 @@ +package com.somemore.community.service.command; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import com.somemore.IntegrationTestSupport; +import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; +import com.somemore.community.repository.CommunityBoardRepository; +import com.somemore.community.usecase.command.CreateCommunityBoardUseCase; +import com.somemore.community.usecase.query.CommunityBoardQueryUseCase; +import com.somemore.global.exception.BadRequestException; +import com.somemore.global.exception.ExceptionMessage; +import org.assertj.core.api.ThrowableAssert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.UUID; + +class DeleteCommunityBoardServiceTest extends IntegrationTestSupport { + @Autowired + private DeleteCommunityBoardService deleteCommunityBoardService; + @Autowired + private CreateCommunityBoardUseCase createCommunityBoardUseCase; + @Autowired + private CommunityBoardQueryUseCase communityBoardQueryUseCase; + @Autowired + private CommunityBoardRepository communityBoardRepository; + + @AfterEach + void tearDown() { + communityBoardRepository.deleteAllInBatch(); + } + + @DisplayName("커뮤니티 게시글 id로 게시글을 삭제한다.") + @Test + void deleteCommunityBoardWithId() { + //given + CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + + UUID writerId = UUID.randomUUID(); + String imgUrl = "https://image.test.url/123"; + + Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); + + //when + deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //then + assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty(); + } + + @DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.") + @Test + void deleteCommunityBoardWithDeletedId() { + //given + CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + + UUID writerId = UUID.randomUUID(); + String imgUrl = "https://image.test.url/123"; + + Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); + deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage()); + } + + @DisplayName("작성자가 아닌 id로 커뮤니티 게시글을 삭제하고자 할 때 예외를 던진다.") + @Test + void deleteCommunityBoardWithNotWriterId() { + //given + CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + + UUID writerId = UUID.randomUUID(); + String imgUrl = "https://image.test.url/123"; + + Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); + + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); + } +} \ No newline at end of file From e0d6b16f0a351e24466ec6dd64b0e02b1c37a4f1 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 15:50:34 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat(community):=20CommunityBoard=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20Usecase=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20Service=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/DeleteCommunityBoardService.java | 40 +++++++++++++++++++ .../command/DeleteCommunityBoardUseCase.java | 7 ++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java create mode 100644 src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java diff --git a/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java new file mode 100644 index 000000000..02f58bbb8 --- /dev/null +++ b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java @@ -0,0 +1,40 @@ +package com.somemore.community.service.command; + +import com.somemore.community.domain.CommunityBoard; +import com.somemore.community.repository.CommunityBoardRepository; +import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase; +import com.somemore.global.exception.BadRequestException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.UUID; + +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD; +import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD; + +@RequiredArgsConstructor +@Transactional +@Service +class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase { + + private final CommunityBoardRepository communityBoardRepository; + + @Override + public void deleteCommunityBoard(UUID writerId, Long id) { + CommunityBoard communityBoard = communityBoardRepository.getCommunityBoardWithId(id) + .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); + + validateWriter(communityBoard, writerId); + + communityBoard.markAsDeleted(); + } + + private void validateWriter(CommunityBoard communityBoard, UUID writerId) { + if (communityBoard.getWriterId().equals(writerId)) { + return; + } + + throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); + } +} \ No newline at end of file diff --git a/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java b/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java new file mode 100644 index 000000000..88bca4548 --- /dev/null +++ b/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java @@ -0,0 +1,7 @@ +package com.somemore.community.usecase.command; + +import java.util.UUID; + +public interface DeleteCommunityBoardUseCase { + void deleteCommunityBoard(UUID writerId, Long id); +} \ No newline at end of file From 0dd592c0b8c43cb7cbefe6fe7f8d55b123ce299d Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 15:57:22 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat(community):=20CommunityBoard=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20ExceptionMessage=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/somemore/global/exception/ExceptionMessage.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/somemore/global/exception/ExceptionMessage.java b/src/main/java/com/somemore/global/exception/ExceptionMessage.java index 9035b71da..9f92eefcb 100644 --- a/src/main/java/com/somemore/global/exception/ExceptionMessage.java +++ b/src/main/java/com/somemore/global/exception/ExceptionMessage.java @@ -9,7 +9,8 @@ public enum ExceptionMessage { NOT_EXISTS_CENTER("존재하지 않는 기관 ID 입니다."), - NOT_EXISTS_COMMUNITY_BOARD("삭제된 게시글 입니다."), + NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."), + UNAUTHORIZED_COMMUNITY_BOARD("게시글 삭제 권한이 없습니다."), NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."), NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."), UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."), From 7013ef9bc67987f78343b6cce9314471bb5eb78a Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 17:24:43 +0900 Subject: [PATCH 4/6] =?UTF-8?q?test(community):=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/query/CommunityBoardQueryServiceTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java b/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java index 068e3b8d1..797edc4b8 100644 --- a/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java @@ -10,6 +10,7 @@ import com.somemore.community.dto.response.CommunityBoardGetResponseDto; import com.somemore.community.repository.CommunityBoardRepository; import com.somemore.community.usecase.command.CreateCommunityBoardUseCase; +import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase; import com.somemore.global.exception.BadRequestException; import com.somemore.global.exception.ExceptionMessage; import com.somemore.volunteer.domain.Volunteer; @@ -37,6 +38,8 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport { @Autowired CreateCommunityBoardUseCase createCommunityBoardUseCase; @Autowired + DeleteCommunityBoardUseCase deleteCommunityBoardUseCase; + @Autowired CommunityBoardQueryService communityBoardQueryService; @AfterEach @@ -209,7 +212,7 @@ void getCommunityBoardDetailWithDeletedId() { Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto1, savedVolunteer.getId(), imgUrl); - communityBoardRepository.deleteAllInBatch(); + deleteCommunityBoardUseCase.deleteCommunityBoard(savedVolunteer.getId(), communityId); //when ThrowableAssert.ThrowingCallable callable = () -> communityBoardQueryService.getCommunityBoardDetail(communityId); From 8bdaafff5b1834d089af0a8e0eb0f40848f82c94 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 17:26:51 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor(community):=20findById=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD=20=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/service/command/DeleteCommunityBoardService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java index 02f58bbb8..3241af844 100644 --- a/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java +++ b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java @@ -22,7 +22,7 @@ class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase { @Override public void deleteCommunityBoard(UUID writerId, Long id) { - CommunityBoard communityBoard = communityBoardRepository.getCommunityBoardWithId(id) + CommunityBoard communityBoard = communityBoardRepository.findById(id) .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); validateWriter(communityBoard, writerId); From 00e5980d93e1a1b223942eca0244fceea6c45996 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Tue, 26 Nov 2024 22:23:11 +0900 Subject: [PATCH 6/6] =?UTF-8?q?refactor(community):=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EC=82=AC=ED=95=AD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - findById 메서드로 추출(추상화 레벨 일관성) - ServiceTest 중복되는 부분 @BeforeEach로 추출 - CommunityBoard 메서드에 isWriter 추가 --- .../community/domain/CommunityBoard.java | 4 ++ .../command/DeleteCommunityBoardService.java | 12 +++-- .../DeleteCommunityBoardServiceTest.java | 49 +++++++------------ 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/somemore/community/domain/CommunityBoard.java b/src/main/java/com/somemore/community/domain/CommunityBoard.java index 457957dd9..16e667158 100644 --- a/src/main/java/com/somemore/community/domain/CommunityBoard.java +++ b/src/main/java/com/somemore/community/domain/CommunityBoard.java @@ -48,4 +48,8 @@ public CommunityBoard(UUID writerId, String title, String content, String imgUrl this.content = content; this.imgUrl = imgUrl; } + + public boolean isWriter(UUID writerId) { + return this.writerId.equals(writerId); + } } \ No newline at end of file diff --git a/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java index 3241af844..0e0fe857a 100644 --- a/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java +++ b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java @@ -22,16 +22,22 @@ class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase { @Override public void deleteCommunityBoard(UUID writerId, Long id) { - CommunityBoard communityBoard = communityBoardRepository.findById(id) - .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); + CommunityBoard communityBoard = getCommunityBoardById(id); validateWriter(communityBoard, writerId); communityBoard.markAsDeleted(); + + communityBoardRepository.save(communityBoard); + } + + private CommunityBoard getCommunityBoardById(Long id) { + return communityBoardRepository.findById(id) + .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); } private void validateWriter(CommunityBoard communityBoard, UUID writerId) { - if (communityBoard.getWriterId().equals(writerId)) { + if (communityBoard.isWriter(writerId)) { return; } diff --git a/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java index adc4f0530..578bec665 100644 --- a/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java +++ b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java @@ -12,6 +12,7 @@ import com.somemore.global.exception.ExceptionMessage; import org.assertj.core.api.ThrowableAssert; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +29,23 @@ class DeleteCommunityBoardServiceTest extends IntegrationTestSupport { @Autowired private CommunityBoardRepository communityBoardRepository; + private UUID writerId; + private Long communityId; + private String imgUrl; + + @BeforeEach + void setUp() { + CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + + writerId = UUID.randomUUID(); + imgUrl = "https://image.test.url/123"; + + communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); + } + @AfterEach void tearDown() { communityBoardRepository.deleteAllInBatch(); @@ -37,16 +55,6 @@ void tearDown() { @Test void deleteCommunityBoardWithId() { //given - CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목") - .content("커뮤니티 테스트 내용") - .build(); - - UUID writerId = UUID.randomUUID(); - String imgUrl = "https://image.test.url/123"; - - Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); - //when deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); @@ -58,15 +66,6 @@ void deleteCommunityBoardWithId() { @Test void deleteCommunityBoardWithDeletedId() { //given - CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목") - .content("커뮤니티 테스트 내용") - .build(); - - UUID writerId = UUID.randomUUID(); - String imgUrl = "https://image.test.url/123"; - - Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); //when @@ -82,16 +81,6 @@ void deleteCommunityBoardWithDeletedId() { @Test void deleteCommunityBoardWithNotWriterId() { //given - CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() - .title("커뮤니티 테스트 제목") - .content("커뮤니티 테스트 내용") - .build(); - - UUID writerId = UUID.randomUUID(); - String imgUrl = "https://image.test.url/123"; - - Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); - //when ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId); @@ -100,4 +89,4 @@ void deleteCommunityBoardWithNotWriterId() { .isThrownBy(callable) .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); } -} \ No newline at end of file +}