use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserRefreshTokenServiceImpl method createRefreshToken.
@Override
public String createRefreshToken(User owner, String userAgent) {
UserRefreshToken urt = new UserRefreshToken();
Timestamp createDate = Timestamp.valueOf(LocalDateTime.now());
urt.setCreateDate(createDate);
urt.setOwner(owner);
urt.setUserAgent(userAgent);
urt = userRefreshTokenRepo.save(urt);
return urt.getId().toString();
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceTest method deleteImgNotAdminNotOwner.
@Test
public void deleteImgNotAdminNotOwner() throws IOException {
// prepare
Long ownerId = 1L;
User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
Long notOwnerId = 2L;
User leonardNotOwner = UserUtils.moderatorLeonardHofstadter(notOwnerId);
String imgName = "test.jpeg";
Path pathTestPng = UPLOAD_PATH.resolve(imgName);
// file name, where we locate our test.png
final String fileUuid = "55420882-1e23-4559-b84e-03c3f4d597af";
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);
Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
fileService.delete(fileEntity, leonardNotOwner);
});
String perfectMessage = new CustomAccessDeniedException().getMessage();
String resultMessage = exception.getMessage();
assertEquals(perfectMessage, resultMessage);
verify(fileRepoMock, never()).delete(fileEntity);
}
use of com.ncedu.fooddelivery.api.v1.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.ncedu.fooddelivery.api.v1.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.ncedu.fooddelivery.api.v1.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));
}
Aggregations