Search in sources :

Example 1 with Order

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());
}
Also used : Order(com.ecommerceapp.shop.model.Order) Category(com.ecommerceapp.inventory.model.Category) Product(com.ecommerceapp.inventory.model.Product) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Order

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)));
}
Also used : Order(com.ecommerceapp.shop.model.Order) Category(com.ecommerceapp.inventory.model.Category) Product(com.ecommerceapp.inventory.model.Product) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Order

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())));
}
Also used : Order(com.ecommerceapp.shop.model.Order) Category(com.ecommerceapp.inventory.model.Category) Product(com.ecommerceapp.inventory.model.Product) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with Order

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());
}
Also used : Order(com.ecommerceapp.shop.model.Order) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with Order

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());
}
Also used : Order(com.ecommerceapp.shop.model.Order) Category(com.ecommerceapp.inventory.model.Category) Product(com.ecommerceapp.inventory.model.Product) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Order (com.ecommerceapp.shop.model.Order)37 Product (com.ecommerceapp.inventory.model.Product)30 Test (org.junit.jupiter.api.Test)28 DisplayName (org.junit.jupiter.api.DisplayName)27 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)20 Category (com.ecommerceapp.inventory.model.Category)19 OrderShipment (com.ecommerceapp.shipment.model.OrderShipment)8 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Location (com.ecommerceapp.shipment.model.Location)6 OrderDto (com.ecommerceapp.shop.dto.request.OrderDto)6 ArrayList (java.util.ArrayList)4 DataMongoTest (org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 HashMap (java.util.HashMap)2 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)1 PactTestFor (au.com.dius.pact.consumer.junit5.PactTestFor)1 Pact (au.com.dius.pact.core.model.annotations.Pact)1 MessagePact (au.com.dius.pact.core.model.messaging.MessagePact)1 PactVerifyProvider (au.com.dius.pact.provider.PactVerifyProvider)1