From 200ceeb0b2873fc58dfefaf18a776c27b4556b5a Mon Sep 17 00:00:00 2001 From: AngeloCaporaso Date: Fri, 29 Mar 2024 12:57:14 +0100 Subject: [PATCH] [PAGOPA] fix --- .../pagopa/canoneunico/functions/CuCsvParsing.java | 12 ++++++++---- .../gov/pagopa/canoneunico/service/CuCsvService.java | 5 +++-- .../canoneunico/functions/CuCsvParsingTest.java | 4 ++-- .../pagopa/canoneunico/service/CuCsvServiceTest.java | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/it/gov/pagopa/canoneunico/functions/CuCsvParsing.java b/src/main/java/it/gov/pagopa/canoneunico/functions/CuCsvParsing.java index 98c652e..f779206 100644 --- a/src/main/java/it/gov/pagopa/canoneunico/functions/CuCsvParsing.java +++ b/src/main/java/it/gov/pagopa/canoneunico/functions/CuCsvParsing.java @@ -48,19 +48,21 @@ public void run( Logger logger = context.getLogger(); LocalDateTime start = LocalDateTime.now(); + CuCsvService csvService = this.getCuCsvServiceInstance(logger); + BlobInfo blobInfo = null; try { - BlobInfo blobInfo = getDataFromEvent(context, events); + blobInfo = getDataFromEvent(context, events); + BlobInfo finalBlobInfo = blobInfo; logger.log(Level.INFO, () -> String.format("[CuCsvParsingFunction START] execution started at [%s] - fileName [%s]", - start, blobInfo.getName())); + start, finalBlobInfo.getName())); // get byte content and convert to String type BinaryData content = getContent(context, blobInfo); String converted = new String(content.toBytes(), StandardCharsets.UTF_8); // initialize csvService and info from ecConfig - CuCsvService csvService = this.getCuCsvServiceInstance(logger); csvService.initEcConfigList(); DebtPositionValidationCsv csvValidation = validateCsv(blobInfo.getName(), logger, csvService, converted); @@ -78,6 +80,8 @@ public void run( } catch (Exception e) { logger.log(Level.SEVERE, () -> String.format( LOG_VALIDATION_PREFIX + "[CuCsvParsingFunction ERROR] [%s] Generic Error: error msg = %s - cause = %s", context.getInvocationId(), e.getMessage(), e.getCause())); + csvService.uploadErrorCsv(blobInfo.getContainer(), ERROR_DIRECTORY_NAME + '/' + blobInfo.getName(), "Generic Error"); + csvService.deleteCsv(blobInfo.getContainer(), blobInfo.getDirectory() + '/' + blobInfo.getName()); } } @@ -158,7 +162,7 @@ private void handleInvalidFile(BlobInfo blobInfo, Logger logger, LocalDateTime s // Upload file in error blob storage long startTime2 = System.currentTimeMillis(); - csvService.uploadCsv(blobInfo.getContainer(), ERROR_DIRECTORY_NAME + '/' + filename, errorCSV); + csvService.uploadErrorCsv(blobInfo.getContainer(), ERROR_DIRECTORY_NAME + '/' + filename, errorCSV); long endTime2 = System.currentTimeMillis(); logger.log(Level.INFO, () -> String.format("[CuCsvParsingFunction] [%s] uploadCsv executed in [%s] ms", filename, (endTime2 - startTime2))); diff --git a/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java b/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java index 0928135..565b130 100644 --- a/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java +++ b/src/main/java/it/gov/pagopa/canoneunico/service/CuCsvService.java @@ -39,6 +39,7 @@ import it.gov.pagopa.canoneunico.util.AzuriteStorageUtil; import it.gov.pagopa.canoneunico.util.ObjectMapperUtils; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.math.NumberUtils; import java.io.ByteArrayInputStream; @@ -143,13 +144,13 @@ public CsvToBean parseCsvToBean(String content) { .build(); } - public void uploadCsv(String containerName, String filePath, String content) { + public void uploadErrorCsv(String containerName, String filePath, String content) { AzuriteStorageUtil azuriteStorageUtil = new AzuriteStorageUtil(); azuriteStorageUtil.createBlob(containerName); BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() .connectionString(this.storageConnectionString).buildClient(); BlobContainerClient cont = blobServiceClient.getBlobContainerClient(containerName); - BlockBlobClient blockBlobClient = cont.getBlobClient(filePath).getBlockBlobClient(); + BlockBlobClient blockBlobClient = cont.getBlobClient(filePath + RandomStringUtils.random(7)).getBlockBlobClient(); InputStream stream = new ByteArrayInputStream(content.getBytes()); blockBlobClient.upload(stream, content.getBytes().length); } diff --git a/src/test/java/it/gov/pagopa/canoneunico/functions/CuCsvParsingTest.java b/src/test/java/it/gov/pagopa/canoneunico/functions/CuCsvParsingTest.java index 488c57e..b5bc571 100644 --- a/src/test/java/it/gov/pagopa/canoneunico/functions/CuCsvParsingTest.java +++ b/src/test/java/it/gov/pagopa/canoneunico/functions/CuCsvParsingTest.java @@ -233,7 +233,7 @@ void checkParseFileKOTest() throws InvalidKeyException, StorageException, URISyn verify(context, times(1)).getLogger(); verify(cuCsvService, times(1)).initEcConfigList(); verify(cuCsvService, times(1)).parseCsvToBean(data); - verify(cuCsvService, times(1)).uploadCsv(any(), any(), any()); + verify(cuCsvService, times(1)).uploadErrorCsv(any(), any(), any()); verify(cuCsvService, times(1)).deleteCsv(any(), any()); } @@ -274,7 +274,7 @@ void checkParseFileKOTest_noRecordInECConfig() throws InvalidKeyException, Stora verify(context, times(1)).getLogger(); verify(cuCsvService, times(1)).initEcConfigList(); verify(cuCsvService, times(1)).parseCsvToBean(data); - verify(cuCsvService, times(1)).uploadCsv(any(), any(), any()); + verify(cuCsvService, times(1)).uploadErrorCsv(any(), any(), any()); verify(cuCsvService, times(1)).deleteCsv(any(), any()); } diff --git a/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java b/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java index 18263cc..6f18d43 100644 --- a/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java +++ b/src/test/java/it/gov/pagopa/canoneunico/service/CuCsvServiceTest.java @@ -382,7 +382,7 @@ void uploadCsv() { if (!container.exists()) { blobServiceClient.createBlobContainer(corporateContainer); } - csvService.uploadCsv(corporateContainer, "fileName.txt", "test content string"); + csvService.uploadErrorCsv(corporateContainer, "fileName.txt", "test content string"); // se arrivo a questa chiamata l'upload รจ andato a buon fine assertTrue(true);