use of com.laurentiuspilca.springsecurityc2.entities.User 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());
}
use of com.laurentiuspilca.springsecurityc2.entities.User 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);
}
use of com.laurentiuspilca.springsecurityc2.entities.User 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));
}
use of com.laurentiuspilca.springsecurityc2.entities.User 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());
}
use of com.laurentiuspilca.springsecurityc2.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceTest method saveJpegLargeHeightClient.
@Test
public void saveJpegLargeHeightClient() throws IOException {
// prepare
Long userId = 1L;
User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
MultipartFile file = getImgWithType("testLargeHeight", "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_HEIGHT, bufferedImage.getHeight());
}
Aggregations