Skip to content

Commit c0859f4

Browse files
a7med3del1973galovics
authored andcommitted
FINERACT-2182: Group creation fails due to NullPointerException when random-account-number=enabled
1 parent a30a34b commit c0859f4

2 files changed

Lines changed: 109 additions & 17 deletions

File tree

fineract-provider/src/main/java/org/apache/fineract/portfolio/account/service/AccountNumberGenerator.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,28 +193,42 @@ private String randomNumberGenerator(int accountMaxLength, Map<String, String> p
193193
return accountNumber;
194194
}
195195

196-
private Boolean checkAccountNumberConflict(Map<String, String> propertyMap, AccountNumberFormat accountNumberFormat,
196+
public Boolean checkAccountNumberConflict(Map<String, String> propertyMap, AccountNumberFormat accountNumberFormat,
197197
String accountNumber) {
198198

199199
String entityType = propertyMap.get(ENTITY_TYPE);
200-
Boolean randomNumberConflict = false;
201-
if (entityType.equals("client")) { // avoid duplication it will loop until it finds new random account no.
200+
if (entityType == null) { // No entityType in map -> cannot check for conflicts.
201+
return false;
202+
}
202203

203-
Client client = this.clientRepository.getClientByAccountNumber(accountNumber);
204-
if (client != null) {
205-
randomNumberConflict = true;
206-
}
207-
} else if (entityType.equals("loan")) {
208-
Loan loan = this.loanRepository.findLoanAccountByAccountNumber(accountNumber);
209-
if (loan != null) {
210-
randomNumberConflict = true;
211-
}
212-
} else if (entityType.equals("savingsAccount")) {
213-
SavingsAccount savingsAccount = this.savingsAccountRepository.findSavingsAccountByAccountNumber(accountNumber);
214-
if (savingsAccount != null) {
215-
randomNumberConflict = true;
216-
}
204+
boolean randomNumberConflict = false;
205+
206+
switch (entityType) {
207+
case "client": // avoid duplication it will loop until it finds new random account no.
208+
Client client = this.clientRepository.getClientByAccountNumber(accountNumber);
209+
if (client != null) {
210+
randomNumberConflict = true;
211+
}
212+
break;
213+
214+
case "loan":
215+
Loan loan = this.loanRepository.findLoanAccountByAccountNumber(accountNumber);
216+
if (loan != null) {
217+
randomNumberConflict = true;
218+
}
219+
break;
220+
221+
case "savingsAccount":
222+
SavingsAccount savingsAccount = this.savingsAccountRepository.findSavingsAccountByAccountNumber(accountNumber);
223+
if (savingsAccount != null) {
224+
randomNumberConflict = true;
225+
}
226+
break;
227+
228+
default:
229+
break;
217230
}
231+
218232
return randomNumberConflict;
219233
}
220234

fineract-provider/src/test/java/org/apache/fineract/portfolio/account/jobs/executestandinginstructions/AccountNumberGeneratorTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.mockito.Mockito.mock;
2323
import static org.mockito.Mockito.when;
2424

25+
import java.util.HashMap;
26+
import java.util.Map;
2527
import org.apache.fineract.infrastructure.accountnumberformat.domain.AccountNumberFormat;
2628
import org.apache.fineract.infrastructure.codes.domain.CodeValue;
2729
import org.apache.fineract.infrastructure.configuration.api.GlobalConfigurationConstants;
@@ -142,4 +144,80 @@ public void testGenerateShareAccountNumber() {
142144
String accountNumber = generator.generate(share, format);
143145
assertThat(accountNumber).isEqualTo("000000321");
144146
}
147+
148+
@Test
149+
public void testCheckAccountNumberConflict_nullEntityType_returnsFalse() {
150+
Map<String, String> props = new HashMap<>();
151+
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
152+
assertThat(result).isFalse();
153+
}
154+
155+
@Test
156+
public void testCheckAccountNumberConflict_clientNoConflict() {
157+
Map<String, String> props = new HashMap<>();
158+
props.put("entityType", "client");
159+
when(clientRepo.getClientByAccountNumber("12345")).thenReturn(null);
160+
161+
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
162+
assertThat(result).isFalse();
163+
}
164+
165+
@Test
166+
public void testCheckAccountNumberConflict_clientWithConflict() {
167+
Map<String, String> props = new HashMap<>();
168+
props.put("entityType", "client");
169+
when(clientRepo.getClientByAccountNumber("12345")).thenReturn(mock(Client.class));
170+
171+
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
172+
assertThat(result).isTrue();
173+
}
174+
175+
@Test
176+
public void testCheckAccountNumberConflict_loanNoConflict() {
177+
Map<String, String> props = new HashMap<>();
178+
props.put("entityType", "loan");
179+
when(loanRepo.findLoanAccountByAccountNumber("77777")).thenReturn(null);
180+
181+
boolean result = generator.checkAccountNumberConflict(props, null, "77777");
182+
assertThat(result).isFalse();
183+
}
184+
185+
@Test
186+
public void testCheckAccountNumberConflict_loanWithConflict() {
187+
Map<String, String> props = new HashMap<>();
188+
props.put("entityType", "loan");
189+
when(loanRepo.findLoanAccountByAccountNumber("77777")).thenReturn(mock(Loan.class));
190+
191+
boolean result = generator.checkAccountNumberConflict(props, null, "77777");
192+
assertThat(result).isTrue();
193+
}
194+
195+
@Test
196+
public void testCheckAccountNumberConflict_savingsNoConflict() {
197+
Map<String, String> props = new HashMap<>();
198+
props.put("entityType", "savingsAccount");
199+
when(savingsRepo.findSavingsAccountByAccountNumber("55555")).thenReturn(null);
200+
201+
boolean result = generator.checkAccountNumberConflict(props, null, "55555");
202+
assertThat(result).isFalse();
203+
}
204+
205+
@Test
206+
public void testCheckAccountNumberConflict_savingsWithConflict() {
207+
Map<String, String> props = new HashMap<>();
208+
props.put("entityType", "savingsAccount");
209+
when(savingsRepo.findSavingsAccountByAccountNumber("55555")).thenReturn(mock(SavingsAccount.class));
210+
211+
boolean result = generator.checkAccountNumberConflict(props, null, "55555");
212+
assertThat(result).isTrue();
213+
}
214+
215+
@Test
216+
public void testCheckAccountNumberConflict_unknownEntityType_returnsFalse() {
217+
Map<String, String> props = new HashMap<>();
218+
props.put("entityType", "foobar");
219+
220+
boolean result = generator.checkAccountNumberConflict(props, null, "12345");
221+
assertThat(result).isFalse();
222+
}
145223
}

0 commit comments

Comments
 (0)