diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduled.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduled.java index 0037c48..aa8d297 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduled.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduled.java @@ -32,6 +32,8 @@ public class RecoverFailedReceiptScheduled { private final Logger logger = LoggerFactory.getLogger(RecoverFailedReceiptScheduled.class); + private final boolean isEnabled = Boolean.parseBoolean(System.getenv().getOrDefault("FAILED_AUTORECOVER_ENABLED", "true")); + private final BizEventToReceiptService bizEventToReceiptService; private final BizEventCosmosClient bizEventCosmosClient; private final ReceiptCosmosService receiptCosmosService; @@ -71,14 +73,16 @@ public void run( OutputBinding> documentdb, final ExecutionContext context ) { - logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now()); - List receiptList = new ArrayList<>(); + if (isEnabled) { + logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now()); + List receiptList = new ArrayList<>(); - receiptList.addAll(recover(context, ReceiptStatusType.INSERTED)); - receiptList.addAll(recover(context, ReceiptStatusType.FAILED)); - receiptList.addAll(recover(context, ReceiptStatusType.NOT_QUEUE_SENT)); + receiptList.addAll(recover(context, ReceiptStatusType.INSERTED)); + receiptList.addAll(recover(context, ReceiptStatusType.FAILED)); + receiptList.addAll(recover(context, ReceiptStatusType.NOT_QUEUE_SENT)); - documentdb.setValue(receiptList); + documentdb.setValue(receiptList); + } } private List recover(ExecutionContext context, ReceiptStatusType statusType) { diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java index e9a2522..3921b69 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java @@ -21,11 +21,15 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.Spy; +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; +import uk.org.webcompere.systemstubs.jupiter.SystemStub; +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; import java.time.LocalDateTime; import java.util.Collections; @@ -35,11 +39,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +@ExtendWith(SystemStubsExtension.class) class RecoverFailedReceiptScheduledTest { private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode"; @@ -63,6 +70,9 @@ class RecoverFailedReceiptScheduledTest { @Spy private OutputBinding> documentdb; + @SystemStub + private EnvironmentVariables environment; + private AutoCloseable closeable; private RecoverFailedReceiptScheduled sut; @@ -70,7 +80,6 @@ class RecoverFailedReceiptScheduledTest { @BeforeEach public void openMocks() { closeable = MockitoAnnotations.openMocks(this); - sut = spy(new RecoverFailedReceiptScheduled(bizEventToReceiptServiceMock, bizEventCosmosClientMock, receiptCosmosServiceMock)); } @AfterEach @@ -80,20 +89,21 @@ public void releaseMocks() throws Exception { @Test void recoverFailedReceiptScheduledSuccess() throws BizEventNotFoundException { + sut = spy(new RecoverFailedReceiptScheduled(bizEventToReceiptServiceMock, bizEventCosmosClientMock, receiptCosmosServiceMock)); when(receiptCosmosServiceMock.getFailedReceiptByStatus(any(), any(), eq(ReceiptStatusType.FAILED))) .thenReturn(Collections.singletonList(ModelBridgeInternal .createFeedResponse(Collections.singletonList( - createFailedReceipt(EVENT_ID_1, ReceiptStatusType.FAILED)), + createFailedReceipt(EVENT_ID_1, ReceiptStatusType.FAILED)), Collections.emptyMap()))); when(receiptCosmosServiceMock.getFailedReceiptByStatus(any(), any(), eq(ReceiptStatusType.INSERTED))) .thenReturn(Collections.singletonList(ModelBridgeInternal .createFeedResponse(Collections.singletonList( - createFailedReceipt(EVENT_ID_2, ReceiptStatusType.INSERTED)), + createFailedReceipt(EVENT_ID_2, ReceiptStatusType.INSERTED)), Collections.emptyMap()))); when(receiptCosmosServiceMock.getFailedReceiptByStatus(any(), any(), eq(ReceiptStatusType.NOT_QUEUE_SENT))) .thenReturn(Collections.singletonList(ModelBridgeInternal .createFeedResponse(Collections.singletonList( - createFailedReceipt(EVENT_ID_3, ReceiptStatusType.NOT_QUEUE_SENT)), + createFailedReceipt(EVENT_ID_3, ReceiptStatusType.NOT_QUEUE_SENT)), Collections.emptyMap()))); when(bizEventCosmosClientMock.getBizEventDocument(EVENT_ID_1)) @@ -131,7 +141,23 @@ void recoverFailedReceiptScheduledSuccess() throws BizEventNotFoundException { assertEquals(1, captured.getEventData().getCart().size()); } - private BizEvent generateValidBizEvent(String eventId){ + @Test + void recoverFailedReceiptScheduledDisabled() throws BizEventNotFoundException { + environment.set("FAILED_AUTORECOVER_ENABLED", "false"); + sut = spy(new RecoverFailedReceiptScheduled(bizEventToReceiptServiceMock, bizEventCosmosClientMock, receiptCosmosServiceMock)); + + assertEquals("false", System.getenv("FAILED_AUTORECOVER_ENABLED")); + + // test execution + assertDoesNotThrow(() -> sut.run("info", documentdb, contextMock)); + + verify(documentdb, never()).setValue(any()); + verify(receiptCosmosServiceMock, never()).getFailedReceiptByStatus(any(), any(), any()); + verify(bizEventCosmosClientMock, never()).getBizEventDocument(anyString()); + + } + + private BizEvent generateValidBizEvent(String eventId) { BizEvent item = new BizEvent(); Payer payer = new Payer();