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);
}
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);
}
});
}
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);
}
Aggregations