From 15ec9358f1bc730dfaa35306a2be1720791ba8f9 Mon Sep 17 00:00:00 2001 From: Damien Trouillet Date: Thu, 1 Jul 2021 17:06:54 +0200 Subject: [PATCH] Use Dto for directory in fileDto (#124) * Use Dto for directory in fileDto * Fix unit test --- .../fr/icdc/ebad/service/DirectoryService.java | 13 +++++++++---- .../fr/icdc/ebad/web/rest/dto/FilesDto.java | 5 ++--- .../icdc/ebad/service/DirectoryServiceTest.java | 17 +++++++++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/fr/icdc/ebad/service/DirectoryService.java b/src/main/java/fr/icdc/ebad/service/DirectoryService.java index 8da3be82..ea625183 100644 --- a/src/main/java/fr/icdc/ebad/service/DirectoryService.java +++ b/src/main/java/fr/icdc/ebad/service/DirectoryService.java @@ -5,7 +5,9 @@ import fr.icdc.ebad.domain.QDirectory; import fr.icdc.ebad.repository.DirectoryRepository; import fr.icdc.ebad.service.util.EbadServiceException; +import fr.icdc.ebad.web.rest.dto.DirectoryDto; import fr.icdc.ebad.web.rest.dto.FilesDto; +import ma.glasnost.orika.MapperFacade; import org.apache.sshd.sftp.client.SftpClient; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -27,10 +29,12 @@ public class DirectoryService { private final DirectoryRepository directoryRepository; private final ShellService shellService; + private final MapperFacade mapper; - public DirectoryService(DirectoryRepository directoryRepository, ShellService shellService) { + public DirectoryService(DirectoryRepository directoryRepository, ShellService shellService, MapperFacade mapper) { this.directoryRepository = directoryRepository; this.shellService = shellService; + this.mapper = mapper; } @Transactional @@ -49,7 +53,7 @@ public List listAllFiles(Long idDirectory, String subDirectory) throws return true; }) .forEach(file -> - filesDtoList.add(new FilesDto(directory, file.getFilename(), file.getAttributes().getSize(), Date.from(file.getAttributes().getModifyTime().toInstant()), Date.from(file.getAttributes().getModifyTime().toInstant()), file.getAttributes().isDirectory(), subDirectory)) + filesDtoList.add(new FilesDto(mapper.map(directory, DirectoryDto.class), file.getFilename(), file.getAttributes().getSize(), Date.from(file.getAttributes().getModifyTime().toInstant()), Date.from(file.getAttributes().getModifyTime().toInstant()), file.getAttributes().isDirectory(), subDirectory)) ); return filesDtoList; @@ -72,14 +76,15 @@ public InputStream readFile(FilesDto filesDTO) throws EbadServiceException { @Transactional public void uploadFile(MultipartFile multipartFile, Long directoryId, String subDirectory) throws EbadServiceException { - FilesDto filesDTO = new FilesDto(getDirectory(directoryId), multipartFile.getOriginalFilename(), 0L, Date.from(Instant.now()), Date.from(Instant.now()), false, subDirectory); + Directory directory = getDirectory(directoryId); + FilesDto filesDTO = new FilesDto(mapper.map(directory, DirectoryDto.class), multipartFile.getOriginalFilename(), 0L, Date.from(Instant.now()), Date.from(Instant.now()), false, subDirectory); if (filesDTO.getDirectory() == null) { throw new IllegalAccessError("Pas de permission pour supprimer ce fichier"); } try { - shellService.uploadFile(filesDTO.getDirectory(), multipartFile.getInputStream(), filesDTO.getName(), filesDTO.getSubDirectory()); + shellService.uploadFile(directory, multipartFile.getInputStream(), filesDTO.getName(), filesDTO.getSubDirectory()); } catch (IOException e) { throw new EbadServiceException("Erreur lors de l'écriture d'un fichier du dossiers " + filesDTO.getDirectory().getName(), e); } diff --git a/src/main/java/fr/icdc/ebad/web/rest/dto/FilesDto.java b/src/main/java/fr/icdc/ebad/web/rest/dto/FilesDto.java index b81a6abc..41f5b6b2 100644 --- a/src/main/java/fr/icdc/ebad/web/rest/dto/FilesDto.java +++ b/src/main/java/fr/icdc/ebad/web/rest/dto/FilesDto.java @@ -1,6 +1,5 @@ package fr.icdc.ebad.web.rest.dto; -import fr.icdc.ebad.domain.Directory; import lombok.Data; import lombok.NoArgsConstructor; @@ -12,7 +11,7 @@ @Data @NoArgsConstructor public class FilesDto { - private Directory directory; + private DirectoryDto directory; private String name; private Long size; private Date updateDate; @@ -20,7 +19,7 @@ public class FilesDto { private boolean isFolder; private String subDirectory; - public FilesDto(Directory directory, String name, Long size, Date updateDate, Date createDate, boolean isFolder, String subDirectory) { + public FilesDto(DirectoryDto directory, String name, Long size, Date updateDate, Date createDate, boolean isFolder, String subDirectory) { this.directory = directory; this.name = name; this.size = size; diff --git a/src/test/java/fr/icdc/ebad/service/DirectoryServiceTest.java b/src/test/java/fr/icdc/ebad/service/DirectoryServiceTest.java index d996275c..ea27f930 100644 --- a/src/test/java/fr/icdc/ebad/service/DirectoryServiceTest.java +++ b/src/test/java/fr/icdc/ebad/service/DirectoryServiceTest.java @@ -6,7 +6,9 @@ import fr.icdc.ebad.domain.QDirectory; import fr.icdc.ebad.repository.DirectoryRepository; import fr.icdc.ebad.service.util.EbadServiceException; +import fr.icdc.ebad.web.rest.dto.DirectoryDto; import fr.icdc.ebad.web.rest.dto.FilesDto; +import ma.glasnost.orika.MapperFacade; import org.apache.commons.io.IOUtils; import org.apache.sshd.sftp.client.SftpClient; import org.joda.time.DateTime; @@ -21,7 +23,7 @@ import org.springframework.mock.web.MockMultipartFile; import java.io.InputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.attribute.FileTime; import java.util.ArrayList; import java.util.List; @@ -36,6 +38,8 @@ public class DirectoryServiceTest { private DirectoryRepository directoryRepository; @Mock private ShellService shellService; + @Mock + private MapperFacade mapperFacade; @InjectMocks private DirectoryService directoryService; @@ -63,8 +67,9 @@ public void listAllFilesException() throws EbadServiceException { @Test(expected = IllegalAccessError.class) public void removeFile() throws EbadServiceException { Directory directory = Directory.builder().id(1L).name("test").canWrite(true).build(); + DirectoryDto directoryDto = DirectoryDto.builder().id(1L).name("test").canWrite(true).build(); FilesDto filesDTO = new FilesDto(); - filesDTO.setDirectory(directory); + filesDTO.setDirectory(directoryDto); filesDTO.setSubDirectory("testSub"); when(directoryRepository.getById(1L)).thenReturn(directory); doNothing().when(shellService).removeFile(eq(directory), eq(filesDTO.getName()), anyString()); @@ -77,10 +82,11 @@ public void removeFile() throws EbadServiceException { @Test(expected = EbadServiceException.class) public void readFile() throws EbadServiceException { - InputStream inputStream = IOUtils.toInputStream("hello", Charset.forName("UTF-8")); + InputStream inputStream = IOUtils.toInputStream("hello", StandardCharsets.UTF_8); Directory directory = Directory.builder().id(1L).name("test").canWrite(true).build(); + DirectoryDto directoryDto = DirectoryDto.builder().id(1L).name("test").canWrite(true).build(); FilesDto filesDTO = new FilesDto(); - filesDTO.setDirectory(directory); + filesDTO.setDirectory(directoryDto); when(directoryRepository.getById(1L)).thenReturn(directory); when(shellService.getFile(eq(directory), eq(filesDTO.getName()), any())).thenReturn(inputStream); InputStream result = directoryService.readFile(filesDTO); @@ -94,7 +100,9 @@ public void readFile() throws EbadServiceException { public void uploadFile() throws EbadServiceException { Directory directory = Directory.builder().id(1L).name("test").canWrite(true).build(); + DirectoryDto directoryDto = DirectoryDto.builder().id(1L).name("test").canWrite(true).build(); when(directoryRepository.getById(eq(1L))).thenReturn(directory); + when(mapperFacade.map(eq(directory),eq(DirectoryDto.class))).thenReturn(directoryDto); MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", "text/plain", "some other type".getBytes()); doNothing().when(shellService).uploadFile(eq(directory), notNull(), eq("other-file-name.data"), anyString()); @@ -108,6 +116,7 @@ public void uploadFile() throws EbadServiceException { @Test(expected = IllegalAccessError.class) public void uploadFileKo() throws EbadServiceException { + MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", "text/plain", "some other type".getBytes()); directoryService.uploadFile(secondFile, 1L, null);