FINERACT-1919: Fix monthly loan schedule due dates to maximize accord… (#3109)

* FINERACT-1919: Fix monthly loan schedule due dates to maximize according to seed date and number of days in the month
This commit is contained in:
Mohit Sinha
2023-04-12 15:56:22 +05:30
committed by GitHub
parent 3b6c652c31
commit 3c7c6dfd1d
3 changed files with 460 additions and 30 deletions
@@ -22,8 +22,8 @@ import com.google.gson.JsonElement;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
@@ -90,36 +90,13 @@ public final class CalendarUtils {
}
public static Temporal adjustDate(final Temporal date, final Temporal seedDate, final PeriodFrequencyType frequencyType) {
Temporal adjustedVal = date;
if (frequencyType.isMonthly() && seedDate.get(ChronoField.DAY_OF_MONTH) > 28 && date.get(ChronoField.DAY_OF_MONTH) > 28) {
switch (date.get(ChronoField.MONTH_OF_YEAR)) {
case 2:
if (IsoChronology.INSTANCE.isLeapYear(date.get(ChronoField.YEAR))) {
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, 29);
}
break;
case 4:
case 6:
case 9:
case 11:
if (seedDate.get(ChronoField.DAY_OF_MONTH) > 30) {
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, 30);
} else {
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, seedDate.get(ChronoField.DAY_OF_MONTH));
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
adjustedVal = date.with(ChronoField.DAY_OF_MONTH, seedDate.get(ChronoField.DAY_OF_MONTH));
break;
}
if (frequencyType.isMonthly() && seedDate.get(ChronoField.DAY_OF_MONTH) > 28 && date.get(ChronoField.DAY_OF_MONTH) >= 28) {
int noOfDaysInCurrentMonth = YearMonth.from(date).lengthOfMonth();
int seedDay = seedDate.get(ChronoField.DAY_OF_MONTH);
int adjustedDay = Math.min(noOfDaysInCurrentMonth, seedDay);
return date.with(ChronoField.DAY_OF_MONTH, adjustedDay);
}
return adjustedVal;
return date;
}
private static LocalDateTime getNextRecurringDate(final Recur recur, final LocalDateTime seedDate, final LocalDateTime startDate) {
@@ -0,0 +1,139 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.calendar.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import org.apache.fineract.portfolio.common.domain.PeriodFrequencyType;
import org.junit.jupiter.api.Test;
public class CalendarUtilsTest {
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateBefore28() {
// given
Temporal date = LocalDate.of(2023, Month.APRIL, 27);
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 27);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(27, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasLessDays() {
// given
Temporal date = LocalDate.of(2023, Month.FEBRUARY, 28);
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(28, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasEqualDays() {
// given
Temporal date = LocalDate.of(2023, Month.MARCH, 31);
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(31, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthHasMoreDays() {
// given
Temporal date = LocalDate.of(2023, Month.MAY, 31);
Temporal seedDate = LocalDate.of(2023, Month.APRIL, 30);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(30, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthIsMarch() {
// given
Temporal date = LocalDate.of(2023, Month.MARCH, 28);
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 31);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(31, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateWithMonthlyFrequencyAndSeedDateAfter28AndCurrentMonthIsFebLeapYear() {
// given
Temporal date = LocalDate.of(2024, Month.FEBRUARY, 29);
Temporal seedDate = LocalDate.of(2024, Month.JANUARY, 30);
PeriodFrequencyType frequencyType = PeriodFrequencyType.MONTHS;
// when
Temporal adjustedDate = CalendarUtils.adjustDate(date, seedDate, frequencyType);
// then
assertEquals(29, adjustedDate.get(ChronoField.DAY_OF_MONTH));
}
@Test
public void testAdjustDateNonMonthlyPeriod() {
// given
Temporal date = LocalDate.of(2023, Month.APRIL, 30);
Temporal seedDate = LocalDate.of(2023, Month.JANUARY, 27);
// when
Temporal adjustedDateWeekly = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.WEEKS);
// then
assertEquals(30, adjustedDateWeekly.get(ChronoField.DAY_OF_MONTH));
// when
Temporal adjustedDateYearly = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.YEARS);
// then
assertEquals(30, adjustedDateYearly.get(ChronoField.DAY_OF_MONTH));
// when
Temporal adjustedDateDaily = CalendarUtils.adjustDate(date, seedDate, PeriodFrequencyType.DAYS);
// then
assertEquals(30, adjustedDateDaily.get(ChronoField.DAY_OF_MONTH));
}
}
@@ -0,0 +1,314 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.loans.LoanApplicationTestBuilder;
import org.apache.fineract.integrationtests.common.loans.LoanProductTestBuilder;
import org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoanApplicationScheduleMonthlyTest {
private static final Logger LOG = LoggerFactory.getLogger(LoanApplicationScheduleMonthlyTest.class);
public static final Integer TOTAL_REPAYMENTS = 14;
public static final String NUMBER_OF_REPAYMENTS = String.valueOf(TOTAL_REPAYMENTS);
public static final String DISBURSEMENT_DATE = "30 December 2022";
public static final String CLIENT_ACTIVATION_DATE = "13 October 2022";
public static final String DUE_DATE = "dueDate";
private ResponseSpecification responseSpec;
private RequestSpecification requestSpec;
private LoanTransactionHelper loanTransactionHelper;
@BeforeEach
public void setup() {
Utils.initializeRESTAssured();
this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build();
this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey());
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec);
}
@SuppressWarnings({ "unchecked" })
@Test
public void validateSeedDate31() {
final Integer clientId = createClient(CLIENT_ACTIVATION_DATE);
String firstRepaymentDate = "31 January 2023";
Integer loanProductId = createLoanProductEntity();
Integer loanId = applyForLoanApplication(clientId, loanProductId, firstRepaymentDate);
final ArrayList<HashMap> repaymentPeriods = (ArrayList<HashMap>) this.loanTransactionHelper
.getLoanRepaymentSchedule(this.requestSpec, this.responseSpec, loanId);
assertEquals(new ArrayList<>(Arrays.asList(2023, 1, 31)), repaymentPeriods.get(1).get(DUE_DATE),
"Checking for Due Date for 1st Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 2, 28)), repaymentPeriods.get(2).get(DUE_DATE),
"Checking for Due Date for 2nd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 3, 31)), repaymentPeriods.get(3).get(DUE_DATE),
"Checking for Due Date for 3rd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 4, 30)), repaymentPeriods.get(4).get(DUE_DATE),
"Checking for Due Date for 4th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 5, 31)), repaymentPeriods.get(5).get(DUE_DATE),
"Checking for Due Date for 5th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 6, 30)), repaymentPeriods.get(6).get(DUE_DATE),
"Checking for Due Date for 6th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 7, 31)), repaymentPeriods.get(7).get(DUE_DATE),
"Checking for Due Date for 7th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 8, 31)), repaymentPeriods.get(8).get(DUE_DATE),
"Checking for Due Date for 8th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 9, 30)), repaymentPeriods.get(9).get(DUE_DATE),
"Checking for Due Date for 9th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 10, 31)), repaymentPeriods.get(10).get(DUE_DATE),
"Checking for Due Date for 10th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 11, 30)), repaymentPeriods.get(11).get(DUE_DATE),
"Checking for Due Date for 11th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 12, 31)), repaymentPeriods.get(12).get(DUE_DATE),
"Checking for Due Date for 12th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 1, 31)), repaymentPeriods.get(13).get(DUE_DATE),
"Checking for Due Date for 13th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 2, 29)), repaymentPeriods.get(14).get(DUE_DATE),
"Checking for Due Date for 14th Month");
}
private Integer createClient(String activationDate) {
final Integer clientId = ClientHelper.createClient(this.requestSpec, this.responseSpec, activationDate);
return clientId;
}
@SuppressWarnings({ "unchecked" })
@Test
public void validateSeedDate30() {
final Integer clientId = createClient(CLIENT_ACTIVATION_DATE);
String firstRepaymentDate = "30 January 2023";
Integer loanProductId = createLoanProductEntity();
Integer loanId = applyForLoanApplication(clientId, loanProductId, firstRepaymentDate);
final ArrayList<HashMap> repaymentPeriods = (ArrayList<HashMap>) this.loanTransactionHelper
.getLoanRepaymentSchedule(this.requestSpec, this.responseSpec, loanId);
assertEquals(new ArrayList<>(Arrays.asList(2023, 1, 30)), repaymentPeriods.get(1).get(DUE_DATE),
"Checking for Due Date for 1st Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 2, 28)), repaymentPeriods.get(2).get(DUE_DATE),
"Checking for Due Date for 2nd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 3, 30)), repaymentPeriods.get(3).get(DUE_DATE),
"Checking for Due Date for 3rd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 4, 30)), repaymentPeriods.get(4).get(DUE_DATE),
"Checking for Due Date for 4th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 5, 30)), repaymentPeriods.get(5).get(DUE_DATE),
"Checking for Due Date for 5th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 6, 30)), repaymentPeriods.get(6).get(DUE_DATE),
"Checking for Due Date for 6th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 7, 30)), repaymentPeriods.get(7).get(DUE_DATE),
"Checking for Due Date for 7th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 8, 30)), repaymentPeriods.get(8).get(DUE_DATE),
"Checking for Due Date for 8th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 9, 30)), repaymentPeriods.get(9).get(DUE_DATE),
"Checking for Due Date for 9th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 10, 30)), repaymentPeriods.get(10).get(DUE_DATE),
"Checking for Due Date for 10th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 11, 30)), repaymentPeriods.get(11).get(DUE_DATE),
"Checking for Due Date for 11th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 12, 30)), repaymentPeriods.get(12).get(DUE_DATE),
"Checking for Due Date for 12th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 1, 30)), repaymentPeriods.get(13).get(DUE_DATE),
"Checking for Due Date for 13th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 2, 29)), repaymentPeriods.get(14).get(DUE_DATE),
"Checking for Due Date for 14th Month");
}
@SuppressWarnings({ "unchecked" })
@Test
public void validateSeedDate28() {
final Integer clientId = createClient(CLIENT_ACTIVATION_DATE);
String firstRepaymentDate = "28 January 2023";
Integer loanProductId = createLoanProductEntity();
Integer loanId = applyForLoanApplication(clientId, loanProductId, firstRepaymentDate);
final ArrayList<HashMap> repaymentPeriods = (ArrayList<HashMap>) this.loanTransactionHelper
.getLoanRepaymentSchedule(this.requestSpec, this.responseSpec, loanId);
assertEquals(new ArrayList<>(Arrays.asList(2023, 1, 28)), repaymentPeriods.get(1).get(DUE_DATE),
"Checking for Due Date for 1st Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 2, 28)), repaymentPeriods.get(2).get(DUE_DATE),
"Checking for Due Date for 2nd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 3, 28)), repaymentPeriods.get(3).get(DUE_DATE),
"Checking for Due Date for 3rd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 4, 28)), repaymentPeriods.get(4).get(DUE_DATE),
"Checking for Due Date for 4th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 5, 28)), repaymentPeriods.get(5).get(DUE_DATE),
"Checking for Due Date for 5th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 6, 28)), repaymentPeriods.get(6).get(DUE_DATE),
"Checking for Due Date for 6th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 7, 28)), repaymentPeriods.get(7).get(DUE_DATE),
"Checking for Due Date for 7th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 8, 28)), repaymentPeriods.get(8).get(DUE_DATE),
"Checking for Due Date for 8th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 9, 28)), repaymentPeriods.get(9).get(DUE_DATE),
"Checking for Due Date for 9th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 10, 28)), repaymentPeriods.get(10).get(DUE_DATE),
"Checking for Due Date for 10th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 11, 28)), repaymentPeriods.get(11).get(DUE_DATE),
"Checking for Due Date for 11th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 12, 28)), repaymentPeriods.get(12).get(DUE_DATE),
"Checking for Due Date for 12th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 1, 28)), repaymentPeriods.get(13).get(DUE_DATE),
"Checking for Due Date for 13th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 2, 28)), repaymentPeriods.get(14).get(DUE_DATE),
"Checking for Due Date for 14th Month");
}
@SuppressWarnings({ "unchecked" })
@Test
public void validateSeedDate25() {
final Integer clientId = createClient(CLIENT_ACTIVATION_DATE);
String firstRepaymentDate = "25 January 2023";
Integer loanProductId = createLoanProductEntity();
Integer loanId = applyForLoanApplication(clientId, loanProductId, firstRepaymentDate);
final ArrayList<HashMap> repaymentPeriods = (ArrayList<HashMap>) this.loanTransactionHelper
.getLoanRepaymentSchedule(this.requestSpec, this.responseSpec, loanId);
assertEquals(new ArrayList<>(Arrays.asList(2023, 1, 25)), repaymentPeriods.get(1).get(DUE_DATE),
"Checking for Due Date for 1st Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 2, 25)), repaymentPeriods.get(2).get(DUE_DATE),
"Checking for Due Date for 2nd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 3, 25)), repaymentPeriods.get(3).get(DUE_DATE),
"Checking for Due Date for 3rd Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 4, 25)), repaymentPeriods.get(4).get(DUE_DATE),
"Checking for Due Date for 4th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 5, 25)), repaymentPeriods.get(5).get(DUE_DATE),
"Checking for Due Date for 5th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 6, 25)), repaymentPeriods.get(6).get(DUE_DATE),
"Checking for Due Date for 6th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 7, 25)), repaymentPeriods.get(7).get(DUE_DATE),
"Checking for Due Date for 7th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 8, 25)), repaymentPeriods.get(8).get(DUE_DATE),
"Checking for Due Date for 8th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 9, 25)), repaymentPeriods.get(9).get(DUE_DATE),
"Checking for Due Date for 9th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 10, 25)), repaymentPeriods.get(10).get(DUE_DATE),
"Checking for Due Date for 10th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 11, 25)), repaymentPeriods.get(11).get(DUE_DATE),
"Checking for Due Date for 11th Month");
assertEquals(new ArrayList<>(Arrays.asList(2023, 12, 25)), repaymentPeriods.get(12).get(DUE_DATE),
"Checking for Due Date for 12th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 1, 25)), repaymentPeriods.get(13).get(DUE_DATE),
"Checking for Due Date for 13th Month");
assertEquals(new ArrayList<>(Arrays.asList(2024, 2, 25)), repaymentPeriods.get(14).get(DUE_DATE),
"Checking for Due Date for 14th Month");
}
/**
* create a new loan product
**/
private Integer createLoanProductEntity() {
final String loanProductJSON = new LoanProductTestBuilder().withPrincipal("10000").withRepaymentAfterEvery("1")
.withNumberOfRepayments(NUMBER_OF_REPAYMENTS).withRepaymentTypeAsMonth().withInterestRateFrequencyTypeAsMonths()
.build(null);
Integer loanProductId = this.loanTransactionHelper.getLoanProductId(loanProductJSON);
return loanProductId;
}
/**
* Apply for a Loan
*/
private Integer applyForLoanApplication(final Integer clientID, final Integer loanProductID, String firstRepaymentDate) {
final String loanApplication = new LoanApplicationTestBuilder().withPrincipal("10000").withLoanTermFrequency(NUMBER_OF_REPAYMENTS)
.withLoanTermFrequencyAsMonths().withNumberOfRepayments(NUMBER_OF_REPAYMENTS).withRepaymentEveryAfter("1")
.withRepaymentFrequencyTypeAsMonths().withInterestRatePerPeriod("2").withExpectedDisbursementDate(DISBURSEMENT_DATE)
.withSubmittedOnDate(DISBURSEMENT_DATE).withFirstRepaymentDate(firstRepaymentDate)
.build(clientID.toString(), loanProductID.toString(), null);
return this.loanTransactionHelper.getLoanId(loanApplication);
}
}