Skip to content

Commit

Permalink
Merge pull request #28 from leung018/request-id-length-limitation
Browse files Browse the repository at this point in the history
Request-id-length-limitation
  • Loading branch information
leung018 authored Dec 20, 2024
2 parents 50a4214 + 55cad05 commit 3bdf0a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.leungcheng.spring_simple_backend.domain.order.OrderService;
import com.leungcheng.spring_simple_backend.domain.order.PurchaseItems;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -30,5 +31,6 @@ Order newOrder(
return orderService.createOrder(userId, purchaseItems, createOrderRequest.requestId());
}

public record CreateOrderRequest(String requestId, Map<String, Integer> productIdToQuantity) {}
public record CreateOrderRequest(
@Size(max = 36) String requestId, Map<String, Integer> productIdToQuantity) {}
}
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 @@ -287,13 +285,27 @@ void shouldCreateOrder() throws Exception {
.andExpect(jsonPath("$.buyerUserId").value(userId));
}

@Test
void shouldCreateOrderRejectTooLongRequestId() throws Exception {
useNewUserAccessToken();

CreateProductParams productParams = CreateProductParams.sample();
productParams.price = "1";
productParams.quantity = 99;
String productId = createProductAndGetId(productParams);

CreateOrderParams createOrderParams =
new CreateOrderParams("1".repeat(37), ImmutableMap.of(productId, 1));

createOrder(createOrderParams).andExpect(status().isBadRequest());
}

@Test
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 @@ -309,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 @@ -354,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 3bdf0a3

Please sign in to comment.