Skip to content

Commit 5ade3f8

Browse files
committed
fix(board): fix command url and dto and test code
1 parent 8fda352 commit 5ade3f8

File tree

4 files changed

+71
-70
lines changed

4 files changed

+71
-70
lines changed

src/main/java/me/nettee/board/adapter/driving/web/BoardCommandApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.springframework.web.bind.annotation.*;
1414

1515
@RestController
16-
@RequestMapping("/api/v1/board")
16+
@RequestMapping("/api/v1/boards")
1717
@RequiredArgsConstructor
1818
public class BoardCommandApi {
1919

src/main/java/me/nettee/board/adapter/driving/web/mapper/BoardDtoMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@
55
import me.nettee.board.adapter.driving.web.dto.BoardQueryDto.BoardDetailResponse;
66
import me.nettee.board.application.domain.Board;
77
import me.nettee.board.application.model.BoardReadDetailModel;
8-
import org.mapstruct.BeanMapping;
98
import org.mapstruct.Mapper;
10-
import org.mapstruct.Mapping;
119

1210
@Mapper(componentModel = "spring")
1311
public interface BoardDtoMapper {
1412

15-
// @BeanMapping(ignoreByDefault = true)
16-
// @Mapping(target = "title", source = "title")
17-
// @Mapping(target = "content", source = "content")
1813
Board toDomain(BoardCreateCommand command);
1914

20-
// @BeanMapping(ignoreByDefault = true)
21-
// @Mapping(target = "id", source = "id")
22-
// @Mapping(target = "title", source = "command.title")
23-
// @Mapping(target = "content", source = "command.content")
2415
Board toDomain(Long id, BoardUpdateCommand command);
2516

2617
BoardDetailResponse toDtoDetail(BoardReadDetailModel board);

src/test/kotlin/me/nettee/board/adapter/driving/web/BoardCommandControllerTest.kt renamed to src/test/kotlin/me/nettee/board/adapter/driving/web/BoardCommandApiTest.kt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension
2424
import org.springframework.test.web.servlet.*
2525

2626
@ExtendWith(SpringExtension::class)
27-
@WebMvcTest(BoardCommandController::class)
27+
@WebMvcTest(BoardCommandApi::class)
2828
@AutoConfigureMockMvc
29-
class BoardCommandControllerTest(
29+
class BoardCommandApiTest(
3030
@Autowired private val mvc: MockMvc,
3131
@Autowired private val objectMapper: ObjectMapper,
32-
@MockitoBean private val boardCreateUseCase : BoardCreateUseCase,
33-
@MockitoBean private val boardUpdateUseCase : BoardUpdateUseCase,
34-
@MockitoBean private val boardDeleteUseCase : BoardDeleteUseCase,
35-
@MockitoBean private val boardDtoMapper : BoardDtoMapper
36-
): FreeSpec({
32+
@MockitoBean private val boardCreateUseCase: BoardCreateUseCase,
33+
@MockitoBean private val boardUpdateUseCase: BoardUpdateUseCase,
34+
@MockitoBean private val boardDeleteUseCase: BoardDeleteUseCase,
35+
@MockitoBean private val boardDtoMapper: BoardDtoMapper
36+
) : FreeSpec({
3737

3838
// given
3939
val boardDomain = Board.builder().id(1L).title("테스트게시판").content("테스트게시판내용").status(BoardStatus.ACTIVE).build()
@@ -57,7 +57,7 @@ class BoardCommandControllerTest(
5757

5858
"2xx 응답 상태 반환" {
5959
// then
60-
mvcRequest(HttpMethod.POST, "/api/v1/board", emptyMap(), createCommand)
60+
mvcRequest(HttpMethod.POST, "/api/v1/boards", emptyMap(), createCommand)
6161
.andExpect {
6262
status { is2xxSuccessful() }
6363
jsonPath("board.id") { value(boardCreateResponse.id) }
@@ -75,7 +75,7 @@ class BoardCommandControllerTest(
7575

7676
// then
7777
"4xx 응답 상태 반환" {
78-
mvcRequest(HttpMethod.POST, "/api/v1/board", emptyMap(), failCreateCommand)
78+
mvcRequest(HttpMethod.POST, "/api/v1/boards", emptyMap(), failCreateCommand)
7979
.andExpect {
8080
status { is4xxClientError() }
8181
}
@@ -90,7 +90,7 @@ class BoardCommandControllerTest(
9090

9191
// then
9292
"4xx 응답 상태 반환" {
93-
mvcRequest(HttpMethod.POST, "/api/v1/board", emptyMap(), failCreateCommand)
93+
mvcRequest(HttpMethod.POST, "/api/v1/boards", emptyMap(), failCreateCommand)
9494
.andExpect {
9595
status { is4xxClientError() }
9696
}
@@ -111,7 +111,7 @@ class BoardCommandControllerTest(
111111

112112
"2xx 정상 상태 반환" {
113113
// then
114-
mvcRequest(HttpMethod.PATCH, "/api/v1/board/{id}", mapOf("id" to boardUpdateResponse.id), updateCommand)
114+
mvcRequest(HttpMethod.PATCH, "/api/v1/boards/{id}", mapOf("id" to boardUpdateResponse.id), updateCommand)
115115
.andExpect {
116116
status { is2xxSuccessful() }
117117
jsonPath("board.id") { value(boardUpdateResponse.id) }
@@ -129,7 +129,12 @@ class BoardCommandControllerTest(
129129

130130
"4xx 응답 상태 반환" {
131131
// then
132-
mvcRequest(HttpMethod.PATCH, "/api/v1/board/{id}", mapOf("id" to boardUpdateResponse.id), failUpdateCommand)
132+
mvcRequest(
133+
HttpMethod.PATCH,
134+
"/api/v1/boards/{id}",
135+
mapOf("id" to boardUpdateResponse.id),
136+
failUpdateCommand
137+
)
133138
.andExpect {
134139
status { is4xxClientError() }
135140
}
@@ -144,7 +149,12 @@ class BoardCommandControllerTest(
144149

145150
"4xx 응답 상태 반환" {
146151
// then
147-
mvcRequest(HttpMethod.PATCH, "/api/v1/board/{id}", mapOf("id" to boardUpdateResponse.id), failUpdateCommand)
152+
mvcRequest(
153+
HttpMethod.PATCH,
154+
"/api/v1/boards/{id}",
155+
mapOf("id" to boardUpdateResponse.id),
156+
failUpdateCommand
157+
)
148158
.andExpect {
149159
status { is4xxClientError() }
150160
}
@@ -157,22 +167,18 @@ class BoardCommandControllerTest(
157167
"[DELETE] 게시판 삭제 요청" - {
158168
"[정상 요청] 해당 커뮤니티 게시판 ID가 존재 할 때" - {
159169
"2xx 응답 상태 반환" {
160-
mvcRequest(HttpMethod.DELETE, "/api/v1/board/{id}", mapOf("id" to boardDomain.id), null)
161-
.andExpect {
162-
status { is2xxSuccessful() }
163-
}
170+
mvcRequest(HttpMethod.DELETE, "/api/v1/boards/{id}", mapOf("id" to boardDomain.id), null)
171+
.andExpect {
172+
status { is2xxSuccessful() }
173+
}
164174
}
165175
}
166-
167-
"[실패 요청] 해당 커뮤니티 게시판 ID 미존재 할 때" - {
168-
169-
}
170176
}
171177

172178
beforeSpec {
173-
`when` (boardDtoMapper.toDomain(any())).thenReturn(boardDomain)
174-
`when` (boardCreateUseCase.createBoard(any())).thenReturn(boardDomain)
175-
`when` (boardUpdateUseCase.updateBoard(any())).thenReturn(boardDomain)
179+
`when`(boardDtoMapper.toDomain(any())).thenReturn(boardDomain)
180+
`when`(boardCreateUseCase.createBoard(any())).thenReturn(boardDomain)
181+
`when`(boardUpdateUseCase.updateBoard(any())).thenReturn(boardDomain)
176182
doNothing().`when`(boardDeleteUseCase).deleteBoard(any())
177183
}
178184
})

src/test/kotlin/me/nettee/board/adapter/driving/web/BoardQueryApiTest.kt

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package me.nettee.board.adapter.driving.web
2+
23
import io.kotest.core.spec.style.FreeSpec
34
import me.nettee.board.adapter.driving.web.dto.BoardQueryDto.BoardDetailResponse
45
import me.nettee.board.adapter.driving.web.mapper.BoardDtoMapper
@@ -32,16 +33,16 @@ import java.time.Instant
3233
@WebMvcTest(BoardQueryApi::class)
3334
@AutoConfigureMockMvc
3435
class BoardQueryApiTest(
35-
@MockitoBean private val boardReadUseCase : BoardReadUseCase,
36-
@MockitoBean private val boardReadByStatusesUseCase : BoardReadByStatusesUseCase,
37-
@MockitoBean private val boardDtoMapper : BoardDtoMapper,
36+
@MockitoBean private val boardReadUseCase: BoardReadUseCase,
37+
@MockitoBean private val boardReadByStatusesUseCase: BoardReadByStatusesUseCase,
38+
@MockitoBean private val boardDtoMapper: BoardDtoMapper,
3839
@Autowired private val mvc: MockMvc
3940
) : FreeSpec({
4041

4142
lateinit var boardList: List<BoardReadSummaryModel>
4243

43-
"[GET]게시판 상세 조회" - {
44-
val mvcGet = fun(boardId: Long): ResultActionsDsl{
44+
"[GET]게시판 상세 조회" - {
45+
val mvcGet = fun(boardId: Long): ResultActionsDsl {
4546
return mvc.get("/api/v1/boards/$boardId") {
4647
contentType = MediaType.APPLICATION_JSON
4748
}
@@ -56,15 +57,15 @@ class BoardQueryApiTest(
5657

5758
"[4xx] 비정상 요청 상태 반환" {
5859
mvcGet(200L)
59-
.andExpect { status{is4xxClientError()}}
60+
.andExpect { status { is4xxClientError() } }
6061
.andDo { print() }
6162
.andReturn()
6263
}
6364

6465
}
6566

6667
"[GET] statuses 사용 게시판 목록 조회" - {
67-
val mvcGet = fun(statuses: Set<BoardStatus>, page: Int): ResultActionsDsl{
68+
val mvcGet = fun(statuses: Set<BoardStatus>, page: Int): ResultActionsDsl {
6869
return mvc.get("/api/v1/boards") {
6970
queryParam("statuses", statuses.joinToString(",") { it.name })
7071
queryParam("page", page.toString())
@@ -82,30 +83,52 @@ class BoardQueryApiTest(
8283
}
8384

8485
"[4xx] 비정상 요청 상태 반환" {
85-
val statuses = setOf( BoardStatus.REMOVED, BoardStatus.ACTIVE)
86-
mvcGet(statuses, 100)
86+
val statuses = setOf(BoardStatus.REMOVED, BoardStatus.ACTIVE)
87+
mvcGet(statuses, 100)
8788
.andExpect { status { is4xxClientError() } }
8889
.andDo { print() }
8990
.andReturn()
9091
}
9192
}
9293

94+
var boardReadSummaryModelPage: (
95+
List<BoardReadSummaryModel>,
96+
Pageable,
97+
Set<BoardStatus>
98+
) -> Page<BoardReadSummaryModel>
99+
93100
beforeSpec {
94-
val boardDetail = BoardReadDetailModel(1L,"title1", "content1", BoardStatus.ACTIVE, Instant.now(), null);
101+
boardReadSummaryModelPage = { boardList, pageable, boardStatus ->
102+
// val filteredBoards = boardList.filter { it.status in boardStatus }
103+
val filteredBoards = boardList
104+
.takeIf { it.isNotEmpty() }
105+
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
106+
107+
val pageContent = filteredBoards.drop(pageable.pageNumber * pageable.pageSize)
108+
.take(pageable.pageSize)
109+
.takeIf { it.isNotEmpty() }
110+
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
111+
112+
PageImpl(pageContent, pageable, filteredBoards.size.toLong())
113+
}
114+
115+
val boardDetail = BoardReadDetailModel(1L, "title1", "content1", BoardStatus.ACTIVE, Instant.now(), Instant.now(), null)
95116
// list 반복문 완성 코드 ***
96117
boardList = (1..14).flatMap {
97118
listOf(
98-
BoardReadSummaryModel(it.toLong(), "title$it", "content$it", BoardStatus.ACTIVE, Instant.now(), null),
99-
BoardReadSummaryModel(it.toLong(), "title$it", "content$it", BoardStatus.SUSPENDED, Instant.now(), null)
119+
BoardReadSummaryModel(it.toLong(), "title$it", "content$it", Instant.now(), Instant.now()),
120+
BoardReadSummaryModel(it.toLong(), "title$it", "content$it", Instant.now(), Instant.now())
100121
)
101122
}
102123

103-
`when` (boardReadUseCase.getBoard(1L)).thenAnswer { boardDetail }
124+
`when`(boardReadUseCase.getBoard(1L)).thenAnswer { boardDetail }
104125
`when`(boardReadUseCase.getBoard(argThat { it != 1L })).thenThrow(ResponseStatusException(HttpStatus.NOT_FOUND))
105-
`when`(boardReadByStatusesUseCase.findByStatuses(
106-
any<Set<BoardStatus>>(),
107-
any<Pageable>()
108-
)).thenAnswer { invocation ->
126+
`when`(
127+
boardReadByStatusesUseCase.findByStatuses(
128+
any<Set<BoardStatus>>(),
129+
any<Pageable>()
130+
)
131+
).thenAnswer { invocation ->
109132
val statuses = invocation.getArgument<Set<BoardStatus>>(0)
110133
val pageable = invocation.getArgument<Pageable>(1)
111134
boardReadSummaryModelPage(boardList, pageable, statuses)
@@ -114,23 +137,4 @@ class BoardQueryApiTest(
114137
}
115138
})
116139

117-
fun boardReadSummaryModelPage(
118-
boardList: List<BoardReadSummaryModel>,
119-
pageable: Pageable,
120-
boardStatus: Set<BoardStatus>
121-
): Page<BoardReadSummaryModel> {
122-
println("Board Statuses: $boardStatus")
123-
println("Board List: ${boardList.map { it.status }}")
124-
125-
val filteredBoards = boardList.filter { it.status in boardStatus }
126-
.takeIf { it.isNotEmpty() }
127-
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
128-
129-
val pageContent = filteredBoards.drop(pageable.pageNumber * pageable.pageSize)
130-
.take(pageable.pageSize)
131-
.takeIf { it.isNotEmpty() }
132-
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
133-
134-
return PageImpl(pageContent, pageable, filteredBoards.size.toLong())
135-
}
136140

0 commit comments

Comments
 (0)