Add: Integration Test for Undoing Waive Charge

This commit is contained in:
BLasan
2021-09-12 21:35:56 +08:00
committed by Petri Tuomola
parent a23844b621
commit eade79f73c
4 changed files with 83 additions and 4 deletions
@@ -214,7 +214,8 @@ final class LoanTransactionsApiResourceSwagger {
private PutChargeTransactionChangesRequest() {}
@Schema(example = "1")
public Integer transactionId;
public Integer id;
@Schema(example = "2")
public Integer loanId;
}
}
@@ -1820,7 +1820,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withEntityId(command.entityId()) //
.withEntityId(loanChargeId) //
.withLoanId(loanId) //
.with(changes).build();
}
@@ -20,7 +20,9 @@ package org.apache.fineract.integrationtests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
@@ -701,7 +703,62 @@ public class ClientLoanIntegrationTest {
validateChargeExcludePrecission(flat, loanCharges, "50.0", "200", "0.0", "0.0");
Integer waivePeriodnum = 1;
this.loanTransactionHelper.waiveChargesForLoan(loanID, (Integer) getloanCharge(flat, loanCharges).get("id"),
final Integer waivedChargeId = this.loanTransactionHelper.waiveChargesForLoan(loanID,
(Integer) getloanCharge(flat, loanCharges).get("id"),
LoanTransactionHelper.getWaiveChargeJSON(String.valueOf(waivePeriodnum)));
// Get loan transaction details
ArrayList<HashMap> loanDetails = this.loanTransactionHelper.getLoanTransactionDetails(this.requestSpec, this.responseSpec, loanID);
Assertions.assertNotNull(loanDetails, "Empty Loan Details");
Gson gson = new Gson();
Integer transId = null;
Integer chargeId = null;
for (int i = 0; i < loanDetails.size(); i++) {
String resultObject = gson.toJson(loanDetails.get(i));
JsonObject reportObject = JsonParser.parseString(resultObject).getAsJsonObject();
JsonObject type = reportObject.getAsJsonObject("type");
final Integer transTypeId = type.get("id").getAsInt();
Assertions.assertNotNull(transTypeId);
if (Integer.valueOf(9).compareTo(transTypeId) == 0) {
transId = reportObject.get("id").getAsInt();
Assertions.assertNotNull(transId);
final HashMap<String, String> map = new HashMap<>();
map.put("id", transId.toString());
map.put("loanId", loanID.toString());
final String putBody = gson.toJson(map);
chargeId = this.loanTransactionHelper.undoWaiveChargesForLoan(loanID, transId, putBody);
break;
}
}
Assertions.assertEquals(waivedChargeId, chargeId);
// Validate the undo process
ArrayList<HashMap> loanTransactionDetails = this.loanTransactionHelper.getLoanTransactionDetails(this.requestSpec,
this.responseSpec, loanID);
Assertions.assertNotNull(loanTransactionDetails, "Empty Loan Transaction Details");
for (int i = 0; i < loanTransactionDetails.size(); i++) {
String resultObject = gson.toJson(loanTransactionDetails.get(i));
JsonObject reportObject = JsonParser.parseString(resultObject).getAsJsonObject();
final Boolean isReversed = reportObject.get("manuallyReversed").getAsBoolean();
final Integer id = reportObject.get("id").getAsInt();
if (transId.compareTo(id) == 0) {
final HashMap chargeDetails = this.loanTransactionHelper.getLoanCharge(loanID, waivedChargeId);
String resultChargeObject = gson.toJson(chargeDetails);
JsonObject reportChargeObject = JsonParser.parseString(resultChargeObject).getAsJsonObject();
BigDecimal waiveAmount = reportChargeObject.get("amountWaived").getAsBigDecimal();
Assertions.assertEquals(true, isReversed);
Assertions.assertEquals(Double.valueOf(0), waiveAmount.doubleValue());
break;
} else if (transId.compareTo(id) != 0 && i == loanTransactionDetails.size() - 1) {
Assertions.assertEquals(transId, id);
}
}
// Re-waive charge
this.loanTransactionHelper.waiveChargesForLoan(loanID, waivedChargeId,
LoanTransactionHelper.getWaiveChargeJSON(String.valueOf(waivePeriodnum)));
loanCharges = this.loanTransactionHelper.getLoanCharges(loanID);
loanSchedule = this.loanTransactionHelper.getLoanRepaymentSchedule(this.requestSpec, this.responseSpec, loanID);
@@ -338,6 +338,14 @@ public class LoanTransactionHelper {
return (Integer) response.get("resourceId");
}
public Integer undoWaiveChargesForLoan(final Integer loanId, final Integer transactionId, final String body) {
LOG.info("--------------------------------- UNDO WAIVE CHARGES FOR LOAN --------------------------------");
final String TRANSAC_URL = "/fineract-provider/api/v1/loans/" + loanId + "/transactions/" + transactionId + "?"
+ Utils.TENANT_IDENTIFIER;
final HashMap response = Utils.performServerPut(requestSpec, responseSpec, TRANSAC_URL, body, "");
return (Integer) response.get("resourceId");
}
public Integer payChargesForLoan(final Integer loanId, final Integer loanchargeId, final String json) {
LOG.info("--------------------------------- WAIVE CHARGES FOR LOAN --------------------------------");
final String CHARGES_URL = "/fineract-provider/api/v1/loans/" + loanId + "/charges/" + loanchargeId + "?command=pay&"
@@ -346,6 +354,19 @@ public class LoanTransactionHelper {
return (Integer) response.get("resourceId");
}
public ArrayList<HashMap> getLoanTransactionDetails(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final Integer loanID) {
final String URL = "/fineract-provider/api/v1/loans/" + loanID + "?associations=all&exclude=guarantors,futureSchedule&"
+ Utils.TENANT_IDENTIFIER;
return Utils.performServerGet(requestSpec, responseSpec, URL, "transactions");
}
public HashMap getLoanCharge(final Integer loanId, final Integer chargeId) {
final String GET_LOAN_CHARGES_URL = "/fineract-provider/api/v1/loans/" + loanId + "/charges/" + chargeId + "?"
+ Utils.TENANT_IDENTIFIER;
return Utils.performServerGet(requestSpec, responseSpec, GET_LOAN_CHARGES_URL, "");
}
private String getDisburseLoanAsJSON(final String actualDisbursementDate, final String transactionAmount) {
final HashMap<String, String> map = new HashMap<>();
map.put("locale", "en");