Search in sources :

Example 21 with File

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

the class FileServiceTest method saveJpegLargeWidthClient.

@Test
public void saveJpegLargeWidthClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeWidth", "jpeg");
    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 less than income because convert from jpeg to jpeg with small resolution
    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 22 with File

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

the class FileServiceTest method getFileList.

@Test
public void getFileList() {
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    File file1 = new File(UUID.randomUUID(), FileType.JPEG, "test", 0L, Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    File file2 = new File(UUID.randomUUID(), FileType.JPEG, "test2", 0L, Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    List<File> fileList = new ArrayList<>();
    fileList.add(file1);
    fileList.add(file2);
    Pageable pageable = PageRequest.of(0, 2);
    Page page = new PageImpl(fileList, pageable, fileList.size());
    when(fileRepoMock.findAll(pageable)).thenReturn(page);
    List<FileInfoDTO> resultDTOs = fileService.getAllFiles(pageable);
    List<FileInfoDTO> perfectDTOs = new ArrayList<>();
    for (File file : fileList) {
        perfectDTOs.add(createFileDTO(file));
    }
    assertEquals(perfectDTOs.size(), resultDTOs.size());
    assertEquals(perfectDTOs, resultDTOs);
}
Also used : 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) FileInfoDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with File

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

the class FileServiceTest method downloadPng.

@Test
public void downloadPng() throws IOException {
    String imgName = "test.png";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "6f10ce6c-74b0-4ad1-a268-c2715b07b59d";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File();
    fileEntity.setId(UUID.fromString(fileUuid));
    Resource resultResource = fileService.load(fileEntity);
    assertEquals(fileUuid, resultResource.getFilename());
    Resource perfectResource = new UrlResource(fileUuidPath.toUri());
    assertEquals(perfectResource, resultResource);
}
Also used : Path(java.nio.file.Path) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) 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 24 with File

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

the class FileServiceTest method replaceJpgNotOwnerNotAdmin.

@Test
public void replaceJpgNotOwnerNotAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User pennyClient = UserUtils.clientPennyTeller(userId);
    Long notOwnerId = 2L;
    User rajeshNotOwner = UserUtils.clientRajeshKoothrappali(notOwnerId);
    String oldImgName = "test.jpeg";
    Path pathTestJpeg = UPLOAD_PATH.resolve(oldImgName);
    final String fileUuid = "e897e931-12ce-4b3f-afbc-42f6c165dfc8";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestJpeg, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File oldFileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, oldImgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), pennyClient);
    MultipartFile newFile = getImgWithType("testLargeWidth", "png");
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.replace(newFile, oldFileEntity, rajeshNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).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) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) 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)

Example 25 with File

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

the class FileServiceTest method savePngLargeHeightClient.

@Test
public void savePngLargeHeightClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeHeight", "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_HEIGHT, bufferedImage.getHeight());
}
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)

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