Search in sources :

Example 1 with WarehouseService

use of com.ncedu.fooddelivery.api.v1.services.WarehouseService in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method countOrderCostSuccessfulTest.

@Test
public void countOrderCostSuccessfulTest() {
    WarehouseInfoDTO fakeWarehouseInfoDTO = getFakeWarehouseInfoDTO();
    List<Product> fakeProducts = getFakeProducts();
    List<ProductPosition> fakePositions = getFakeProductPositions();
    Mockito.when(courierRepo.countWorkingCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(courierRepo.countDeliveringCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(warehouseService.getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class))).thenReturn(fakeWarehouseInfoDTO);
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(1L, 1L)).thenReturn(Arrays.asList(fakePositions.get(0), fakePositions.get(1)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(2L, 1L)).thenReturn(Arrays.asList(fakePositions.get(2), fakePositions.get(3)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(3L, 1L)).thenReturn(Arrays.asList(fakePositions.get(4), fakePositions.get(5)));
    Mockito.when(productRepo.findById(1l)).thenReturn(Optional.of(fakeProducts.get(0)));
    Mockito.when(productRepo.findById(2l)).thenReturn(Optional.of(fakeProducts.get(1)));
    Mockito.when(productRepo.findById(3l)).thenReturn(Optional.of(fakeProducts.get(2)));
    HashMap<Long, Integer> fakeHashMap = new HashMap<>();
    fakeHashMap.put(1L, 2);
    fakeHashMap.put(2L, 3);
    fakeHashMap.put(3L, 5);
    Double[] res = orderService.countOrderCost(getFakeCoordsDTO(), fakeHashMap, 1L);
    Assertions.assertEquals(res[0], 760.0);
    Assertions.assertEquals(res[1], 20.0);
    Assertions.assertEquals(res[2], 2.0);
    Mockito.verify(courierRepo, Mockito.times(1)).countWorkingCouriersByWarehouse(1L);
    Mockito.verify(courierRepo, Mockito.times(1)).countDeliveringCouriersByWarehouse(1L);
    Mockito.verify(productPositionRepo, Mockito.times(3)).findByProductIdAndWarehouseIdWithLock(Mockito.any(Long.class), Mockito.eq(1L));
    Mockito.verify(warehouseService, Mockito.times(1)).getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class));
}
Also used : OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) BigDecimal(java.math.BigDecimal) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with WarehouseService

use of com.ncedu.fooddelivery.api.v1.services.WarehouseService in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method countOrderCostNotAvailableExTest.

@Test
public void countOrderCostNotAvailableExTest() {
    WarehouseInfoDTO fakeWarehouseInfoDTO = getFakeWarehouseInfoDTO();
    List<Product> fakeProducts = getFakeProducts();
    List<ProductPosition> fakePositions = getFakeProductPositions();
    Mockito.when(courierRepo.countWorkingCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(courierRepo.countDeliveringCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(warehouseService.getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class))).thenReturn(fakeWarehouseInfoDTO);
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(1L, 1L)).thenReturn(Arrays.asList(fakePositions.get(0), fakePositions.get(1)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(2L, 1L)).thenReturn(Arrays.asList(fakePositions.get(2), fakePositions.get(3)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(3L, 1L)).thenReturn(Arrays.asList(fakePositions.get(4), fakePositions.get(5)));
    Mockito.when(productRepo.findById(1l)).thenReturn(Optional.of(fakeProducts.get(0)));
    Mockito.when(productRepo.findById(2l)).thenReturn(Optional.of(fakeProducts.get(1)));
    Mockito.when(productRepo.findById(3l)).thenReturn(Optional.of(fakeProducts.get(2)));
    HashMap<Long, Integer> fakeHashMap = new HashMap<>();
    fakeHashMap.put(1L, 4);
    fakeHashMap.put(2L, 3);
    fakeHashMap.put(3L, 5);
    Assertions.assertThrows(ProductAvailabilityEx.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            orderService.countOrderCost(getFakeCoordsDTO(), fakeHashMap, 1L);
        }
    });
    Mockito.verify(productPositionRepo, Mockito.times(3)).findByProductIdAndWarehouseIdWithLock(Mockito.any(Long.class), Mockito.eq(1L));
    Mockito.verify(warehouseService, Mockito.times(1)).getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class));
}
Also used : BigDecimal(java.math.BigDecimal) Executable(org.junit.jupiter.api.function.Executable) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with WarehouseService

use of com.ncedu.fooddelivery.api.v1.services.WarehouseService in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method createOrderTest.

@Test
public void createOrderTest() {
    CreateOrderDTO fakeDTO = new CreateOrderDTO();
    User fakeUser = getFakeUserClient();
    HashMap<Long, Integer> fakeHashMap = new HashMap<>();
    fakeHashMap.put(1L, 2);
    fakeHashMap.put(2L, 3);
    fakeHashMap.put(3L, 5);
    fakeDTO.setProductAmountPairs(fakeHashMap);
    fakeDTO.setOverallCost(380.0);
    fakeDTO.setHighDemandCoeff(1.0);
    fakeDTO.setDiscount(20.0);
    fakeDTO.setAddress("address");
    fakeDTO.setGeo(getFakeCoordsDTO());
    fakeDTO.setWarehouseId(1L);
    WarehouseInfoDTO fakeWarehouseInfoDTO = getFakeWarehouseInfoDTO();
    List<Product> fakeProducts = getFakeProducts();
    List<ProductPosition> fakePositions = getFakeProductPositions();
    Mockito.when(courierRepo.countWorkingCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(courierRepo.countDeliveringCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 50);
    Mockito.when(warehouseService.getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class))).thenReturn(fakeWarehouseInfoDTO);
    Mockito.when(warehouseService.findById(1L)).thenReturn(getFakeWarehouse1());
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(1L, 1L)).thenReturn(Arrays.asList(fakePositions.get(0), fakePositions.get(1)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(2L, 1L)).thenReturn(Arrays.asList(fakePositions.get(2), fakePositions.get(3)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(3L, 1L)).thenReturn(Arrays.asList(fakePositions.get(4), fakePositions.get(5)));
    Mockito.when(productPositionRepo.findById(1L)).thenReturn(Optional.of(fakePositions.get(0)));
    Mockito.when(productPositionRepo.findById(2L)).thenReturn(Optional.of(fakePositions.get(1)));
    Mockito.when(productPositionRepo.findById(3L)).thenReturn(Optional.of(fakePositions.get(2)));
    Mockito.when(productPositionRepo.findById(4L)).thenReturn(Optional.of(fakePositions.get(3)));
    Mockito.when(productPositionRepo.findById(5L)).thenReturn(Optional.of(fakePositions.get(4)));
    Mockito.when(productPositionRepo.findById(6L)).thenReturn(Optional.of(fakePositions.get(5)));
    Mockito.when(productRepo.findById(1l)).thenReturn(Optional.of(fakeProducts.get(0)));
    Mockito.when(productRepo.findById(2l)).thenReturn(Optional.of(fakeProducts.get(1)));
    Mockito.when(productRepo.findById(3l)).thenReturn(Optional.of(fakeProducts.get(2)));
    Mockito.when(orderRepo.save(Mockito.any(Order.class))).thenReturn(getFakeOrder());
    Mockito.when(courierRepo.getWaitingCourierByWarehouse(1L)).thenReturn(Mockito.mock(Courier.class));
    Mockito.when(orderProductPositionRepo.save(Mockito.any(OrderProductPosition.class))).thenReturn(Mockito.mock(OrderProductPosition.class));
    orderService.createOrder(fakeDTO, fakeUser);
    Mockito.verify(courierRepo, Mockito.times(1)).countWorkingCouriersByWarehouse(1L);
    Mockito.verify(courierRepo, Mockito.times(1)).countDeliveringCouriersByWarehouse(1L);
    Mockito.verify(productPositionRepo, Mockito.times(6)).findByProductIdAndWarehouseIdWithLock(Mockito.any(Long.class), Mockito.eq(1L));
    Mockito.verify(warehouseService, Mockito.times(1)).getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class));
    Mockito.verify(orderProductPositionRepo, Mockito.times(3)).save(Mockito.any(OrderProductPosition.class));
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) BigDecimal(java.math.BigDecimal) CreateOrderDTO(com.ncedu.fooddelivery.api.v1.dto.order.CreateOrderDTO) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)3 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)3 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)3 BigDecimal (java.math.BigDecimal)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 CreateOrderDTO (com.ncedu.fooddelivery.api.v1.dto.order.CreateOrderDTO)1 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)1 Executable (org.junit.jupiter.api.function.Executable)1