Skip to content

Commit

Permalink
fix : error case 처리 방식 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
soleu committed Sep 28, 2024
1 parent 2ed0081 commit 3daeecf
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
4 changes: 1 addition & 3 deletions src/main/kotlin/com/get_offer/common/ApiResponse.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.springframework.http.HttpStatus

class ApiResponse<T>(
val status: String,
val message: String? = null,
Expand All @@ -13,7 +11,7 @@ class ApiResponse<T>(
)
}

fun <T> error(message: String, statusCode: HttpStatus): ApiResponse<T> {
fun <T> error(message: String): ApiResponse<T> {
return ApiResponse(
status = "ERROR",
message = message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package com.get_offer.common.exception

import ApiResponse
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@ControllerAdvice
@RestControllerAdvice
class ExceptionControllerAdvice {
@ExceptionHandler
fun handleIllegalStateException(ex: IllegalStateException): ApiResponse<Any> {
return ApiResponse.error(ex.message ?: "DEFAULT ERROR", HttpStatus.BAD_REQUEST)
fun handleIllegalStateException(ex: IllegalStateException): ResponseEntity<ApiResponse<Any>> {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ex.message ?: "DEFAULT ERROR"))
}

@ExceptionHandler
fun handleNotFoundException(ex: NotFoundException): ApiResponse<Any> {
return ApiResponse.error(ex.message, HttpStatus.NOT_FOUND)
fun handleNotFoundException(ex: NotFoundException): ResponseEntity<ApiResponse<Any>> {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.error(ex.message))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ProductController(
}

@GetMapping("{id}/detail")
fun getProductDetail(@PathVariable id: String, @RequestParam userId: String): ProductDetailDto {
return productService.getProductDetail(id.toLong(), userId.toLong())
fun getProductDetail(@PathVariable id: String, @RequestParam userId: String): ApiResponse<ProductDetailDto> {
return ApiResponse.success(productService.getProductDetail(id.toLong(), userId.toLong()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.get_offer.product.domain.ProductStatus
import java.time.LocalDateTime

data class ProductListDto(
val id: Long?,
val writerId: Long,
val name: String,
val category: Category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProductService(
val imageList = x.images.split(";")

ProductListDto(
id = x.id,
writerId = x.writerId,
name = x.name,
category = x.category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@ class ProductIntegrationTest(
@Autowired val mockMvc: MockMvc,
) {
@Test
fun activeProductListIntegrationTest() {
fun productListIntegrationTest() {
mockMvc.perform(
get("/products").param("userId", "1")
).andDo(MockMvcResultHandlers.print()).andExpect(status().isOk).andExpect(jsonPath("$.size()").value(1))
.andExpect(jsonPath("$[0].id").value("1"))
.andExpect(jsonPath("$[0].writerId").value("1"))
.andExpect(jsonPath("$[0].name").value("nintendo"))
.andExpect(jsonPath("$[0].category").value("GAMES"))
.andExpect(jsonPath("$[0].thumbNail").value("png"))
.andExpect(jsonPath("$[0].currentPrice").value("10000"))
.andExpect(jsonPath("$[0].status").value("IN_PROGRESS"))
.andExpect(jsonPath("$[0].startDate").value("2024-01-02T00:00:00"))
.andExpect(jsonPath("$[0].endDate").value("2024-01-04T00:00:00"))
.andExpect(jsonPath("$[0].isMine").value("true"))
).andDo(MockMvcResultHandlers.print()).andExpect(status().isOk).andExpect(jsonPath("$.size()").value(3))
.andExpect(jsonPath("$.data[0].id").value("1"))
.andExpect(jsonPath("$.data[0].writerId").value("1"))
.andExpect(jsonPath("$.data[0].name").value("nintendo"))
.andExpect(jsonPath("$.data[0].category").value("GAMES"))
.andExpect(jsonPath("$.data[0].thumbNail").value("png"))
.andExpect(jsonPath("$.data[0].currentPrice").value("10000"))
.andExpect(jsonPath("$.data[0].status").value("IN_PROGRESS"))
.andExpect(jsonPath("$.data[0].startDate").value("2024-01-02T00:00:00"))
.andExpect(jsonPath("$.data[0].endDate").value("2024-01-04T00:00:00"))
.andExpect(jsonPath("$.data[0].isMine").value("true"))
}

@Test
fun productDetailIntegrationTest() {
mockMvc.perform(
get("/products/1/detail").param("userId", "1")
).andDo(MockMvcResultHandlers.print()).andExpect(status().isOk)
.andExpect(jsonPath("$.id").value("1"))
.andExpect(jsonPath("$.name").value("nintendo"))
.andExpect(jsonPath("$.writerId").value("1"))
.andExpect(jsonPath("$.writerNickname").value("test"))
.andExpect(jsonPath("$.writerProfileImg").value("https://drive.google.com/file/d/1R9EIOoEWWgPUhY6e-t4VFuqMgknl7rm8/view?usp=sharing"))
.andExpect(jsonPath("$.name").value("nintendo"))
.andExpect(jsonPath("$.category").value("GAMES"))
.andExpect(jsonPath("$.images.size()").value(2))
.andExpect(jsonPath("$.images[0]").value("png"))
.andExpect(jsonPath("$.description").value("닌텐도 새 제품"))
.andExpect(jsonPath("$.startPrice").value("5000"))
.andExpect(jsonPath("$.currentPrice").value("10000"))
.andExpect(jsonPath("$.status").value("IN_PROGRESS"))
.andExpect(jsonPath("$.startDate").value("2024-01-02T00:00:00"))
.andExpect(jsonPath("$.endDate").value("2024-01-04T00:00:00"))
.andExpect(jsonPath("$.isMine").value("true"))
.andExpect(jsonPath("$.data.id").value("1"))
.andExpect(jsonPath("$.data.name").value("nintendo"))
.andExpect(jsonPath("$.data.writerId").value("1"))
.andExpect(jsonPath("$.data.writerNickname").value("test"))
.andExpect(jsonPath("$.data.writerProfileImg").value("https://drive.google.com/file/d/1R9EIOoEWWgPUhY6e-t4VFuqMgknl7rm8/view?usp=sharing"))
.andExpect(jsonPath("$.data.name").value("nintendo"))
.andExpect(jsonPath("$.data.category").value("GAMES"))
.andExpect(jsonPath("$.data.images.size()").value(2))
.andExpect(jsonPath("$.data.images[0]").value("png"))
.andExpect(jsonPath("$.data.description").value("닌텐도 새 제품"))
.andExpect(jsonPath("$.data.startPrice").value("5000"))
.andExpect(jsonPath("$.data.currentPrice").value("10000"))
.andExpect(jsonPath("$.data.status").value("IN_PROGRESS"))
.andExpect(jsonPath("$.data.startDate").value("2024-01-02T00:00:00"))
.andExpect(jsonPath("$.data.endDate").value("2024-01-04T00:00:00"))
.andExpect(jsonPath("$.data.isMine").value("true"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ class ProductServiceTest {
LocalDateTime.now()
)

`when`(mockProductRepository.findAllByStatusInOrderByEndDateDesc(any())).thenReturn(
listOf(
givenProduct,
givenProduct2
`when`(
mockProductRepository.findAllByStatusInOrderByEndDateDesc(
listOf(ProductStatus.IN_PROGRESS, ProductStatus.WAIT)
)
).thenReturn(
listOf(givenProduct, givenProduct2)
)

// when
Expand Down

0 comments on commit 3daeecf

Please sign in to comment.