Search in sources :

Example 1 with File

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

the class FileServiceTest method deleteImgAdmin.

@Test
public void deleteImgAdmin() throws IOException {
    // prepare
    Long adminId = 1L;
    User sheldonAdmin = UserUtils.adminSheldonCooper(adminId);
    Long ownerId = 2L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "e2233b13-8439-4cf4-a5a3-f3351ebe6368";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, imgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    doNothing().when(fileRepoMock).delete(fileEntity);
    fileService.delete(fileEntity, sheldonAdmin);
    assertFalse(Files.exists(fileUuidPath));
    verify(fileRepoMock, times(1)).delete(fileEntity);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with File

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

the class FileServiceTest method savePngClient.

@Test
public void savePngClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("test", "png");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.save(file, rajeshClient);
    verify(fileRepoMock, times(1)).save(any(File.class));
    Path uploadedFile = getUploadedFilePath(fileLinkDTO.getFileUuid());
    assertTrue(Files.exists(uploadedFile));
    Long uploadedFileSize = Files.size(uploadedFile);
    log.info("FILE SIZE after uploading: " + uploadedFileSize + " FILE UUID: " + fileLinkDTO.getFileUuid());
    // uploaded size more than income because convert from png to jpeg
    assertTrue(fileSize < uploadedFileSize);
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with File

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

the class FileServiceTest method savePngAdmin.

@Test
public void savePngAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User sheldonAdmin = UserUtils.adminSheldonCooper(userId);
    MultipartFile file = getImgWithType("test", "png");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.save(file, sheldonAdmin);
    Path uploadedFile = getUploadedFilePath(fileLinkDTO.getFileUuid());
    assertTrue(Files.exists(uploadedFile));
    Long uploadedFileSize = Files.size(uploadedFile);
    log.info("FILE SIZE after uploading: " + uploadedFileSize + " FILE UUID: " + fileLinkDTO.getFileUuid());
    assertEquals(fileSize, uploadedFileSize);
    verify(fileRepoMock, times(1)).save(any(File.class));
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with File

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

the class FileServiceTest method savePngLargeWidthClient.

@Test
public void savePngLargeWidthClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeWidth", "png");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.save(file, rajeshClient);
    verify(fileRepoMock, times(1)).save(any(File.class));
    Path uploadedFile = getUploadedFilePath(fileLinkDTO.getFileUuid());
    assertTrue(Files.exists(uploadedFile));
    Long uploadedFileSize = Files.size(uploadedFile);
    log.info("FILE SIZE after uploading: " + uploadedFileSize + " FILE UUID: " + fileLinkDTO.getFileUuid());
    // uploaded size more than income because convert from png to jpeg
    assertTrue(fileSize < uploadedFileSize);
    BufferedImage bufferedImage = ImageIO.read(uploadedFile.toFile());
    assertEquals(CLIENT_IMAGE_WIDTH, bufferedImage.getWidth());
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with File

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

the class FileServiceTest method saveWithNotSupportedExt.

@Test
public void saveWithNotSupportedExt() {
    // prepare
    Long userId = 1L;
    User sheldonAdmin = UserUtils.adminSheldonCooper(userId);
    MultipartFile file = getImgWithType("test", "bmp");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    Exception exception = assertThrows(BadFileExtensionException.class, () -> {
        fileService.save(file, sheldonAdmin);
    });
    String perfectMessage = new BadFileExtensionException().getMessage();
    String resultMessage = exception.getMessage();
    verify(fileRepoMock, never()).save(any(File.class));
    assertEquals(perfectMessage, resultMessage);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

File (com.ncedu.fooddelivery.api.v1.entities.File)24 Path (java.nio.file.Path)22 MultipartFile (org.springframework.web.multipart.MultipartFile)22 Test (org.junit.jupiter.api.Test)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)21 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)21 User (com.ncedu.fooddelivery.api.v1.entities.User)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 BufferedImage (java.awt.image.BufferedImage)10 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)8 IOException (java.io.IOException)8 BadFileExtensionException (com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)7 FileDeleteException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)4 FileStorageException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException)3 MalformedURLException (java.net.MalformedURLException)3 Resource (org.springframework.core.io.Resource)3 UrlResource (org.springframework.core.io.UrlResource)3 FileType (com.ncedu.fooddelivery.api.v1.entities.FileType)2 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)2 InputStream (java.io.InputStream)2