-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from pagopa/develop
* fix symlink resolution * add wrapper for path resolver as external dependency * decrease diff * add negative case test * add bean init for path resolver * add negative test * refactor symlink resolution * update pom version * typo * typo * move pathResolver to utils package * solve sonar issue text concatenation * improve readability
- Loading branch information
Showing
17 changed files
with
189 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
api/batch/src/main/java/it/gov/pagopa/rtd/transaction_filter/batch/utils/PathResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package it.gov.pagopa.rtd.transaction_filter.batch.utils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
|
||
@RequiredArgsConstructor | ||
public class PathResolver { | ||
|
||
public static final String FILE_PREFIX = "file:"; | ||
public static final String REGEX_CSV_EXTENSION = "/*.csv"; | ||
private final PathMatchingResourcePatternResolver resolver; | ||
|
||
/** | ||
* Resolve symlinks for <code>file:</code> path and retrieve all the Resources inside the target | ||
* path with csv format. Other path type e.g. <code>classpath:</code> won't resolve symlink. | ||
* | ||
* @return array of Resource | ||
* @throws IOException if the file does not exist | ||
*/ | ||
public Resource[] getCsvResources(String path) throws IOException { | ||
var targetPathWithPrefix = resolveSymlink(path); | ||
return resolver.getResources(targetPathWithPrefix + REGEX_CSV_EXTENSION); | ||
} | ||
|
||
private String resolveSymlink(String symlink) throws IOException { | ||
|
||
if (symlink.startsWith(FILE_PREFIX)) { | ||
var targetPath = Path.of(symlink.replace(FILE_PREFIX, "")).toRealPath(); | ||
return FILE_PREFIX + targetPath; | ||
} | ||
|
||
return symlink; | ||
} | ||
|
||
public Resource[] getResources(String locationPattern) throws IOException { | ||
return resolver.getResources(locationPattern); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...atch/src/test/java/it/gov/pagopa/rtd/transaction_filter/batch/utils/PathResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package it.gov.pagopa.rtd.transaction_filter.batch.utils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
|
||
class PathResolverTest { | ||
|
||
public static final String FILENAME_DUMMY = "CSTAR.12345.TRNLOG.20231013.123407.001.csv"; | ||
|
||
private final PathResolver pathResolver = new PathResolver(new PathMatchingResourcePatternResolver()); | ||
|
||
@TempDir | ||
private Path tempInputFolder; | ||
@TempDir | ||
private Path linkTestFolder; | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenSymlinkWhenReadFilesThenFoundExpectedFiles() { | ||
// create a file inside a temp folder and create a symlink to that folder | ||
var folderTarget = Path.of(linkTestFolder.toString(), "targetFolder/"); | ||
Path inputFile = Files.createFile(tempInputFolder.resolve(FILENAME_DUMMY)); | ||
Files.createSymbolicLink(folderTarget, inputFile.getParent()); | ||
|
||
var resources = pathResolver.getCsvResources("file:" + folderTarget); | ||
|
||
assertThat(resources).isNotEmpty().hasSize(1); | ||
assertThat(resources[0].getFile()).hasName(FILENAME_DUMMY); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenRealDirectoryWhenReadFilesThenFoundExpectedFiles() { | ||
Files.createFile(tempInputFolder.resolve(FILENAME_DUMMY)); | ||
|
||
var resources = pathResolver.getCsvResources("file:" + tempInputFolder); | ||
|
||
assertThat(resources).isNotEmpty().hasSize(1); | ||
assertThat(resources[0].getFile()).hasName(FILENAME_DUMMY); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenFilenameWithoutFilePrefixWhenResolvePathThenRaiseException() { | ||
Files.createFile(tempInputFolder.resolve(FILENAME_DUMMY)); | ||
|
||
assertThatThrownBy(() -> pathResolver.getCsvResources(tempInputFolder.toString())) | ||
.isInstanceOf(FileNotFoundException.class); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenEmptyDirectoryWhenResolvePathThenReturnEmptyArray() { | ||
var resources = pathResolver.getCsvResources("file:" + tempInputFolder); | ||
|
||
assertThat(resources).isEmpty(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenDirectoryWithFileNotCsvWhenResolvePathThenReturnEmptyArray() { | ||
Files.createFile(tempInputFolder.resolve("not-a-csv-file.txt")); | ||
|
||
var resources = pathResolver.getCsvResources("file:" + tempInputFolder); | ||
|
||
assertThat(resources).isEmpty(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenClassPathWhenResolvePathThenReturnFiles() { | ||
var resources = pathResolver.getCsvResources("classpath:test-encrypt/transactions"); | ||
|
||
assertThat(resources).isNotEmpty().hasSize(1); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
void givenClassPathWhenResolvePathThenReturnEmptyArray() { | ||
var resources = pathResolver.getCsvResources("classpath:test-encrypt"); | ||
|
||
assertThat(resources).isEmpty(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.