Loan externalId unique validation

This commit is contained in:
Jose Alberto Hernandez
2022-07-04 15:55:05 +02:00
committed by Arnold Gálovics
parent 2d07e8c867
commit 357062a186
6 changed files with 74 additions and 2 deletions
@@ -161,4 +161,6 @@ public interface LoanRepository extends JpaRepository<Loan, Long>, JpaSpecificat
@Query(FIND_BY_ACCOUNT_NUMBER)
Loan findLoanAccountByAccountNumber(@Param("accountNumber") String accountNumber);
boolean existsByExternalId(@Param("externalId") String externalId);
}
@@ -240,6 +240,10 @@ public class LoanRepositoryWrapper {
return this.repository.findNonClosedLoanByAccountNumber(accountNumber);
}
public boolean existLoanByExternalId(final String externalId) {
return this.repository.existsByExternalId(externalId);
}
// Looks like we need complete entity
@Transactional(readOnly = true)
public Loan findNonClosedLoanThatBelongsToClient(@Param("loanId") Long loanId, @Param("clientId") Long clientId) {
@@ -303,6 +303,16 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
this.fromApiJsonDeserializer.validateForCreate(command.json(), isMeetingMandatoryForJLGLoans, loanProduct);
// Validate If the externalId is already registered
final String externalId = this.fromJsonHelper.extractStringNamed("externalId", command.parsedJson());
if (StringUtils.isNotBlank(externalId)) {
final boolean existByExternalId = this.loanRepositoryWrapper.existLoanByExternalId(externalId);
if (existByExternalId) {
throw new GeneralPlatformDomainRuleException("error.msg.loan.with.externalId.already.used",
"Loan with externalId is already registered.");
}
}
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("loan");
@@ -19,6 +19,7 @@
package org.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@@ -125,6 +126,26 @@ public class ClientLoanIntegrationTest {
verifyLoanRepaymentSchedule(loanSchedule);
}
@Test
public void validateClientLoanWithUniqueExternalId() {
// Given
this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec);
final ResponseSpecification responseSpec403 = new ResponseSpecBuilder().expectStatusCode(403).build();
final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec);
final Integer loanProductID = createLoanProduct(false, NONE);
final String externalId = Utils.randomStringGenerator("qwerty", 10);
// When
final Integer loanID = applyForLoanApplicationWithExternalId(this.requestSpec, this.responseSpec, clientID, loanProductID,
"12,000.00", externalId);
// Then
assertNotNull(loanID);
applyForLoanApplicationWithExternalId(this.requestSpec, responseSpec403, clientID, loanProductID, "12,000.00", externalId);
}
@Test
public void testLoanCharges_DISBURSEMENT_FEE() {
this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec);
@@ -1155,6 +1176,27 @@ public class ClientLoanIntegrationTest {
return this.loanTransactionHelper.getLoanId(loanApplicationJSON);
}
private Integer applyForLoanApplicationWithExternalId(RequestSpecification requestSpec, ResponseSpecification responseSpec,
final Integer clientID, final Integer loanProductID, String principal, final String externalId) {
LOG.info("------------------------APPLYING FOR LOAN APPLICATION WITH EXTERNALID------------------------");
final String loanApplicationJSON = new LoanApplicationTestBuilder() //
.withPrincipal(principal) //
.withExternalId(externalId) //
.withLoanTermFrequency("4") //
.withLoanTermFrequencyAsMonths() //
.withNumberOfRepayments("4") //
.withRepaymentEveryAfter("1") //
.withRepaymentFrequencyTypeAsMonths() //
.withInterestRatePerPeriod("2") //
.withAmortizationTypeAsEqualInstallments() //
.withInterestTypeAsDecliningBalance() //
.withInterestCalculationPeriodTypeSameAsRepaymentPeriod() //
.withExpectedDisbursementDate("20 September 2011") //
.withSubmittedOnDate("20 September 2011") //
.build(clientID.toString(), loanProductID.toString(), null);
return this.loanTransactionHelper.getLoanId(loanApplicationJSON, requestSpec, responseSpec);
}
private Integer applyForLoanApplicationWithTranches(final Integer clientID, final Integer loanProductID, List<HashMap> charges,
final String savingsId, String principal, List<HashMap> tranches, List<HashMap> collaterals) {
LOG.info("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------");
@@ -43,6 +43,7 @@ public class LoanApplicationTestBuilder {
public static final String RBI_INDIA_STRATEGY = "4";
public static final String INTEREST_PRINCIPAL_PENALTIES_FEES_ORDER_STRATEGY = "6";
private String externalId = null;
private String principal = "10,000";
private String glimPrincipal = "1000";
private String loanTermFrequency = "";
@@ -124,7 +125,7 @@ public class LoanApplicationTestBuilder {
if (this.glimPrincipal != null) {
map.put("glimPrincipal", this.glimPrincipal);
}
map.put("locale", "en_GB");
map.put("locale", LOCALE);
String approvalFormData = new Gson().toJson(map);
LOG.info("approvalFormData: {} ", approvalFormData);
@@ -153,6 +154,10 @@ public class LoanApplicationTestBuilder {
map.put("collateral", this.collaterals);
map.put("interestChargedFromDate", this.interestChargedFromDate);
if (this.externalId != null) {
map.put("externalId", this.externalId);
}
if (repaymentsStartingFromDate != null) {
map.put("repaymentsStartingFromDate", this.repaymentsStartingFromDate);
}
@@ -185,6 +190,11 @@ public class LoanApplicationTestBuilder {
return new Gson().toJson(map);
}
public LoanApplicationTestBuilder withExternalId(final String externalId) {
this.externalId = externalId;
return this;
}
public LoanApplicationTestBuilder withPrincipal(final String principalAmount) {
this.principal = principalAmount;
return this;
@@ -81,7 +81,11 @@ public class LoanTransactionHelper {
}
public Integer getLoanId(final String loanApplicationJSON) {
return Utils.performServerPost(this.requestSpec, this.responseSpec, APPLY_LOAN_URL, loanApplicationJSON, "loanId");
return this.getLoanId(loanApplicationJSON, this.requestSpec, this.responseSpec);
}
public Integer getLoanId(final String loanApplicationJSON, RequestSpecification requestSpec, ResponseSpecification responseSpec) {
return Utils.performServerPost(requestSpec, responseSpec, APPLY_LOAN_URL, loanApplicationJSON, "loanId");
}
public HashMap<String, Integer> getGlimId(final String loanApplicationJSON) {