FINERACT-2182: Group creation fails due to NullPointerException when random-account-number=enabled

This commit is contained in:
a7med3del1973
2025-07-18 17:51:10 +02:00
committed by Arnold Gálovics
parent a30a34bea8
commit c0859f4fda
2 changed files with 110 additions and 18 deletions
@@ -193,28 +193,42 @@ public class AccountNumberGenerator {
return accountNumber;
}
private Boolean checkAccountNumberConflict(Map<String, String> propertyMap, AccountNumberFormat accountNumberFormat,
public Boolean checkAccountNumberConflict(Map<String, String> propertyMap, AccountNumberFormat accountNumberFormat,
String accountNumber) {
String entityType = propertyMap.get(ENTITY_TYPE);
Boolean randomNumberConflict = false;
if (entityType.equals("client")) { // avoid duplication it will loop until it finds new random account no.
Client client = this.clientRepository.getClientByAccountNumber(accountNumber);
if (client != null) {
randomNumberConflict = true;
}
} else if (entityType.equals("loan")) {
Loan loan = this.loanRepository.findLoanAccountByAccountNumber(accountNumber);
if (loan != null) {
randomNumberConflict = true;
}
} else if (entityType.equals("savingsAccount")) {
SavingsAccount savingsAccount = this.savingsAccountRepository.findSavingsAccountByAccountNumber(accountNumber);
if (savingsAccount != null) {
randomNumberConflict = true;
}
if (entityType == null) { // No entityType in map -> cannot check for conflicts.
return false;
}
boolean randomNumberConflict = false;
switch (entityType) {
case "client": // avoid duplication it will loop until it finds new random account no.
Client client = this.clientRepository.getClientByAccountNumber(accountNumber);
if (client != null) {
randomNumberConflict = true;
}
break;
case "loan":
Loan loan = this.loanRepository.findLoanAccountByAccountNumber(accountNumber);
if (loan != null) {
randomNumberConflict = true;
}
break;
case "savingsAccount":
SavingsAccount savingsAccount = this.savingsAccountRepository.findSavingsAccountByAccountNumber(accountNumber);
if (savingsAccount != null) {
randomNumberConflict = true;
}
break;
default:
break;
}
return randomNumberConflict;
}
@@ -22,6 +22,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import org.apache.fineract.infrastructure.accountnumberformat.domain.AccountNumberFormat;
import org.apache.fineract.infrastructure.codes.domain.CodeValue;
import org.apache.fineract.infrastructure.configuration.api.GlobalConfigurationConstants;
@@ -142,4 +144,80 @@ public class AccountNumberGeneratorTest {
String accountNumber = generator.generate(share, format);
assertThat(accountNumber).isEqualTo("000000321");
}
@Test
public void testCheckAccountNumberConflict_nullEntityType_returnsFalse() {
Map<String, String> props = new HashMap<>();
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
assertThat(result).isFalse();
}
@Test
public void testCheckAccountNumberConflict_clientNoConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "client");
when(clientRepo.getClientByAccountNumber("12345")).thenReturn(null);
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
assertThat(result).isFalse();
}
@Test
public void testCheckAccountNumberConflict_clientWithConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "client");
when(clientRepo.getClientByAccountNumber("12345")).thenReturn(mock(Client.class));
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
assertThat(result).isTrue();
}
@Test
public void testCheckAccountNumberConflict_loanNoConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "loan");
when(loanRepo.findLoanAccountByAccountNumber("77777")).thenReturn(null);
boolean result = generator.checkAccountNumberConflict(props, null, "77777");
assertThat(result).isFalse();
}
@Test
public void testCheckAccountNumberConflict_loanWithConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "loan");
when(loanRepo.findLoanAccountByAccountNumber("77777")).thenReturn(mock(Loan.class));
boolean result = generator.checkAccountNumberConflict(props, null, "77777");
assertThat(result).isTrue();
}
@Test
public void testCheckAccountNumberConflict_savingsNoConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "savingsAccount");
when(savingsRepo.findSavingsAccountByAccountNumber("55555")).thenReturn(null);
boolean result = generator.checkAccountNumberConflict(props, null, "55555");
assertThat(result).isFalse();
}
@Test
public void testCheckAccountNumberConflict_savingsWithConflict() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "savingsAccount");
when(savingsRepo.findSavingsAccountByAccountNumber("55555")).thenReturn(mock(SavingsAccount.class));
boolean result = generator.checkAccountNumberConflict(props, null, "55555");
assertThat(result).isTrue();
}
@Test
public void testCheckAccountNumberConflict_unknownEntityType_returnsFalse() {
Map<String, String> props = new HashMap<>();
props.put("entityType", "foobar");
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
assertThat(result).isFalse();
}
}