Skip to content

Commit

Permalink
[PRDP-267] added flag for recover failed scheduled function
Browse files Browse the repository at this point in the history
  • Loading branch information
giomella committed Dec 6, 2023
1 parent a719f92 commit db1c77d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,14 +73,16 @@ public void run(
OutputBinding<List<Receipt>> documentdb,
final ExecutionContext context
) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());
List<Receipt> receiptList = new ArrayList<>();
if (isEnabled) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());
List<Receipt> 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<Receipt> recover(ExecutionContext context, ReceiptStatusType statusType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -63,14 +70,16 @@ class RecoverFailedReceiptScheduledTest {
@Spy
private OutputBinding<List<Receipt>> documentdb;

@SystemStub
private EnvironmentVariables environment;

private AutoCloseable closeable;

private RecoverFailedReceiptScheduled sut;

@BeforeEach
public void openMocks() {
closeable = MockitoAnnotations.openMocks(this);
sut = spy(new RecoverFailedReceiptScheduled(bizEventToReceiptServiceMock, bizEventCosmosClientMock, receiptCosmosServiceMock));
}

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

0 comments on commit db1c77d

Please sign in to comment.