Skip to content

Commit

Permalink
Merge pull request #58 from pagopa/fix_zip_blob
Browse files Browse the repository at this point in the history
feat: NOD-642 fix conversion
  • Loading branch information
mscattarella authored Feb 1, 2024
2 parents 6709a97 + 00da2f8 commit 522b863
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public enum AppErrorCodeMessageEnum implements AppErrorCodeMessageInterface {
FDR_HISTORY_SAVE_TABLE_STORAGE_ERROR(
"0728", "fdr.fdrHistorySaveOnTableStorageError", Status.INTERNAL_SERVER_ERROR),
FILE_UTILS_CONVERSION_ERROR("0729", "fdr.fileUtilsConversionError", Status.INTERNAL_SERVER_ERROR),
FILE_UTILS_FILE_NOT_FOUND("0730", "fdr.fileUtilsFileNotFound", Status.INTERNAL_SERVER_ERROR);
FILE_UTILS_FILE_NOT_FOUND("0730", "fdr.fileUtilsFileNotFound", Status.INTERNAL_SERVER_ERROR),
COMPRESS_JSON("0731", "compress.json.error", Status.INTERNAL_SERVER_ERROR);
private final String errorCode;
private final String errorMessageKey;
private final RestResponse.Status httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import it.gov.pagopa.fdr.service.history.model.HistoryBlobBody;
import it.gov.pagopa.fdr.service.history.model.JsonSchemaVersionEnum;
import it.gov.pagopa.fdr.util.FileUtil;
import it.gov.pagopa.fdr.util.StringUtil;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -114,14 +116,15 @@ public HistoryBlobBody saveJsonFile(
fdrHistoryEntity.setPaymentList(fdrHistoryPaymentEntityList);
String fileName =
String.format(
"%s_%s_%s.json",
"%s_%s_%s.json.zip",
fdrEntity.getFdr(), fdrEntity.getSender().getPspId(), fdrEntity.getRevision());

try {
objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String fdrHistoryEntityJson = objMapper.writeValueAsString(fdrHistoryEntity);
isJsonValid(fdrHistoryEntityJson, jsonSchema);
BinaryData jsonFile = BinaryData.fromString(fdrHistoryEntityJson);
String compressedFdrHistoryEntityJson = StringUtil.zip(fdrHistoryEntityJson);
BinaryData jsonFile = BinaryData.fromString(compressedFdrHistoryEntityJson);

uploadBlob(fileName, jsonFile);
return HistoryBlobBody.builder()
Expand All @@ -131,7 +134,7 @@ public HistoryBlobBody saveJsonFile(
.fileLength(jsonFile.getLength())
.jsonSchemaVersion(JsonSchemaVersionEnum.valueOf(jsonSchemaVersion))
.build();
} catch (JsonProcessingException e) {
} catch (IOException e) {
logger.error("Error processing fdrHistoryEntity", e);
throw new AppException(AppErrorCodeMessageEnum.FDR_HISTORY_JSON_PROCESSING_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface PspsServiceServiceMapper {
List<FdrPaymentInsertEntity> toFdrPaymentInsertEntityList(List<PaymentDto> paymentDto);

@Mapping(target = "published", ignore = true)
@Mapping(target = "refJson", ignore = true)
FdrPublishEntity toFdrPublishEntity(FdrInsertEntity fdrInsertEntity);

List<FdrPaymentPublishEntity> toFdrPaymentPublishEntityList(
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/it/gov/pagopa/fdr/service/re/ReService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import it.gov.pagopa.fdr.service.re.model.ReAbstract;
import it.gov.pagopa.fdr.service.re.model.ReInterface;
import it.gov.pagopa.fdr.util.AppConstant;
import it.gov.pagopa.fdr.util.StringUtil;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -110,11 +112,19 @@ public <T extends ReAbstract> void writeBlobIfExist(T re) {
if (bodyStr != null && !bodyStr.isBlank()) {
String fileName =
String.format(
"%s_%s_%s", re.getSessionId(), re.getFdrAction(), reInterface.getHttpType().name());

"%s_%s_%s.json.zip",
re.getSessionId(), re.getFdrAction(), reInterface.getHttpType().name());

String compressedBody = null;
try {
compressedBody = StringUtil.zip(bodyStr);
} catch (IOException e) {
log.errorf("Compress json error", e);
throw new AppException(AppErrorCodeMessageEnum.COMPRESS_JSON);
}
BinaryData body =
BinaryData.fromStream(
new ByteArrayInputStream(bodyStr.getBytes(StandardCharsets.UTF_8)));
new ByteArrayInputStream(compressedBody.getBytes(StandardCharsets.UTF_8)));
BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
blobClient.upload(body);

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/it/gov/pagopa/fdr/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.gov.pagopa.fdr.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;

public class StringUtil {

public static String zip(String str) throws IOException {
byte[] strBytes = str.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream bais = new ByteArrayOutputStream(strBytes.length);
GZIPOutputStream gzipOut = new GZIPOutputStream(bais);
gzipOut.write(strBytes);
gzipOut.close();
String compressed = bais.toString(StandardCharsets.UTF_8);
bais.close();
return compressed;
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ fdr.name-psp.wrongFormat=Fdr [{0}] has wrong psp

eHub.re.parse=EventHub RE: Error when write object to json
eHub.re.tooLarge=EventHub RE: Event is too large for an empty batch. Max size: [{0}]
compress.json.error=Json compression error
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class InternalOrganizationResourceTest {

private static final String GET_ALL_PUBLISHED_FLOW_URL =
"/internal/organizations/ndp/fdrs?" + AppConstant.PSP + "=%s";
"/internal/organizations/%s/fdrs?" + AppConstant.PSP + "=%s";
private static final String GET_REPORTING_FLOW_URL =
"/internal/organizations/%s/fdrs/%s/revisions/%s/psps/%s";
private static final String GET_REPORTING_FLOW_PAYMENTS_URL =
Expand All @@ -48,7 +48,7 @@ class InternalOrganizationResourceTest {
void testOrganization_getAllPublishedFlow_Ok() {
String flowName = TestUtil.getDynamicFlowName();
TestUtil.pspSunnyDay(flowName);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(PSP_CODE);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(EC_CODE, PSP_CODE);
GetAllResponse res =
given()
.header(HEADER)
Expand All @@ -71,7 +71,7 @@ void testOrganization_getAllPublishedFlow_Ok() {
void testOrganization_getAllPublishedFlow_OkNoResults() {
String flowName = TestUtil.getDynamicFlowName();
TestUtil.pspSunnyDay(flowName);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(PSP_CODE_2);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(EC_CODE, PSP_CODE_2);
GetAllResponse res =
given()
.header(HEADER)
Expand All @@ -88,7 +88,7 @@ void testOrganization_getAllPublishedFlow_OkNoResults() {
@DisplayName("ORGANIZATIONS - KO FDR-0708 - psp unknown")
void testOrganization_getAllPublishedFlow_KO_FDR0708() {
String pspUnknown = "PSP_UNKNOWN";
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(pspUnknown);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(EC_CODE, pspUnknown);
ErrorResponse res =
given()
.header(HEADER)
Expand All @@ -110,7 +110,7 @@ void testOrganization_getAllPublishedFlow_KO_FDR0708() {
@Test
@DisplayName("ORGANIZATIONS - KO FDR-0709 - psp not enabled")
void testOrganization_getAllPublishedFlow_KO_FDR0709() {
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(PSP_CODE_NOT_ENABLED);
String url = GET_ALL_PUBLISHED_FLOW_URL.formatted(EC_CODE, PSP_CODE_NOT_ENABLED);

ErrorResponse res =
given()
Expand Down Expand Up @@ -181,7 +181,7 @@ void testOrganization_getReportingFlow_KO_FDR0701() {
TestUtil.pspSunnyDay(flowName);

String flowNameWrong = TestUtil.getDynamicFlowName();
String url = GET_REPORTING_FLOW_URL.formatted(EC_CODE_NDP, flowNameWrong, 1, PSP_CODE);
String url = GET_REPORTING_FLOW_URL.formatted(EC_CODE, flowNameWrong, 1, PSP_CODE);

ErrorResponse res =
given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class AppConstantTestHelper {
public static final String CHANNEL_CODE_NOT_ENABLED = "CANALE_NOT_ENABLED";
public static final String EC_CODE = "15376371009";
public static final String EC_CODE_NOT_ENABLED = "PAtestDOFF";
public static final String EC_CODE_NDP = "ndp";

public static final Header HEADER = new Header("Content-Type", "application/json");
}

0 comments on commit 522b863

Please sign in to comment.