Skip to content

Commit

Permalink
feat: P4ADEV-1653 wf ingestion flow file worker dao for saving data (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: antonioT90 <34568575+antonioT90@users.noreply.github.com>
  • Loading branch information
macacia and antonioT90 authored Dec 19, 2024
1 parent 7a02bf5 commit e510fd1
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repositories {
val springDocOpenApiVersion = "2.6.0"
val openApiToolsVersion = "0.2.6"
val micrometerVersion = "1.4.0"
val p4paActivitiesVersion = "1.14.0"
val p4paActivitiesVersion = "1.16.0"
val postgresJdbcVersion = "42.7.4"

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ io.opentelemetry:opentelemetry-sdk:1.37.0=compileClasspath
io.swagger.core.v3:swagger-annotations-jakarta:2.2.22=compileClasspath
io.swagger.core.v3:swagger-core-jakarta:2.2.22=compileClasspath
io.swagger.core.v3:swagger-models-jakarta:2.2.22=compileClasspath
it.gov.pagopa.payhub:p4pa-payhub-activities:1.14.0=compileClasspath
it.gov.pagopa.payhub:p4pa-payhub-activities:1.16.0=compileClasspath
jakarta.activation:jakarta.activation-api:2.1.3=compileClasspath
jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath
jakarta.persistence:jakarta.persistence-api:3.1.0=compileClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package it.gov.pagopa.pu.worker.ingestionflowfile;

import it.gov.pagopa.payhub.activities.dao.IngestionFlowFileDao;
import it.gov.pagopa.payhub.activities.dto.IngestionFlowFileDTO;
import it.gov.pagopa.pu.worker.ingestionflowfile.mapper.IngestionFlowFileMapper;
import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;
import it.gov.pagopa.pu.worker.ingestionflowfile.repository.IngestionFlowFileRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class IngestionFlowFileDaoImpl implements IngestionFlowFileDao {
private final IngestionFlowFileRepository repository;
private final IngestionFlowFileMapper ingestionFlowFileMapper;

public IngestionFlowFileDaoImpl(IngestionFlowFileRepository repository,
IngestionFlowFileMapper ingestionFlowFileMapper) {
this.repository = repository;
this.ingestionFlowFileMapper = ingestionFlowFileMapper;
}

@Override
public Optional<IngestionFlowFileDTO> findById(Long ingestionFlowFileId) {
Optional<IngestionFlowFile> ingestionFlowFile = repository.findById(ingestionFlowFileId);
return ingestionFlowFile.map(ingestionFlowFileMapper::mapIngestionFlowFile2DTO);
}

@Override
public boolean updateStatus(Long ingestionFlowFileId, String status) {
return false; //TODO: implementation will be added with the task n. P4ADEV-1638
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package it.gov.pagopa.pu.worker.ingestionflowfile.mapper;

import it.gov.pagopa.payhub.activities.dto.IngestionFlowFileDTO;
import it.gov.pagopa.payhub.activities.dto.OrganizationDTO;
import it.gov.pagopa.payhub.activities.enums.IngestionFlowFileType;
import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class IngestionFlowFileMapper {

public IngestionFlowFile mapIngestionFlowFileDTO2Model(IngestionFlowFileDTO dto) {
return IngestionFlowFile.builder()
.ingestionFlowFileId(dto.getIngestionFlowFileId())
.flowFileType(dto.getFlowFileType().name())
.version(dto.getVersion())
.org(dto.getOrg().getOrgId())
.status(dto.getStatus())
.iuf(dto.getIuf())
.numTotalRows(dto.getNumTotalRows().intValue())
.numCorrectlyImportedRows(dto.getNumCorrectlyImportedRows().intValue())
.creationDate(dto.getCreationDate().toInstant())
.lastUpdateDate(dto.getLastUpdateDate().toInstant())
.flagActive(dto.isFlagActive())
.operatorName(dto.getOperatorName())
.flagSpontaneous(dto.getFlagSpontaneous())
.filePathName(dto.getFilePath())
.fileName(dto.getFileName())
.pdfGenerated(dto.getPdfGenerated().intValue())
.codRequestToken(dto.getCodRequestToken())
.codError(dto.getCodError())
.pspIdentifier(dto.getPspIdentifier())
.flowDateTime(dto.getFlowDateTime())
.fileSourceCode(dto.getFileSourceCode())
.discardFileName(dto.getDiscardFileName())
.build();
}

public IngestionFlowFileDTO mapIngestionFlowFile2DTO(IngestionFlowFile model) {
return IngestionFlowFileDTO.builder()
.ingestionFlowFileId(model.getIngestionFlowFileId())
.flowFileType(IngestionFlowFileType.valueOf(model.getFlowFileType()))
.version(model.getVersion())
.org(OrganizationDTO.builder().orgId(model.getOrg()).build())
.status(model.getStatus())
.iuf(model.getIuf())
.numTotalRows(Long.valueOf(model.getNumTotalRows()))
.numCorrectlyImportedRows(Long.valueOf(model.getNumCorrectlyImportedRows()))
.creationDate(Date.from(model.getCreationDate()))
.lastUpdateDate(Date.from(model.getLastUpdateDate()))
.flagActive(model.isFlagActive())
.operatorName(model.getOperatorName())
.flagSpontaneous(model.isFlagSpontaneous())
.filePath(model.getFilePathName())
.fileName(model.getFileName())
.pdfGenerated(Long.valueOf(model.getPdfGenerated()))
.codRequestToken(model.getCodRequestToken())
.codError(model.getCodError())
.pspIdentifier(model.getPspIdentifier())
.flowDateTime(model.getFlowDateTime())
.fileSourceCode(model.getFileSourceCode())
.discardFileName(model.getDiscardFileName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package it.gov.pagopa.pu.worker.ingestionflowfile.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;

@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "ingestion_flow_file")
public class IngestionFlowFile implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ingestion_flow_file_generator")
@SequenceGenerator(name = "ingestion_flow_file_generator", sequenceName = "ingestion_flow_file_seq", allocationSize = 1)
private Long ingestionFlowFileId;
private String flowFileType;
private int version;
private Long org;
private String status;
private String iuf;
private int numTotalRows;
private int numCorrectlyImportedRows;
private Instant creationDate;
private Instant lastUpdateDate;
private boolean flagActive;
private String operatorName;
private boolean flagSpontaneous;
private String filePathName;
private String fileName;
private int pdfGenerated;
private String codRequestToken;
private String codError;
private String pspIdentifier;
private LocalDateTime flowDateTime;
private String state;
private String fileSourceCode;
private String discardFileName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.gov.pagopa.pu.worker.ingestionflowfile.repository;

import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface IngestionFlowFileRepository extends JpaRepository<IngestionFlowFile, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package it.gov.pagopa.pu.worker.ingestionflowfile;

import it.gov.pagopa.payhub.activities.dto.IngestionFlowFileDTO;
import it.gov.pagopa.pu.worker.ingestionflowfile.mapper.IngestionFlowFileMapper;
import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;
import it.gov.pagopa.pu.worker.ingestionflowfile.repository.IngestionFlowFileRepository;
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.junit.jupiter.MockitoExtension;

import java.util.Optional;

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

@ExtendWith(MockitoExtension.class)
class IngestionFlowFileDaoImplTest {

@Mock
private IngestionFlowFileRepository repositoryMock;
@Mock
private IngestionFlowFileMapper mapperMock;

private IngestionFlowFileDaoImpl service;

@BeforeEach
void setUp() {
service = new IngestionFlowFileDaoImpl(repositoryMock, mapperMock);
}

@Test
void testFindById() {
// Arrange
Long id = 123L;
IngestionFlowFile ingestionFlowFile = IngestionFlowFile.builder()
.ingestionFlowFileId(id)
.build();

IngestionFlowFileDTO expectedDTO = IngestionFlowFileDTO.builder()
.ingestionFlowFileId(id).build();

when(mapperMock.mapIngestionFlowFile2DTO(ingestionFlowFile)).thenReturn(expectedDTO);
when(repositoryMock.findById(id)).thenReturn(Optional.of(ingestionFlowFile));

// Act
Optional<IngestionFlowFileDTO> result = service.findById(id);

// Assert
assertEquals(Optional.of(expectedDTO), result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.gov.pagopa.pu.worker.ingestionflowfile.mapper;

import it.gov.pagopa.payhub.activities.dto.IngestionFlowFileDTO;
import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static it.gov.pagopa.pu.worker.util.TestUtils.checkNotNullFields;
import static it.gov.pagopa.pu.worker.util.faker.IngestionFlowFileFakerBuilder.buildFakeIngestionFlowFile;
import static it.gov.pagopa.pu.worker.util.faker.IngestionFlowFileFakerBuilder.buildFakeIngestionFlowFileDTO;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class IngestionFlowFileMapperTest {
private IngestionFlowFileMapper mapper;

@BeforeEach
void setUp() { mapper = new IngestionFlowFileMapper(); }

@Test
void mapIngestionFlowFile2DTO() {
IngestionFlowFile model = buildFakeIngestionFlowFile();
IngestionFlowFileDTO result = mapper.mapIngestionFlowFile2DTO(model);

assertNotNull(result);
checkNotNullFields(result);
}

@Test
void mapIngestionFlowFileDTO2Model() {
IngestionFlowFileDTO dto = buildFakeIngestionFlowFileDTO();
IngestionFlowFile result = mapper.mapIngestionFlowFileDTO2Model(dto);
assertNotNull(result);
checkNotNullFields(result, "state");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package it.gov.pagopa.pu.worker.util.faker;

import it.gov.pagopa.payhub.activities.dto.IngestionFlowFileDTO;
import it.gov.pagopa.payhub.activities.dto.OrganizationDTO;
import it.gov.pagopa.payhub.activities.enums.IngestionFlowFileType;
import it.gov.pagopa.pu.worker.ingestionflowfile.model.IngestionFlowFile;


import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Date;

public class IngestionFlowFileFakerBuilder {

// Static builder for IngestionFlowFile entity
public static IngestionFlowFile buildFakeIngestionFlowFile() {
return IngestionFlowFile.builder()
.ingestionFlowFileId(1L)
.flowFileType(IngestionFlowFileType.PAYMENTS_REPORTING.name())
.version(1)
.org(1001L)
.status("ACTIVE")
.iuf("IUF12345")
.numTotalRows(100)
.numCorrectlyImportedRows(95)
.creationDate(Instant.now())
.lastUpdateDate(Instant.now())
.flagActive(true)
.operatorName("Operator Name")
.flagSpontaneous(false)
.filePathName("/path/to/file")
.fileName("example_file.csv")
.pdfGenerated(5)
.codRequestToken("REQ123TOKEN")
.codError("NO_ERROR")
.pspIdentifier("PSP001")
.flowDateTime(LocalDateTime.now())
.state("VALID")
.fileSourceCode("SRC001")
.discardFileName("discarded_file.csv")
.build();
}

// Static builder for IngestionFlowFileDTO
public static IngestionFlowFileDTO buildFakeIngestionFlowFileDTO() {
return IngestionFlowFileDTO.builder()
.ingestionFlowFileId(1L)
.flowFileType(IngestionFlowFileType.PAYMENTS_REPORTING)
.version(1)
.org(OrganizationDTO.builder().orgId(123L).build())
.status("ACTIVE")
.iuf("IUF12345")
.numTotalRows(100L)
.numCorrectlyImportedRows(95L)
.creationDate(new Date())
.lastUpdateDate(new Date())
.flagActive(true)
.operatorName("Operator Name")
.flagSpontaneous(false)
.filePath("/path/to/file")
.fileName("example_file.csv")
.pdfGenerated(5L)
.codRequestToken("REQ123TOKEN")
.codError("NO_ERROR")
.pspIdentifier("PSP001")
.flowDateTime(LocalDateTime.now())
.fileSourceCode("SRC001")
.discardFileName("discarded_file.csv")
.build();
}
}

0 comments on commit e510fd1

Please sign in to comment.