Search in sources :

Example 1 with ProductPositionsShipmentDTO

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

the class ProductPositionServiceImpl1 method shipProductPositions.

@Override
public void shipProductPositions(Long orderId, ProductPositionsShipmentDTO productPositionsShipmentDTO) {
    Optional<Order> optionalOrder = orderRepo.findById(orderId);
    if (optionalOrder.isEmpty())
        throw new NotFoundEx(String.valueOf(orderId));
    Order order = optionalOrder.get();
    Long orderWarehouseId = order.getWarehouse().getId();
    List<ProductPositionsShipmentDTO.ProductPositionAmountPair> positionAmountPairs = productPositionsShipmentDTO.getPositionAmountPairs();
    checkIdsUnique(positionAmountPairs);
    checkPositionsExist(positionAmountPairs, orderWarehouseId);
    checkPositionsCurrentAmount(positionAmountPairs);
    // if everything is ok, we can finally manipulate with database
    // updating order status
    order.setStatus(OrderStatus.DELIVERING);
    orderRepo.save(order);
    shipPositionsFromWarehouse(positionAmountPairs);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order)

Example 2 with ProductPositionsShipmentDTO

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

the class ProductPositionController method shipProductPositionsFromOrder.

@PatchMapping("/api/v1/order/{id}/productPositions/currentAmount")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> shipProductPositionsFromOrder(@Min(value = 1) @Max(value = Long.MAX_VALUE) @PathVariable(name = "id") Long id, @Valid @RequestBody ProductPositionsShipmentDTO productPositionsShipmentDTO, @AuthenticationPrincipal User user) {
    Order order = orderService.getOrder(id);
    if (order == null)
        throw new NotFoundEx(String.valueOf(id));
    if (user.getRole() == Role.MODERATOR) {
        if (!user.getModerator().getWarehouseId().equals(order.getWarehouse().getId()))
            throw new CustomAccessDeniedException();
    }
    productPositionService.shipProductPositions(id, productPositionsShipmentDTO);
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with ProductPositionsShipmentDTO

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

the class ProductPositionServiceTest method shipProductPositionsNotUniqueTest.

@Test
public void shipProductPositionsNotUniqueTest() {
    ProductPositionsShipmentDTO mockDTO = Mockito.mock(ProductPositionsShipmentDTO.class);
    List<ProductPositionsShipmentDTO.ProductPositionAmountPair> mockPairs;
    ProductPositionsShipmentDTO.ProductPositionAmountPair mockPair1 = new ProductPositionsShipmentDTO.ProductPositionAmountPair(), mockPair2 = new ProductPositionsShipmentDTO.ProductPositionAmountPair();
    mockPair1.setId(12L);
    mockPair1.setAmount(10);
    mockPair2.setId(13L);
    mockPair2.setAmount(20);
    mockPairs = new ArrayList<>(Arrays.asList(mockPair1, mockPair2, mockPair1));
    Long mockOrderId = 110L;
    Order mockOrder = new Order();
    Warehouse mockWarehouse = Mockito.mock(Warehouse.class);
    mockOrder.setWarehouse(mockWarehouse);
    Mockito.when(mockWarehouse.getId()).thenReturn(1L);
    Mockito.when(mockDTO.getPositionAmountPairs()).thenReturn(mockPairs);
    Mockito.when(orderRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockOrder));
    assertThrows(NotUniqueIdException.class, new Executable() {

        @Override
        public void execute() {
            productPositionService.shipProductPositions(mockOrderId, mockDTO);
        }
    });
    Mockito.verify(orderRepo, times(1)).findById(110L);
    Mockito.verify(mockDTO, times(1)).getPositionAmountPairs();
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Warehouse(com.ncedu.fooddelivery.api.v1.entities.Warehouse) Executable(org.junit.jupiter.api.function.Executable) ProductPositionsShipmentDTO(com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ProductPositionsShipmentDTO

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

the class ProductPositionServiceTest method shipProductPositionsNotEnoughTest.

@Test
public void shipProductPositionsNotEnoughTest() {
    Long mockOrderId = 110L;
    Order mockOrder = Mockito.mock(Order.class);
    ProductPositionsShipmentDTO mockDTO = Mockito.mock(ProductPositionsShipmentDTO.class);
    ProductPositionsShipmentDTO.ProductPositionAmountPair mockPair = new ProductPositionsShipmentDTO.ProductPositionAmountPair();
    mockPair.setId(10L);
    mockPair.setAmount(1000);
    ProductPosition mockPosition = Mockito.mock(ProductPosition.class);
    mockPosition.setCurrentAmount(1);
    Warehouse mockWarehouse = Mockito.mock(Warehouse.class);
    Mockito.when(mockWarehouse.getId()).thenReturn(1L);
    Mockito.when(mockOrder.getWarehouse()).thenReturn(mockWarehouse);
    Mockito.when(mockDTO.getPositionAmountPairs()).thenReturn(new ArrayList<>(Arrays.asList(mockPair)));
    Mockito.when(orderRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockOrder));
    Mockito.when(productPositionRepo.findById(Mockito.any(Long.class))).thenReturn(Optional.of(mockPosition));
    Mockito.when(mockPosition.getWarehouse()).thenReturn(mockWarehouse);
    assertThrows(ProductPositionNotEnoughException.class, new Executable() {

        @Override
        public void execute() {
            productPositionService.shipProductPositions(mockOrderId, mockDTO);
        }
    });
    verify(mockDTO, times(1)).getPositionAmountPairs();
    verify(orderRepo, times(1)).findById(110L);
    verify(productPositionRepo, times(2)).findById(10L);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Warehouse(com.ncedu.fooddelivery.api.v1.entities.Warehouse) Executable(org.junit.jupiter.api.function.Executable) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) ProductPositionsShipmentDTO(com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)4 ProductPositionsShipmentDTO (com.ncedu.fooddelivery.api.v1.dto.ProductPositionDTOs.ProductPositionsShipmentDTO)2 Warehouse (com.ncedu.fooddelivery.api.v1.entities.Warehouse)2 Test (org.junit.jupiter.api.Test)2 Executable (org.junit.jupiter.api.function.Executable)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)1 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)1 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1