Search in sources :

Example 1 with Product

use of com.ecommerceapp.inventory.model.Product 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 Product

use of com.ecommerceapp.inventory.model.Product 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 Product

use of com.ecommerceapp.inventory.model.Product 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 Product

use of com.ecommerceapp.inventory.model.Product 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)

Example 5 with Product

use of com.ecommerceapp.inventory.model.Product in project EcommerceApp by teixeira-fernando.

the class OrderControllerTest method testGetOrdersSuccess.

@Test
@DisplayName("GET /orders - Success")
void testGetOrdersSuccess() throws Exception {
    // Arrange: Setup our mock
    Product product1 = new Product("Samsung TV Led", 50, Category.ELECTRONICS);
    Order order1 = new Order();
    Order order2 = new Order();
    order1.getProducts().add(product1);
    doReturn(Arrays.asList(order1, order2)).when(service).findAll();
    // Execute the GET request
    mockMvc.perform(get("/orders")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(2)));
}
Also used : Order(com.ecommerceapp.shop.model.Order) 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)

Aggregations

Product (com.ecommerceapp.inventory.model.Product)56 DisplayName (org.junit.jupiter.api.DisplayName)46 Test (org.junit.jupiter.api.Test)46 Category (com.ecommerceapp.inventory.model.Category)40 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)31 Order (com.ecommerceapp.shop.model.Order)30 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ChangeStockDto (com.ecommerceapp.inventory.dto.request.ChangeStockDto)7 OrderShipment (com.ecommerceapp.shipment.model.OrderShipment)7 DataMongoTest (org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest)7 Location (com.ecommerceapp.shipment.model.Location)5 OrderDto (com.ecommerceapp.shop.dto.request.OrderDto)5 ArrayList (java.util.ArrayList)4 ProductDto (com.ecommerceapp.inventory.dto.request.ProductDto)3 ApiResponses (io.swagger.annotations.ApiResponses)2 ResponseEntity (org.springframework.http.ResponseEntity)2 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)1 Pact (au.com.dius.pact.core.model.annotations.Pact)1 MessagePact (au.com.dius.pact.core.model.messaging.MessagePact)1