Skip to content

Commit

Permalink
Refactor into createProductAndGetId
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed Dec 20, 2024
1 parent cb4d989 commit 55cad05
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,10 @@ void shouldCreateOrder() throws Exception {
// Product 1
productParams.price = "1";
productParams.quantity = 99;
MvcResult result = createProduct(productParams).andReturn();
String product1Id = JsonPath.read(result.getResponse().getContentAsString(), "$.id");
String product1Id = createProductAndGetId(productParams);

// Product 2
result = createProduct(productParams).andReturn();
String product2Id = JsonPath.read(result.getResponse().getContentAsString(), "$.id");
String product2Id = createProductAndGetId(productParams);

// Create Order
CreateOrderParams createOrderParams =
Expand All @@ -294,8 +292,7 @@ void shouldCreateOrderRejectTooLongRequestId() throws Exception {
CreateProductParams productParams = CreateProductParams.sample();
productParams.price = "1";
productParams.quantity = 99;
MvcResult result = createProduct(productParams).andReturn();
String productId = JsonPath.read(result.getResponse().getContentAsString(), "$.id");
String productId = createProductAndGetId(productParams);

CreateOrderParams createOrderParams =
new CreateOrderParams("1".repeat(37), ImmutableMap.of(productId, 1));
Expand All @@ -308,8 +305,7 @@ void shouldCreateOrderApiHandleExceptionDueToNegativeQuantity() throws Exception
useNewUserAccessToken();

CreateProductParams productParams = CreateProductParams.sample();
MvcResult result = createProduct(productParams).andReturn();
String productId = JsonPath.read(result.getResponse().getContentAsString(), "$.id");
String productId = createProductAndGetId(productParams);

CreateOrderParams createOrderParams =
new CreateOrderParams("request-001", ImmutableMap.of(productId, -1));
Expand All @@ -325,8 +321,7 @@ void shouldCreateOrderApiHandleCreateOrderExceptionFromOrderService() throws Exc

CreateProductParams productParams = CreateProductParams.sample();
productParams.quantity = 0;
MvcResult result = createProduct(productParams).andReturn();
String productId = JsonPath.read(result.getResponse().getContentAsString(), "$.id");
String productId = createProductAndGetId(productParams);

CreateOrderParams createOrderParams =
new CreateOrderParams("request-001", ImmutableMap.of(productId, 1));
Expand Down Expand Up @@ -370,6 +365,11 @@ private ResultActions createProduct(CreateProductParams params) throws Exception
return mockMvc.perform(builder);
}

private String createProductAndGetId(CreateProductParams params) throws Exception {
MvcResult result = createProduct(params).andReturn();
return JsonPath.read(result.getResponse().getContentAsString(), "$.id");
}

private ResultActions getProduct(String id) throws Exception {
MockHttpServletRequestBuilder builder = get("/products/" + id);
addAuthHeader(builder);
Expand Down

0 comments on commit 55cad05

Please sign in to comment.