use of com.ecommerceapp.shop.model.Order in project EcommerceApp by teixeira-fernando.
the class OrderServiceIntegrationTest method testCreateOrderErrorNotEnoughStock.
@Test
@DisplayName("POST /order - Error Not Enough Stock")
void testCreateOrderErrorNotEnoughStock() throws Exception {
// Setup product to create
String id = "1";
String productName = "Samsung TV Led";
Integer quantity = 10;
Category category = Category.ELECTRONICS;
Product productInStock = new Product(id, productName, quantity, category);
Product productInOrder = new Product(id, productName, quantity + 1, category);
Order order = new Order();
order.getProducts().add(productInOrder);
wireMockServer.stubFor(WireMock.get(WireMock.urlEqualTo("/product/" + id)).willReturn(aResponse().withStatus(200).withBody(new ObjectMapper().writeValueAsString(productInStock))));
mockMvc.perform(post("/order").contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(order))).andExpect(status().isBadRequest());
}
use of com.ecommerceapp.shop.model.Order in project EcommerceApp by teixeira-fernando.
the class OrderServiceIntegrationTest method testGetOrdersSuccess.
@Test
@DisplayName("GET /orders - Success")
void testGetOrdersSuccess() throws Exception {
String id = "1";
String productName = "Samsung TV Led";
Integer quantity = 50;
Category category = Category.ELECTRONICS;
Order order = new Order(new ArrayList<>());
order.getProducts().add(new Product(productName, quantity, category));
mongoTemplate.insert(order, "Order");
// Execute the GET request
mockMvc.perform(get("/orders")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(1)));
}
use of com.ecommerceapp.shop.model.Order in project EcommerceApp by teixeira-fernando.
the class OrderControllerTest method testGetOrderByIdSuccess.
@Test
@DisplayName("GET /order/{id} - Success")
void testGetOrderByIdSuccess() throws Exception {
// Arrange: Setup our mock
String id = "12345";
String productName = "Samsung TV Led";
Integer quantity = 50;
Category category = Category.ELECTRONICS;
Product mockProduct = new Product(id, productName, quantity, category);
Order mockOrder = new Order();
mockOrder.getProducts().add(mockProduct);
doReturn(mockOrder).when(service).findById(id);
// Execute the GET request
mockMvc.perform(get("/order/{id}", id)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(header().string(HttpHeaders.LOCATION, "/order/" + mockOrder.getId())).andExpect(jsonPath("$.status", is(OrderStatus.CREATED.toString()))).andExpect(jsonPath("$.products[0].id", is(id))).andExpect(jsonPath("$.products[0].name", is(productName))).andExpect(jsonPath("$.products[0].quantity", is(quantity))).andExpect(jsonPath("$.products[0].category", is(category.toString())));
}
use of com.ecommerceapp.shop.model.Order in project EcommerceApp by teixeira-fernando.
the class OrderControllerTest method testCreateOrderEmptyError.
@Test
@DisplayName("POST /order - Empty Order")
void testCreateOrderEmptyError() throws Exception {
// Arrange: Setup our mock
Order mockOrder = new Order();
when(service.createOrder(any())).thenThrow(EmptyOrderException.class);
// Execute the POST request
mockMvc.perform(post("/order").contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(mockOrder))).andExpect(status().isBadRequest());
}
use of com.ecommerceapp.shop.model.Order in project EcommerceApp by teixeira-fernando.
the class OrderControllerTest method testCreateOrderProductNotFoundError.
@Test
@DisplayName("POST /order - Product not found in the repository")
void testCreateOrderProductNotFoundError() throws Exception {
// Arrange: Setup our mock
String id = "12345";
String productName = "Dark Souls 3";
Integer quantity = 20;
Category category = Category.VIDEOGAMES;
Product mockProduct = new Product(id, productName, quantity, category);
Order mockOrder = new Order();
mockOrder.getProducts().add(mockProduct);
when(service.createOrder(any())).thenThrow(InvalidParameterException.class);
// Execute the POST request
mockMvc.perform(post("/order").contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(mockOrder))).andExpect(status().isBadRequest());
}
Aggregations