Search in sources :

Example 1 with Category

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

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

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

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

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

the class CreateShipmentProviderPact method verifyMessageForOrderJson.

@PactVerifyProvider("Order in Json format")
public String verifyMessageForOrderJson() {
    try {
        String id = "1";
        String productName = "Samsung TV Led";
        Integer quantity = 50;
        Category category = Category.ELECTRONICS;
        Order order = new Order("1", new ArrayList<>());
        order.getProducts().add(new Product(id, productName, quantity, category));
        String body = mapper.writeValueAsString(order);
        return body;
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Order(com.ecommerceapp.shop.model.Order) Category(com.ecommerceapp.inventory.model.Category) Product(com.ecommerceapp.inventory.model.Product) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PactVerifyProvider(au.com.dius.pact.provider.PactVerifyProvider)

Aggregations

Category (com.ecommerceapp.inventory.model.Category)40 Product (com.ecommerceapp.inventory.model.Product)40 DisplayName (org.junit.jupiter.api.DisplayName)32 Test (org.junit.jupiter.api.Test)32 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)21 Order (com.ecommerceapp.shop.model.Order)19 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ChangeStockDto (com.ecommerceapp.inventory.dto.request.ChangeStockDto)7 Location (com.ecommerceapp.shipment.model.Location)5 OrderShipment (com.ecommerceapp.shipment.model.OrderShipment)5 DataMongoTest (org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest)5 ProductDto (com.ecommerceapp.inventory.dto.request.ProductDto)3 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 PactVerifyProvider (au.com.dius.pact.provider.PactVerifyProvider)1 State (au.com.dius.pact.provider.junitsupport.State)1 OrderDto (com.ecommerceapp.shop.dto.request.OrderDto)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1