Skip to content

Commit

Permalink
temp (rebase later)
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed Dec 11, 2024
1 parent 544a67d commit 845e620
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.leungcheng.spring_simple_backend.controller;

import com.google.common.collect.ImmutableMap;
import com.leungcheng.spring_simple_backend.auth.UserAuthenticatedInfoToken;
import com.leungcheng.spring_simple_backend.domain.order.Order;
import com.leungcheng.spring_simple_backend.domain.order.OrderService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
@Autowired private OrderService orderService;

@PostMapping("/orders")
@ResponseStatus(HttpStatus.CREATED)
Order newOrder(
@Valid @RequestBody CreateOrderRequest createOrderRequest,
UserAuthenticatedInfoToken authToken) {
throw new UnsupportedOperationException("Not implemented");
}

public record CreateOrderRequest(
String requestId, ImmutableMap<String, Integer> productIdToQuantity) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.google.common.collect.ImmutableMap;
import com.jayway.jsonpath.JsonPath;
import com.leungcheng.spring_simple_backend.domain.Product;
import com.leungcheng.spring_simple_backend.domain.ProductRepository;
import com.leungcheng.spring_simple_backend.domain.User;
import com.leungcheng.spring_simple_backend.domain.UserRepository;
import com.leungcheng.spring_simple_backend.validation.ObjectValidator.ObjectValidationException;
import java.math.BigDecimal;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -253,6 +255,30 @@ void shouldGetAccountInfo() throws Exception {
assertBigDecimalEquals(User.INITIAL_BALANCE, actualBalance);
}

@Test
void shouldCreateOrder() throws Exception {
String userId = useNewUserAccessToken();

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

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

CreateOrderParams createOrderParams =
new CreateOrderParams("request-001", ImmutableMap.of(productId, 4));

createOrder(createOrderParams)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists())
.andExpect(jsonPath("$.totalCost").value("5"))
.andExpect(jsonPath("$.purchaseItems").exists())
.andExpect(jsonPath("$.purchaseItems." + productId).value(4))
.andExpect(jsonPath("$.requestId").value("request-001"))
.andExpect(jsonPath("$.buyerUserId").value(userId));
}

private static class CreateProductParams {
String name;
String price;
Expand Down Expand Up @@ -326,6 +352,37 @@ private ResultActions getAccountInfo() throws Exception {
return mockMvc.perform(builder);
}

private ResultActions createOrder(CreateOrderParams createOrderParams) throws Exception {
MockHttpServletRequestBuilder builder =
post("/orders").contentType("application/json").content(createOrderParams.toContent());
addAuthHeader(builder);
return mockMvc.perform(builder);
}

private static class CreateOrderParams {
String requestId;
ImmutableMap<String, Integer> productIdToQuantity;

CreateOrderParams(String requestId, ImmutableMap<String, Integer> productIdToQuantity) {
this.requestId = requestId;
this.productIdToQuantity = productIdToQuantity;
}

String toContent() {
return "{\"requestId\": \""
+ this.requestId
+ "\", \"productIdToQuantity\": "
+ productIdToQuantityContent()
+ "}";
}

private String productIdToQuantityContent() {
return productIdToQuantity.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\": " + entry.getValue())
.collect(Collectors.joining(", ", "{", "}"));
}
}

private void addAuthHeader(MockHttpServletRequestBuilder builder) {
if (isAccessTokenSet()) {
builder.header("Authorization", "Bearer " + this.accessToken);
Expand Down

0 comments on commit 845e620

Please sign in to comment.