FINERACT-2354: Reversal of re-aging transaction - remove restriction of reversal if there was payment after or on that day

This commit is contained in:
Peter Kovacs
2025-11-07 10:23:26 +01:00
committed by Adam Saghy
parent 9320dd1bd7
commit 1b2801447b
4 changed files with 663 additions and 54 deletions
@@ -24,7 +24,13 @@ import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ResponseBody;
import org.apache.fineract.client.models.PostLoansLoanIdTransactionsRequest;
@@ -57,11 +63,20 @@ public class LoanReAgingStepDef extends AbstractStepDef {
Response<PostLoansResponse> loanResponse = testContext().get(TestContextKey.LOAN_CREATE_RESPONSE);
long loanId = loanResponse.body().getLoanId();
List<String> data = table.asLists().get(1);
int frequencyNumber = Integer.parseInt(data.get(0));
String frequencyType = data.get(1);
String startDate = data.get(2);
int numberOfInstallments = Integer.parseInt(data.get(3));
List<List<String>> tableRows = table.asLists();
List<String> headers = tableRows.get(0);
List<String> values = tableRows.get(1);
Map<String, String> rowData = new LinkedHashMap<>();
int columnCount = Math.min(headers.size(), values.size());
for (int i = 0; i < columnCount; i++) {
rowData.put(headers.get(i), values.get(i));
}
int frequencyNumber = Integer.parseInt(resolveValue(rowData, values, 0, "frequencyNumber"));
String frequencyType = resolveValue(rowData, values, 1, "frequencyType");
String startDate = resolveValue(rowData, values, 2, "startDate");
int numberOfInstallments = Integer.parseInt(resolveValue(rowData, values, 3, "numberOfInstallments"));
PostLoansLoanIdTransactionsRequest reAgingRequest = LoanRequestFactory//
.defaultReAgingRequest()//
@@ -70,12 +85,102 @@ public class LoanReAgingStepDef extends AbstractStepDef {
.startDate(startDate)//
.numberOfInstallments(numberOfInstallments);//
applyAdditionalFields(reAgingRequest, rowData, Set.of("frequencyNumber", "frequencyType", "startDate", "numberOfInstallments"));
Response<PostLoansLoanIdTransactionsResponse> response = loanTransactionsApi.executeLoanTransaction(loanId, reAgingRequest, "reAge")
.execute();
ErrorHelper.checkSuccessfulApiCall(response);
testContext().set(TestContextKey.LOAN_REAGING_RESPONSE, response);
}
private void applyAdditionalFields(PostLoansLoanIdTransactionsRequest request, Map<String, String> rowData, Set<String> excludedKeys) {
rowData.forEach((key, value) -> {
if (!excludedKeys.contains(key)) {
setRequestField(request, key, value);
}
});
}
private void setRequestField(PostLoansLoanIdTransactionsRequest request, String fieldName, String rawValue) {
if (fieldName == null || fieldName.isBlank()) {
return;
}
try {
Method targetMethod = Arrays.stream(PostLoansLoanIdTransactionsRequest.class.getMethods())
.filter(method -> method.getParameterCount() == 1 && method.getName().equals(fieldName)).findFirst().orElse(null);
if (targetMethod == null) {
log.warn("No setter method found on PostLoansLoanIdTransactionsRequest for field {}", fieldName);
return;
}
Class<?> parameterType = targetMethod.getParameterTypes()[0];
Object convertedValue = convertValue(rawValue, parameterType);
if (convertedValue == null && parameterType.isPrimitive()) {
log.warn("Cannot assign null to primitive field {} on PostLoansLoanIdTransactionsRequest", fieldName);
return;
}
targetMethod.invoke(request, convertedValue);
} catch (Exception ex) {
log.warn("Failed to set additional field {} on PostLoansLoanIdTransactionsRequest", fieldName, ex);
}
}
private Object convertValue(String rawValue, Class<?> targetType) {
if (rawValue == null || rawValue.isBlank()) {
return null;
}
try {
if (String.class.equals(targetType)) {
return rawValue;
}
if (Integer.class.equals(targetType) || int.class.equals(targetType)) {
return Integer.valueOf(rawValue);
}
if (Long.class.equals(targetType) || long.class.equals(targetType)) {
return Long.valueOf(rawValue);
}
if (Double.class.equals(targetType) || double.class.equals(targetType)) {
return Double.valueOf(rawValue);
}
if (Float.class.equals(targetType) || float.class.equals(targetType)) {
return Float.valueOf(rawValue);
}
if (Short.class.equals(targetType) || short.class.equals(targetType)) {
return Short.valueOf(rawValue);
}
if (Byte.class.equals(targetType) || byte.class.equals(targetType)) {
return Byte.valueOf(rawValue);
}
if (Boolean.class.equals(targetType) || boolean.class.equals(targetType)) {
return Boolean.parseBoolean(rawValue);
}
if (BigDecimal.class.equals(targetType)) {
return new BigDecimal(rawValue);
}
} catch (NumberFormatException ex) {
log.warn("Unable to convert value '{}' to type {}. Falling back to raw string.", rawValue, targetType.getSimpleName(), ex);
return rawValue;
}
return rawValue;
}
private String resolveValue(Map<String, String> rowData, List<String> values, int index, String key) {
String value = rowData.get(key);
if (value != null) {
return value;
}
if (index >= 0 && index < values.size()) {
return values.get(index);
}
return null;
}
@When("Admin creates a Loan re-aging transaction by Loan external ID with the following data:")
public void createReAgingTransactionByLoanExternalId(DataTable table) throws IOException {
Response<PostLoansResponse> loanResponse = testContext().get(TestContextKey.LOAN_CREATE_RESPONSE);