From 9ed1cd4d11d0140d57f516b158f18005c4dd4f0d Mon Sep 17 00:00:00 2001 From: Giampiero Ferrara Date: Fri, 13 Dec 2024 17:49:29 +0100 Subject: [PATCH] Added managerName and managerSurname --- .../onboarding/service/ContractService.java | 5 +- .../service/ContractServiceDefault.java | 8 +-- .../onboarding/service/OnboardingService.java | 25 ++++++--- .../selfcare/onboarding/utils/PdfMapper.java | 5 +- .../service/ContractServiceDefaultTest.java | 54 +++++++++++++++---- .../service/OnboardingServiceTest.java | 40 ++++++++++++-- 6 files changed, 107 insertions(+), 30 deletions(-) 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 d827833c1..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 @@ -223,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); @@ -235,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 = @@ -304,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")) @@ -317,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..0097a3f3f 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 { @@ -202,6 +224,14 @@ 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); @@ -211,7 +241,7 @@ void createAttachments() { // 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()); + .createAttachmentPDF(captorTemplatePath.capture(), any(), any(), any(), any()); // Check that the correct template was used assertEquals(