Skip to content

Commit

Permalink
Merge pull request #402 from pagopa/develop
Browse files Browse the repository at this point in the history
* 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
and-mora authored Sep 4, 2024
2 parents 0a1da8d + b0fadd9 commit 1a9e0fe
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 55 deletions.
4 changes: 2 additions & 2 deletions api/batch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<parent>
<artifactId>rtd-ms-transaction-filter-api</artifactId>
<groupId>it.gov.pagopa.rtd.ms.transaction_filter.api</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<artifactId>rtd-ms-transaction-filter-api-batch</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import it.gov.pagopa.rtd.transaction_filter.batch.step.PanReaderStep;
import it.gov.pagopa.rtd.transaction_filter.batch.step.TransactionFilterStep;
import it.gov.pagopa.rtd.transaction_filter.batch.utils.PathResolver;
import it.gov.pagopa.rtd.transaction_filter.service.StoreService;
import java.util.Date;
import lombok.Data;
Expand All @@ -15,7 +16,6 @@
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

@Slf4j
Expand All @@ -31,8 +31,7 @@ public class BatchExecutor {
private final StoreService storeService;
@Value("${batchConfiguration.TransactionFilterBatch.hpanListRecovery.enabled}")
private Boolean hpanListRecoveryEnabled;
private final PathMatchingResourcePatternResolver resolver;

private final PathResolver pathResolver;

/**
*
Expand All @@ -41,11 +40,12 @@ public class BatchExecutor {
*/
@SneakyThrows
public JobExecution execute(Date startDate) {
Resource[] transactionResources = resolver.getResources(transactionFilterStep.getTransactionDirectoryPath() + "/*.csv");
Resource[] transactionResources = pathResolver.getCsvResources(
transactionFilterStep.getTransactionDirectoryPath());
transactionResources = TransactionFilterStep.filterValidFilenames(transactionResources);

String hpanPath = panReaderStep.getHpanDirectoryPath();
Resource[] hpanResources = resolver.getResources(hpanPath);
Resource[] hpanResources = pathResolver.getResources(hpanPath);

JobExecution execution = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.gov.pagopa.rtd.transaction_filter.batch.config;

import it.gov.pagopa.rtd.transaction_filter.batch.utils.PathResolver;
import it.gov.pagopa.rtd.transaction_filter.batch.utils.TransactionMaskPolicy;
import it.gov.pagopa.rtd.transaction_filter.batch.utils.TransactionMaskPolicyImpl;
import org.springframework.context.annotation.Bean;
Expand All @@ -19,4 +20,9 @@ public PathMatchingResourcePatternResolver resolver() {
return new PathMatchingResourcePatternResolver();
}

@Bean
public PathResolver pathResolver(PathMatchingResourcePatternResolver resolver) {
return new PathResolver(resolver);
}

}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import it.gov.pagopa.rtd.transaction_filter.batch.step.PanReaderStep;
import it.gov.pagopa.rtd.transaction_filter.batch.step.TransactionFilterStep;
import it.gov.pagopa.rtd.transaction_filter.batch.utils.PathResolver;
import it.gov.pagopa.rtd.transaction_filter.service.StoreService;
import java.util.Date;
import java.util.stream.Stream;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -18,27 +19,26 @@
import org.mockito.BDDMockito;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@ExtendWith(MockitoExtension.class)
class BatchExecutorTest {

@Mock
JobLauncher jobLauncher;
@Mock
TransactionFilterStep transactionFilterStep;
@Mock
PathMatchingResourcePatternResolver resolver;
@Mock
PanReaderStep panReaderStep;
@Mock
StoreService storeService;
@Mock
BatchExecutor batchExecutor;

AutoCloseable closeable;
@Mock
PathResolver pathResolver;

static class JobExecutionPreconditionsProvider implements ArgumentsProvider {
@Override
Expand All @@ -63,15 +63,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext extensionCo

@BeforeEach
void setUp() {
closeable = MockitoAnnotations.openMocks(this);

batchExecutor = new BatchExecutor(null, jobLauncher, transactionFilterStep, panReaderStep, storeService, resolver);
}

@SneakyThrows
@AfterEach
void tearDown() {
closeable.close();
batchExecutor = new BatchExecutor(null, jobLauncher, transactionFilterStep, panReaderStep, storeService, pathResolver);
}

@SneakyThrows
Expand All @@ -85,9 +77,9 @@ void batchExecutorWithParameters(Resource[] transactionFilesMocked,
String hpanPath = "/hpan";
BDDMockito.when(transactionFilterStep.getTransactionDirectoryPath())
.thenReturn(resourcesPath);
BDDMockito.when(resolver.getResources(resourcesPath + "/*.csv")).thenReturn(transactionFilesMocked);
BDDMockito.when(pathResolver.getCsvResources(resourcesPath)).thenReturn(transactionFilesMocked);
BDDMockito.when(panReaderStep.getHpanDirectoryPath()).thenReturn(hpanPath);
BDDMockito.when(resolver.getResources(hpanPath)).thenReturn(hpanFilesMocked);
BDDMockito.when(pathResolver.getResources(hpanPath)).thenReturn(hpanFilesMocked);
batchExecutor.setHpanListRecoveryEnabled(hpanRecoveryEnabled);

batchExecutor.execute(new Date());
Expand Down
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();
}

}
4 changes: 2 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<artifactId>rtd-ms-transaction-filter</artifactId>
<groupId>it.gov.pagopa.rtd.ms</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter.api</groupId>
<artifactId>rtd-ms-transaction-filter-api</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<packaging>pom</packaging>

Expand Down
4 changes: 2 additions & 2 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<artifactId>rtd-ms-transaction-filter</artifactId>
<groupId>it.gov.pagopa.rtd.ms</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter</groupId>
<artifactId>transaction-filter-app</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<dependencies>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ batchConfiguration:
rest-client:
user-agent:
prefix: BatchService
version: 2.1.1
version: 2.2.1
hpan:
serviceCode: hpan-service
base-url: ${HPAN_SERVICE_URL:https://bpd-dev.azure-api.net:${HPAN_SERVICE_PORT:443}}
Expand Down
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<artifactId>rtd-ms-transaction-filter</artifactId>
<groupId>it.gov.pagopa.rtd.ms</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter</groupId>
<artifactId>rtd-ms-transaction-filter-core</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions integration/jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<artifactId>rtd-ms-transaction-filter-integration</artifactId>
<groupId>it.gov.pagopa.rtd.ms.transaction_filter</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter.integration</groupId>
<artifactId>rtd-ms-transaction-filter-integration-jpa</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<artifactId>rtd-ms-transaction-filter</artifactId>
<groupId>it.gov.pagopa.rtd.ms</groupId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter</groupId>
<artifactId>rtd-ms-transaction-filter-integration</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<packaging>pom</packaging>

Expand Down
4 changes: 2 additions & 2 deletions integration/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>it.gov.pagopa.rtd.ms.transaction_filter</groupId>
<artifactId>rtd-ms-transaction-filter-integration</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
</parent>

<groupId>it.gov.pagopa.rtd.ms.transaction_filter.integration</groupId>
<artifactId>rtd-ms-transaction-filter-integration-rest</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<dependencies>
<dependency>
Expand Down
Loading

0 comments on commit 1a9e0fe

Please sign in to comment.