Search in sources :

Example 1 with WarehouseInfoDTO

use of com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO 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 WarehouseInfoDTO

use of com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO in project 2021-msk-food-delivery by netcracker-edu.

the class ProductServiceTest method searchProductsSuccess.

@Test
public void searchProductsSuccess() {
    Pageable pageable = PageRequest.of(0, 2);
    Page<Product> productPage = ProductUtils.createPageWithMilkProducts(pageable);
    String requestSearchPhrase = "Milk taste";
    String perfectPhrase = "Milk:* & taste:*";
    Long warehouseId = 4L;
    Point geo = makeGeo();
    WarehouseInfoDTO warehouseInfoDTO = new WarehouseInfoDTO(warehouseId, null, null, null, null, false);
    when(warehouseServiceMock.getNearestWarehouse(geo)).thenReturn(warehouseInfoDTO);
    when(productRepoMock.searchProducts(perfectPhrase, warehouseId, pageable)).thenReturn(productPage);
    SearchProductDTO searchDTO = createSearchProductDTO(requestSearchPhrase);
    List<ProductDTO> resultList = productService.searchProducts(searchDTO, pageable);
    List<ProductDTO> perfectList = ProductUtils.createProductDTOListFromPage(productPage);
    verify(warehouseServiceMock, times(1)).getNearestWarehouse(geo);
    verify(productRepoMock, times(1)).searchProducts(perfectPhrase, warehouseId, pageable);
    assertEquals(resultList, perfectList);
}
Also used : Pageable(org.springframework.data.domain.Pageable) Product(com.ncedu.fooddelivery.api.v1.entities.Product) Point(com.vividsolutions.jts.geom.Point) SearchProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO) ProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) SearchProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with WarehouseInfoDTO

use of com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method countOrderCost.

@Override
public CountOrderCostResponseDTO countOrderCost(CountOrderCostRequestDTO dto) {
    WarehouseInfoDTO warehouse = warehouseService.getNearestWarehouse(dto.getGeo().getLat(), dto.getGeo().getLon());
    if (warehouse == null)
        throw new NotFoundEx(String.format("{Lat: %s; lon: %s}", dto.getGeo().getLat().toString(), dto.getGeo().getLon().toString()));
    Long warehouseId = warehouse.getId();
    if (!warehouseId.equals(dto.getWarehouseId()))
        throw new WarehouseCoordsBindingEx();
    Map<Long, Integer> productsAvailableAmounts = new HashMap<>();
    Double overallOrderCost = 0.0;
    Double overallOrderDiscount = 0.0;
    Double orderHighDemandCoeff;
    boolean enoughProductPositions = true;
    for (Map.Entry<Long, Integer> pair : dto.getProductAmountPairs().entrySet()) {
        Long productId = pair.getKey();
        Integer requestedAmount = pair.getValue();
        List<ProductPosition> productPositions = productPositionRepo.findByProductIdAndWarehouseId(productId, warehouseId);
        // filtering expired product positions
        productPositions = productPositions.stream().filter(new Predicate<ProductPosition>() {

            @Override
            public boolean test(ProductPosition productPosition) {
                Short expirationDays = productPosition.getProduct().getExpirationDays();
                Date manufactureDate = productPosition.getManufactureDate();
                Calendar c = Calendar.getInstance();
                c.setTime(manufactureDate);
                c.add(Calendar.DATE, expirationDays);
                if (c.before(Calendar.getInstance()))
                    return false;
                return true;
            }
        }).collect(Collectors.toList());
        Integer overallAmount = 0;
        for (ProductPosition pos : productPositions) {
            overallAmount += pos.getCurrentAmount();
        }
        if (overallAmount < requestedAmount) {
            productsAvailableAmounts.put(productId, overallAmount);
            enoughProductPositions = false;
        }
        if (enoughProductPositions) {
            // product price count
            Product product = productRepo.findById(productId).get();
            Double productDiscount = product.getDiscount();
            Double productPrice = product.getPrice();
            overallOrderCost += requestedAmount * (productPrice - productDiscount);
            overallOrderDiscount += productDiscount * requestedAmount;
        }
    }
    if (!enoughProductPositions) {
        throw new ProductAvailabilityEx(productsAvailableAmounts);
    }
    orderHighDemandCoeff = countHighDemandCoeff(warehouseId);
    overallOrderCost = Math.round(overallOrderCost * orderHighDemandCoeff * 100.0) / 100.0;
    return new CountOrderCostResponseDTO(overallOrderCost, overallOrderDiscount, orderHighDemandCoeff);
}
Also used : ProductAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx) WarehouseCoordsBindingEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) 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)

Example 4 with WarehouseInfoDTO

use of com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method countOrderCost.

@Override
public Double[] countOrderCost(CoordsDTO geo, HashMap<Long, Integer> pairs, Long clientWarehouseId) {
    WarehouseInfoDTO warehouse = warehouseService.getNearestWarehouse(geo.getLat(), geo.getLon());
    if (warehouse == null)
        throw new NotFoundEx(String.format("Lat: %s; lon: %s", geo.getLat().toString(), geo.getLon().toString()));
    Long warehouseId = warehouse.getId();
    if (!warehouseId.equals(clientWarehouseId))
        throw new WarehouseCoordsBindingEx();
    Map<Long, Integer> productsAvailableAmounts = new HashMap<>();
    Double overallOrderCost = 0.0;
    Double overallOrderDiscount = 0.0;
    Double orderHighDemandCoeff;
    boolean enoughProductPositions = true;
    for (Map.Entry<Long, Integer> pair : pairs.entrySet()) {
        Long productId = pair.getKey();
        Integer requestedAmount = pair.getValue();
        // 'findByProductIdAndWarehouseIdWithLock' sets lock on every product position in result set. So no one
        // other thread
        // will be able to interact with these positions before whole transaction will complete.
        List<ProductPosition> productPositions = productPositionRepo.findByProductIdAndWarehouseIdWithLock(productId, warehouseId);
        // filtering expired product positions
        productPositions = productPositions.stream().filter(new Predicate<ProductPosition>() {

            @Override
            public boolean test(ProductPosition productPosition) {
                Short expirationDays = productPosition.getProduct().getExpirationDays();
                Date manufactureDate = productPosition.getManufactureDate();
                Calendar c = Calendar.getInstance();
                c.setTime(manufactureDate);
                c.add(Calendar.DATE, expirationDays);
                if (c.before(Calendar.getInstance()))
                    return false;
                return true;
            }
        }).collect(Collectors.toList());
        Integer overallAmount = 0;
        for (ProductPosition pos : productPositions) {
            overallAmount += pos.getCurrentAmount();
        }
        if (overallAmount < requestedAmount) {
            productsAvailableAmounts.put(productId, overallAmount);
            enoughProductPositions = false;
        }
        if (enoughProductPositions) {
            // product price count
            Product product = productPositions.get(0).getProduct();
            Double productDiscount = product.getDiscount();
            Double productPrice = product.getPrice();
            overallOrderCost += requestedAmount * (productPrice - productDiscount);
            overallOrderDiscount += productDiscount * requestedAmount;
        }
    }
    if (!enoughProductPositions) {
        throw new ProductAvailabilityEx(productsAvailableAmounts);
    }
    orderHighDemandCoeff = countHighDemandCoeff(warehouseId);
    overallOrderCost = Math.round(overallOrderCost * orderHighDemandCoeff * 100.0) / 100.0;
    return new Double[] { overallOrderCost, overallOrderDiscount, orderHighDemandCoeff };
}
Also used : ProductAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx) WarehouseCoordsBindingEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) 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)

Example 5 with WarehouseInfoDTO

use of com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO in project 2021-msk-food-delivery by netcracker-edu.

the class ProductServiceImpl method getWarehouseIdByCoordinates.

private Long getWarehouseIdByCoordinates(CoordsDTO coordinates) {
    Point geo = makeGeoCoordinates(coordinates);
    WarehouseInfoDTO nearestWarehouse = warehouseService.getNearestWarehouse(geo);
    log.debug("WAREHOUSE " + nearestWarehouse.getId() + " for coords: " + geo);
    return nearestWarehouse.getId();
}
Also used : Point(com.vividsolutions.jts.geom.Point) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)

Aggregations

WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)12 Test (org.junit.jupiter.api.Test)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 Point (com.vividsolutions.jts.geom.Point)7 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)5 SearchProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO)5 Product (com.ncedu.fooddelivery.api.v1.entities.Product)5 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)5 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)5 Pageable (org.springframework.data.domain.Pageable)5 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)3 BigDecimal (java.math.BigDecimal)3 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2 WarehouseCoordsBindingEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx)2 CreateOrderDTO (com.ncedu.fooddelivery.api.v1.dto.order.CreateOrderDTO)1 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)1 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 ArrayList (java.util.ArrayList)1 Executable (org.junit.jupiter.api.function.Executable)1