Skip to content

Commit

Permalink
Use Dto for directory in fileDto (#124)
Browse files Browse the repository at this point in the history
* Use Dto for directory in fileDto

* Fix unit test
  • Loading branch information
dtrouillet authored Jul 1, 2021
1 parent 57dbdd8 commit 15ec935
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/main/java/fr/icdc/ebad/service/DirectoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -49,7 +53,7 @@ public List<FilesDto> 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;
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/fr/icdc/ebad/web/rest/dto/FilesDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.icdc.ebad.web.rest.dto;

import fr.icdc.ebad.domain.Directory;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -12,15 +11,15 @@
@Data
@NoArgsConstructor
public class FilesDto {
private Directory directory;
private DirectoryDto directory;
private String name;
private Long size;
private Date updateDate;
private Date createDate;
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;
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/fr/icdc/ebad/service/DirectoryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,6 +38,8 @@ public class DirectoryServiceTest {
private DirectoryRepository directoryRepository;
@Mock
private ShellService shellService;
@Mock
private MapperFacade mapperFacade;
@InjectMocks
private DirectoryService directoryService;

Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit 15ec935

Please sign in to comment.