Skip to content

Commit

Permalink
feat: P4ADEV-1625 classifyiufactivity-retrieve-iuvs-save (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanotamburini authored Dec 23, 2024
1 parent 2ca15ce commit f9462fa
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package it.gov.pagopa.payhub.activities.activity.classifications;

import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityMethod;
import it.gov.pagopa.payhub.activities.dto.classifications.IufClassificationActivityResult;

/**
* Interface for defining an activity to process payment reporting classifications based on IUF.
*/
@ActivityInterface
public interface IufClassificationActivity {

/**
* Processes IUF classification based on the provided parameters.
*
* @param organizationId the unique identifier of the organization
* @param treasuryId the unique identifier of treasury
* @param iuf the unique identifier of the payment reporting flow (IUF)
* @return true if the classification is successful, false otherwise
* @return IufClassificationActivityResult containing a list of payments and success flag
*/
boolean classify(String organizationId, String iuf);
@ActivityMethod
IufClassificationActivityResult classify(Long organizationId, Long treasuryId, String iuf);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package it.gov.pagopa.payhub.activities.activity.classifications;

import it.gov.pagopa.payhub.activities.dao.ClassificationDao;
import it.gov.pagopa.payhub.activities.dao.PaymentsReportingDao;
import it.gov.pagopa.payhub.activities.dto.classifications.ClassificationDTO;
import it.gov.pagopa.payhub.activities.dto.classifications.IufClassificationActivityResult;
import it.gov.pagopa.payhub.activities.dto.classifications.Transfer2ClassifyDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Lazy
@Component
public class IufClassificationActivityImpl implements IufClassificationActivity {
private final PaymentsReportingDao paymentsReportingDao;
private final ClassificationDao classificationDao;

public IufClassificationActivityImpl(PaymentsReportingDao paymentsReportingDao, ClassificationDao classificationDao) {
this.paymentsReportingDao = paymentsReportingDao;
this.classificationDao = classificationDao;
}

@Override
public IufClassificationActivityResult classify(Long organizationId, Long treasuryId, String iuf) {
log.debug("Starting IUF Classification for organization id {} and iuf {}", organizationId,iuf);

List<Transfer2ClassifyDTO> transfers2classify =
paymentsReportingDao.findByOrganizationIdAndIuf(organizationId, iuf)
.stream()
.map(paymentsReportingDTO ->
Transfer2ClassifyDTO.builder()
.iuv(paymentsReportingDTO.getCreditorReferenceId())
.iur(paymentsReportingDTO.getRegulationUniqueIdentifier())
.transferIndex(paymentsReportingDTO.getTransferIndex())
.build())
.toList();

if (transfers2classify.isEmpty()) {
log.debug("Saving payments reporting found for organization id {} and iuf: {}", organizationId, iuf);
saveClassification(organizationId, treasuryId, iuf);
}

return IufClassificationActivityResult.builder()
.organizationId(organizationId)
.transfers2classify(transfers2classify)
.success(true)
.build();
}

/**
* save classification data
*
* @param organizationId organization id
* @param treasuryId treasury id
* @param iuf flow unique identifier
*/
private void saveClassification(Long organizationId, Long treasuryId, String iuf) {
log.debug("Saving classification TES_NO_MATCH for organizationId: {} - treasuryId: {} - iuf: {}", organizationId, treasuryId, iuf);

classificationDao.save(ClassificationDTO.builder()
.organizationId(organizationId)
.treasuryId(treasuryId)
.iuf(iuf)
.classificationsEnum(ClassificationsEnum.TES_NO_MATCH)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package it.gov.pagopa.payhub.activities.dao;

import it.gov.pagopa.payhub.activities.dto.classifications.ClassificationDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;

/**
* Data Access Object interface for saving payments classification
* Data Access Object interface for saving payments classification
*/
public interface ClassificationDao {
/**
* save classification
*
* @param organizationId organization id
* @param iuf fow identifier
* @param classification classification to delete
* @return true for success deletion
*/
* @param classificationDTO dto classification to save
*/
void save(ClassificationDTO classificationDTO);

/**
* delete classification
*
* @param organizationId organization id
* @param iuf fow identifier
* @param classification classification to delete
* @return true for success deletion
*/
boolean deleteClassificationByIuf(Long organizationId, String iuf, ClassificationsEnum classification);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public interface PaymentsReportingDao {
* fields (e.g., generated IDs or timestamps).
*/
List<PaymentsReportingDTO> saveAll(List<PaymentsReportingDTO> dtos);

/**
*
* @param organizationId organization id
* @param iuf identifies a specific reporting code
* @return List of PaymentsReportingDTO objects that may be an empty list
*/
List<PaymentsReportingDTO> findByOrganizationIdAndIuf(Long organizationId, String iuf);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package it.gov.pagopa.payhub.activities.dto.classifications;

import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class ClassificationDTO implements Serializable {
private Long organizationId;
private Long transferId;
private Long paymentNotifyId;
private Long paymentReportingId;
private Long treasuryId;
private String iuf;
private String iud;
private String iuv;
private String iur;
private int transferIndex;
private ClassificationsEnum classificationsEnum;
private LocalDate creationDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package it.gov.pagopa.payhub.activities.dto.classifications;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class IufClassificationActivityResult {
private Long organizationId;
private List<Transfer2ClassifyDTO> transfers2classify;
private boolean success;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.gov.pagopa.payhub.activities.dto.classifications;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Transfer2ClassifyDTO {
private String iuv;
private String iur;
private int transferIndex;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package it.gov.pagopa.payhub.activities.activity.classifications;

import it.gov.pagopa.payhub.activities.dao.ClassificationDao;
import it.gov.pagopa.payhub.activities.dao.PaymentsReportingDao;
import it.gov.pagopa.payhub.activities.dto.classifications.ClassificationDTO;
import it.gov.pagopa.payhub.activities.dto.classifications.Transfer2ClassifyDTO;
import it.gov.pagopa.payhub.activities.dto.classifications.IufClassificationActivityResult;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.utility.faker.ClassificationFaker;
import it.gov.pagopa.payhub.activities.utility.faker.PaymentsReportingFaker;
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.util.ArrayList;
import java.util.List;

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

@ExtendWith(MockitoExtension.class)
class IufClassificationActivityTest {
@Mock
private PaymentsReportingDao paymentsReportingDaoMock;

@Mock
private ClassificationDao classificationDaoMock;

private IufClassificationActivity iufClassificationActivity;

private static final Long ORGANIZATIONID = 1L;
private static final Long TREASURYID = 1L;
private static final String IUF = "IUF";

@BeforeEach
void init() {
iufClassificationActivity = new IufClassificationActivityImpl(paymentsReportingDaoMock, classificationDaoMock);
}

@AfterEach
void verifyNoMoreInteractions(){
Mockito.verifyNoMoreInteractions(paymentsReportingDaoMock,classificationDaoMock);
}

@Test
void givenReportedTransferWhenClassifyThenOk() {
ClassificationDTO classificationDTO = ClassificationFaker.buildClassificationDTO();

PaymentsReportingDTO expectedPaymentsReportingDTO = PaymentsReportingFaker.buildClassifyResultDTO();
List<PaymentsReportingDTO> expectedPaymentsReportingDTOS = new ArrayList<>();
expectedPaymentsReportingDTOS.add(expectedPaymentsReportingDTO);

List<Transfer2ClassifyDTO> expectedTransfer2ClassifyDTOS =
expectedPaymentsReportingDTOS
.stream()
.map(paymentsReportingDTO ->
Transfer2ClassifyDTO.builder()
.iuv(paymentsReportingDTO.getCreditorReferenceId())
.iur(paymentsReportingDTO.getRegulationUniqueIdentifier())
.transferIndex(paymentsReportingDTO.getTransferIndex())
.build())
.toList();

IufClassificationActivityResult expectedIufClassificationActivityResult =
IufClassificationActivityResult
.builder()
.organizationId(1L)
.transfers2classify(expectedTransfer2ClassifyDTOS)
.success(true)
.build();

when(paymentsReportingDaoMock.findByOrganizationIdAndIuf(ORGANIZATIONID, IUF))
.thenReturn(expectedPaymentsReportingDTOS);

IufClassificationActivityResult iufClassificationActivityResult =
iufClassificationActivity.classify(ORGANIZATIONID, TREASURYID, IUF);

assertEquals(iufClassificationActivityResult,expectedIufClassificationActivityResult);

Mockito.verify(paymentsReportingDaoMock, Mockito.times(1)).findByOrganizationIdAndIuf(ORGANIZATIONID, IUF);
Mockito.verify(classificationDaoMock, Mockito.times(0)).save(classificationDTO);
}

@Test
void givenNoReportedTransferWhenClassifyThenAnomalyClassificationSave() {
ClassificationDTO classificationDTO = ClassificationFaker.buildClassificationDTO();

IufClassificationActivityResult expectedIufClassificationActivityResult =
IufClassificationActivityResult
.builder()
.organizationId(1L)
.transfers2classify(new ArrayList<>())
.success(true)
.build();

when(paymentsReportingDaoMock.findByOrganizationIdAndIuf(ORGANIZATIONID, IUF))
.thenReturn(new ArrayList<>());

IufClassificationActivityResult iufClassificationActivityResult =
iufClassificationActivity.classify(ORGANIZATIONID, TREASURYID, IUF);

assertEquals(iufClassificationActivityResult,expectedIufClassificationActivityResult);

Mockito.verify(paymentsReportingDaoMock, Mockito.times(1)).findByOrganizationIdAndIuf(ORGANIZATIONID, IUF);
Mockito.verify(classificationDaoMock, Mockito.times(1)).save(classificationDTO);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.gov.pagopa.payhub.activities.utility.faker;

import it.gov.pagopa.payhub.activities.dto.classifications.ClassificationDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;

public class ClassificationFaker {

public static ClassificationDTO buildClassificationDTO(){
return ClassificationDTO.builder()
.organizationId(1L)
.treasuryId(1L)
.iuf("IUF")
.classificationsEnum(ClassificationsEnum.TES_NO_MATCH)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package it.gov.pagopa.payhub.activities.utility.faker;

import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;

public class PaymentsReportingFaker {

public static PaymentsReportingDTO buildClassifyResultDTO(){
return PaymentsReportingDTO.builder()
.organizationId(1L)
.creditorReferenceId("IUV")
.regulationUniqueIdentifier("IUR")
.transferIndex(1)
.build();
}
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.20.0
1.20.2-SNAPSHOT

0 comments on commit f9462fa

Please sign in to comment.