Skip to content

Commit

Permalink
[Order API TDD] API 테스트로 전환하기
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 27, 2024
1 parent d6f496f commit 1a5ce7a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
2 changes: 2 additions & 0 deletions product-order-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ final UpdateProductRequest request = new UpdateProductRequest(name, price, disco
**API 테스트로 전환하기**

> [API 테스트로 전환하기]()
**JPA 적용**

## 주문 결제 기능 구현하기
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.example.productorderservice.order;

import com.example.productorderservice.product.Product;
import org.springframework.stereotype.Component;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Component
@RestController
@RequestMapping("/orders")
class OrderService {
private final OrderPort orderPort;

OrderService(final OrderPort orderPort) {
this.orderPort = orderPort;
}

public void createOrder(final CreateOrderRequest request) {
@PostMapping
public ResponseEntity<Void> createOrder(@RequestBody final CreateOrderRequest request) {
final Product product = orderPort.getProductById(request.productId());

final Order order = new Order(product, request.quantity());

orderPort.save(order);

return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.productorderservice.order;

import com.example.productorderservice.product.ApiTest;
import com.example.productorderservice.product.ProductSteps;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import static org.assertj.core.api.Assertions.assertThat;

class OrderApiTest extends ApiTest {

@Test
void 상품주문() {
ProductSteps.requestAddProduct(ProductSteps.getAddProductRequest());
final CreateOrderRequest request = getCreateOrderRequest();

final ExtractableResponse<Response> response = RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(request)
.when()
.post("/orders")
.then()
.log().all().extract();

assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED.value());
}

private static CreateOrderRequest getCreateOrderRequest() {
final long productId = 1L;
final int quantity = 2;
return new CreateOrderRequest(productId, quantity);
}

}

This file was deleted.

0 comments on commit 1a5ce7a

Please sign in to comment.