Skip to content

Commit

Permalink
FINERACT-2081: Retrieving Loan Product details with invalid external-…
Browse files Browse the repository at this point in the history
…id results in 500 error
  • Loading branch information
Jose Alberto Hernandez committed Nov 28, 2024
1 parent 32cc83f commit d73d303
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public LoanProductNotFoundException(final Long id) {
}

public LoanProductNotFoundException(final ExternalId externalId) {
super("error.msg.loanproduct.id.invalid", "Loan product with identifier " + externalId + " does not exist", externalId);
super("error.msg.loanproduct.id.invalid", "Loan product with identifier " + externalId.getValue() + " does not exist",
externalId.getValue());
}

public LoanProductNotFoundException(Long id, EmptyResultDataAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ public LoanProductData retrieveLoanProduct(final Long loanProductId) {

@Override
public LoanProduct retrieveLoanProductByExternalId(final ExternalId externalId) {
return loanProductRepository.findByExternalId(externalId);
final LoanProduct loanProduct = loanProductRepository.findByExternalId(externalId);
if (loanProduct == null) {
throw new LoanProductNotFoundException(externalId);
}
return loanProduct;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
Expand All @@ -31,6 +33,7 @@
import org.apache.fineract.client.models.GetLoanProductsProductIdResponse;
import org.apache.fineract.client.models.PutLoanProductsProductIdRequest;
import org.apache.fineract.client.models.PutLoanProductsProductIdResponse;
import org.apache.fineract.client.util.CallFailedRuntimeException;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.loans.LoanProductHelper;
import org.apache.fineract.integrationtests.common.loans.LoanProductTestBuilder;
Expand Down Expand Up @@ -73,4 +76,22 @@ public void testLoanProductWithExternalId() {
assertNotNull(putLoanProductsProductIdResponse.getResourceId());
assertEquals(loanProductId, putLoanProductsProductIdResponse.getResourceId().intValue());
}

@Test
public void testLoanProductWithInvalidExternalId() {
String externalId = UUID.randomUUID().toString();
HashMap<String, Object> request = new LoanProductTestBuilder().withExternalId(externalId).build(null, null);
Integer loanProductId = loanTransactionHelper.getLoanProductId(Utils.convertToJson(request));
assertNotNull(loanProductId);

GetLoanProductsProductIdResponse getLoanProductsProductIdResponse = loanProductHelper.retrieveLoanProductByExternalId(externalId);
assertNotNull(getLoanProductsProductIdResponse.getId());
assertEquals(loanProductId, getLoanProductsProductIdResponse.getId().intValue());

CallFailedRuntimeException exception = assertThrows(CallFailedRuntimeException.class,
() -> loanProductHelper.retrieveLoanProductByExternalId(externalId.substring(2)));
assertEquals(404, exception.getResponse().code());
assertTrue(exception.getMessage().contains("error.msg.loanproduct.id.invalid"));
}

}

0 comments on commit d73d303

Please sign in to comment.