FINERACT-1992: Multi delinquency implication on events

This commit is contained in:
Ruchi Dhamankar
2023-11-23 10:30:19 +01:00
committed by Adam Saghy
parent f089f01116
commit 48300cd19d
7 changed files with 280 additions and 10 deletions
@@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.fineract.avro.loan.v1.LoanAccountDelinquencyRangeDataV1;
import org.apache.fineract.avro.loan.v1.LoanInstallmentDelinquencyBucketDataV1;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.core.domain.ActionContext;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
@@ -59,6 +60,7 @@ import org.apache.fineract.portfolio.charge.domain.ChargeCalculationType;
import org.apache.fineract.portfolio.charge.domain.ChargePaymentMode;
import org.apache.fineract.portfolio.charge.domain.ChargeTimeType;
import org.apache.fineract.portfolio.delinquency.data.DelinquencyRangeData;
import org.apache.fineract.portfolio.delinquency.data.LoanInstallmentDelinquencyTagData;
import org.apache.fineract.portfolio.delinquency.service.DelinquencyReadPlatformService;
import org.apache.fineract.portfolio.loanaccount.data.CollectionData;
import org.apache.fineract.portfolio.loanaccount.data.LoanAccountData;
@@ -75,9 +77,12 @@ import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.test.util.ReflectionTestUtils;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class LoanAccountDelinquencyRangeEventSerializerTest {
@Mock
@@ -101,7 +106,7 @@ public class LoanAccountDelinquencyRangeEventSerializerTest {
}
@Test
public void testLoanRepaymentEventPayloadSerialization() throws IOException {
public void testLoanDelinquencyRangeEventPayloadSerialization() throws IOException {
// given
LoanDelinquencyRangeChangeBusinessEventSerializer serializer = new LoanDelinquencyRangeChangeBusinessEventSerializer(
loanReadPlatformService, new LoanDelinquencyRangeDataMapperImpl(), loanChargeReadPlatformService,
@@ -123,6 +128,7 @@ public class LoanAccountDelinquencyRangeEventSerializerTest {
when(loanAccountData.getCurrency()).thenAnswer(a -> new CurrencyData(loanCurrency.getCode(), loanCurrency.getDigitsAfterDecimal(),
loanCurrency.getCurrencyInMultiplesOf()));
when(loanForProcessing.getCurrency()).thenReturn(loanCurrency);
when(loanForProcessing.isEnableInstallmentLevelDelinquency()).thenReturn(false);
when(delinquentData.getDelinquentDate()).thenReturn(delinquentDate);
when(loanReadPlatformService.retrieveOne(any(Long.class))).thenReturn(loanAccountData);
when(delinquencyReadPlatformService.calculateLoanCollectionData(any(Long.class))).thenReturn(delinquentData);
@@ -163,6 +169,141 @@ public class LoanAccountDelinquencyRangeEventSerializerTest {
moneyHelper.close();
}
@Test
public void testLoanDelinquencyRangeEventPayloadSerializationWithInstallmentDelinquencyData() throws IOException {
// given
LoanDelinquencyRangeChangeBusinessEventSerializer serializer = new LoanDelinquencyRangeChangeBusinessEventSerializer(
loanReadPlatformService, new LoanDelinquencyRangeDataMapperImpl(), loanChargeReadPlatformService,
delinquencyReadPlatformService, new LoanChargeDataMapperImpl(null, null, null), new CurrencyDataMapperImpl(), mapper);
Loan loanForProcessing = Mockito.mock(Loan.class);
LoanAccountData loanAccountData = mock(LoanAccountData.class);
CollectionData delinquentData = mock(CollectionData.class);
MonetaryCurrency loanCurrency = new MonetaryCurrency("CODE", 1, 1);
MockedStatic<MoneyHelper> moneyHelper = Mockito.mockStatic(MoneyHelper.class);
String delinquentDateAsStr = "2022-12-01";
LocalDate delinquentDate = LocalDate.parse(delinquentDateAsStr);
when(loanForProcessing.getId()).thenReturn(1L);
when(loanAccountData.getId()).thenReturn(1L);
when(loanAccountData.getAccountNo()).thenReturn("0001");
when(loanAccountData.getExternalId()).thenReturn(ExternalIdFactory.produce("externalId"));
when(loanAccountData.getDelinquencyRange()).thenReturn(new DelinquencyRangeData(1L, "classification", 1, 10));
when(loanAccountData.getCurrency()).thenAnswer(a -> new CurrencyData(loanCurrency.getCode(), loanCurrency.getDigitsAfterDecimal(),
loanCurrency.getCurrencyInMultiplesOf()));
when(loanForProcessing.getCurrency()).thenReturn(loanCurrency);
when(loanForProcessing.isEnableInstallmentLevelDelinquency()).thenReturn(true);
when(delinquentData.getDelinquentDate()).thenReturn(delinquentDate);
when(loanReadPlatformService.retrieveOne(any(Long.class))).thenReturn(loanAccountData);
when(delinquencyReadPlatformService.calculateLoanCollectionData(any(Long.class))).thenReturn(delinquentData);
when(mapper.mapLocalDate(delinquentDate)).thenReturn(delinquentDateAsStr);
LoanDelinquencyRangeChangeBusinessEvent event = new LoanDelinquencyRangeChangeBusinessEvent(loanForProcessing);
List<LoanRepaymentScheduleInstallment> repaymentScheduleInstallments = new ArrayList<>();
LoanRepaymentScheduleInstallment repaymentScheduleInstallment_1 = buildInstallment(loanForProcessing, loanCurrency,
BigDecimal.valueOf(100), BigDecimal.valueOf(10), BigDecimal.valueOf(20), BigDecimal.valueOf(20), BigDecimal.valueOf(150),
new BigDecimal("10"), new BigDecimal("20"));
when(repaymentScheduleInstallment_1.getId()).thenReturn(1L);
when(repaymentScheduleInstallment_1.getFromDate()).thenReturn(LocalDate.of(2022, 6, 20));
when(repaymentScheduleInstallment_1.getDueDate()).thenReturn(LocalDate.of(2022, 6, 30));
repaymentScheduleInstallments.add(repaymentScheduleInstallment_1);
LoanRepaymentScheduleInstallment repaymentScheduleInstallment_2 = buildInstallment(loanForProcessing, loanCurrency,
BigDecimal.valueOf(100), BigDecimal.valueOf(0), BigDecimal.valueOf(20), BigDecimal.valueOf(0), BigDecimal.valueOf(120));
when(repaymentScheduleInstallment_2.getId()).thenReturn(2L);
when(repaymentScheduleInstallment_2.getFromDate()).thenReturn(LocalDate.of(2022, 7, 1));
when(repaymentScheduleInstallment_2.getDueDate()).thenReturn(LocalDate.of(2022, 7, 10));
repaymentScheduleInstallments.add(repaymentScheduleInstallment_2);
LoanRepaymentScheduleInstallment repaymentScheduleInstallment_3 = buildInstallment(loanForProcessing, loanCurrency,
BigDecimal.valueOf(100), BigDecimal.valueOf(0), BigDecimal.valueOf(20), BigDecimal.valueOf(0), BigDecimal.valueOf(120));
when(repaymentScheduleInstallment_3.getId()).thenReturn(3L);
when(repaymentScheduleInstallment_3.getFromDate()).thenReturn(LocalDate.of(2022, 7, 11));
when(repaymentScheduleInstallment_3.getDueDate()).thenReturn(LocalDate.of(2022, 7, 20));
repaymentScheduleInstallments.add(repaymentScheduleInstallment_3);
when(loanForProcessing.getRepaymentScheduleInstallments()).thenReturn(repaymentScheduleInstallments);
when(loanChargeReadPlatformService.retrieveLoanCharges(anyLong())).thenAnswer(a -> repaymentScheduleInstallments.get(0)
.getInstallmentCharges().stream().map(c -> c.getLoanCharge().toData()).collect(Collectors.toList()));
List<LoanInstallmentDelinquencyTagData> installmentDelinquencyTags = new ArrayList<>();
installmentDelinquencyTags.add(buildInstallmentDelinquencyTag(1L, 1L));
installmentDelinquencyTags.add(buildInstallmentDelinquencyTag(2L, 1L));
installmentDelinquencyTags.add(buildInstallmentDelinquencyTag(3L, 2L));
when(delinquencyReadPlatformService.retrieveLoanInstallmentsCurrentDelinquencyTag(anyLong()))
.thenReturn(installmentDelinquencyTags);
when(loanForProcessing.getLoanCharges()).thenAnswer(a -> repaymentScheduleInstallments.get(0).getInstallmentCharges().stream()
.map(c -> c.getLoanCharge()).collect(Collectors.toList()));
moneyHelper.when(() -> MoneyHelper.getRoundingMode()).thenReturn(RoundingMode.UP);
// when
LoanAccountDelinquencyRangeDataV1 data = (LoanAccountDelinquencyRangeDataV1) serializer.toAvroDTO(event);
// then
assertEquals(1L, data.getLoanId());
assertEquals("0001", data.getLoanAccountNo());
assertEquals("externalId", data.getLoanExternalId());
assertEquals(1L, data.getDelinquencyRange().getId());
assertEquals("classification", data.getDelinquencyRange().getClassification());
assertEquals(1, data.getDelinquencyRange().getMinimumAgeDays());
assertEquals(10, data.getDelinquencyRange().getMaximumAgeDays());
assertEquals(2, data.getCharges().size());
assertTrue(data.getCharges().stream().anyMatch(a -> a.getAmount().compareTo(new BigDecimal("10")) == 0));
assertTrue(data.getCharges().stream().anyMatch(a -> a.getAmount().compareTo(new BigDecimal("20")) == 0));
assertEquals(0, data.getAmount().getTotalAmount().compareTo(new BigDecimal("390.0")));
assertEquals(0, data.getAmount().getPrincipalAmount().compareTo(new BigDecimal("300.0")));
assertEquals(0, data.getAmount().getInterestAmount().compareTo(new BigDecimal("60.0")));
assertEquals(0, data.getAmount().getFeeAmount().compareTo(new BigDecimal("10.0")));
assertEquals(0, data.getAmount().getPenaltyAmount().compareTo(new BigDecimal("20.0")));
assertEquals(delinquentDateAsStr, data.getDelinquentDate());
// check installment delinquency data
assertEquals(2, data.getInstallmentDelinquencyBuckets().size());
// check calculations
LoanInstallmentDelinquencyBucketDataV1 installmentDelinquencyBucketDataV1_1 = data.getInstallmentDelinquencyBuckets().get(0);
assertEquals(1L, installmentDelinquencyBucketDataV1_1.getDelinquencyRange().getId());
assertEquals("range_1", installmentDelinquencyBucketDataV1_1.getDelinquencyRange().getClassification());
assertEquals(0, installmentDelinquencyBucketDataV1_1.getAmount().getTotalAmount().compareTo(new BigDecimal("270.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_1.getAmount().getPrincipalAmount().compareTo(new BigDecimal("200.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_1.getAmount().getInterestAmount().compareTo(new BigDecimal("40.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_1.getAmount().getFeeAmount().compareTo(new BigDecimal("10.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_1.getAmount().getPenaltyAmount().compareTo(new BigDecimal("20.0")));
assertEquals(2, installmentDelinquencyBucketDataV1_1.getCharges().size());
assertTrue(installmentDelinquencyBucketDataV1_1.getCharges().stream()
.anyMatch(a -> a.getAmount().compareTo(new BigDecimal("10")) == 0));
assertTrue(installmentDelinquencyBucketDataV1_1.getCharges().stream()
.anyMatch(a -> a.getAmount().compareTo(new BigDecimal("20")) == 0));
LoanInstallmentDelinquencyBucketDataV1 installmentDelinquencyBucketDataV1_2 = data.getInstallmentDelinquencyBuckets().get(1);
assertEquals(2L, installmentDelinquencyBucketDataV1_2.getDelinquencyRange().getId());
assertEquals("range_2", installmentDelinquencyBucketDataV1_2.getDelinquencyRange().getClassification());
assertEquals(0, installmentDelinquencyBucketDataV1_2.getAmount().getTotalAmount().compareTo(new BigDecimal("120.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_2.getAmount().getPrincipalAmount().compareTo(new BigDecimal("100.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_2.getAmount().getInterestAmount().compareTo(new BigDecimal("20.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_2.getAmount().getFeeAmount().compareTo(new BigDecimal("0.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_2.getAmount().getPenaltyAmount().compareTo(new BigDecimal("0.0")));
assertEquals(0, installmentDelinquencyBucketDataV1_2.getCharges().size());
moneyHelper.close();
}
private LoanInstallmentDelinquencyTagData buildInstallmentDelinquencyTag(long installmentId, long rangeId) {
LoanInstallmentDelinquencyTagData.InstallmentDelinquencyRange delinquencyRange = mock(
LoanInstallmentDelinquencyTagData.InstallmentDelinquencyRange.class);
when(delinquencyRange.getId()).thenReturn(rangeId);
when(delinquencyRange.getClassification()).thenReturn("range_" + rangeId);
when(delinquencyRange.getMaximumAgeDays()).thenReturn(1);
when(delinquencyRange.getMinimumAgeDays()).thenReturn(2);
LoanInstallmentDelinquencyTagData installmentDelinquencyTagData = mock(LoanInstallmentDelinquencyTagData.class);
when(installmentDelinquencyTagData.getId()).thenReturn(installmentId);
when(installmentDelinquencyTagData.getDelinquencyRange()).thenReturn(delinquencyRange);
return installmentDelinquencyTagData;
}
private LoanRepaymentScheduleInstallment buildInstallment(Loan loan, MonetaryCurrency currency, BigDecimal principalAmount,
BigDecimal freeAmount, BigDecimal interestAmount, BigDecimal penaltyAmount, BigDecimal totalAmount, BigDecimal... charges) {
@@ -173,6 +314,7 @@ public class LoanAccountDelinquencyRangeEventSerializerTest {
when(installment.getFeeChargesOutstanding(any())).thenAnswer(a -> Money.of(currency, freeAmount));
when(installment.getTotalOutstanding(any())).thenAnswer(a -> Money.of(currency, totalAmount));
Charge charge = mock(Charge.class);
when(charge.getName()).thenReturn("charge");
when(charge.toData()).thenAnswer(a -> {
ChargeData chargeData = mock(ChargeData.class);
when(chargeData.getCurrency()).thenAnswer(b -> new CurrencyData(currency.getCode()));
@@ -193,7 +335,10 @@ public class LoanAccountDelinquencyRangeEventSerializerTest {
}
private LoanCharge buildLoanCharge(Loan loan, BigDecimal amount, Charge charge) {
return new LoanCharge(loan, charge, amount, amount, ChargeTimeType.TRANCHE_DISBURSEMENT, ChargeCalculationType.FLAT,
LoanCharge loanCharge = new LoanCharge(loan, charge, amount, amount, ChargeTimeType.SPECIFIED_DUE_DATE, ChargeCalculationType.FLAT,
LocalDate.of(2022, 6, 27), ChargePaymentMode.REGULAR, 1, new BigDecimal(100), ExternalId.generate());
ReflectionTestUtils.setField(loanCharge, "id", 1L);
return loanCharge;
}
}