Skip to content

Commit

Permalink
Feat: 주문 실패 시 현재 재고 수량을 반환해주도록 GlobalExceptionHandler 구현
Browse files Browse the repository at this point in the history
- 인수 테스트 작성
  • Loading branch information
Louie-03 committed Apr 28, 2022
1 parent ecac1da commit d84f4a6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class OrderController {

@PostMapping("/api/products/{id}/order")
@ResponseStatus(HttpStatus.CREATED)
public OrderSaveResponse order(@PathVariable Long id, @RequestBody @Valid OrderSaveRequest orderSaveRequest) {
public OrderSaveResponse order(@PathVariable Long id,
@RequestBody @Valid OrderSaveRequest orderSaveRequest) {
return orderService.save(id, orderSaveRequest.getCount());
}

}
4 changes: 2 additions & 2 deletions BE/src/main/java/sidedish/com/domain/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Product(Long id, DiscountPolicy discountPolicy,
String description, long originalPrice, long stockQuantity, String mealCategory,
String bestCategory) {
if (stockQuantity < 0) {
throw new NotEnoughStockQuantityException("재고가 충분하지 않습니다");
throw new NotEnoughStockQuantityException(stockQuantity);
}
this.id = id;
this.discountPolicy = discountPolicy;
Expand All @@ -53,7 +53,7 @@ private long calculateFixedPrice() {

public void minusStockQuantity(long count) {
if (count > stockQuantity) {
throw new NotEnoughStockQuantityException("재고가 충분하지 않습니다");
throw new NotEnoughStockQuantityException(stockQuantity);
}
stockQuantity -= count;
}
Expand Down
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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NoSuchProductsException extends RuntimeException {

}
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;
}
}
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));
}
}
11 changes: 11 additions & 0 deletions BE/src/test/resources/testdb/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
drop table if exists orders;
drop table if exists product_image;
drop table if exists product;
drop table if exists discount_policy;
Expand Down Expand Up @@ -40,3 +41,13 @@ create table product_image
image_url varchar(1000) not null,
foreign key (product_id) references product (id)
);

create table orders
(
id bigint auto_increment primary key,
product_id bigint not null,
total_price bigint not null,
count bigint not null,
delivery_price bigint not null,
foreign key (product_id) references product (id)
);

0 comments on commit d84f4a6

Please sign in to comment.