Search in sources :

Example 91 with User

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

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

the class OrderServiceTest method replaceCourierAccessExTest.

@Test
public void replaceCourierAccessExTest() {
    User fakeUser = getFakeUserModerator();
    Courier mockCourier = Mockito.mock(Courier.class);
    Order fakeOrder = getFakeOrder();
    fakeOrder.setWarehouse(getFakeWarehouse2());
    Assertions.assertThrows(CustomAccessDeniedException.class, new Executable() {

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

Example 93 with User

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

the class UserServiceTest method changeEmailSuccess.

@Test
public void changeEmailSuccess() {
    Long userId = 1L;
    User howard = UserUtils.courierHowardWolowitz(userId);
    String newEmail = "happy-howard@bigbang.theory";
    User howardWithNewEmail = UserUtils.courierHowardWolowitz(userId);
    howardWithNewEmail.setEmail(newEmail);
    when(userRepoMock.findByEmail(newEmail)).thenReturn(null);
    when(userRepoMock.save(howardWithNewEmail)).thenReturn(howardWithNewEmail);
    String password = "password";
    EmailChangeDTO changeDTO = new EmailChangeDTO();
    changeDTO.setEmail(newEmail);
    changeDTO.setPassword(password);
    boolean result = userService.changeEmail(howard, changeDTO);
    assertTrue(result);
    verify(userRepoMock, times(1)).findByEmail(newEmail);
    verify(userRepoMock, times(1)).save(howardWithNewEmail);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) EmailChangeDTO(com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 94 with User

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

the class UserServiceTest method changeFullNameSuccess.

@Test
public void changeFullNameSuccess() {
    Long userId = 1L;
    User penny = UserUtils.clientPennyTeller(userId);
    String newFullName = "Penny Hofstadter";
    User pennyWithNewFullName = UserUtils.clientPennyTeller(userId);
    pennyWithNewFullName.setFullName(newFullName);
    when(userRepoMock.findById(userId)).thenReturn(Optional.of(penny));
    when(userRepoMock.save(pennyWithNewFullName)).thenReturn(pennyWithNewFullName);
    boolean result = userService.changeFullName(userId, newFullName);
    assertTrue(result);
    verify(userRepoMock, times(1)).findById(userId);
    verify(userRepoMock, times(1)).save(pennyWithNewFullName);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 95 with User

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

the class UserServiceTest method setLastSigninSuccess.

@Test
public void setLastSigninSuccess() {
    User howard = UserUtils.courierHowardWolowitz(1L);
    Timestamp current = new Timestamp(System.currentTimeMillis());
    howard.setLastSigninDate(current);
    when(userRepoMock.save(any(User.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    userService.setLastSigninFromNow(howard);
    assertTrue(current.before(howard.getLastSigninDate()));
    verify(userRepoMock, times(1)).save(any());
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) Timestamp(java.sql.Timestamp) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 File (com.ncedu.fooddelivery.api.v1.entities.File)21 Path (java.nio.file.Path)19 MultipartFile (org.springframework.web.multipart.MultipartFile)19 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)14 BufferedImage (java.awt.image.BufferedImage)10 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 Role (com.ncedu.fooddelivery.api.v1.entities.Role)7 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)7 IOException (java.io.IOException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)6