diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractService.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractService.java index 9878b39ed..5ecaaaa40 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractService.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractService.java @@ -3,11 +3,10 @@ import it.pagopa.selfcare.onboarding.entity.Onboarding; import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment; import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow; -import org.openapi.quarkus.user_registry_json.model.UserResource; - import java.io.File; import java.util.List; import java.util.Optional; +import org.openapi.quarkus.user_registry_json.model.UserResource; public interface ContractService { File createContractPDF( @@ -19,7 +18,7 @@ File createContractPDF( String pdfFormatFilename); File createAttachmentPDF( - String templatePath, Onboarding onboarding, String productName, String pdfFormatFilename); + String templatePath, Onboarding onboarding, String productName, String pdfFormatFilename, UserResource userResource); File loadContractPDF(String contractTemplatePath, String onboardingId, String productName); diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefault.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefault.java index 0177311b5..2a6719bef 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefault.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefault.java @@ -1,5 +1,26 @@ package it.pagopa.selfcare.onboarding.service; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_INTEROP; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_IO; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_IO_PREMIUM; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_IO_SIGN; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PAGOPA; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PN; +import static it.pagopa.selfcare.onboarding.utils.GenericError.CREATE_AGGREGATES_CSV_ERROR; +import static it.pagopa.selfcare.onboarding.utils.GenericError.GENERIC_ERROR; +import static it.pagopa.selfcare.onboarding.utils.GenericError.LOAD_AGGREGATES_CSV_ERROR; +import static it.pagopa.selfcare.onboarding.utils.GenericError.UNABLE_TO_DOWNLOAD_FILE; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setECData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setUpAttachmentData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setUpCommonData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupPRVData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupPSPData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupProdIOData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupProdIODataAggregates; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupProdPNData; +import static it.pagopa.selfcare.onboarding.utils.PdfMapper.setupSAProdInteropData; +import static it.pagopa.selfcare.onboarding.utils.Utils.CONTRACT_FILENAME_FUNC; + import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; import com.openhtmltopdf.svgsupport.BatikSVGDrawer; import it.pagopa.selfcare.azurestorage.AzureBlobClient; @@ -9,20 +30,14 @@ import it.pagopa.selfcare.onboarding.config.PagoPaSignatureConfig; import it.pagopa.selfcare.onboarding.crypto.PadesSignService; import it.pagopa.selfcare.onboarding.crypto.entity.SignatureInformation; -import it.pagopa.selfcare.onboarding.entity.*; +import it.pagopa.selfcare.onboarding.entity.AggregateInstitution; +import it.pagopa.selfcare.onboarding.entity.Institution; +import it.pagopa.selfcare.onboarding.entity.Onboarding; +import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow; import it.pagopa.selfcare.onboarding.exception.GenericOnboardingException; import it.pagopa.selfcare.onboarding.utils.ClassPathStream; import jakarta.enterprise.context.ApplicationScoped; -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVPrinter; -import org.apache.commons.text.StringSubstitutor; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.jsoup.Jsoup; -import org.jsoup.helper.W3CDom; -import org.openapi.quarkus.user_registry_json.model.UserResource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; @@ -33,13 +48,22 @@ import java.nio.file.Path; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; import java.util.function.Function; - -import static it.pagopa.selfcare.onboarding.common.ProductId.*; -import static it.pagopa.selfcare.onboarding.utils.GenericError.*; -import static it.pagopa.selfcare.onboarding.utils.PdfMapper.*; -import static it.pagopa.selfcare.onboarding.utils.Utils.CONTRACT_FILENAME_FUNC; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.text.StringSubstitutor; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.jsoup.Jsoup; +import org.jsoup.helper.W3CDom; +import org.openapi.quarkus.user_registry_json.model.UserResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @ApplicationScoped public class ContractServiceDefault implements ContractService { @@ -199,7 +223,7 @@ public File createAttachmentPDF( String attachmentTemplatePath, Onboarding onboarding, String productName, - String attachmentName) { + String attachmentName, UserResource userResource) { log.info("START - createAttachmentPDF for template: {}", attachmentTemplatePath); @@ -211,7 +235,7 @@ public File createAttachmentPDF( File attachmentPdfFile = "pdf".equals(fileType) ? azureBlobClient.getFileAsPdf(attachmentTemplatePath) - : createPdfFileAttachment(attachmentTemplatePath, onboarding); + : createPdfFileAttachment(attachmentTemplatePath, onboarding, userResource); // Define the filename and path for storage. final String filename = @@ -259,7 +283,7 @@ private File createPdfFileContract( && InstitutionType.PSP == institution.getInstitutionType()) { setupPSPData(data, manager, onboarding); } else if (PROD_PAGOPA.getValue().equalsIgnoreCase(productId) - && InstitutionType.PRV == institution.getInstitutionType()) { + && InstitutionType.PRV == institution.getInstitutionType() || InstitutionType.GPU == institution.getInstitutionType()) { setupPRVData(data, onboarding, baseUrl.toString(), users); } else if (PROD_PAGOPA.getValue().equalsIgnoreCase(productId) && InstitutionType.PSP != institution.getInstitutionType() @@ -280,7 +304,7 @@ private File createPdfFileContract( return temporaryPdfFile.toFile(); } - private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding onboarding) + private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding onboarding, UserResource userResource) throws IOException { final String builder = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) @@ -293,7 +317,7 @@ private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding o // Create a temporary PDF file to store the contract. Path attachmentPdfFile = Files.createTempFile(builder, ".pdf"); // Prepare common data for the contract document. - Map data = setUpAttachmentData(onboarding); + Map data = setUpAttachmentData(onboarding, userResource); log.debug("data Map for PDF: {}", data); fillPDFAsFile(attachmentPdfFile, attachmentTemplateText, data); diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingService.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingService.java index bcf811fc3..81947643c 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingService.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingService.java @@ -1,6 +1,6 @@ package it.pagopa.selfcare.onboarding.service; -import static it.pagopa.selfcare.onboarding.utils.Utils.*; +import static it.pagopa.selfcare.onboarding.utils.Utils.CONTRACT_FILENAME_FUNC; import static it.pagopa.selfcare.onboarding.utils.Utils.NOT_ALLOWED_WORKFLOWS_FOR_INSTITUTION_NOTIFICATIONS; import com.microsoft.azure.functions.ExecutionContext; @@ -14,7 +14,11 @@ import it.pagopa.selfcare.onboarding.config.MailTemplatePlaceholdersConfig; import it.pagopa.selfcare.onboarding.dto.NotificationCountResult; import it.pagopa.selfcare.onboarding.dto.ResendNotificationsFilters; -import it.pagopa.selfcare.onboarding.entity.*; +import it.pagopa.selfcare.onboarding.entity.Onboarding; +import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow; +import it.pagopa.selfcare.onboarding.entity.Token; +import it.pagopa.selfcare.onboarding.entity.User; import it.pagopa.selfcare.onboarding.exception.GenericOnboardingException; import it.pagopa.selfcare.onboarding.repository.OnboardingRepository; import it.pagopa.selfcare.onboarding.repository.TokenRepository; @@ -29,7 +33,11 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; import org.bson.Document; @@ -84,8 +92,6 @@ public Optional getOnboarding(String onboardingId) { public void createContract(OnboardingWorkflow onboardingWorkflow) { Onboarding onboarding = onboardingWorkflow.getOnboarding(); - String validManagerId = getValidManagerId(onboarding.getUsers()); - UserResource manager = userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, validManagerId); List delegates = onboarding.getUsers().stream() @@ -99,19 +105,24 @@ public void createContract(OnboardingWorkflow onboardingWorkflow) { contractService.createContractPDF( onboardingWorkflow.getContractTemplatePath(product), onboarding, - manager, + getUserResource(onboarding), delegates, product.getTitle(), onboardingWorkflow.getPdfFormatFilename()); } + private UserResource getUserResource(Onboarding onboarding) { + String validManagerId = getValidManagerId(onboarding.getUsers()); + return userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, validManagerId); + } + public void createAttachment(OnboardingAttachment onboardingAttachment) { Onboarding onboarding = onboardingAttachment.getOnboarding(); Product product = productService.getProductIsValid(onboarding.getProductId()); AttachmentTemplate attachment = onboardingAttachment.getAttachment(); contractService.createAttachmentPDF( - attachment.getTemplatePath(), onboarding, product.getTitle(), attachment.getName()); + attachment.getTemplatePath(), onboarding, product.getTitle(), attachment.getName(), getUserResource(onboarding)); } public void loadContract(Onboarding onboarding) { diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/PdfMapper.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/PdfMapper.java index 63b40af68..b822e58eb 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/PdfMapper.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/PdfMapper.java @@ -107,11 +107,14 @@ public static Map setUpCommonData( return map; } - public static Map setUpAttachmentData(Onboarding onboarding) { + public static Map setUpAttachmentData(Onboarding onboarding, UserResource userResource) { Map map = new HashMap<>(); + map.put(INSTITUTION_NAME, onboarding.getInstitution().getDescription()); map.put("institutionTaxCode", Optional.ofNullable(onboarding.getInstitution().getTaxCode()).orElse(UNDERSCORE)); map.put("institutionMail", onboarding.getInstitution().getDigitalAddress()); + map.put("managerName", getStringValue(userResource.getName())); + map.put("managerSurname", getStringValue(userResource.getFamilyName())); if (Objects.nonNull(onboarding.getInstitution().getGpuData())) { map.put( "businessRegisterNumber", diff --git a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefaultTest.java b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefaultTest.java index 59ce40c77..88cf9b2d4 100644 --- a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefaultTest.java +++ b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/ContractServiceDefaultTest.java @@ -1,5 +1,14 @@ package it.pagopa.selfcare.onboarding.service; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import io.quarkus.test.InjectMock; import io.quarkus.test.junit.QuarkusTest; import it.pagopa.selfcare.azurestorage.AzureBlobClient; @@ -8,8 +17,23 @@ import it.pagopa.selfcare.onboarding.config.MailTemplatePlaceholdersConfig; import it.pagopa.selfcare.onboarding.config.PagoPaSignatureConfig; import it.pagopa.selfcare.onboarding.crypto.PadesSignService; -import it.pagopa.selfcare.onboarding.entity.*; +import it.pagopa.selfcare.onboarding.entity.AggregateInstitution; +import it.pagopa.selfcare.onboarding.entity.GPUData; +import it.pagopa.selfcare.onboarding.entity.Institution; +import it.pagopa.selfcare.onboarding.entity.Onboarding; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowAggregator; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowInstitution; +import it.pagopa.selfcare.onboarding.entity.PaymentServiceProvider; +import it.pagopa.selfcare.onboarding.entity.User; import jakarta.inject.Inject; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -21,13 +45,6 @@ import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; -import java.io.File; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - @QuarkusTest class ContractServiceDefaultTest { @@ -237,12 +254,13 @@ void createAttachmentPDF() { Onboarding onboarding = createOnboarding(); Mockito.when(azureBlobClient.getFileAsText(contractFilepath)).thenReturn(contractHtml); - Mockito.when(azureBlobClient.uploadFile(any(), any(), any())).thenReturn(contractHtml); + UserResource userResource = createUserResource(); + File attachmentPDF = contractService.createAttachmentPDF( - contractFilepath, onboarding, productNameAccent, pdfFormatFile); + contractFilepath, onboarding, productNameAccent, pdfFormatFile, userResource); assertNotNull(attachmentPDF); @@ -500,4 +518,20 @@ void createContractPRV() { Mockito.verify(azureBlobClient, Mockito.times(1)).uploadFile(any(), any(), any()); Mockito.verifyNoMoreInteractions(azureBlobClient); } + + private UserResource createUserResource() { + UserResource userResource = new UserResource(); + userResource.setId(UUID.randomUUID()); + + CertifiableFieldResourceOfstring resourceOfName = new CertifiableFieldResourceOfstring(); + resourceOfName.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE); + resourceOfName.setValue("name"); + userResource.setName(resourceOfName); + + CertifiableFieldResourceOfstring resourceOfSurname = new CertifiableFieldResourceOfstring(); + resourceOfSurname.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE); + resourceOfSurname.setValue("surname"); + userResource.setFamilyName(resourceOfSurname); + return userResource; + } } diff --git a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceTest.java b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceTest.java index 0f43dba3e..07b08293e 100644 --- a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceTest.java +++ b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceTest.java @@ -2,9 +2,18 @@ import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_FIELD_LIST; import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_WORKS_FIELD_LIST; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; import com.microsoft.azure.functions.ExecutionContext; import eu.europa.esig.dss.enumerations.DigestAlgorithm; @@ -19,7 +28,13 @@ import it.pagopa.selfcare.onboarding.common.WorkflowType; import it.pagopa.selfcare.onboarding.dto.NotificationCountResult; import it.pagopa.selfcare.onboarding.dto.ResendNotificationsFilters; -import it.pagopa.selfcare.onboarding.entity.*; +import it.pagopa.selfcare.onboarding.entity.Institution; +import it.pagopa.selfcare.onboarding.entity.Onboarding; +import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow; +import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowInstitution; +import it.pagopa.selfcare.onboarding.entity.Token; +import it.pagopa.selfcare.onboarding.entity.User; import it.pagopa.selfcare.onboarding.exception.GenericOnboardingException; import it.pagopa.selfcare.onboarding.repository.OnboardingRepository; import it.pagopa.selfcare.onboarding.repository.TokenRepository; @@ -29,7 +44,13 @@ import it.pagopa.selfcare.product.service.ProductService; import jakarta.inject.Inject; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; import java.util.logging.Logger; import org.eclipse.microprofile.rest.client.inject.RestClient; import org.junit.jupiter.api.Test; @@ -38,6 +59,7 @@ import org.openapi.quarkus.user_registry_json.api.UserApi; import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; import org.openapi.quarkus.user_registry_json.model.UserResource; +import org.openapi.quarkus.user_registry_json.model.WorkContactResource; @QuarkusTest class OnboardingServiceTest { @@ -98,6 +120,20 @@ void getOnboarding() { void createContract_shouldThrowIfManagerNotfound() { Onboarding onboarding = createOnboarding(); OnboardingWorkflow onboardingWorkflow = getOnboardingWorkflowInstitution(onboarding); + + UserResource userResource = createUserResource(); + User user = new User(); + user.setId(userResource.getId().toString()); + user.setRole(PartyRole.MANAGER); + + when(userRegistryApi.findByIdUsingGET(USERS_FIELD_LIST, user.getId())) + .thenReturn(userResource); + + Product product = new Product(); + product.setTitle("title"); + + when(productService.getProductIsValid(any())).thenReturn(product); + assertThrows( GenericOnboardingException.class, () -> onboardingService.createContract(onboardingWorkflow)); @@ -194,6 +230,11 @@ void createAttachments() { // Arrange Onboarding onboarding = createOnboarding(); + User user = new User(); + user.setRole(PartyRole.MANAGER); + user.setId("id"); + onboarding.setUsers(List.of(user)); + AttachmentTemplate attachmentTemplate = createDummyAttachmentTemplate(); Product product = createDummyProduct(); OnboardingAttachment onboardingAttachment = new OnboardingAttachment(); @@ -202,21 +243,21 @@ void createAttachments() { when(productService.getProductIsValid(onboarding.getProductId())).thenReturn(product); + UserResource userResource = new UserResource(); + userResource.setId(UUID.randomUUID()); + Map map = new HashMap<>(); + userResource.setWorkContacts(map); + + when(userRegistryApi.findByIdUsingGET(anyString(), anyString())) + .thenReturn(userResource); + // Act onboardingService.createAttachment(onboardingAttachment); // Assert Mockito.verify(productService, Mockito.times(1)).getProductIsValid(onboarding.getProductId()); - - // Capture the path of the template used for the PDF - ArgumentCaptor captorTemplatePath = ArgumentCaptor.forClass(String.class); Mockito.verify(contractService, Mockito.times(1)) - .createAttachmentPDF(captorTemplatePath.capture(), any(), any(), any()); - - // Check that the correct template was used - assertEquals( - "path", // This is the template matching the onboarding filter - captorTemplatePath.getValue()); + .createAttachmentPDF(any(), any(), any(), any(), any()); } private Product createDummyProduct() { @@ -650,4 +691,5 @@ private Token createDummyToken() { token.setId(UUID.randomUUID().toString()); return token; } + }