From ce520d41e6ed168abe1b1e518babcbaebf7d7fd5 Mon Sep 17 00:00:00 2001 From: jingyu Date: Fri, 28 Feb 2025 20:58:30 +0900 Subject: [PATCH 1/4] test(board): update imports for board --- .../board/application/service/BoardQueryServiceTest.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt b/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt index ca19d39..95a1353 100644 --- a/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt +++ b/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt @@ -7,8 +7,8 @@ import io.mockk.every import io.mockk.mockk import io.mockk.verify import me.nettee.board.application.domain.type.BoardStatus -import me.nettee.board.application.exception.BoardCommandErrorCode.BOARD_NOT_FOUND -import me.nettee.board.application.exception.BoardCommandException +import me.nettee.board.application.exception.BoardQueryErrorCode.BOARD_NOT_FOUND +import me.nettee.board.application.exception.BoardQueryException import me.nettee.board.application.model.BoardQueryModels.BoardDetail import me.nettee.board.application.model.BoardQueryModels.BoardSummary import me.nettee.board.application.port.BoardQueryPort @@ -60,7 +60,7 @@ class BoardQueryServiceTest : FreeSpec({ } returns Optional.empty() // when & then - val exception = shouldThrow { + val exception = shouldThrow { boardQueryService.getBoard(boardId) } exception.errorCode shouldBe BOARD_NOT_FOUND @@ -87,7 +87,7 @@ class BoardQueryServiceTest : FreeSpec({ ), BoardSummary( 2L, - "Suspended Board", + null, BoardStatus.SUSPENDED, now, now @@ -129,6 +129,8 @@ class BoardQueryServiceTest : FreeSpec({ result shouldBe expectedPage verify(exactly = 1) { boardQueryPort.findByStatusesList(statuses, pageable) } } + + } } }) From a2eb945221db3854d80b0fa8d3996a8417579faf Mon Sep 17 00:00:00 2001 From: jingyu Date: Fri, 28 Feb 2025 21:09:55 +0900 Subject: [PATCH 2/4] feat(board): set title to null for SUSPENDED status --- .../board/application/service/BoardQueryService.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/nettee/board/application/service/BoardQueryService.java b/src/main/java/me/nettee/board/application/service/BoardQueryService.java index 3f73ce6..4a42844 100644 --- a/src/main/java/me/nettee/board/application/service/BoardQueryService.java +++ b/src/main/java/me/nettee/board/application/service/BoardQueryService.java @@ -28,6 +28,14 @@ public BoardDetail getBoard(Long id) { @Override public Page findByStatuses(Set statuses, Pageable pageable) { - return boardQueryPort.findByStatusesList(statuses, pageable); + var boardPage = boardQueryPort.findByStatusesList(statuses, pageable); + + var filterBoardPage = boardPage.map(board -> + board.status() == BoardStatus.SUSPENDED + ? new BoardSummary(board.id(), null, board.status(), board.createdAt(), board.updatedAt()) + : board + ); + + return filterBoardPage; } } From 8c20da3c0b6ac4467106bd95088b107fbfbcb5d5 Mon Sep 17 00:00:00 2001 From: github-insu <93insu@gmail.com> Date: Fri, 28 Feb 2025 21:29:28 +0900 Subject: [PATCH 3/4] feat(board): throw exception when findById() board is in suspended status --- .../board/application/service/BoardQueryService.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/nettee/board/application/service/BoardQueryService.java b/src/main/java/me/nettee/board/application/service/BoardQueryService.java index 4a42844..24ef227 100644 --- a/src/main/java/me/nettee/board/application/service/BoardQueryService.java +++ b/src/main/java/me/nettee/board/application/service/BoardQueryService.java @@ -12,6 +12,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import static me.nettee.board.application.exception.BoardQueryErrorCode.BOARD_FORBIDDEN; import static me.nettee.board.application.exception.BoardQueryErrorCode.BOARD_NOT_FOUND; @Service @@ -22,8 +23,14 @@ public class BoardQueryService implements BoardReadUseCase, BoardReadByStatusesU @Override public BoardDetail getBoard(Long id) { - return boardQueryPort.findById(id) + BoardDetail boardDetail = boardQueryPort.findById(id) .orElseThrow(BOARD_NOT_FOUND::exception); + + if (boardDetail.status() == BoardStatus.SUSPENDED) { + throw BOARD_FORBIDDEN.exception(); + } + + return boardDetail; } @Override From 64844642a68c82dd3620fe4185a1f52498a751a9 Mon Sep 17 00:00:00 2001 From: github-insu <93insu@gmail.com> Date: Fri, 28 Feb 2025 22:03:08 +0900 Subject: [PATCH 4/4] test(board): throw exception when boardDetail status is in suspended --- .../service/BoardQueryServiceTest.kt | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt b/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt index 95a1353..a5b66aa 100644 --- a/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt +++ b/src/test/kotlin/me/nettee/board/application/service/BoardQueryServiceTest.kt @@ -3,10 +3,12 @@ package me.nettee.board.application.service import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe +import io.mockk.clearMocks import io.mockk.every import io.mockk.mockk import io.mockk.verify import me.nettee.board.application.domain.type.BoardStatus +import me.nettee.board.application.exception.BoardQueryErrorCode.BOARD_FORBIDDEN import me.nettee.board.application.exception.BoardQueryErrorCode.BOARD_NOT_FOUND import me.nettee.board.application.exception.BoardQueryException import me.nettee.board.application.model.BoardQueryModels.BoardDetail @@ -23,6 +25,10 @@ class BoardQueryServiceTest : FreeSpec({ val boardQueryPort = mockk() // mocking val boardQueryService = BoardQueryService(boardQueryPort) // 주입 + beforeTest { + clearMocks(boardQueryPort) + } + "BoardQueryService" - { "getBoard" - { "board detail 조회 by board id" { @@ -52,6 +58,35 @@ class BoardQueryServiceTest : FreeSpec({ verify(exactly = 1) { boardQueryPort.findById(boardId) } } + "[예외] boardDetail status가 suspended 일 때 Exception 발생" { + // given + val boardId = 1L + val now = Instant.now() + + val expectedDetail = BoardDetail( + boardId, + "Test Title", + "test Content", + BoardStatus.SUSPENDED, + now, + now + ) + + every { + boardQueryPort.findById(boardId) + } returns Optional.of(expectedDetail) + + // when + val exception = shouldThrow { + boardQueryService.getBoard(boardId) + } + + // then + exception.errorCode shouldBe BOARD_FORBIDDEN + + verify(exactly = 1) { boardQueryPort.findById(boardId) } + } + "board id에 해당하는 게시판이 없으면 예외 반환" { // given val boardId = 999L @@ -129,8 +164,6 @@ class BoardQueryServiceTest : FreeSpec({ result shouldBe expectedPage verify(exactly = 1) { boardQueryPort.findByStatusesList(statuses, pageable) } } - - } } })