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 298e366 commit 8990a65
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ public ResponseEntity<GetProductResponse> getProduct(@PathVariable final long pr
return ResponseEntity.ok(response);
}

public void updateProduct(final Long productId, final UpdateProductRequest request) {
@PatchMapping("/{productId}")
@Transactional
public ResponseEntity<Void> updateProduct(
@PathVariable final Long productId,
@RequestBody final UpdateProductRequest request) {
final Product product = productPort.getProduct(productId);

product.update(request.name(), request.price(), request.discountPolicy());

productPort.save(product);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.example.productorderservice.product;

import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

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

class ProductApiTest extends ApiTest {

@Autowired
ProductRepository productRepository;

@Test
void 상품등록() {
final var request = ProductSteps.getAddProductRequest();
Expand All @@ -19,7 +26,7 @@ class ProductApiTest extends ApiTest {
@Test
void 상품조회() {
ProductSteps.requestAddProduct(ProductSteps.getAddProductRequest());
Long productId = 1L;
final long productId = 1L;

final var response = ProductSteps.requestInquiryProduct(productId);

Expand All @@ -28,4 +35,15 @@ class ProductApiTest extends ApiTest {
assertThat(response.jsonPath().getString("name")).isEqualTo("상품명");
assertThat(response.jsonPath().getString("price")).isEqualTo("1000");
}
@Test
void 상품수정() {
ProductSteps.requestAddProduct(ProductSteps.getAddProductRequest());
final long productId = 1L;
final ExtractableResponse<Response> response = ProductSteps.requestUpdateProduct(productId);
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Assertions.assertEquals("상품 수정", productRepository.findById(1L).get().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ProductServiceTest {
void updateProduct() {
productService.addProduct(ProductSteps.getAddProductRequest());
final Long productId = 1L;
final UpdateProductRequest request = new UpdateProductRequest("상품 수정", 2000, DiscountPolicy.NONE);
final UpdateProductRequest request = ProductSteps.getUpdateProductRequest();

productService.updateProduct(productId, request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ public static ExtractableResponse<Response> requestInquiryProduct(final Long pro
.then().log().all()
.extract();
}

public static UpdateProductRequest getUpdateProductRequest() {
return new UpdateProductRequest("상품 수정", 2000, DiscountPolicy.NONE);
}

public static ExtractableResponse<Response> requestUpdateProduct(final long productId) {
return RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(ProductSteps.getUpdateProductRequest())
.when()
.patch("/products/{productId}", productId)
.then().log().all()
.extract();
}
}

0 comments on commit 8990a65

Please sign in to comment.