Skip to content

Commit

Permalink
fix: Nesting files based on organizationId (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 authored Jan 17, 2025
1 parent 96e3c2c commit 6a0f23b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public FileStorerService(FoldersPathsConfig foldersPathsConfig,
this.fileEncryptPassword = fileEncryptPassword;
}

public String saveToSharedFolder(MultipartFile file, String relativePath){
public String saveToSharedFolder(Long organizationId, MultipartFile file, String relativePath){
if(file==null){
log.debug("File is mandatory");
throw new FileUploadException("File is mandatory");
Expand All @@ -38,7 +38,8 @@ public String saveToSharedFolder(MultipartFile file, String relativePath){
StringUtils.defaultString(file.getOriginalFilename()));
FileService.validateFilename(filename);
Path relativeFileLocation = concatenatePaths(relativePath,filename);
Path absolutePath = concatenatePaths(foldersPathsConfig.getShared(), relativeFileLocation.toString());
Path organizationBasePath = concatenatePaths(foldersPathsConfig.getShared(), String.valueOf(organizationId));
Path absolutePath = concatenatePaths(organizationBasePath.toString(), relativeFileLocation.toString());
//create missing parent folder, if any
try {
if (!Files.exists(absolutePath.getParent())){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType
FileOrigin fileOrigin, MultipartFile ingestionFlowFile, UserInfo user, String accessToken) {
userAuthorizationService.checkUserAuthorization(organizationId, user, accessToken);
fileService.validateFile(ingestionFlowFile, validIngestionFlowFileExt);
String filePath = fileStorerService.saveToSharedFolder(ingestionFlowFile,
String filePath = fileStorerService.saveToSharedFolder(organizationId, ingestionFlowFile,
foldersPathsConfig.getIngestionFlowFilePath(ingestionFlowFileType));

return ingestionFlowFileClient.createIngestionFlowFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void givenInvalidFileWhenSaveToSharedFolderThenFileUploadException() {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(null, "");
fileStorerService.saveToSharedFolder(0L, null, "");
Assertions.fail("Expected FileUploadException");
} catch (FileUploadException e) {
Mockito.verifyNoInteractions(foldersPathsConfig);
Expand All @@ -61,7 +61,7 @@ void givenInvalidFilenameWhenSaveToSharedFolderThenInvalidFileException() {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(file, "");
fileStorerService.saveToSharedFolder(0L, file, "");
Assertions.fail("Expected InvalidFileException");
} catch (InvalidFileException e) {
Mockito.verifyNoInteractions(foldersPathsConfig);
Expand Down Expand Up @@ -90,7 +90,7 @@ void givenErrorWhenSaveToSharedFolderThenFileUploadException() {
.thenThrow(new RuntimeException());

try {
fileStorerService.saveToSharedFolder(file, "/relative");
fileStorerService.saveToSharedFolder(0L, file, "/relative");
Assertions.fail("Expected FileUploadException");
} catch (FileUploadException e) {
Mockito.verify(foldersPathsConfig).getShared();
Expand Down Expand Up @@ -120,21 +120,23 @@ void givenValidFileWhenSaveToSharedFolderThenOK() {
Files.class)
) {
String sharedFolderPath = "/shared";
long organizationId = 0L;
String organizationFolder = organizationId + "";
Mockito.when(foldersPathsConfig.getShared()).thenReturn(sharedFolderPath);
String relativePath = "/relative";

String result = fileStorerService.saveToSharedFolder(file, relativePath);
String result = fileStorerService.saveToSharedFolder(organizationId, file, relativePath);

Assertions.assertEquals(Paths.get(relativePath,filename).toString(),result);
Mockito.verify(foldersPathsConfig).getShared();
Mockito.verifyNoMoreInteractions(foldersPathsConfig);
aesUtilsMockedStatic.verify(() -> AESUtils.encrypt(Mockito.anyString(),(InputStream) Mockito.any()));
aesUtilsMockedStatic.verifyNoMoreInteractions();
filesMockedStatic.verify(() -> Files.exists(Mockito.eq(
Paths.get(sharedFolderPath,relativePath))));
Paths.get(sharedFolderPath, organizationFolder, relativePath))));
filesMockedStatic.verify(() -> Files.createDirectories(Mockito.eq(
Paths.get(sharedFolderPath,relativePath))));
filesMockedStatic.verify(() -> Files.copy((InputStream) Mockito.any(),Mockito.eq(Paths.get(sharedFolderPath,relativePath,filename)), Mockito.any()));
Paths.get(sharedFolderPath, organizationFolder, relativePath))));
filesMockedStatic.verify(() -> Files.copy((InputStream) Mockito.any(),Mockito.eq(Paths.get(sharedFolderPath, organizationFolder,relativePath,filename)), Mockito.any()));
filesMockedStatic.verifyNoMoreInteractions();
}
}
Expand All @@ -153,7 +155,7 @@ void givenInvalidPathWhenSaveToSharedFolderThenInvalidFileException() {
MockedStatic<Files> filesMockedStatic = Mockito.mockStatic(
Files.class)) {
try {
fileStorerService.saveToSharedFolder(file, "/relative/../../test");
fileStorerService.saveToSharedFolder(0L, file, "/relative/../../test");
Assertions.fail("Expected InvalidFileException");
} catch (InvalidFileException e) {
Mockito.verifyNoInteractions(foldersPathsConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk(){

Mockito.when(foldersPathsConfigMock.getIngestionFlowFilePath(IngestionFlowFileType.RECEIPT))
.thenReturn(receiptFilePath);
Mockito.when(fileStorerServiceMock.saveToSharedFolder(file,receiptFilePath))
Mockito.when(fileStorerServiceMock.saveToSharedFolder(organizationId, file,receiptFilePath))
.thenReturn(filePath);
Mockito.when(ingestionFlowFileDTOMapperMock.mapToIngestionFlowFileDTO(file,
IngestionFlowFileType.RECEIPT, FileOrigin.PAGOPA, organizationId,filePath))
Expand Down

0 comments on commit 6a0f23b

Please sign in to comment.