Search in sources :

Example 26 with Product

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

the class ProductServiceImpl method updateProduct.

@Override
public void updateProduct(Long id, ProductUpdateDTO updatedProduct) {
    Product product = getProductById(id);
    productMapper.updateToEntity(product, updatedProduct);
    productRepo.save(product);
}
Also used : Product(com.ncedu.fooddelivery.api.v1.entities.Product)

Example 27 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product 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 28 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product 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)

Example 29 with Product

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

the class ProductServiceTest method searchProductInShowcaseSuccess.

@Test
public void searchProductInShowcaseSuccess() {
    Pageable pageable = PageRequest.of(0, 2);
    Page<Product> productPage = ProductUtils.createPageProductsInShowcase(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.searchProductsInShowcase(perfectPhrase, warehouseId, pageable)).thenReturn(productPage);
    SearchProductDTO searchDTO = createSearchProductDTO(requestSearchPhrase);
    List<ProductDTO> resultList = productService.searchProductsInShowcase(searchDTO, pageable);
    List<ProductDTO> perfectList = ProductUtils.createProductDTOListFromPage(productPage);
    verify(warehouseServiceMock, times(1)).getNearestWarehouse(geo);
    verify(productRepoMock, times(1)).searchProductsInShowcase(perfectPhrase, warehouseId, pageable);
    assertEquals(perfectList, resultList);
}
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 30 with Product

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

the class ProductServiceTest method getProductsInShowcaseSuccess.

@Test
public void getProductsInShowcaseSuccess() {
    Pageable pageable = PageRequest.of(0, 2);
    Page<Product> productPage = ProductUtils.createPageProductsInShowcase(pageable);
    Long warehouseId = 4L;
    Point geo = makeGeo();
    WarehouseInfoDTO warehouseInfoDTO = new WarehouseInfoDTO(warehouseId, null, null, null, null, false);
    when(warehouseServiceMock.getNearestWarehouse(geo)).thenReturn(warehouseInfoDTO);
    when(productRepoMock.findAllByInShowcase(warehouseId, pageable)).thenReturn(productPage);
    CoordsDTO coordsDTO = makeCoordsDTO();
    List<ProductDTO> resultList = productService.getProductsInShowcase(coordsDTO, pageable);
    List<ProductDTO> perfectList = ProductUtils.createProductDTOListFromPage(productPage);
    verify(warehouseServiceMock, times(1)).getNearestWarehouse(geo);
    verify(productRepoMock, times(1)).findAllByInShowcase(warehouseId, pageable);
    assertEquals(perfectList, resultList);
}
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) CoordsDTO(com.ncedu.fooddelivery.api.v1.dto.CoordsDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Product (com.ncedu.fooddelivery.api.v1.entities.Product)17 Test (org.junit.jupiter.api.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)10 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)9 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)9 Product (entities.Product)9 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)8 SearchProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Point (com.vividsolutions.jts.geom.Point)5 Pageable (org.springframework.data.domain.Pageable)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)4 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)3 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)3 PageImpl (org.springframework.data.domain.PageImpl)3 com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)2 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2