diff --git a/src/test/java/site/goldenticket/common/utils/RestAssuredUtils.java b/src/test/java/site/goldenticket/common/utils/RestAssuredUtils.java index 5c8bec56..31b29e19 100644 --- a/src/test/java/site/goldenticket/common/utils/RestAssuredUtils.java +++ b/src/test/java/site/goldenticket/common/utils/RestAssuredUtils.java @@ -15,7 +15,7 @@ public static ExtractableResponse restAssuredGet(String url) { return RestAssured .given().log().all() .when() - .post(url) + .get(url) .then().log().all() .extract(); } @@ -33,6 +33,22 @@ public static ExtractableResponse restAssuredGetWithToken( .extract(); } + public static ExtractableResponse restAssuredGetWithTokenAndQueryParam( + String url, + String parameterName, + String parameterValues, + String accessToken + ) { + return RestAssured + .given().log().all() + .header("Authorization", "Bearer " + accessToken) + .queryParam(parameterName, parameterValues) + .when() + .get(url) + .then().log().all() + .extract(); + } + public static ExtractableResponse restAssuredPost( String url, Object request @@ -58,7 +74,7 @@ public static ExtractableResponse restAssuredPostWithToken( .contentType(APPLICATION_JSON_VALUE) .body(request) .when() - .get(url) + .post(url) .then().log().all() .extract(); } diff --git a/src/test/java/site/goldenticket/domain/product/controller/ProductControllerTest.java b/src/test/java/site/goldenticket/domain/product/controller/ProductControllerTest.java index d5497e9a..0b8d8ac6 100644 --- a/src/test/java/site/goldenticket/domain/product/controller/ProductControllerTest.java +++ b/src/test/java/site/goldenticket/domain/product/controller/ProductControllerTest.java @@ -1,10 +1,8 @@ package site.goldenticket.domain.product.controller; -import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; -import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -29,6 +27,7 @@ import static site.goldenticket.common.utils.NegoUtils.createNego; import static site.goldenticket.common.utils.OrderUtils.createOrder; import static site.goldenticket.common.utils.ProductUtils.createProduct; +import static site.goldenticket.common.utils.RestAssuredUtils.*; import static site.goldenticket.domain.product.constants.ProductStatus.EXPIRED; import static site.goldenticket.domain.product.constants.ProductStatus.SOLD_OUT; @@ -57,7 +56,7 @@ void getProduct() { String url = "/products/" + product.getId(); // when - final ExtractableResponse response = performGetRequest(url, false); + final ExtractableResponse response = restAssuredGet(url); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -71,7 +70,7 @@ void deleteProduct() { String url = "/products/" + product.getId(); // when - final ExtractableResponse response = performDeleteRequest(url); + final ExtractableResponse response = restAssuredDeleteWithToken(url, accessToken); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -88,7 +87,7 @@ void getProgressProducts() { String url = "/products/history/progress"; // when - final ExtractableResponse response = performGetRequest(url, true); + final ExtractableResponse response = restAssuredGetWithToken(url, accessToken); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -107,7 +106,7 @@ void getAllCompletedProducts() { String url = "/products/history/completed"; // when - final ExtractableResponse response = performGetRequest(url, true); + final ExtractableResponse response = restAssuredGetWithToken(url, accessToken); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -131,7 +130,7 @@ void getCompletedProductDetails() { String parameterValues = product.getProductStatus().toString(); // when - final ExtractableResponse response = performGetRequestWithQueryParam(url, parameterName, parameterValues, true); + final ExtractableResponse response = restAssuredGetWithTokenAndQueryParam(url, parameterName, parameterValues, accessToken); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -147,7 +146,7 @@ void deleteCompletedProduct(){ String url = "/products/history/completed/" + product.getId(); // when - final ExtractableResponse response = performDeleteRequest(url); + final ExtractableResponse response = restAssuredDeleteWithToken(url, accessToken); // then assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); @@ -193,42 +192,4 @@ private void saveOrder(Product product) { Order order = createOrder(product, user); orderRepository.save(order); } - - private RequestSpecification setupRequestSpecification(boolean needsAuthentication) { - RequestSpecification requestSpecification = RestAssured.given().log().all(); - - if (needsAuthentication) { - requestSpecification.header("Authorization", "Bearer " + accessToken); - } - - return requestSpecification; - } - - private ExtractableResponse performGetRequest(String url, boolean needsAuthentication) { - RequestSpecification requestSpecification = setupRequestSpecification(needsAuthentication); - - return requestSpecification - .when().get(url) - .then().log().all() - .extract(); - } - - private ExtractableResponse performGetRequestWithQueryParam(String url, String parameterName, String parameterValues, boolean needsAuthentication) { - RequestSpecification requestSpecification = setupRequestSpecification(needsAuthentication); - - return requestSpecification - .queryParam(parameterName, parameterValues) - .when().get(url) - .then().log().all() - .extract(); - } - - private ExtractableResponse performDeleteRequest(String url) { - RequestSpecification requestSpecification = setupRequestSpecification(true); - - return requestSpecification - .when().delete(url) - .then().log().all() - .extract(); - } }