Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: P4ADEV-2067-OrganizationPaymentsReportingPagoPaRetrieverActivity #102

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.gov.pagopa.payhub.activities.activity.paymentsreporting;

import java.util.List;

/**
* Interface for retrieving the list of PaymentsReportingIdDTOs that have not been imported yet.
*/
public interface OrganizationPaymentsReportingPagoPaRetrieverActivity {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Retrieves the list of PaymentsReportingIdDTOs that have not been imported yet.
*
* @param organizationId the ID of the organization
* @return a list of IDs of PaymentsReportingIdDTOs that have not been imported
*/
List<Long> retrieve(Long organizationId);
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package it.gov.pagopa.payhub.activities.activity.paymentsreporting;

import it.gov.pagopa.payhub.activities.connector.pagopapayments.PaymentsReportingPagoPaService;
import it.gov.pagopa.payhub.activities.connector.processexecutions.IngestionFlowFileService;
import it.gov.pagopa.pu.pagopapayments.dto.generated.PaymentsReportingIdDTO;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile.FlowFileTypeEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.Comparator;
import java.util.List;

@Lazy
@Service
public class OrganizationPaymentsReportingPagoPaRetrieverActivityImpl implements OrganizationPaymentsReportingPagoPaRetrieverActivity {
private static final FlowFileTypeEnum FLOW_FILE_TYPE = FlowFileTypeEnum.PAYMENTS_REPORTING_PAGOPA;
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

private final PaymentsReportingPagoPaService paymentsReportingPagoPaService;
private final IngestionFlowFileService ingestionFlowFileService;

public OrganizationPaymentsReportingPagoPaRetrieverActivityImpl(PaymentsReportingPagoPaService paymentsReportingPagoPaService,
IngestionFlowFileService ingestionFlowFileService) {
this.paymentsReportingPagoPaService = paymentsReportingPagoPaService;
this.ingestionFlowFileService = ingestionFlowFileService;
}

@Override
public List<Long> retrieve(Long organizationId) {
List<PaymentsReportingIdDTO> paymentsReportingIds = paymentsReportingPagoPaService.getPaymentsReportingList(organizationId);
OffsetDateTime oldestDate = findOldestDate(paymentsReportingIds);
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

List<IngestionFlowFile> ingestionFlowFiles = ingestionFlowFileService.findByOrganizationIdFlowTypeCreateDate(organizationId, FLOW_FILE_TYPE, oldestDate);

getNotImportedFilteredByFileName(ingestionFlowFiles, paymentsReportingIds);

// TODO in P4ADEV-2005: implement loop to retrieve ingestion flow file from PagoPA
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return List.of();
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Filters the list of PaymentsReportingIdDTOs to find those that have not been imported yet based on file names.
*
* @param ingestionFlowFiles the list of ingestion flow files
* @param paymentsReportingIds the list of payments reporting IDs
* @return a list of PaymentsReportingIdDTOs that have not been imported
*/
private List<PaymentsReportingIdDTO> getNotImportedFilteredByFileName(List<IngestionFlowFile> ingestionFlowFiles, List<PaymentsReportingIdDTO> paymentsReportingIds) {
List<String> importedFileNames = ingestionFlowFiles.stream()
.map(IngestionFlowFile::getFileName)
.toList();
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

return paymentsReportingIds.stream()
.filter(item -> !importedFileNames.contains(item.getPaymentsReportingFileName()))
.toList();
}

/**
* Finds the oldest date in the list of PaymentsReportingIdDTOs.
*
* @param paymentsReportingIds the list of payments reporting IDs
* @return the oldest date found in the list, or null if the list is empty
*/
private OffsetDateTime findOldestDate(List<PaymentsReportingIdDTO> paymentsReportingIds) {
return paymentsReportingIds.stream()
.map(PaymentsReportingIdDTO::getFlowDateTime)
.min(Comparator.naturalOrder())
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.gov.pagopa.payhub.activities.connector.pagopapayments;

import it.gov.pagopa.pu.pagopapayments.dto.generated.PaymentsReportingIdDTO;

import java.util.List;

/**
* This interface provides a method for payments reporting on PagoPa service
*/
public interface PaymentsReportingPagoPaService {

/**
* Retrieve the payments reporting for the organization
*
* @param organizationId the organization id
* @return the list of payments reporting info data to download
*/
List<PaymentsReportingIdDTO> getPaymentsReportingList(Long organizationId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it.gov.pagopa.payhub.activities.connector.pagopapayments;

import it.gov.pagopa.payhub.activities.connector.auth.AuthnService;
import it.gov.pagopa.payhub.activities.connector.pagopapayments.client.PaymentsReportingPagoPaClient;
import it.gov.pagopa.pu.pagopapayments.dto.generated.PaymentsReportingIdDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Lazy
@Service
@Slf4j
public class PaymentsReportingPagoPaServiceImpl implements PaymentsReportingPagoPaService {
private final PaymentsReportingPagoPaClient paymentsReportingPagoPaClient;
private final AuthnService authnService;

public PaymentsReportingPagoPaServiceImpl(PaymentsReportingPagoPaClient paymentsReportingPagoPaClient, AuthnService authnService) {
this.paymentsReportingPagoPaClient = paymentsReportingPagoPaClient;
this.authnService = authnService;
}

@Override
public List<PaymentsReportingIdDTO> getPaymentsReportingList(Long organizationId) {
return paymentsReportingPagoPaClient.getPaymentsReportingList(organizationId, authnService.getAccessToken());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package it.gov.pagopa.payhub.activities.connector.pagopapayments.client;

import it.gov.pagopa.payhub.activities.connector.pagopapayments.config.PagoPaPaymentsApisHolder;
import it.gov.pagopa.pu.pagopapayments.dto.generated.PaymentsReportingIdDTO;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Lazy
@Service
public class PaymentsReportingPagoPaClient {
private final PagoPaPaymentsApisHolder pagoPaPaymentsApisHolder;

public PaymentsReportingPagoPaClient(PagoPaPaymentsApisHolder pagoPaPaymentsApisHolder) {
this.pagoPaPaymentsApisHolder = pagoPaPaymentsApisHolder;
}

public List<PaymentsReportingIdDTO> getPaymentsReportingList(Long organizationId, String accessToken) {
return pagoPaPaymentsApisHolder.getPaymentsReportingApi(accessToken).getPaymentsReportingList(organizationId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.payhub.activities.connector.pagopapayments.config;

import it.gov.pagopa.pu.pagopapayments.client.generated.AcaApi;
import it.gov.pagopa.pu.pagopapayments.client.generated.PaymentsReportingApi;
import it.gov.pagopa.pu.pagopapayments.generated.ApiClient;
import it.gov.pagopa.pu.pagopapayments.generated.BaseApi;
import jakarta.annotation.PreDestroy;
Expand All @@ -13,9 +14,8 @@
@Lazy
@Service
public class PagoPaPaymentsApisHolder {

private final AcaApi acaApi;

private final PaymentsReportingApi paymentsReportingApi;
private final ThreadLocal<String> bearerTokenHolder = new ThreadLocal<>();

public PagoPaPaymentsApisHolder(
Expand All @@ -27,6 +27,7 @@ public PagoPaPaymentsApisHolder(
apiClient.setBearerToken(bearerTokenHolder::get);

this.acaApi = new AcaApi(apiClient);
this.paymentsReportingApi = new PaymentsReportingApi(apiClient);
}

@PreDestroy
Expand All @@ -39,6 +40,11 @@ public AcaApi getAcaApi(String accessToken){
return getApi(accessToken, acaApi);
}

/** It will return a {@link PaymentsReportingApi} instrumented with the provided accessToken. Use null if auth is not required */
public PaymentsReportingApi getPaymentsReportingApi(String accessToken){
return getApi(accessToken, paymentsReportingApi);
}

private <T extends BaseApi> T getApi(String accessToken, T api) {
bearerTokenHolder.set(accessToken);
return api;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package it.gov.pagopa.payhub.activities.connector.processexecutions;

import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile.FlowFileTypeEnum;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;


Expand All @@ -10,6 +13,5 @@ public interface IngestionFlowFileService {

Optional<IngestionFlowFile> findById(Long ingestionFlowFileId);
Integer updateStatus(Long ingestionFlowFileId, IngestionFlowFile.StatusEnum status, String codError, String discardFileName);


List<IngestionFlowFile> findByOrganizationIdFlowTypeCreateDate(Long organizationId, FlowFileTypeEnum flowFileType, OffsetDateTime creationDateFrom);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import it.gov.pagopa.payhub.activities.connector.auth.AuthnService;
import it.gov.pagopa.payhub.activities.connector.processexecutions.client.IngestionFlowFileClient;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile.FlowFileTypeEnum;
import it.gov.pagopa.pu.processexecutions.dto.generated.PagedModelIngestionFlowFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Lazy
Expand Down Expand Up @@ -34,4 +39,11 @@ public Integer updateStatus(Long ingestionFlowFileId, IngestionFlowFile.StatusEn
return ingestionFlowFileClient.updateStatus(ingestionFlowFileId,status, codError,discardFileName, authnService.getAccessToken());

}

@Override
public List<IngestionFlowFile> findByOrganizationIdFlowTypeCreateDate(Long organizationId, FlowFileTypeEnum flowFileType, OffsetDateTime creationDateFrom) {
PagedModelIngestionFlowFile pagedModelIngestionFlowFile = ingestionFlowFileClient
.findByOrganizationIDFlowTypeCreateDate(organizationId, flowFileType, creationDateFrom, authnService.getAccessToken());
return Objects.requireNonNull(pagedModelIngestionFlowFile.getEmbedded()).getIngestionFlowFiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import it.gov.pagopa.payhub.activities.connector.processexecutions.config.ProcessExecutionsApisHolder;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile.FlowFileTypeEnum;
import it.gov.pagopa.pu.processexecutions.dto.generated.PagedModelIngestionFlowFile;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;

@Lazy
@Service
public class IngestionFlowFileClient {
Expand All @@ -26,6 +30,9 @@ public Integer updateStatus(Long ingestionFlowFileId, IngestionFlowFile.StatusEn
.updateStatus(ingestionFlowFileId, status.name() ,codError, discardFileName);
}


public PagedModelIngestionFlowFile findByOrganizationIDFlowTypeCreateDate(Long organizationId, FlowFileTypeEnum flowFileType, OffsetDateTime creationDateFrom, String accessToken) {
return processExecutionsApisHolder.getIngestionFlowFileSearchControllerApi(accessToken)
.crudIngestionFlowFilesFindByOrganizationIDFlowTypeCreateDate(String.valueOf(organizationId), flowFileType.getValue(), creationDateFrom, null,null, null, null, null, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.gov.pagopa.pu.processexecutions.client.generated.IngestionFlowFileEntityControllerApi;
import it.gov.pagopa.pu.processexecutions.client.generated.IngestionFlowFileEntityExtendedControllerApi;
import it.gov.pagopa.pu.processexecutions.client.generated.IngestionFlowFileSearchControllerApi;
import it.gov.pagopa.pu.processexecutions.generated.ApiClient;
import it.gov.pagopa.pu.processexecutions.generated.BaseApi;
import jakarta.annotation.PreDestroy;
Expand All @@ -17,7 +18,7 @@ public class ProcessExecutionsApisHolder {

private final IngestionFlowFileEntityControllerApi ingestionFlowFileEntityControllerApi;
private final IngestionFlowFileEntityExtendedControllerApi ingestionFlowFileEntityExtendedControllerApi;

private final IngestionFlowFileSearchControllerApi ingestionFlowFileSearchControllerApi;
private final ThreadLocal<String> bearerTokenHolder = new ThreadLocal<>();

public ProcessExecutionsApisHolder(
Expand All @@ -30,6 +31,7 @@ public ProcessExecutionsApisHolder(

this.ingestionFlowFileEntityControllerApi = new IngestionFlowFileEntityControllerApi(apiClient);
this.ingestionFlowFileEntityExtendedControllerApi = new IngestionFlowFileEntityExtendedControllerApi(apiClient);
this.ingestionFlowFileSearchControllerApi = new IngestionFlowFileSearchControllerApi(apiClient);
}

@PreDestroy
Expand All @@ -45,7 +47,9 @@ public IngestionFlowFileEntityExtendedControllerApi getIngestionFlowFileEntityEx
return getApi(accessToken, ingestionFlowFileEntityExtendedControllerApi);
}


public IngestionFlowFileSearchControllerApi getIngestionFlowFileSearchControllerApi(String accessToken){
return getApi(accessToken, ingestionFlowFileSearchControllerApi);
}

private <T extends BaseApi> T getApi(String accessToken, T api) {
bearerTokenHolder.set(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ public List<Path> unzip(Path source, Path target) {
* <li>An I/O error occurs during extraction.</li>
* </ul>
*/
public List<Path> unzip(Path path) {
return unzip(path, path);
}
public List<Path> unzip(Path path) { return unzip(path, path.getParent()); }

/**
* Validates the entry count against the maximum allowed entries.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package it.gov.pagopa.payhub.activities.activity.paymentsreporting;

import it.gov.pagopa.payhub.activities.connector.pagopapayments.PaymentsReportingPagoPaService;
import it.gov.pagopa.payhub.activities.connector.processexecutions.IngestionFlowFileService;
import it.gov.pagopa.payhub.activities.util.faker.IngestionFlowFileFaker;
import it.gov.pagopa.pu.pagopapayments.dto.generated.PaymentsReportingIdDTO;
import it.gov.pagopa.pu.processexecutions.dto.generated.IngestionFlowFile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.OffsetDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class OrganizationPaymentsReportingPagoPaRetrieverActivityImplTest {
@Mock
private PaymentsReportingPagoPaService paymentsReportingPagoPaServiceMock;
@Mock
private IngestionFlowFileService ingestionFlowFileServiceMock;

private OrganizationPaymentsReportingPagoPaRetrieverActivity activity;

@BeforeEach
void setUp() {
activity = new OrganizationPaymentsReportingPagoPaRetrieverActivityImpl(
paymentsReportingPagoPaServiceMock,
ingestionFlowFileServiceMock
);
}

@AfterEach
void tearDown() {
Mockito.verifyNoMoreInteractions(paymentsReportingPagoPaServiceMock, ingestionFlowFileServiceMock);
}

@Test
void retrieve() {
// Given
Long organizationId = 1L;
String idFlow1 = "flow-123";
String idFlow2 = "flow-456";
OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime yesterday = now.minusDays(1);
OffsetDateTime theDayBeforeYesterday = now.minusDays(2);
IngestionFlowFile.FlowFileTypeEnum flowFileType = IngestionFlowFile.FlowFileTypeEnum.PAYMENTS_REPORTING_PAGOPA;
PaymentsReportingIdDTO paymentsReportingIdDTO1 = PaymentsReportingIdDTO.builder()
.pagopaPaymentsReportingId(idFlow1)
.flowDateTime(theDayBeforeYesterday)
.paymentsReportingFileName(idFlow1 + theDayBeforeYesterday +".xml")
.build();
PaymentsReportingIdDTO paymentsReportingIdDTO2 = PaymentsReportingIdDTO.builder()
.pagopaPaymentsReportingId(idFlow2)
.flowDateTime(yesterday)
.paymentsReportingFileName(idFlow2 + yesterday +".xml")
.build();
IngestionFlowFile ingestionFlowFile = IngestionFlowFileFaker.buildIngestionFlowFile();
ingestionFlowFile.setFileName(idFlow1 + theDayBeforeYesterday +".xml");

when(paymentsReportingPagoPaServiceMock.getPaymentsReportingList(organizationId))
.thenReturn(List.of(paymentsReportingIdDTO1, paymentsReportingIdDTO2));
doReturn(List.of(ingestionFlowFile)).when(ingestionFlowFileServiceMock)
.findByOrganizationIdFlowTypeCreateDate(organizationId, flowFileType, theDayBeforeYesterday);

// When Then
assertDoesNotThrow(() -> activity.retrieve(organizationId));
}
}
Loading