diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanProductTestBuilder.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanProductTestBuilder.java index cf5063221..342db5e25 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanProductTestBuilder.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/loans/LoanProductTestBuilder.java @@ -105,6 +105,10 @@ public class LoanProductTestBuilder { private String graceOnInterestPayment = "1"; private JsonObject allowAttributeOverrides = null; private Boolean allowPartialPeriodInterestCalcualtion = false; + + private Boolean allowVariableInstallments = Boolean.FALSE; + private Integer minimumGap; + private Integer maximumGap; public String build(final String chargeId) { final HashMap map = new HashMap<>(); @@ -185,6 +189,11 @@ public class LoanProductTestBuilder { map.put("allowAttributeOverrides", this.allowAttributeOverrides); } map.put("allowPartialPeriodInterestCalcualtion", this.allowPartialPeriodInterestCalcualtion); + map.put("allowVariableInstallments", allowVariableInstallments) ; + if(allowVariableInstallments) { + map.put("minimumGap", minimumGap) ; + map.put("maximumGap", maximumGap) ; + } return new Gson().toJson(map); } @@ -461,4 +470,11 @@ public class LoanProductTestBuilder { this.allowAttributeOverrides = loanProductConfigurableAttributes; return this; } + + public LoanProductTestBuilder withVariableInstallmentsConfig(Boolean allowVariableInstallments, Integer minimumGap, Integer maximumGap) { + this.allowVariableInstallments = allowVariableInstallments ; + this.minimumGap = minimumGap; + this.maximumGap = maximumGap; + return this ; + } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsDecliningBalanceHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsDecliningBalanceHelper.java new file mode 100644 index 000000000..449225d52 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsDecliningBalanceHelper.java @@ -0,0 +1,298 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.integrationtests.variableinstallments; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.mifosplatform.integrationtests.common.accounting.Account; +import org.mifosplatform.integrationtests.common.loans.LoanApplicationTestBuilder; +import org.mifosplatform.integrationtests.common.loans.LoanProductTestBuilder; + +import com.google.gson.Gson; + +@SuppressWarnings({"rawtypes", "unchecked"}) +public class VariableInstallmentsDecliningBalanceHelper { + + public static String createLoanProductWithoutVaribleConfig(final boolean multiDisburseLoan, final String accountingRule, final Account... accounts) { + System.out.println("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + final String loanProductJSON = new LoanProductTestBuilder() // + .withPrincipal("1,00,000.00") // + .withNumberOfRepayments("4") // + .withRepaymentAfterEvery("1") // + .withRepaymentTypeAsMonth() // + .withinterestRatePerPeriod("1") // + .withInterestRateFrequencyTypeAsMonths() // + .withAmortizationTypeAsEqualInstallments() // + .withInterestTypeAsDecliningBalance() // + .withTranches(multiDisburseLoan) // + .withAccounting(accountingRule, accounts).build(null); + return loanProductJSON ; + } + + public static String createLoanProductWithVaribleConfig(final boolean multiDisburseLoan, final String accountingRule, final Account... accounts) { + System.out.println("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + final String loanProductJSON = new LoanProductTestBuilder() // + .withPrincipal("1,00,000.00") // + .withNumberOfRepayments("4") // + .withRepaymentAfterEvery("1") // + .withRepaymentTypeAsMonth() // + .withinterestRatePerPeriod("1") // + .withInterestRateFrequencyTypeAsMonths() // + .withAmortizationTypeAsEqualInstallments() // + .withInterestTypeAsDecliningBalance() // + .withTranches(multiDisburseLoan) // + .withInterestCalculationPeriodTypeAsRepaymentPeriod(true)// + .withVariableInstallmentsConfig(Boolean.TRUE, new Integer(5), new Integer(90))// + .withAccounting(accountingRule, accounts).build(null); + return loanProductJSON ; + } + + public static String createLoanProductWithVaribleConfigwithEqualPrincipal(final boolean multiDisburseLoan, final String accountingRule, final Account... accounts) { + System.out.println("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + final String loanProductJSON = new LoanProductTestBuilder() // + .withPrincipal("1,00,000.00") // + .withNumberOfRepayments("4") // + .withRepaymentAfterEvery("1") // + .withRepaymentTypeAsMonth() // + .withinterestRatePerPeriod("1") // + .withInterestRateFrequencyTypeAsMonths() // + .withAmortizationTypeAsEqualPrincipalPayment() // + .withInterestTypeAsDecliningBalance() // + .withTranches(multiDisburseLoan) // + .withInterestCalculationPeriodTypeAsRepaymentPeriod(true)// + .withVariableInstallmentsConfig(Boolean.TRUE, new Integer(5), new Integer(90))// + .withAccounting(accountingRule, accounts).build(null); + return loanProductJSON ; + } + + public static String applyForLoanApplication(final Integer clientID, final Integer loanProductID, List charges, + final String savingsId, String principal) { + System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + final String loanApplicationJSON = new LoanApplicationTestBuilder() // + .withPrincipal(principal) // + .withLoanTermFrequency("4") // + .withLoanTermFrequencyAsMonths() // + .withNumberOfRepayments("4") // + .withRepaymentEveryAfter("1") // + .withRepaymentFrequencyTypeAsMonths() // + .withInterestRatePerPeriod("2") // + .withAmortizationTypeAsEqualInstallments() // + .withInterestTypeAsDecliningBalance() // + .withInterestCalculationPeriodTypeSameAsRepaymentPeriod() // + .withExpectedDisbursementDate("20 September 2011") // + .withSubmittedOnDate("20 September 2011") // + .withCharges(charges).build(clientID.toString(), loanProductID.toString(), savingsId); + return loanApplicationJSON ; + } + + public static String applyForLoanApplicationWithEqualPrincipal(final Integer clientID, final Integer loanProductID, List charges, + final String savingsId, String principal) { + System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + final String loanApplicationJSON = new LoanApplicationTestBuilder() // + .withPrincipal(principal) // + .withLoanTermFrequency("4") // + .withLoanTermFrequencyAsMonths() // + .withNumberOfRepayments("4") // + .withRepaymentEveryAfter("1") // + .withRepaymentFrequencyTypeAsMonths() // + .withInterestRatePerPeriod("2") // + .withAmortizationTypeAsEqualPrincipalPayments() // + .withInterestTypeAsDecliningBalance() // + .withInterestCalculationPeriodTypeSameAsRepaymentPeriod() // + .withExpectedDisbursementDate("20 September 2011") // + .withSubmittedOnDate("20 September 2011") // + .withCharges(charges).build(clientID.toString(), loanProductID.toString(), savingsId); + return loanApplicationJSON ; + } + + public static String createDeleteVariations(ArrayList deletedInstallments) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("deletedinstallments", createDeletedMap(deletedInstallments)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createDeletedMap(ArrayList deletedItems) { + ArrayList toReturn = new ArrayList<>(); + for (Map map : deletedItems) { + ArrayList dueDate = (ArrayList) map.get("dueDate"); + Map tosend = new HashMap(); + tosend.put("dueDate", formatDate(dueDate)); + toReturn.add(tosend); + } + return toReturn; + } + + public static String createAddVariations() { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("newinstallments", createNewInstallments()); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createNewInstallments() { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", "31 October 2011"); + tosend.put("installmentAmount", "5000"); + toReturn.add(tosend); + return toReturn; + } + + public static String createModifiyVariations(Map firstSchedule) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createModifyMap(firstSchedule)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createNewInstallments(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("installmentAmount", "5000"); + toReturn.add(tosend); + return toReturn; + } + + private static ArrayList createModifyMap(Map firstSchedule) { + ArrayList toReturn = new ArrayList<>(); + ArrayList dueDate = (ArrayList) firstSchedule.get("dueDate"); + Map tosend = new HashMap(); + tosend.put("dueDate", formatDate(dueDate)); + tosend.put("installmentAmount", 30000) ; + toReturn.add(tosend); + return toReturn; + } + + public static String createAllVariations() { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createModifyMap("20 November 2011")); + exceptions.put("newinstallments", createNewInstallments("25 December 2011")); + exceptions.put("deletedinstallments", createDeletedMap("20 December 2011")); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + public static String createAllVariationsWithEqualPrincipal() { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createModifyMapWithPrinciapl("20 November 2011")); + exceptions.put("newinstallments", createNewInstallmentsWithPrincipal("25 December 2011")); + exceptions.put("deletedinstallments", createDeletedMap("20 December 2011")); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createNewInstallmentsWithPrincipal(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("principal", "5000"); + toReturn.add(tosend); + return toReturn; + } + + private static ArrayList createModifyMapWithPrinciapl(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("principal", 30000) ; + toReturn.add(tosend); + return toReturn; + } + private static ArrayList createDeletedMap(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + toReturn.add(tosend); + return toReturn; + } + + private static ArrayList createModifyMap(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("installmentAmount", 30000) ; + toReturn.add(tosend); + return toReturn; + } + + public static String createModifiyDateVariations(String[] date, String[] newdate, String[] principal) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createDateModifyMap(date, newdate, principal)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createDateModifyMap(String[] date, String[] newdate, String[] installments) { + ArrayList toReturn = new ArrayList<>(); + for(int i = 0 ; i < date.length; i++) { + Map tosend = new HashMap(); + tosend.put("dueDate", date[i]); + tosend.put("modifiedDueDate", newdate[i]) ; + if(i < installments.length) { + tosend.put("installmentAmount", installments[i]) ; + } + toReturn.add(tosend); + } + return toReturn; + } + public static String formatDate(ArrayList list) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, (int) list.get(0)); + cal.set(Calendar.MONTH, (int) list.get(1) - 1); + cal.set(Calendar.DAY_OF_MONTH, (int) list.get(2)); + Date date = cal.getTime(); + DateFormat requiredFormat = new SimpleDateFormat("dd MMMM YYYY"); + return requiredFormat.format(date); + } + + public Map createModifyVarations() { + return null; + } + + public static ArrayList constructVerifyData(String[] dates, String[] installments) { + ArrayList toReturn = new ArrayList<>(); + for (int i = 0; i < dates.length; i++) { + Map map = new HashMap<>(); + map.put("dueDate", dates[i]); + map.put("installmentAmount", installments[i]); + toReturn.add(map); + } + return toReturn; + } +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsFlatHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsFlatHelper.java new file mode 100644 index 000000000..3987a2b83 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsFlatHelper.java @@ -0,0 +1,217 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.integrationtests.variableinstallments; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.mifosplatform.integrationtests.common.accounting.Account; +import org.mifosplatform.integrationtests.common.loans.LoanApplicationTestBuilder; +import org.mifosplatform.integrationtests.common.loans.LoanProductTestBuilder; + +import com.google.gson.Gson; + +@SuppressWarnings({"rawtypes", "unchecked"}) +public class VariableInstallmentsFlatHelper { + + public static String createLoanProductWithVaribleConfig(final boolean multiDisburseLoan, final String accountingRule, final Account... accounts) { + System.out.println("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + final String loanProductJSON = new LoanProductTestBuilder() // + .withPrincipal("1,00,000.00") // + .withNumberOfRepayments("4") // + .withRepaymentAfterEvery("1") // + .withRepaymentTypeAsMonth() // + .withinterestRatePerPeriod("1") // + .withAmortizationTypeAsEqualPrincipalPayment() + .withInterestTypeAsFlat() // + .withTranches(multiDisburseLoan) // + .withInterestCalculationPeriodTypeAsRepaymentPeriod(true)// + .withVariableInstallmentsConfig(Boolean.TRUE, new Integer(5), new Integer(90))// + .withAccounting(accountingRule, accounts).build(null); + return loanProductJSON ; + } + + + + public static String applyForLoanApplication(final Integer clientID, final Integer loanProductID, List charges, + final String savingsId, String principal) { + System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + final String loanApplicationJSON = new LoanApplicationTestBuilder() // + .withPrincipal(principal) // + .withLoanTermFrequency("4") // + .withLoanTermFrequencyAsMonths() // + .withNumberOfRepayments("4") // + .withRepaymentEveryAfter("1") // + .withRepaymentFrequencyTypeAsMonths() // + .withInterestRatePerPeriod("2") // + .withAmortizationTypeAsEqualPrincipalPayments() // + .withInterestTypeAsFlatBalance() // + .withInterestCalculationPeriodTypeSameAsRepaymentPeriod() // + .withExpectedDisbursementDate("20 September 2011") // + .withSubmittedOnDate("20 September 2011") // + .withCharges(charges).build(clientID.toString(), loanProductID.toString(), savingsId); + return loanApplicationJSON ; + } + + public static String createDeleteVariations(ArrayList deletedInstallments) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("deletedinstallments", createDeletedMap(deletedInstallments)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createDeletedMap(ArrayList deletedItems) { + ArrayList toReturn = new ArrayList<>(); + for (Map map : deletedItems) { + ArrayList dueDate = (ArrayList) map.get("dueDate"); + Map tosend = new HashMap(); + tosend.put("dueDate", formatDate(dueDate)); + toReturn.add(tosend); + } + return toReturn; + } + + public static String createAddVariations() { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("newinstallments", createNewInstallments()); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createNewInstallments() { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", "31 October 2011"); + tosend.put("principal", "5000"); + toReturn.add(tosend); + return toReturn; + } + + public static String createModifiyVariations(Map firstSchedule) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createModifyMap(firstSchedule)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createNewInstallments(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("principal", "5000"); + toReturn.add(tosend); + return toReturn; + } + + public static String createModifiyDateVariations(String[] date, String[] newdate, String[] principal) { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createDateModifyMap(date, newdate, principal)); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createDateModifyMap(String[] date, String[] newdate, String[] principal) { + ArrayList toReturn = new ArrayList<>(); + for(int i = 0 ; i < date.length; i++) { + Map tosend = new HashMap(); + tosend.put("dueDate", date[i]); + tosend.put("modifiedDueDate", newdate[i]) ; + if(i < principal.length) { + tosend.put("principal", principal[i]) ; + } + toReturn.add(tosend); + } + return toReturn; + } + + + private static ArrayList createModifyMap(Map firstSchedule) { + ArrayList toReturn = new ArrayList<>(); + ArrayList dueDate = (ArrayList) firstSchedule.get("dueDate"); + Map tosend = new HashMap(); + tosend.put("dueDate", formatDate(dueDate)); + tosend.put("principal", 30000) ; + toReturn.add(tosend); + return toReturn; + } + + public static String createAllVariations() { + Map toReturn = new HashMap<>(); + toReturn.put("locale", "en"); + toReturn.put("dateFormat", "dd MMMM yyyy"); + Map exceptions = new HashMap<>(); + exceptions.put("modifiedinstallments", createModifyMap("20 November 2011")); + exceptions.put("newinstallments", createNewInstallments("25 December 2011")); + exceptions.put("deletedinstallments", createDeletedMap("20 December 2011")); + toReturn.put("exceptions", exceptions); + String json = new Gson().toJson(toReturn); + return json; + } + + private static ArrayList createDeletedMap(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + toReturn.add(tosend); + return toReturn; + } + + private static ArrayList createModifyMap(String date) { + ArrayList toReturn = new ArrayList<>(); + Map tosend = new HashMap(); + tosend.put("dueDate", date); + tosend.put("principal", 30000) ; + toReturn.add(tosend); + return toReturn; + } + + public static String formatDate(ArrayList list) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, (int) list.get(0)); + cal.set(Calendar.MONTH, (int) list.get(1) - 1); + cal.set(Calendar.DAY_OF_MONTH, (int) list.get(2)); + Date date = cal.getTime(); + DateFormat requiredFormat = new SimpleDateFormat("dd MMMM YYYY"); + return requiredFormat.format(date); + } + + public Map createModifyVarations() { + return null; + } + + public static ArrayList constructVerifyData(String[] dates, String[] installments) { + ArrayList toReturn = new ArrayList<>(); + for (int i = 0; i < dates.length; i++) { + Map map = new HashMap<>(); + map.put("dueDate", dates[i]); + map.put("installmentAmount", installments[i]); + toReturn.add(map); + } + return toReturn; + } +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsIntegrationTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsIntegrationTest.java new file mode 100644 index 000000000..4a35efae7 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableInstallmentsIntegrationTest.java @@ -0,0 +1,427 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.integrationtests.variableinstallments; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mifosplatform.integrationtests.common.ClientHelper; +import org.mifosplatform.integrationtests.common.Utils; +import org.mifosplatform.integrationtests.common.loans.LoanStatusChecker; +import org.mifosplatform.integrationtests.common.loans.LoanTransactionHelper; + +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.builder.ResponseSpecBuilder; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +@SuppressWarnings({"rawtypes", "unchecked"}) +public class VariableInstallmentsIntegrationTest { + + + private static final String NONE = "1"; + private RequestSpecification requestSpec; + private ResponseSpecification responseSpec; + private LoanTransactionHelper loanTransactionHelper; + + @Before + public void setup() { + Utils.initializeRESTAssured(); + this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.loanTransactionHelper = new LoanTransactionHelper(this.requestSpec, this.responseSpec); + } + + @Test + public void testVariableLoanProductCreation() { + final String json = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + final Integer loanProductID = this.loanTransactionHelper.getLoanProductId(json); + System.out.println("------------------------------RETRIEVING CREATED LOAN PRODUCT DETAILS ---------------------------------------"); + Map loanProduct = (Map)loanTransactionHelper.getLoanProductDetail(requestSpec, responseSpec, loanProductID, "") ; + Assert.assertTrue((Boolean)loanProduct.get("allowVariableInstallments")) ; + Assert.assertEquals(new Integer(5), loanProduct.get("minimumGap")) ; + Assert.assertEquals(new Integer(90), loanProduct.get("maximumGap")) ; + } + + + @Test + public void testLoanProductCreation() { + final String josn = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithoutVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(josn); + System.out.println("------------------------------RETRIEVING CREATED LOAN PRODUCT DETAILS ---------------------------------------"); + Map loanProduct = (Map)loanTransactionHelper.getLoanProductDetail(requestSpec, responseSpec, loanProductID, "") ; + Assert.assertTrue(!(Boolean)loanProduct.get("allowVariableInstallments")) ; + } + + @Test + public void testDeleteInstallmentsWithDecliningBalanceEqualInstallments() { + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 49 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + ArrayList toDelete = new ArrayList<>() ; + toDelete.add(periods.get(1)) ; + String toDeletedata = VariableInstallmentsDecliningBalanceHelper.createDeleteVariations(toDelete) ; + HashMap modifiedReschdule = transactionHelper.validateVariations(toDeletedata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"34675.47", "34675.47", "36756.26"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(toDeletedata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + + } + private void assertAfterSubmit(ArrayList serverData, ArrayList expectedData) { + Assert.assertTrue(serverData.size() == expectedData.size()) ; + for(int i = 0 ; i < serverData.size(); i++) { + Map serverMap = serverData.get(i) ; + Map expectedMap = expectedData.get(i) ; + Assert.assertTrue(VariableInstallmentsDecliningBalanceHelper.formatDate((ArrayList)serverMap.get("dueDate")).equals(expectedMap.get("dueDate"))) ; + Assert.assertTrue(serverMap.get("totalOutstandingForPeriod").toString().equals(expectedMap.get("installmentAmount"))) ; + } + } + + @Test + public void testAddInstallmentsWithDecliningBalanceEqualInstallments() { + //31 October 2011 - 5000 + //Result: 20 October 2011 - 21,215.84, 31 October 2011 - 5000, 20 November 2011 26,477.31, 20 December 2011 26,477.31, 20 January 2012 25,947.7 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + // Integer loanID = 57 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + String addVariationsjsondata = VariableInstallmentsDecliningBalanceHelper.createAddVariations() ; + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 October 2011", "31 October 2011", "20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"21215.84", "5000.0", "26477.31", "26477.31", "25947.7"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testModifyInstallmentWithDecliningBalanceEqualInstallments() { + //20 October 2011 - 30000 modify + //Result 20 October 2011 - 30000.0, 20 November 2011 - 24,966.34, 20 December 2011 - 24,966.34, 20 January 2012 - 24,966.33 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 57 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + String addVariationsjsondata = VariableInstallmentsDecliningBalanceHelper.createModifiyVariations((Map)periods.get(1)) ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"30000.0", "24966.34", "24966.34", "24966.33"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + + } + + @Test + public void testAllVariationsDecliningBalancewithEqualInstallments() { + // Request: Delete 20 December 2011 26,262.38, Modify 20 November 2011 from 26,262.38 to 30000, Add 25 December 2011 5000 + // Result: 20 October 2011 - 26262.38, 20 November 2011 - 30000, 25 December 2011 - 5000, 20 January 2012 - 44077 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 57 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + + String addVariationsjsondata = VariableInstallmentsDecliningBalanceHelper.createAllVariations() ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "25 December 2011", "20 January 2012"}, + new String[] {"26262.38", "30000.0", "5000.0", "44077.0"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testAllVariationsDecliningBalancewithEqualPrincipal() { + // Request: Delete 20 December 2011 26,262.38, Modify 20 November 2011 from 26,262.38 to 30000, Add 25 December 2011 5000 + // Result: 20 October 2011 - 27000.0, 20 November 2011 - 31500.0, 25 December 2011 - 6045.16, 20 January 2012 - 40670.97 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfigwithEqualPrincipal(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplicationWithEqualPrincipal(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + // Integer loanID = 109 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + + String addVariationsjsondata = VariableInstallmentsDecliningBalanceHelper.createAllVariationsWithEqualPrincipal() ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "25 December 2011", "20 January 2012"}, + new String[] {"27000.0", "31500.0", "6045.16", "40670.97"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testModifyDatesWithDecliningBalanceEqualInstallments() { + //Modify 20 December 2011:25000 -> 04 January 2012:20000 + //Modify 20 January 2012 -> 08 February 2012 + //Result 20 October 2011 -26262.38, 20 November 2011 - 26262.38, 04 January 2012 -20000, 08 February 2012 - 33242.97 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsDecliningBalanceHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsDecliningBalanceHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 57 ; + + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + // + // + String addVariationsjsondata = VariableInstallmentsDecliningBalanceHelper.createModifiyDateVariations(new String[]{"20 December 2011", "20 January 2012"}, new String[]{"04 January 2012", "08 February 2012"}, new String[]{"20000"}) ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsDecliningBalanceHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "04 January 2012", "08 February 2012"}, + new String[] {"26262.38", "26262.38", "20000.0", "33242.97"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + // Interest Type is FLAT + @Test + public void testDeleteInstallmentsWithInterestTypeFlat() { + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsFlatHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsFlatHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + ArrayList toDelete = new ArrayList<>() ; + toDelete.add(periods.get(1)) ; + String toDeletedata = VariableInstallmentsFlatHelper.createDeleteVariations(toDelete) ; + HashMap modifiedReschdule = transactionHelper.validateVariations(toDeletedata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsFlatHelper.constructVerifyData(new String[] {"20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"36000.0", "36000.0", "36000.0"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(toDeletedata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testAddInstallmentsWithInterestTypeFlat() { + //31 October 2011 - 5000 + //Result: 20 October 2011 - 21600.0, 31 October 2011 - 6600.0, 20 November 2011 26600.0, 20 December 2011 26600.0, 20 January 2012 26600.0 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsFlatHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsFlatHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 67 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + String addVariationsjsondata = VariableInstallmentsFlatHelper.createAddVariations() ; + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsFlatHelper.constructVerifyData(new String[] {"20 October 2011", "31 October 2011", "20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"21600.0", "6600.0", "26600.0", "26600.0", "26600.0"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testModifyInstallmentsWithInterestTypeisFlat() { + //20 October 2011 - 30000 modify + //Result 20 October 2011 - 32000.0, 20 November 2011 - 25333.33, 20 December 2011 - 25333.33, 20 January 2012 - 25333.34 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsFlatHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsFlatHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 67 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + String addVariationsjsondata = VariableInstallmentsFlatHelper.createModifiyVariations((Map)periods.get(1)) ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsFlatHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "20 December 2011", "20 January 2012"}, + new String[] {"32000.0", "25333.33", "25333.33", "25333.34"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testAllVariationsWithInterestTypeFlat() { + // Request: Delete 20 December 2011 25000.0, Modify 20 November 2011 from 25,000 to 30000, Add 25 December 2011 5000 + // Result: 20 October 2011 - 27000.0, 20 November 2011 - 32000.0, 25 December 2011 - 7000.0, 20 January 2012 - 42000.0 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsFlatHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsFlatHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 67 ; + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + + String addVariationsjsondata = VariableInstallmentsFlatHelper.createAllVariations() ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsFlatHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "25 December 2011", "20 January 2012"}, + new String[] {"27000.0", "32000.0", "7000.0", "42000.0"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } + + @Test + public void testModifyDatesWithInterestTypeFlat() { + //Modify 20 December 2011:25000 -> 04 January 2012:20000 + //Modify 20 January 2012 -> 08 February 2012 + //Result 20 October 2011 -27306.45, 20 November 2011 - 27306.45, 04 January 2012 -22306.45, 08 February 2012 - 32306.46 + VariableIntallmentsTransactionHelper transactionHelper = new VariableIntallmentsTransactionHelper(requestSpec, responseSpec) ; + final String loanProductJson = VariableInstallmentsFlatHelper.createLoanProductWithVaribleConfig(false, NONE); + Integer loanProductID = this.loanTransactionHelper.getLoanProductId(loanProductJson); + final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec); + ClientHelper.verifyClientCreatedOnServer(this.requestSpec, this.responseSpec, clientID); + final String json = VariableInstallmentsFlatHelper.applyForLoanApplication(clientID, loanProductID, null, null, "1,00,000.00"); + final Integer loanID = this.loanTransactionHelper.getLoanId(json); + HashMap loanStatusHashMap = LoanStatusChecker.getStatusOfLoan(this.requestSpec, this.responseSpec, loanID); + LoanStatusChecker.verifyLoanIsPending(loanStatusHashMap); + //Integer loanID = 67 ; + + Map list = transactionHelper.retrieveSchedule(loanID) ; + Map repaymentSchedule = (Map)list.get("repaymentSchedule") ; + ArrayList periods = (ArrayList)repaymentSchedule.get("periods") ; + // + // + String addVariationsjsondata = VariableInstallmentsFlatHelper.createModifiyDateVariations(new String[]{"20 December 2011", "20 January 2012"}, new String[]{"04 January 2012", "08 February 2012"}, new String[]{"20000"}) ; //0th position will have disbursement + HashMap modifiedReschdule = transactionHelper.validateVariations(addVariationsjsondata, loanID) ; + ArrayList newperiods = (ArrayList) modifiedReschdule.get("periods") ; + ArrayList toVerifyData = VariableInstallmentsFlatHelper.constructVerifyData(new String[] {"20 October 2011", "20 November 2011", "04 January 2012", "08 February 2012"}, + new String[] {"27306.45", "27306.45", "22306.45", "32306.46"}) ; + assertAfterSubmit(newperiods, toVerifyData) ; + transactionHelper.submitVariations(addVariationsjsondata, loanID) ; + list = transactionHelper.retrieveSchedule(loanID) ; + repaymentSchedule = (Map)list.get("repaymentSchedule") ; + periods = (ArrayList)repaymentSchedule.get("periods") ; + periods.remove(0) ; //Repayments Schedule includes disbursement also. So remove this. + assertAfterSubmit(periods, toVerifyData) ; + } +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableIntallmentsTransactionHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableIntallmentsTransactionHelper.java new file mode 100644 index 000000000..a61cec5a2 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/variableinstallments/VariableIntallmentsTransactionHelper.java @@ -0,0 +1,46 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.integrationtests.variableinstallments; + +import java.util.HashMap; +import java.util.Map; + +import org.mifosplatform.integrationtests.common.Utils; + +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + + +@SuppressWarnings("rawtypes") +public class VariableIntallmentsTransactionHelper { + + private final String URL = "https://localhost:8443/mifosng-provider/api/v1/loans/" ; + + private final RequestSpecification requestSpec; + private final ResponseSpecification responseSpec; + + public VariableIntallmentsTransactionHelper(final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + this.requestSpec = requestSpec ; + this.responseSpec = responseSpec ; + } + + + public Map retrieveSchedule(Integer loanId) { + String url = URL+loanId+"?associations=repaymentSchedule&exclude=guarantors&"+Utils.TENANT_IDENTIFIER ; + return Utils.performServerGet(requestSpec, responseSpec, url, ""); + } + + public HashMap validateVariations(final String exceptions, Integer loanId) { + String url = URL+loanId+"/schedule?command=calculateLoanSchedule&"+Utils.TENANT_IDENTIFIER ; + return Utils.performServerPost(this.requestSpec, this.responseSpec, url, exceptions, ""); + } + + public HashMap submitVariations(final String exceptions, Integer loanId) { + String url = URL+loanId+"/schedule?command=addVariations&"+Utils.TENANT_IDENTIFIER ; + return Utils.performServerPost(this.requestSpec, this.responseSpec, url, exceptions, ""); + } +}