Skip to content

Commit

Permalink
fix: helm values and merged tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed May 30, 2024
1 parent 69954fe commit a06d4af
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 85 deletions.
1 change: 1 addition & 0 deletions helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ microservice-chart:
OTEL_TRACES_SAMPLER: "always_on"
NOTICE_REQUEST_MONGO_DB_NAME: "noticesMongoDb"
NOTICE_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request"
NOTICE_ERR_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request_error"
BLOB_STORAGE_CONTAINER_NAME: "notices"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopadprintitnotices.blob.core.windows.net"
envSecret:
Expand Down
1 change: 1 addition & 0 deletions helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ microservice-chart:
OTEL_TRACES_SAMPLER: "always_on"
NOTICE_REQUEST_MONGO_DB_NAME: "noticesMongoDb"
NOTICE_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request"
NOTICE_ERR_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request_error"
BLOB_STORAGE_CONTAINER_NAME: "notices"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopapprintitnotices.blob.core.windows.net"
envSecret:
Expand Down
1 change: 1 addition & 0 deletions helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ microservice-chart:
OTEL_TRACES_SAMPLER: "always_on"
NOTICE_REQUEST_MONGO_DB_NAME: "noticesMongoDb"
NOTICE_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request"
NOTICE_ERR_REQUEST_MONGO_COLLECTION_NAME: "payment_notice_generation_request_error"
BLOB_STORAGE_CONTAINER_NAME: "notices"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopauprintitnotices.blob.core.windows.net"
envSecret:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ManageNoticeErrors {

private final Integer maxRetriesOnErrors;

private String ERROR_STRING = "[{}] error recovering error data with id {}";

public ManageNoticeErrors() {
this.noticeFolderService = new NoticeFolderServiceImpl();
this.maxRetriesOnErrors = Integer.parseInt(System.getenv("MAX_RETRIES_ON_ERRORS"));
Expand Down Expand Up @@ -92,7 +94,7 @@ public void processNoticeErrors(
new PaymentNoticeManagementException("Request error not found",
HttpStatus.INTERNAL_SERVER_ERROR.value()));
} catch (Exception e) {
logger.error("[{}] error recovering error data with id {}",
logger.error(ERROR_STRING,
context.getFunctionName(), error.getFolderId(), e);
}

Expand All @@ -101,57 +103,66 @@ public void processNoticeErrors(

if (error.isCompressionError() &&
!"UNKNOWN".equals(paymentNoticeGenerationRequestError.getFolderId())) {
try {
PaymentNoticeGenerationRequest paymentNoticeGenerationRequest =
noticeFolderService.findRequest(error.getId());
if (paymentNoticeGenerationRequest.getStatus().equals(
PaymentGenerationRequestStatus.COMPLETING)) {
completionToRetry.add(
it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest
.builder()
.id(paymentNoticeGenerationRequest.getId())
.numberOfElementsTotal(paymentNoticeGenerationRequest
.getNumberOfElementsTotal())
.numberOfElementsFailed(paymentNoticeGenerationRequest
.getNumberOfElementsFailed())
.status(paymentNoticeGenerationRequest.getStatus())
.userId(paymentNoticeGenerationRequest.getUserId())
.items(paymentNoticeGenerationRequest.getItems())
.build()
);
}
} catch (RequestRecoveryException e) {
logger.error("[{}] error recovering notice request with id {}",
context.getFunctionName(), paymentNoticeGenerationRequestError.getId(), e);
}
addRequestsToRetry(completionToRetry, context, error, paymentNoticeGenerationRequestError);
} else {

try {
String plainRequestData = Aes256Utils.decrypt(error.getData());
NoticeRequestEH noticeRequestEH =
ObjectMapperUtils.mapString(plainRequestData, NoticeRequestEH.class);
noticeRequestEH.setErrorId(error.getId());
noticesToRetry.add(noticeRequestEH);
} catch (Aes256Exception | JsonProcessingException e) {
logger.error("[{}] error recovering error data with id {}",
context.getFunctionName(), paymentNoticeGenerationRequestError.getId(), e);
throw new RuntimeException(e);
}

addToNoticesToRetry(noticesToRetry, context, error, paymentNoticeGenerationRequestError);
}

try {
paymentNoticeGenerationRequestErrorClient.updatePaymentGenerationRequestError(
paymentNoticeGenerationRequestError);
} catch (Exception e) {
logger.error("[{}] error recovering error data with id {}",
context.getFunctionName(), paymentNoticeGenerationRequestError.getId(), e);
}
updateError(context, paymentNoticeGenerationRequestError);

}

});

}

private void updateError(ExecutionContext context, PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError) {
try {
paymentNoticeGenerationRequestErrorClient.updatePaymentGenerationRequestError(
paymentNoticeGenerationRequestError);
} catch (Exception e) {
logger.error(ERROR_STRING,
context.getFunctionName(), paymentNoticeGenerationRequestError.getId(), e);
}
}

private void addRequestsToRetry(List<it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest> completionToRetry, ExecutionContext context, it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequestError error, PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError) {
try {
PaymentNoticeGenerationRequest paymentNoticeGenerationRequest =
noticeFolderService.findRequest(error.getId());
if (paymentNoticeGenerationRequest.getStatus().equals(
PaymentGenerationRequestStatus.COMPLETING)) {
completionToRetry.add(
it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest
.builder()
.id(paymentNoticeGenerationRequest.getId())
.numberOfElementsTotal(paymentNoticeGenerationRequest
.getNumberOfElementsTotal())
.numberOfElementsFailed(paymentNoticeGenerationRequest
.getNumberOfElementsFailed())
.status(paymentNoticeGenerationRequest.getStatus())
.userId(paymentNoticeGenerationRequest.getUserId())
.items(paymentNoticeGenerationRequest.getItems())
.build()
);
}
} catch (RequestRecoveryException e) {
logger.error(ERROR_STRING,
context.getFunctionName(), paymentNoticeGenerationRequestError.getId(), e);
}
}

private void addToNoticesToRetry(List<NoticeRequestEH> noticesToRetry, ExecutionContext context, it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequestError error, PaymentNoticeGenerationRequestError paymentNoticeGenerationRequestError) {
try {
String plainRequestData = Aes256Utils.decrypt(error.getData());
NoticeRequestEH noticeRequestEH =
ObjectMapperUtils.mapString(plainRequestData, NoticeRequestEH.class);
noticeRequestEH.setErrorId(error.getId());
noticesToRetry.add(noticeRequestEH);
} catch (Aes256Exception | JsonProcessingException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class PaymentNoticeGenerationRequestErrorClientImpl implements PaymentNot
private final MongoCollection<PaymentNoticeGenerationRequestError> mongoCollection;

private PaymentNoticeGenerationRequestErrorClientImpl() {
String connectionString = System.getenv("NOTICE_ERR_REQUEST_MONGODB_CONN_STRING");
String databaseName = System.getenv("NOTICE_ERR_REQUEST_MONGO_DB_NAME");
String connectionString = System.getenv("NOTICE_REQUEST_MONGODB_CONN_STRING");
String databaseName = System.getenv("NOTICE_REQUEST_MONGO_DB_NAME");
String collectionName = System.getenv("NOTICE_ERR_REQUEST_MONGO_COLLECTION_NAME");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public PaymentNoticeGenerationRequest findRequest(String id) throws RequestRecov
return paymentNoticeGenerationRequestClient.findById(id).orElseThrow(
() -> new RuntimeException("Error on folder recovery"));
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RequestRecoveryException(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -132,6 +131,64 @@ void shouldSendNoticeOnValidData() throws Exception {
}


@Test
void shouldntSendRequestOnMissingData() throws SaveNoticeToBlobException, RequestRecoveryException {
List<it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest>
paymentNoticeGenerationRequestList = new ArrayList<>();
List<it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequestError>
paymentNoticeGenerationRequestErrors =
Collections.singletonList(it.gov.pagopa.print.payment.notice.functions.model
.PaymentNoticeGenerationRequestError.builder().numberOfAttempts(0)
.compressionError(true)
.id("test")
.folderId("test")
.build());
doReturn(Optional.ofNullable(null))
.when(paymentNoticeGenerationRequestErrorClient)
.findOne(any());
List<NoticeRequestEH> eventsToRetry = new ArrayList<>();
assertDoesNotThrow(() -> sut.processNoticeErrors(
paymentNoticeGenerationRequestErrors,
eventsToRetry,
paymentNoticeGenerationRequestList,
executionContextMock));
assertEquals(0, eventsToRetry.size());
assertEquals(0, paymentNoticeGenerationRequestList.size());
}

@Test
void shouldntSendRequestOnException() throws SaveNoticeToBlobException, RequestRecoveryException {
List<it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequest>
paymentNoticeGenerationRequestList = new ArrayList<>();
List<it.gov.pagopa.print.payment.notice.functions.model.PaymentNoticeGenerationRequestError>
paymentNoticeGenerationRequestErrors =
Collections.singletonList(it.gov.pagopa.print.payment.notice.functions.model
.PaymentNoticeGenerationRequestError.builder().numberOfAttempts(0)
.compressionError(true)
.id("test")
.folderId("test")
.build());
doReturn(Optional.of(PaymentNoticeGenerationRequestError.builder().compressionError(true)
.folderId("test")
.id("test")
.numberOfAttempts(0)
.compressionError(true)
.build()))
.when(paymentNoticeGenerationRequestErrorClient)
.findOne(any());
doAnswer(item -> {throw new RequestRecoveryException("test",500);}).when(noticeFolderService).findRequest(any());
List<NoticeRequestEH> eventsToRetry = new ArrayList<>();
assertDoesNotThrow(() -> sut.processNoticeErrors(
paymentNoticeGenerationRequestErrors,
eventsToRetry,
paymentNoticeGenerationRequestList,
executionContextMock));
verify(noticeFolderService).findRequest(any());
assertEquals(0, eventsToRetry.size());
assertEquals(0, paymentNoticeGenerationRequestList.size());
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void testSingleton() throws Exception {
withEnvironmentVariables(
"NOTICE_REQUEST_MONGO_DB_NAME", "personDB",
"NOTICE_REQUEST_MONGODB_CONN_STRING", "mongodb://localhost:27017/personDB",
"NOTICE_REQUEST_MONGO_COLLECTION_NAME", "notice"
).execute(() -> Assertions.assertDoesNotThrow(PaymentNoticeGenerationRequestClientImpl::getInstance));
"NOTICE_ERR_REQUEST_MONGO_COLLECTION_NAME", "notice-errors"
).execute(() -> Assertions.assertDoesNotThrow(PaymentNoticeGenerationRequestErrorClientImpl::getInstance));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.gov.pagopa.print.payment.notice.functions.client.PaymentNoticeGenerationRequestClient;
import it.gov.pagopa.print.payment.notice.functions.client.PaymentNoticeGenerationRequestErrorClient;
import it.gov.pagopa.print.payment.notice.functions.entity.PaymentNoticeGenerationRequest;
import it.gov.pagopa.print.payment.notice.functions.exception.RequestRecoveryException;
import it.gov.pagopa.print.payment.notice.functions.exception.SaveNoticeToBlobException;
import it.gov.pagopa.print.payment.notice.functions.model.response.BlobStorageResponse;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -57,6 +58,17 @@ void manageShouldCompleteWithSuccess() {
verify(paymentNoticeGenerationRequestClient).updatePaymentGenerationRequest(any());
}

@Test
void manageShouldCompleteWithSuccessWithPartialFailures() {
BlobStorageResponse blobStorageResponse = new BlobStorageResponse();
blobStorageResponse.setStatusCode(200);
when(paymentNoticeBlobClient.compressFolder(any())).thenReturn(blobStorageResponse);
assertDoesNotThrow(() -> noticeFolderService.manageFolder(PaymentNoticeGenerationRequest.builder()
.numberOfElementsFailed(1).build()));
verify(paymentNoticeBlobClient).compressFolder(any());
verify(paymentNoticeGenerationRequestClient).updatePaymentGenerationRequest(any());
}

@Test
void manageShouldThrowExceptionOnBlobNotSuccessful() {
BlobStorageResponse blobStorageResponse = new BlobStorageResponse();
Expand Down Expand Up @@ -88,4 +100,12 @@ void findRequestShouldReturnData() {
assertNotNull(assertDoesNotThrow(() -> noticeFolderService.findRequest(any())));
}

@Test
void findRequestShouldThrowException() {
doReturn(Optional.ofNullable(null))
.when(paymentNoticeGenerationRequestClient).findById(any());
assertThrows(RequestRecoveryException.class, () -> noticeFolderService.findRequest(any()));
}


}

0 comments on commit a06d4af

Please sign in to comment.