FINERACT-1981: Fix down-payment handling on overpaid loan (Advanced payment allocation loan)

This commit is contained in:
Adam Saghy
2024-02-28 15:53:14 +01:00
parent cd6258bd91
commit 65b1c45a5f
5 changed files with 55 additions and 9 deletions
@@ -23,12 +23,21 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.event.business.domain.loan.LoanStatusChangedBusinessEvent;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.apache.fineract.organisation.monetary.domain.Money;
import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
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.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
@@ -39,11 +48,22 @@ class DefaultLoanLifecycleStateMachineTest {
private DefaultLoanLifecycleStateMachine underTest;
private MockedStatic<MoneyHelper> moneyHelperStatic;
@BeforeEach
public void setUp() {
moneyHelperStatic = Mockito.mockStatic(MoneyHelper.class);
moneyHelperStatic.when(() -> MoneyHelper.getRoundingMode()).thenReturn(RoundingMode.UP);
underTest = new DefaultLoanLifecycleStateMachine(businessEventNotifierService);
}
@AfterEach
public void deregister() {
ThreadLocalContextUtil.reset();
moneyHelperStatic.close();
}
@Test
public void testTransitionShouldWorkProperlyForLoanCreation() {
// given
@@ -113,14 +133,33 @@ class DefaultLoanLifecycleStateMachineTest {
@Test
public void testTransitionShouldWorkProperlyForLoanDisbursementWhenLoanIsOverpaid() {
// given
Loan loan = createLoanWithStatus(LoanStatus.OVERPAID);
Money overpayment = Money.of(new MonetaryCurrency("USD", 2, null), BigDecimal.ZERO);
Loan loan = Mockito.mock(Loan.class);
Mockito.when(loan.getPlainStatus()).thenReturn(LoanStatus.OVERPAID.getValue());
Mockito.when(loan.getStatus()).thenReturn(LoanStatus.OVERPAID);
Mockito.when(loan.getTotalOverpaidAsMoney()).thenReturn(overpayment);
// when
underTest.transition(LoanEvent.LOAN_DISBURSED, loan);
// then
assertThat(loan.getStatus()).isEqualTo(LoanStatus.ACTIVE);
verify(loan, Mockito.times(1)).setLoanStatus(LoanStatus.ACTIVE.getValue());
verify(businessEventNotifierService).notifyPostBusinessEvent(any(LoanStatusChangedBusinessEvent.class));
}
@Test
public void testTransitionShouldWorkProperlyForLoanDisbursementWhenLoanIsOverpaidAndRemainsOverpaid() {
// given
Money overpayment = Money.of(new MonetaryCurrency("USD", 2, null), BigDecimal.TEN);
Loan loan = Mockito.mock(Loan.class);
Mockito.when(loan.getPlainStatus()).thenReturn(LoanStatus.OVERPAID.getValue());
Mockito.when(loan.getStatus()).thenReturn(LoanStatus.OVERPAID);
Mockito.when(loan.getTotalOverpaidAsMoney()).thenReturn(overpayment);
// when
underTest.transition(LoanEvent.LOAN_DISBURSED, loan);
// then
verify(loan, Mockito.never()).setLoanStatus(LoanStatus.ACTIVE.getValue());
verify(businessEventNotifierService, Mockito.never()).notifyPostBusinessEvent(any(LoanStatusChangedBusinessEvent.class));
}
@Test
public void testTransitionShouldWorkProperlyForLoanApprovalUndo() {
// given