From 3daeecf08082bfd223170cdfb1be336fdbc3d974 Mon Sep 17 00:00:00 2001 From: pine_lee Date: Sun, 29 Sep 2024 02:46:05 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20error=20case=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/get_offer/common/ApiResponse.kt | 4 +- .../exception/ExceptionControllerAdvice.kt | 13 +++-- .../product/controller/ProductController.kt | 4 +- .../product/service/ProductListDto.kt | 1 + .../product/service/ProductService.kt | 1 + .../controller/ProductIntegrationTest.kt | 56 +++++++++---------- .../product/service/ProductServiceTest.kt | 9 +-- 7 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main/kotlin/com/get_offer/common/ApiResponse.kt b/src/main/kotlin/com/get_offer/common/ApiResponse.kt index c67f45a..4a31df6 100644 --- a/src/main/kotlin/com/get_offer/common/ApiResponse.kt +++ b/src/main/kotlin/com/get_offer/common/ApiResponse.kt @@ -1,5 +1,3 @@ -import org.springframework.http.HttpStatus - class ApiResponse( val status: String, val message: String? = null, @@ -13,7 +11,7 @@ class ApiResponse( ) } - fun error(message: String, statusCode: HttpStatus): ApiResponse { + fun error(message: String): ApiResponse { return ApiResponse( status = "ERROR", message = message, diff --git a/src/main/kotlin/com/get_offer/common/exception/ExceptionControllerAdvice.kt b/src/main/kotlin/com/get_offer/common/exception/ExceptionControllerAdvice.kt index 1d20b48..8d0ee0c 100644 --- a/src/main/kotlin/com/get_offer/common/exception/ExceptionControllerAdvice.kt +++ b/src/main/kotlin/com/get_offer/common/exception/ExceptionControllerAdvice.kt @@ -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 { - return ApiResponse.error(ex.message ?: "DEFAULT ERROR", HttpStatus.BAD_REQUEST) + fun handleIllegalStateException(ex: IllegalStateException): ResponseEntity> { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.error(ex.message ?: "DEFAULT ERROR")) } @ExceptionHandler - fun handleNotFoundException(ex: NotFoundException): ApiResponse { - return ApiResponse.error(ex.message, HttpStatus.NOT_FOUND) + fun handleNotFoundException(ex: NotFoundException): ResponseEntity> { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.error(ex.message)) } } \ No newline at end of file diff --git a/src/main/kotlin/com/get_offer/product/controller/ProductController.kt b/src/main/kotlin/com/get_offer/product/controller/ProductController.kt index b468814..ff356e6 100644 --- a/src/main/kotlin/com/get_offer/product/controller/ProductController.kt +++ b/src/main/kotlin/com/get_offer/product/controller/ProductController.kt @@ -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 { + return ApiResponse.success(productService.getProductDetail(id.toLong(), userId.toLong())) } } \ No newline at end of file diff --git a/src/main/kotlin/com/get_offer/product/service/ProductListDto.kt b/src/main/kotlin/com/get_offer/product/service/ProductListDto.kt index 122d143..0b2bd42 100644 --- a/src/main/kotlin/com/get_offer/product/service/ProductListDto.kt +++ b/src/main/kotlin/com/get_offer/product/service/ProductListDto.kt @@ -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, diff --git a/src/main/kotlin/com/get_offer/product/service/ProductService.kt b/src/main/kotlin/com/get_offer/product/service/ProductService.kt index 64d5fc4..c9d0c7b 100644 --- a/src/main/kotlin/com/get_offer/product/service/ProductService.kt +++ b/src/main/kotlin/com/get_offer/product/service/ProductService.kt @@ -20,6 +20,7 @@ class ProductService( val imageList = x.images.split(";") ProductListDto( + id = x.id, writerId = x.writerId, name = x.name, category = x.category, diff --git a/src/test/kotlin/com/get_offer/product/controller/ProductIntegrationTest.kt b/src/test/kotlin/com/get_offer/product/controller/ProductIntegrationTest.kt index 302dfbe..c9cf3b0 100644 --- a/src/test/kotlin/com/get_offer/product/controller/ProductIntegrationTest.kt +++ b/src/test/kotlin/com/get_offer/product/controller/ProductIntegrationTest.kt @@ -16,20 +16,20 @@ 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 @@ -37,21 +37,21 @@ class ProductIntegrationTest( 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")) } } \ No newline at end of file diff --git a/src/test/kotlin/com/get_offer/product/service/ProductServiceTest.kt b/src/test/kotlin/com/get_offer/product/service/ProductServiceTest.kt index 139bd45..767977a 100644 --- a/src/test/kotlin/com/get_offer/product/service/ProductServiceTest.kt +++ b/src/test/kotlin/com/get_offer/product/service/ProductServiceTest.kt @@ -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