Skip to content

Commit

Permalink
[PAGOPA] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-ang committed Mar 29, 2024
1 parent 3bf4e89 commit 200ceeb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,13 +144,13 @@ public CsvToBean<PaymentNotice> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 200ceeb

Please sign in to comment.