Skip to content

Commit

Permalink
FINERACT-2081: fix loan creation validations
Browse files Browse the repository at this point in the history
  • Loading branch information
kjozsa authored and adamsaghy committed Aug 6, 2024
1 parent e967426 commit 23b5099
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,6 @@ private void validateForCreate(final JsonElement element) {
.integerGreaterThanZero();
}

final BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(LoanApiConstants.principalParamName,
element);
baseDataValidator.reset().parameter(LoanApiConstants.principalParamName).value(principal).notNull().positiveAmount();

final Integer loanTermFrequency = this.fromApiJsonHelper
.extractIntegerWithLocaleNamed(LoanApiConstants.loanTermFrequencyParameterName, element);
baseDataValidator.reset().parameter(LoanApiConstants.loanTermFrequencyParameterName).value(loanTermFrequency).notNull()
Expand Down Expand Up @@ -422,7 +418,7 @@ private void validateForCreate(final JsonElement element) {
baseDataValidator.reset().parameter(LoanApiConstants.isFloatingInterestRate).trueOrFalseRequired(false);
}

if (interestType != null && interestType.equals(InterestMethod.FLAT.getValue())) {
if (InterestMethod.FLAT.getValue().equals(interestType)) {
baseDataValidator.reset().parameter(LoanApiConstants.interestTypeParameterName).failWithCode(
"should.be.0.for.selected.loan.product",
"interestType should be DECLINING_BALANCE for selected Loan Product as it is linked to floating rates.");
Expand Down Expand Up @@ -452,7 +448,7 @@ private void validateForCreate(final JsonElement element) {
.extractBigDecimalWithLocaleNamed(LoanApiConstants.interestRatePerPeriodParameterName, element);
baseDataValidator.reset().parameter(LoanApiConstants.interestRatePerPeriodParameterName).value(interestRatePerPeriod)
.notNull().zeroOrPositiveAmount();
isInterestBearing = interestRatePerPeriod.compareTo(BigDecimal.ZERO) > 0;
isInterestBearing = interestRatePerPeriod != null && interestRatePerPeriod.compareTo(BigDecimal.ZERO) > 0;
}

final Integer amortizationType = this.fromApiJsonHelper
Expand Down Expand Up @@ -622,6 +618,9 @@ private void validateForCreate(final JsonElement element) {
.ignoreIfNull().positiveAmount();
}

final BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(LoanApiConstants.principalParamName,
element);

if (loanProduct.isCanUseForTopup() && this.fromApiJsonHelper.parameterExists(LoanApiConstants.isTopup, element)) {
final Boolean isTopup = this.fromApiJsonHelper.extractBooleanNamed(LoanApiConstants.isTopup, element);
baseDataValidator.reset().parameter(LoanApiConstants.isTopup).value(isTopup).validateForBooleanValue();
Expand Down Expand Up @@ -745,7 +744,7 @@ private void validateForCreate(final JsonElement element) {
loanScheduleValidator.validateDownPaymentAttribute(loanProduct.getLoanProductRelatedDetail().isEnableDownPayment(), element);

checkForProductMixRestrictions(element);
validateSubmittedOnDate(element, submittedOnDate, loanProduct);
validateSubmittedOnDate(element, null, null, loanProduct);
validateDisbursementDetails(loanProduct, element);
validateCollateral(element);
// validate if disbursement date is a holiday or a non-working day
Expand All @@ -754,8 +753,11 @@ private void validateForCreate(final JsonElement element) {
validateDisbursementDateIsOnHoliday(expectedDisbursementDate, officeId);
final Integer recurringMoratoriumOnPrincipalPeriods = this.fromApiJsonHelper
.extractIntegerWithLocaleNamed("recurringMoratoriumOnPrincipalPeriods", element);
loanProductDataValidator.validateRepaymentPeriodWithGraceSettings(numberOfRepayments, graceOnPrincipalPayment,
graceOnInterestPayment, graceOnInterestCharged, recurringMoratoriumOnPrincipalPeriods, baseDataValidator);

if (numberOfRepayments != null) {
loanProductDataValidator.validateRepaymentPeriodWithGraceSettings(numberOfRepayments, graceOnPrincipalPayment,
graceOnInterestPayment, graceOnInterestCharged, recurringMoratoriumOnPrincipalPeriods, baseDataValidator);
}
});
}

Expand All @@ -777,7 +779,8 @@ private void validateBorrowerCycle(JsonElement element, LoanProduct loanProduct,
private void validateDisbursementDateIsOnNonWorkingDay(final LocalDate expectedDisbursementDate) {
final WorkingDays workingDays = this.workingDaysRepository.findOne();
final boolean allowTransactionsOnNonWorkingDay = this.configurationDomainService.allowTransactionsOnNonWorkingDayEnabled();
if (!allowTransactionsOnNonWorkingDay && !WorkingDaysUtil.isWorkingDay(workingDays, expectedDisbursementDate)) {
if (expectedDisbursementDate != null && !allowTransactionsOnNonWorkingDay
&& !WorkingDaysUtil.isWorkingDay(workingDays, expectedDisbursementDate)) {
final String errorMessage = "Expected disbursement date cannot be on a non working day";
throw new LoanApplicationDateException("disbursement.date.on.non.working.day", errorMessage, expectedDisbursementDate);
}
Expand Down Expand Up @@ -1032,7 +1035,7 @@ public void validateForModify(final JsonCommand command, final Loan loan) {
if (interestType == null) {
interestType = loan.getLoanProductRelatedDetail().getInterestMethod().getValue();
}
if (interestType != null && interestType.equals(InterestMethod.FLAT.getValue())) {
if (InterestMethod.FLAT.getValue().equals(interestType)) {
baseDataValidator.reset().parameter(LoanApiConstants.interestTypeParameterName).failWithCode(
"should.be.0.for.selected.loan.product",
"interestType should be DECLINING_BALANCE for selected Loan Product as it is linked to floating rates.");
Expand Down Expand Up @@ -1402,7 +1405,7 @@ public void validateForModify(final JsonCommand command, final Loan loan) {
loanScheduleValidator.validateDownPaymentAttribute(loanProduct.getLoanProductRelatedDetail().isEnableDownPayment(), element);

validateDisbursementDetails(loanProduct, element);
validateSubmittedOnDate(element, loan.getSubmittedOnDate(), loanProduct);
validateSubmittedOnDate(element, loan.getSubmittedOnDate(), loan.getExpectedDisbursementDate(), loanProduct);
validateClientOrGroup(client, group, productId);

// validate if disbursement date is a holiday or a non-working day
Expand Down Expand Up @@ -1759,16 +1762,16 @@ private void validateTransactionProcessingStrategy(final String transactionProce
"Loan transaction processing strategy cannot be Advanced Payment Allocation Strategy if it's not configured on loan product");
} else {
// PROGRESSIVE: Repayment strategy MUST be only "advanced payment allocation"
if (loanProduct.getLoanProductRelatedDetail().getLoanScheduleType().equals(LoanScheduleType.PROGRESSIVE)) {
if (!transactionProcessingStrategy.equals(LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)) {
if (LoanScheduleType.PROGRESSIVE.equals(loanProduct.getLoanProductRelatedDetail().getLoanScheduleType())) {
if (!LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY.equals(transactionProcessingStrategy)) {
// TODO: GeneralPlatformDomainRuleException vs PlatformApiDataValidationException
throw new GeneralPlatformDomainRuleException(
"error.msg.loan.repayment.strategy.can.not.be.different.than.advanced.payment.allocation",
"Loan repayment strategy can not be different than Advanced Payment Allocation");
}
// CUMULATIVE: Repayment strategy CANNOT be "advanced payment allocation"
} else if (loanProduct.getLoanProductRelatedDetail().getLoanScheduleType().equals(LoanScheduleType.CUMULATIVE)) {
if (transactionProcessingStrategy.equals(LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)) {
} else if (LoanScheduleType.CUMULATIVE.equals(loanProduct.getLoanProductRelatedDetail().getLoanScheduleType())) {
if (LoanProductConstants.ADVANCED_PAYMENT_ALLOCATION_STRATEGY.equals(transactionProcessingStrategy)) {
// TODO: GeneralPlatformDomainRuleException vs PlatformApiDataValidationException
throw new GeneralPlatformDomainRuleException(
"error.msg.loan.repayment.strategy.can.not.be.equal.to.advanced.payment.allocation",
Expand Down Expand Up @@ -1813,7 +1816,8 @@ private void checkForProductMixRestrictions(final List<Long> activeLoansLoanProd
}
}

private void validateSubmittedOnDate(final JsonElement element, LocalDate originalSubmittedOnDate, LoanProduct loanProduct) {
private void validateSubmittedOnDate(final JsonElement element, LocalDate originalSubmittedOnDate,
LocalDate originalExpectedDisbursementDate, LoanProduct loanProduct) {
final LocalDate startDate = loanProduct.getStartDate();
final LocalDate closeDate = loanProduct.getCloseDate();
final LocalDate submittedOnDate = this.fromApiJsonHelper.parameterExists(LoanApiConstants.submittedOnDateParameterName, element)
Expand All @@ -1822,7 +1826,9 @@ private void validateSubmittedOnDate(final JsonElement element, LocalDate origin
final Long clientId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.clientIdParameterName, element);
final Long groupId = this.fromApiJsonHelper.extractLongNamed(LoanApiConstants.groupIdParameterName, element);
final LocalDate expectedDisbursementDate = this.fromApiJsonHelper
.extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element);
.parameterExists(LoanApiConstants.expectedDisbursementDateParameterName, element)
? this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.expectedDisbursementDateParameterName, element)
: originalExpectedDisbursementDate;

String defaultUserMessage = "";
if (DateUtils.isBefore(submittedOnDate, startDate)) {
Expand Down Expand Up @@ -1975,7 +1981,7 @@ public void validateApproval(JsonCommand command, Long loanId) {
expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate();
}

if (DateUtils.isBefore(approvedOnDate, loan.getSubmittedOnDate())) {
if (approvedOnDate != null && DateUtils.isBefore(approvedOnDate, loan.getSubmittedOnDate())) {
final String errorMessage = "Loan approval date " + approvedOnDate + " can not be before its submittal date: "
+ loan.getSubmittedOnDate();
throw new InvalidLoanStateTransitionException("approval", "cannot.be.before.submittal.date", errorMessage, approvedOnDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* 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 com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
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.Collections;
import net.minidev.json.JSONArray;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.accounting.Account;
import org.apache.fineract.integrationtests.common.accounting.AccountHelper;
import org.apache.fineract.integrationtests.common.loans.LoanApplicationTestBuilder;
import org.apache.fineract.integrationtests.common.loans.LoanProductTestBuilder;
import org.apache.fineract.integrationtests.common.loans.LoanTestLifecycleExtension;
import org.apache.fineract.integrationtests.common.loans.LoanTransactionHelper;
import org.apache.fineract.integrationtests.common.organisation.StaffHelper;
import org.apache.fineract.integrationtests.useradministration.users.UserHelper;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ExtendWith(LoanTestLifecycleExtension.class)
public class LoanValidationIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(LoanValidationIntegrationTest.class);

private RequestSpecification requestSpec;
private ResponseSpecification responseSpec;
private LoanTransactionHelper loanTransactionHelper;
private AccountHelper accountHelper;

@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);
this.accountHelper = new AccountHelper(this.requestSpec, this.responseSpec);
}

@Test
public void checkPrincipalErrors() {
final Integer staffId = StaffHelper.createStaff(this.requestSpec, this.responseSpec);
String username = Utils.uniqueRandomStringGenerator("user", 8);
UserHelper.createUser(this.requestSpec, this.responseSpec, 1, staffId, username, "P4ssw0rd", "resourceId");

LOG.info("-------------------------Creating Client---------------------------");
final Integer clientID = ClientHelper.createClient(requestSpec, responseSpec);
ClientHelper.verifyClientCreatedOnServer(requestSpec, responseSpec, clientID);

LOG.info("-------------------------Creating Loan---------------------------");
final Account assetAccount = this.accountHelper.createAssetAccount();
final Account incomeAccount = this.accountHelper.createIncomeAccount();
final Account expenseAccount = this.accountHelper.createExpenseAccount();
final Account overpaymentAccount = this.accountHelper.createLiabilityAccount();

LOG.info("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------");
final String loanProductJSON = new LoanProductTestBuilder() //
.withPrincipal("10000000.00") //
.withNumberOfRepayments("24") //
.withRepaymentAfterEvery("1") //
.withRepaymentTypeAsMonth() //
.withinterestRatePerPeriod("2") //
.withInterestRateFrequencyTypeAsMonths() //
.withRepaymentStrategy(LoanProductTestBuilder.DEFAULT_STRATEGY) //
.withAmortizationTypeAsEqualPrincipalPayment() //
.withInterestTypeAsDecliningBalance() //
.currencyDetails("0", "0")
.withAccounting("2", new Account[] { assetAccount, incomeAccount, expenseAccount, overpaymentAccount }).build(null);
final Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJSON);

LOG.info("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------");
final String loanApplicationJSON = new LoanApplicationTestBuilder() //
.withPrincipal("-1") //
.withLoanTermFrequency("6") //
.withLoanTermFrequencyAsMonths() //
.withNumberOfRepayments("6") //
.withRepaymentEveryAfter("1") //
.withRepaymentFrequencyTypeAsMonths() //
.withInterestRatePerPeriod("2") //
.withAmortizationTypeAsEqualInstallments() //
.withInterestTypeAsFlatBalance() //
.withInterestCalculationPeriodTypeSameAsRepaymentPeriod() //
.withExpectedDisbursementDate("12 July 2022") //
.withSubmittedOnDate("10 July 2022") //
.withRepaymentStrategy(LoanApplicationTestBuilder.DEFAULT_STRATEGY) //
.withCharges(Collections.emptyList()) //
.build(clientID.toString(), loanProductID.toString(), null);

ResponseSpecification failedResponseSpec = new ResponseSpecBuilder().expectStatusCode(400).expectBody(new BaseMatcher<String>() {

@Override
public boolean matches(Object body) {
DocumentContext json = JsonPath.parse(body.toString());
LOG.error(body.toString());
JSONArray errors = json.read("$.errors[*].developerMessage");
LOG.info("errors: {}", errors);
return errors.size() == 1;
}

@Override
public void describeTo(Description description) {

}
}).build();
final Integer loanID = this.loanTransactionHelper.getLoanId(loanApplicationJSON, requestSpec, failedResponseSpec);
}
}

0 comments on commit 23b5099

Please sign in to comment.