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 3f30f76 commit 001dc06
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
2 changes: 2 additions & 0 deletions product-order-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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,22 +1,30 @@
package com.example.productorderservice.payment;

import com.example.productorderservice.order.Order;
import org.springframework.stereotype.Component;
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("/payments")
class PaymentService {
private final PaymentPort paymentPort;

PaymentService(final PaymentPort paymentPort) {
this.paymentPort = paymentPort;
}

public void payment(final PaymentRequest request) {
@PostMapping
public ResponseEntity<Void> payment(@RequestBody final PaymentRequest request) {
Order order = paymentPort.getOrder(request.orderId());

final Payment payment = new Payment(order, request.cardNumber());

paymentPort.pay(payment.getPrice(), payment.getCardNumber());
paymentPort.save(payment);

return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.productorderservice.payment;

import com.example.productorderservice.order.OrderSteps;
import com.example.productorderservice.product.ApiTest;
import com.example.productorderservice.product.ProductSteps;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

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

public class PaymentApiTest extends ApiTest {

@Test
void 상품주문() {
ProductSteps.requestAddProduct(ProductSteps.getAddProductRequest());
OrderSteps.requestCreateOrder(OrderSteps.getCreateOrderRequest());
final var request = PaymentSteps.getPaymentRequest();

final var response = PaymentSteps.requestPayment(request);

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

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package com.example.productorderservice.payment;

import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.springframework.http.MediaType;

public class PaymentSteps {
public static PaymentRequest getPaymentRequest() {
final long orderId = 1L;
final String cardNumber = "1234-1234-1234";
return new PaymentRequest(orderId, cardNumber);
}

static ExtractableResponse<Response> requestPayment(final PaymentRequest request) {
return RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(request)
.when()
.post("/payments")
.then().log().all()
.extract();
}
}

0 comments on commit 001dc06

Please sign in to comment.