Search in sources :

Example 1 with ChangeOrderStatusDTO

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

the class OrderServiceImpl1 method changeOrderStatus.

@Override
public void changeOrderStatus(Order order, User user, ChangeOrderStatusDTO dto) {
    if (user.getRole() == Role.MODERATOR) {
        if (!order.getWarehouse().getId().equals(user.getModerator().getWarehouseId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.COURIER) {
        if (!order.getCourier().getId().equals(user.getCourier().getId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.CLIENT) {
        if (!order.getClient().getId().equals(user.getClient().getId()))
            throw new CustomAccessDeniedException();
    }
    OrderStatus oldStatus = order.getStatus();
    OrderStatus newStatus = dto.getNewStatus();
    if (oldStatus == OrderStatus.CANCELLED || oldStatus == OrderStatus.DELIVERED || (oldStatus.ordinal() > newStatus.ordinal()))
        throw new OrderStatusChangeException(order.getId());
    order.setStatus(newStatus);
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 2 with ChangeOrderStatusDTO

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

the class OrderServiceTest method changeOrderStatusAccessDeniedExTest.

@Test
public void changeOrderStatusAccessDeniedExTest() {
    ChangeOrderStatusDTO dto = new ChangeOrderStatusDTO();
    dto.setNewStatus(OrderStatus.DELIVERED);
    Order fakeOrder = getFakeOrder();
    Mockito.when(orderRepo.save(Mockito.any(Order.class))).thenReturn(fakeOrder);
    Assertions.assertThrows(CustomAccessDeniedException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            orderService.changeOrderStatus(fakeOrder, getFakeCourier2().getUser(), dto);
        }
    });
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) Executable(org.junit.jupiter.api.function.Executable) ChangeOrderStatusDTO(com.ncedu.fooddelivery.api.v1.dto.order.ChangeOrderStatusDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ChangeOrderStatusDTO

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

the class OrderServiceTest method changeOrderStatusSuccessTest.

@Test
public void changeOrderStatusSuccessTest() {
    ChangeOrderStatusDTO dto = new ChangeOrderStatusDTO();
    dto.setNewStatus(OrderStatus.DELIVERED);
    Order fakeOrder = getFakeOrder();
    Mockito.when(orderRepo.save(Mockito.any(Order.class))).thenReturn(fakeOrder);
    orderService.changeOrderStatus(fakeOrder, getFakeUserClient(), dto);
    Assertions.assertTrue(fakeOrder.getStatus() == OrderStatus.DELIVERED);
    Mockito.verify(orderRepo, Mockito.times(1)).save(fakeOrder);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) ChangeOrderStatusDTO(com.ncedu.fooddelivery.api.v1.dto.order.ChangeOrderStatusDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ChangeOrderStatusDTO (com.ncedu.fooddelivery.api.v1.dto.order.ChangeOrderStatusDTO)2 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)1 Executable (org.junit.jupiter.api.function.Executable)1