Skip to content

Commit

Permalink
MODTLR-112 Test for transaction status update
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrVidinieiev committed Dec 24, 2024
1 parent 6f20bc1 commit 506a578
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/folio/service/DcbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface DcbService {
void createPickupTransaction(EcsTlrEntity ecsTlr, Request request);
void updateTransactionStatuses(TransactionStatus.StatusEnum newStatus, EcsTlrEntity ecsTlr);
TransactionStatusResponse getTransactionStatus(UUID transactionId, String tenantId);
void updateTransactionStatus(UUID transactionId, TransactionStatus.StatusEnum newStatus,
String tenantId);
}
3 changes: 2 additions & 1 deletion src/main/java/org/folio/service/impl/DcbServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public void updateTransactionStatuses(TransactionStatus.StatusEnum newStatus, Ec
ecsTlr.getSecondaryRequestTenantId());
}

private void updateTransactionStatus(UUID transactionId, StatusEnum newStatus, String tenantId) {
@Override
public void updateTransactionStatus(UUID transactionId, StatusEnum newStatus, String tenantId) {
if (transactionId == null) {
log.info("updateTransactionStatus:: transaction ID is null, doing nothing");
return;
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/org/folio/service/impl/LoanEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
@Log4j2
public class LoanEventHandler implements KafkaEventHandler<Loan> {
private static final String LOAN_ACTION_CHECKED_IN = "checkedin";
private static final EnumSet<TransactionStatusResponse.StatusEnum> RELEVANT_TRANSACTION_STATUSES_FOR_CHECK_IN =
EnumSet.of(ITEM_CHECKED_OUT, ITEM_CHECKED_IN, CLOSED);
private static final EnumSet<TransactionStatusResponse.StatusEnum>
RELEVANT_TRANSACTION_STATUSES_FOR_CHECK_IN = EnumSet.of(ITEM_CHECKED_OUT, ITEM_CHECKED_IN, CLOSED);

private final DcbService dcbService;
private final EcsTlrRepository ecsTlrRepository;
Expand Down Expand Up @@ -61,39 +61,36 @@ private void handleUpdateEvent(KafkaEvent<Loan> event) {
}

private void handleCheckInEvent(KafkaEvent<Loan> event) {
updateEcsTlrForLoan(event.getData().getNewVersion(), event.getTenant());
updateEcsTlr(event.getData().getNewVersion(), event.getTenant());
}

private void updateEcsTlrForLoan(Loan loan, String tenantId) {
private void updateEcsTlr(Loan loan, String tenantId) {
Collection<EcsTlrEntity> ecsTlrs = findEcsTlrs(loan);
for (EcsTlrEntity ecsTlr : ecsTlrs) {
log.info("updateEcsTlrForLoan:: checking ECS TLR {}", ecsTlr::getId);

log.info("updateEcsTlr:: checking ECS TLR {}", ecsTlr::getId);
String primaryTenantId = ecsTlr.getPrimaryRequestTenantId();
String secondaryTenantId = ecsTlr.getSecondaryRequestTenantId();
String intermediateTenantId = ecsTlr.getIntermediateRequestTenantId();

UUID primaryTransactionId = ecsTlr.getPrimaryRequestDcbTransactionId();
UUID secondaryTransactionId = ecsTlr.getSecondaryRequestDcbTransactionId();
UUID intermediateTransactionId = ecsTlr.getIntermediateRequestDcbTransactionId();

if (primaryTransactionId == null || secondaryTransactionId == null) {
log.info("updateEcsTlrForLoan:: ECS TLR does not have primary and/or secondary transaction, skipping");
log.info("updateEcsTlr:: ECS TLR does not have primary/secondary transaction, skipping");
continue;
}

boolean eventTenantIdIsPrimaryTenantId = tenantId.equals(primaryTenantId);
boolean eventTenantIdIsSecondaryTenantId = tenantId.equals(secondaryTenantId);
if (!(eventTenantIdIsPrimaryTenantId || eventTenantIdIsSecondaryTenantId)) {
log.info("updateEcsTlrForLoan:: event tenant ID does not match ECS TLR's primary/secondary request tenant ID, skipping");
log.info("updateEcsTlr:: event tenant ID does not match ECS TLR's primary/secondary request " +
"tenant ID, skipping");
continue;
}

TransactionStatusResponse primaryTransaction = dcbService.getTransactionStatus(
primaryTransactionId, primaryTenantId);
TransactionStatusResponse.StatusEnum primaryTransactionStatus = primaryTransaction.getStatus();
RoleEnum primaryTransactionRole = primaryTransaction.getRole();
log.info("updateEcsTlrForLoan:: primary request transaction: status={}, role={}",
log.info("updateEcsTlr:: primary request transaction: status={}, role={}",
primaryTransactionStatus, primaryTransactionRole);
if (!RELEVANT_TRANSACTION_STATUSES_FOR_CHECK_IN.contains(primaryTransactionStatus)) {
log.info("updateEcsTlrForLoan:: irrelevant primary request transaction status: {}",
Expand All @@ -105,10 +102,10 @@ private void updateEcsTlrForLoan(Loan loan, String tenantId) {
secondaryTransactionId, secondaryTenantId);
TransactionStatusResponse.StatusEnum secondaryTransactionStatus = secondaryTransaction.getStatus();
RoleEnum secondaryTransactionRole = secondaryTransaction.getRole();
log.info("updateEcsTlrForLoan:: secondary request transaction: status={}, role={}",
log.info("updateEcsTlr:: secondary request transaction: status={}, role={}",
secondaryTransactionStatus, secondaryTransactionRole);
if (!RELEVANT_TRANSACTION_STATUSES_FOR_CHECK_IN.contains(secondaryTransactionStatus)) {
log.info("updateEcsTlrForLoan:: irrelevant secondary request transaction status: {}",
log.info("updateEcsTlr:: irrelevant secondary request transaction status: {}",
secondaryTransactionStatus);
continue;
}
Expand All @@ -118,7 +115,8 @@ private void updateEcsTlrForLoan(Loan loan, String tenantId) {
(primaryTransactionStatus == ITEM_CHECKED_OUT || primaryTransactionStatus == ITEM_CHECKED_IN) &&
secondaryTransactionRole == LENDER && secondaryTransactionStatus == ITEM_CHECKED_OUT) {

log.info("updateEcsTlrForLoan:: check-in happened in primary request tenant ({}), updating transactions", primaryTenantId);
log.info("updateEcsTlr:: check-in happened in primary request tenant ({}), updating transactions",
primaryTenantId);
dcbService.updateTransactionStatuses(StatusEnum.ITEM_CHECKED_IN, ecsTlr);
return;
}
Expand All @@ -127,13 +125,13 @@ else if (eventTenantIdIsSecondaryTenantId && secondaryTransactionRole == LENDER
(primaryTransactionRole == BORROWING_PICKUP || primaryTransactionRole == PICKUP) &&
primaryTransactionStatus == ITEM_CHECKED_IN) {

log.info("updateEcsTlrForLoan:: check-in happened in secondary request tenant ({}), updating transactions", secondaryTenantId);
log.info("updateEcsTlr:: check-in happened in secondary request tenant ({}), updating transactions", secondaryTenantId);
dcbService.updateTransactionStatuses(StatusEnum.CLOSED, ecsTlr);
return;
}
log.info("updateEcsTlrForLoan:: ECS TLR {} was not updated", ecsTlr::getId);
log.info("updateEcsTlr:: ECS TLR {} was not updated", ecsTlr::getId);
}
log.info("updateEcsTlrForLoan:: suitable ECS TLR for loan {} in tenant {} was not found", loan.getId(), tenantId);
log.info("updateEcsTlr:: suitable ECS TLR for loan {} in tenant {} was not found", loan.getId(), tenantId);
}

private Collection<EcsTlrEntity> findEcsTlrs(Loan loan) {
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/org/folio/service/DcbServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.folio.service;

import static java.util.UUID.randomUUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.UUID;
import java.util.concurrent.Callable;

import org.folio.client.feign.DcbTransactionClient;
import org.folio.domain.dto.TransactionStatus;
import org.folio.domain.dto.TransactionStatusResponse;
import org.folio.service.impl.DcbServiceImpl;
import org.folio.spring.service.SystemUserScopedExecutionService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DcbServiceTest {

@Mock
private DcbTransactionClient dcbTransactionClient;
@Mock
private SystemUserScopedExecutionService executionService;
@InjectMocks
private DcbServiceImpl dcbService;

@BeforeEach
public void setup() {
// Bypass the use of system user and return the result of Callable immediately
when(executionService.executeSystemUserScoped(any(String.class), any(Callable.class)))
.thenAnswer(invocation -> invocation.getArgument(1, Callable.class).call());
}

@ParameterizedTest
@CsvSource(value = {
"PICKUP, CREATED, OPEN, true",
"PICKUP, OPEN, AWAITING_PICKUP, false",
"PICKUP, AWAITING_PICKUP, ITEM_CHECKED_OUT, false",
"PICKUP, ITEM_CHECKED_OUT, ITEM_CHECKED_IN, false",
"PICKUP, ITEM_CHECKED_IN, CLOSED, true",
"PICKUP, OPEN, CANCELLED, true",

"BORROWING-PICKUP, CREATED, OPEN, true",
"BORROWING-PICKUP, OPEN, AWAITING_PICKUP, false",
"BORROWING-PICKUP, AWAITING_PICKUP, ITEM_CHECKED_OUT, false",
"BORROWING-PICKUP, ITEM_CHECKED_OUT, ITEM_CHECKED_IN, false",
"BORROWING-PICKUP, ITEM_CHECKED_IN, CLOSED, true",
"BORROWING-PICKUP, OPEN, CANCELLED, true",

"BORROWER, CREATED, OPEN, true",
"BORROWER, OPEN, AWAITING_PICKUP, true",
"BORROWER, AWAITING_PICKUP, ITEM_CHECKED_OUT, true",
"BORROWER, ITEM_CHECKED_OUT, ITEM_CHECKED_IN, true",
"BORROWER, ITEM_CHECKED_IN, CLOSED, true",
"BORROWER, OPEN, CANCELLED, true",

"LENDER, CREATED, OPEN, true",
"LENDER, OPEN, AWAITING_PICKUP, true",
"LENDER, AWAITING_PICKUP, ITEM_CHECKED_OUT, true",
"LENDER, ITEM_CHECKED_OUT, ITEM_CHECKED_IN, true",
"LENDER, ITEM_CHECKED_IN, CLOSED, false",
"LENDER, OPEN, CANCELLED, true",
})
void updateTransactionStatusesUpdatesAllTransactions(String role, String oldStatus,
String newStatus, boolean transactionUpdateIsExpected) {

String transactionId = randomUUID().toString();
TransactionStatus newTransactionStatus = new TransactionStatus().status(
TransactionStatus.StatusEnum.fromValue(newStatus));

TransactionStatusResponse mockGetStatusResponse = buildTransactionStatusResponse(role, oldStatus);
TransactionStatusResponse mockUpdateStatusResponse = buildTransactionStatusResponse(role, newStatus);

when(dcbTransactionClient.getDcbTransactionStatus(transactionId))
.thenReturn(mockGetStatusResponse);

if (transactionUpdateIsExpected) {
when(dcbTransactionClient.changeDcbTransactionStatus(transactionId, newTransactionStatus))
.thenReturn(mockUpdateStatusResponse);
}

dcbService.updateTransactionStatus(UUID.fromString(transactionId),
newTransactionStatus.getStatus(), "test_tenant");

verify(dcbTransactionClient, times(transactionUpdateIsExpected ? 1 : 0))
.changeDcbTransactionStatus(transactionId, newTransactionStatus);
}

private static TransactionStatusResponse buildTransactionStatusResponse(String role, String status) {
return new TransactionStatusResponse()
.role(TransactionStatusResponse.RoleEnum.fromValue(role))
.status(TransactionStatusResponse.StatusEnum.fromValue(status));
}

}

0 comments on commit 506a578

Please sign in to comment.