Skip to content

Commit

Permalink
Merge pull request #235 from Yanol-Market/feature/172
Browse files Browse the repository at this point in the history
Feature/172
  • Loading branch information
yurim0628 authored Jan 26, 2024
2 parents 5140013 + 7f4bc13 commit 407461d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 48 deletions.
20 changes: 18 additions & 2 deletions src/test/java/site/goldenticket/common/utils/RestAssuredUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static ExtractableResponse<Response> restAssuredGet(String url) {
return RestAssured
.given().log().all()
.when()
.post(url)
.get(url)
.then().log().all()
.extract();
}
Expand All @@ -33,6 +33,22 @@ public static ExtractableResponse<Response> restAssuredGetWithToken(
.extract();
}

public static ExtractableResponse<Response> 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<Response> restAssuredPost(
String url,
Object request
Expand All @@ -58,7 +74,7 @@ public static ExtractableResponse<Response> restAssuredPostWithToken(
.contentType(APPLICATION_JSON_VALUE)
.body(request)
.when()
.get(url)
.post(url)
.then().log().all()
.extract();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -57,7 +56,7 @@ void getProduct() {
String url = "/products/" + product.getId();

// when
final ExtractableResponse<Response> response = performGetRequest(url, false);
final ExtractableResponse<Response> response = restAssuredGet(url);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand All @@ -71,7 +70,7 @@ void deleteProduct() {
String url = "/products/" + product.getId();

// when
final ExtractableResponse<Response> response = performDeleteRequest(url);
final ExtractableResponse<Response> response = restAssuredDeleteWithToken(url, accessToken);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand All @@ -88,7 +87,7 @@ void getProgressProducts() {
String url = "/products/history/progress";

// when
final ExtractableResponse<Response> response = performGetRequest(url, true);
final ExtractableResponse<Response> response = restAssuredGetWithToken(url, accessToken);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand All @@ -107,7 +106,7 @@ void getAllCompletedProducts() {
String url = "/products/history/completed";

// when
final ExtractableResponse<Response> response = performGetRequest(url, true);
final ExtractableResponse<Response> response = restAssuredGetWithToken(url, accessToken);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand All @@ -131,7 +130,7 @@ void getCompletedProductDetails() {
String parameterValues = product.getProductStatus().toString();

// when
final ExtractableResponse<Response> response = performGetRequestWithQueryParam(url, parameterName, parameterValues, true);
final ExtractableResponse<Response> response = restAssuredGetWithTokenAndQueryParam(url, parameterName, parameterValues, accessToken);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand All @@ -147,7 +146,7 @@ void deleteCompletedProduct(){
String url = "/products/history/completed/" + product.getId();

// when
final ExtractableResponse<Response> response = performDeleteRequest(url);
final ExtractableResponse<Response> response = restAssuredDeleteWithToken(url, accessToken);

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
Expand Down Expand Up @@ -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<Response> performGetRequest(String url, boolean needsAuthentication) {
RequestSpecification requestSpecification = setupRequestSpecification(needsAuthentication);

return requestSpecification
.when().get(url)
.then().log().all()
.extract();
}

private ExtractableResponse<Response> 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<Response> performDeleteRequest(String url) {
RequestSpecification requestSpecification = setupRequestSpecification(true);

return requestSpecification
.when().delete(url)
.then().log().all()
.extract();
}
}

0 comments on commit 407461d

Please sign in to comment.