diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java index 2405288a8d2..78cb2c94152 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationValidator.java @@ -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() @@ -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."); @@ -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 @@ -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(); @@ -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 @@ -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); + } }); } @@ -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); } @@ -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."); @@ -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 @@ -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", @@ -1813,7 +1816,8 @@ private void checkForProductMixRestrictions(final List 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) @@ -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)) { @@ -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, diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java new file mode 100644 index 00000000000..abbc9279a60 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanValidationIntegrationTest.java @@ -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() { + + @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); + } +}