Skip to content

Commit 2a4f230

Browse files
authored
feat: Allowing any extensions when uploading ingestionFlowFile (#31)
1 parent 770653d commit 2a4f230

File tree

5 files changed

+12
-45
lines changed

5 files changed

+12
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package it.gov.pagopa.pu.fileshare.service;
22

33
import it.gov.pagopa.pu.fileshare.exception.custom.InvalidFileException;
4-
import java.util.stream.Stream;
54
import lombok.extern.slf4j.Slf4j;
65
import org.apache.commons.lang3.StringUtils;
76
import org.springframework.stereotype.Service;
87
import org.springframework.web.multipart.MultipartFile;
98

9+
import java.util.stream.Stream;
10+
1011
@Slf4j
1112
@Service
1213
public class FileService {
1314

14-
public void validateFile(MultipartFile ingestionFlowFile, String validFileExt) {
15+
public void validateFile(MultipartFile ingestionFlowFile) {
1516
if( ingestionFlowFile == null){
1617
log.debug("Invalid ingestion flow file");
1718
throw new InvalidFileException("Invalid file");
1819
}
1920
String filename = StringUtils.defaultString(ingestionFlowFile.getOriginalFilename());
20-
validateFileExtension(validFileExt, filename);
2121
validateFilename(filename);
2222
}
2323

@@ -28,10 +28,4 @@ public static void validateFilename(String filename) {
2828
}
2929
}
3030

31-
private static void validateFileExtension(String validFileExt, String filename) {
32-
if(!filename.endsWith(validFileExt)){
33-
log.debug("Invalid ingestion flow file extension");
34-
throw new InvalidFileException("Invalid file extension");
35-
}
36-
}
3731
}

src/main/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileFacadeServiceImpl.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class IngestionFlowFileFacadeServiceImpl implements IngestionFlowFileFaca
3636
private final FoldersPathsConfig foldersPathsConfig;
3737
private final IngestionFlowFileService ingestionFlowFileService;
3838
private final IngestionFlowFileDTOMapper ingestionFlowFileDTOMapper;
39-
private final String validIngestionFlowFileExt;
4039
private final String archivedSubFolder;
4140

4241
public IngestionFlowFileFacadeServiceImpl(
@@ -46,7 +45,6 @@ public IngestionFlowFileFacadeServiceImpl(
4645
FoldersPathsConfig foldersPathsConfig,
4746
IngestionFlowFileService ingestionFlowFileService,
4847
IngestionFlowFileDTOMapper ingestionFlowFileDTOMapper,
49-
@Value("${uploads.ingestion-flow-file.valid-extension}") String validIngestionFlowFileExt,
5048
@Value("${folders.process-target-sub-folders.archive}") String archivedSubFolder
5149
) {
5250
this.userAuthorizationService = userAuthorizationService;
@@ -55,7 +53,6 @@ public IngestionFlowFileFacadeServiceImpl(
5553
this.foldersPathsConfig = foldersPathsConfig;
5654
this.ingestionFlowFileService = ingestionFlowFileService;
5755
this.ingestionFlowFileDTOMapper = ingestionFlowFileDTOMapper;
58-
this.validIngestionFlowFileExt = validIngestionFlowFileExt;
5956
this.archivedSubFolder = archivedSubFolder;
6057
}
6158

@@ -64,7 +61,7 @@ public Long uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType i
6461
FileOrigin fileOrigin, String fileName, MultipartFile ingestionFlowFile,
6562
UserInfo user, String accessToken) {
6663
userAuthorizationService.checkUserAuthorization(organizationId, user, accessToken);
67-
fileService.validateFile(ingestionFlowFile, validIngestionFlowFileExt);
64+
fileService.validateFile(ingestionFlowFile);
6865

6966
String ingestionFlowFilePath = foldersPathsConfig.getIngestionFlowFilePath(ingestionFlowFileType);
7067

src/main/resources/application.yml

-4
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,5 @@ rest:
9595
wait-time-millis: "\${PROCESS_EXECUTIONS_WAIT_TIME_MILLIS:500}"
9696
print-body-when-error: "\${PROCESS_EXECUTIONS_PRINT_BODY_WHEN_ERROR:true}"
9797

98-
uploads:
99-
ingestion-flow-file:
100-
valid-extension: "\${INGESTION_FLOW_FILE_VALID_EXTENSION:.zip}"
101-
10298
app:
10399
fileEncryptPassword: "\${FILE_ENCRYPT_PASSWORD:ENCR_PSW}"

src/test/java/it/gov/pagopa/pu/fileshare/service/FileServiceTest.java

+4-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
@ExtendWith(MockitoExtension.class)
1313
class FileServiceTest {
1414
private FileService fileService;
15-
private static final String VALID_FILE_EXTENSION = ".zip";
1615

1716
@BeforeEach
1817
void setUp() {
@@ -23,35 +22,18 @@ void setUp() {
2322
void givenValidFileExtensionWhenValidateFileThenOk(){
2423
MockMultipartFile file = new MockMultipartFile(
2524
"ingestionFlowFile",
26-
"test"+VALID_FILE_EXTENSION,
25+
"test.zip",
2726
MediaType.TEXT_PLAIN_VALUE,
2827
"this is a test file".getBytes()
2928
);
3029

31-
fileService.validateFile(file, VALID_FILE_EXTENSION);
30+
fileService.validateFile(file);
3231
}
3332

3433
@Test
3534
void givenNoFileWhenValidateFileThenInvalidFileException(){
3635
try{
37-
fileService.validateFile(null, VALID_FILE_EXTENSION);
38-
Assertions.fail("Expected InvalidFileException");
39-
}catch(InvalidFileException e){
40-
//do nothing
41-
}
42-
}
43-
44-
@Test
45-
void givenInvalidFileExtensionWhenValidateFileThenInvalidFileException(){
46-
MockMultipartFile file = new MockMultipartFile(
47-
"ingestionFlowFile",
48-
"test.txt",
49-
MediaType.TEXT_PLAIN_VALUE,
50-
"this is a test file".getBytes()
51-
);
52-
53-
try{
54-
fileService.validateFile(file, VALID_FILE_EXTENSION);
36+
fileService.validateFile(null);
5537
Assertions.fail("Expected InvalidFileException");
5638
}catch(InvalidFileException e){
5739
//do nothing
@@ -68,7 +50,7 @@ void givenInvalidFilenameWhenValidateFileThenInvalidFileException(){
6850
);
6951

7052
try{
71-
fileService.validateFile(file, VALID_FILE_EXTENSION);
53+
fileService.validateFile(file);
7254
Assertions.fail("Expected InvalidFileException");
7355
}catch(InvalidFileException e){
7456
//do nothing

src/test/java/it/gov/pagopa/pu/fileshare/service/ingestion/IngestionFlowFileFacadeServiceImplTest.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class IngestionFlowFileFacadeServiceImplTest {
4747
@Mock
4848
private IngestionFlowFileDTOMapper ingestionFlowFileDTOMapperMock;
4949
private IngestionFlowFileFacadeServiceImpl ingestionFlowFileService;
50-
private static final String VALID_FILE_EXTENSION = ".zip";
5150
private static final String ARCHIVED_SUB_FOLDER = "Archived";
5251

5352
@BeforeEach
@@ -59,7 +58,6 @@ void setUp() {
5958
foldersPathsConfigMock,
6059
ingestionFlowFileServiceMock,
6160
ingestionFlowFileDTOMapperMock,
62-
VALID_FILE_EXTENSION,
6361
ARCHIVED_SUB_FOLDER);
6462
}
6563

@@ -84,7 +82,7 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk() {
8482
String fileName = "fileName.txt";
8583
MockMultipartFile file = new MockMultipartFile(
8684
"ingestionFlowFile",
87-
"test" + VALID_FILE_EXTENSION,
85+
"test.zip",
8886
MediaType.TEXT_PLAIN_VALUE,
8987
"this is a test file".getBytes()
9088
);
@@ -108,7 +106,7 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk() {
108106

109107
Assertions.assertSame(expectedIngestionFlowFileId, result);
110108
Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, TestUtils.getSampleUser(), accessToken);
111-
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
109+
Mockito.verify(fileServiceMock).validateFile(file);
112110
}
113111

114112
@Test
@@ -146,7 +144,7 @@ void givenAlreadyUploadedWhenThenFileAlreadyExistsException() {
146144
fileName, file, userInfo, accessToken));
147145

148146
Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, userInfo, accessToken);
149-
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
147+
Mockito.verify(fileServiceMock).validateFile(file);
150148
}
151149
}
152150

@@ -191,7 +189,7 @@ void givenAlreadyArchivedWhenThenFileAlreadyExistsException() {
191189
fileName, file, userInfo, accessToken));
192190

193191
Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, userInfo, accessToken);
194-
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
192+
Mockito.verify(fileServiceMock).validateFile(file);
195193
}
196194
}
197195

0 commit comments

Comments
 (0)