forked from codesquad-members-2022/sidedish
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 주문 실패 시 현재 재고 수량을 반환해주도록 GlobalExceptionHandler 구현
- 인수 테스트 작성
- Loading branch information
Showing
7 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
BE/src/main/java/sidedish/com/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sidedish.com.exception; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(NotEnoughStockQuantityException.class) | ||
public Map<String, Long> exceptionHandler(NotEnoughStockQuantityException e) { | ||
long stockQuantity = e.getStockQuantity(); | ||
return Collections.singletonMap("currentStockQuantity", stockQuantity); | ||
} | ||
|
||
@ResponseStatus(value = HttpStatus.NOT_FOUND) | ||
@ExceptionHandler(NoSuchProductsException.class) | ||
public void exceptionHandler(NoSuchProductsException e) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
BE/src/main/java/sidedish/com/exception/NotEnoughStockQuantityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package sidedish.com.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import lombok.Getter; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@Getter | ||
public class NotEnoughStockQuantityException extends RuntimeException { | ||
private long stockQuantity; | ||
|
||
public NotEnoughStockQuantityException(String message) { | ||
super(message); | ||
public NotEnoughStockQuantityException(long stockQuantity) { | ||
this.stockQuantity = stockQuantity; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
BE/src/test/java/sidedish/com/acceptancetest/OrderAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package sidedish.com.acceptancetest; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import io.restassured.RestAssured; | ||
import net.minidev.json.JSONObject; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.jdbc.Sql; | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
@Sql({"/testdb/schema.sql", "/testdb/data.sql"}) | ||
@DisplayName("Order 인수테스트") | ||
class OrderAcceptanceTest { | ||
|
||
@LocalServerPort | ||
int port; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.port = port; | ||
} | ||
|
||
@Test | ||
void 재고가_충분할_경우_특정_상품_주문_성공() { | ||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("count", "10"); | ||
|
||
given() | ||
.accept(MediaType.APPLICATION_JSON_VALUE) | ||
.header("Content-type", "application/json") | ||
.body(requestBody) | ||
|
||
.when() | ||
.post("/api/products/1/order") | ||
|
||
.then() | ||
.statusCode(HttpStatus.CREATED.value()) | ||
.assertThat() | ||
.body("id", equalTo(1)); | ||
} | ||
|
||
@Test | ||
void 재고가_충분하지_않을_경우_특정_상품_주문_실패() { | ||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("count", 1000); | ||
|
||
given() | ||
.accept(MediaType.APPLICATION_JSON_VALUE) | ||
.header("Content-type", "application/json") | ||
.body(requestBody) | ||
|
||
.when() | ||
.post("/api/products/1/order") | ||
|
||
.then() | ||
.statusCode(HttpStatus.BAD_REQUEST.value()) | ||
.assertThat() | ||
.body("currentStockQuantity", equalTo(100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters