diff --git a/.gitignore b/.gitignore index 5547434bb..fe5a6fca0 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ repos *.iws rebel.xml *.sublime-workspace +*.DS_Store diff --git a/mifosng-provider/build.gradle b/mifosng-provider/build.gradle index 95166c82f..4df413d56 100644 --- a/mifosng-provider/build.gradle +++ b/mifosng-provider/build.gradle @@ -160,7 +160,8 @@ dependencies { testCompile 'junit:junit:4.10', 'org.mockito:mockito-all:1.9.0', - 'org.hamcrest:hamcrest-all:1.1' + 'org.hamcrest:hamcrest-all:1.1', + 'com.jayway.restassured:rest-assured:1.7.2' } @@ -210,6 +211,24 @@ tomcatRunWar { configFile = file('src/test/resources/META-INF/context.xml') } + +sourceSets { + integrationTest { + } +} + +configurations { + integrationTestCompile.extendsFrom testCompile + integrationTestRuntime.extendsFrom testRuntime +} + +task integrationTest(type:Test){ + description = "Run integration tests (located in src/integrationTest/java). Requires server to be running." + testClassesDir = project.sourceSets.integrationTest.output.classesDir + classpath = project.sourceSets.integrationTest.runtimeClasspath +} + + /* * Support publication of artifacts versioned by topic branch. * CI builds supply `-P BRANCH_NAME=` to gradle at build time. diff --git a/mifosng-provider/src/integrationTest/java/ClientLoanIntegrationTest.java b/mifosng-provider/src/integrationTest/java/ClientLoanIntegrationTest.java new file mode 100644 index 000000000..649873eb9 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/ClientLoanIntegrationTest.java @@ -0,0 +1,2580 @@ + +import com.google.gson.Gson; +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.builder.ResponseSpecBuilder; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.path.json.JsonPath; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Random; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.RestAssured.post; +import static com.jayway.restassured.path.json.JsonPath.from; +import static junit.framework.Assert.assertEquals; + +/** + * Client Loan Integration Test for checking Loan Application Repayment Schedule. + */ +public class ClientLoanIntegrationTest { + String basicAuthKey; + ResponseSpecification responseSpec; + RequestSpecification requestSpec; + + @Before + public void setup() { + basicAuthKey = loginIntoServerAndGetBase64EncodedAuthenticationKey(); + + requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + requestSpec.header("Authorization","Basic "+basicAuthKey); + + responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + } + + @Test + public void checkClientLoanCreateAndDisburseFlow(){ + + Integer clientID = createClient(); + System.out.println("------------------------GENERATED CLIENT ID: "+clientID+"---------------------------\n"); + checkClientCreatedOnServer(clientID); + + Integer loanProductID = createLoanProduct(); + System.out.println("-----------------------GENERATED LOAN PRODUCT ID : "+loanProductID+"---------------------------\n"); + + applyForLoanApplication(clientID, loanProductID); + + ArrayList loanSchedule = getLoanRepaymentSchedule(loanProductID); + verifyLoanRepaymentSchedule(loanSchedule); + + } + + private String loginIntoServerAndGetBase64EncodedAuthenticationKey() { + System.out.println("-----------------------------------LOGIN-----------------------------------------"); + String json = post("/mifosng-provider/api/v1/authentication?username=mifos&password=password&tenantIdentifier=default").asString(); + return JsonPath.with(json).get("base64EncodedAuthenticationKey"); + } + + private Integer createClient() { + System.out.println("---------------------------------CREATING A CLIENT---------------------------------------------"); + String json = given().spec(requestSpec).body(getTestClientAsJSON()) + .expect().spec(responseSpec) + .when().post("/mifosng-provider/api/v1/clients?tenantIdentifier=default") + .andReturn().asString(); + + return from(json).get("clientId"); + } + + private void checkClientCreatedOnServer(Integer generatedClientID) { + System.out.println("------------------------------CHECK CLIENT DETAILS------------------------------------\n"); + given().spec(requestSpec) + .expect().spec(responseSpec) + .when().get("/mifosng-provider/api/v1/clients/" + generatedClientID + "?tenantIdentifier=default"); + } + + private Integer createLoanProduct() { + System.out.println("------------------------------CREATING NEW LOAN PRODUCT ---------------------------------------"); + String json = given().spec(requestSpec).body(getTestLoanProductAsJSON()) + .expect().spec(responseSpec) + .when().post("/mifosng-provider/api/v1/loanproducts?tenantIdentifier=default") + .andReturn().asString(); + return from(json).get("resourceId"); + } + + private void applyForLoanApplication(Integer clientID, Integer loanProductID) { + System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------"); + given().spec(requestSpec).body(getLoanApplicationBodyAsJSON(clientID.toString(), loanProductID.toString())) + .expect().spec(responseSpec) + .when().post("/mifosng-provider/api/v1/loans?tenantIdentifier=default"); + } + + private ArrayList getLoanRepaymentSchedule(Integer productID) + { + System.out.println("---------------------------GETTING LOAN REPAYMENT SCHEDULE--------------------------------"); + String json = given().spec(requestSpec).body(getLoanCalculationBodyAsJSON(productID.toString())) + .expect().spec(responseSpec) + .when().post("/mifosng-provider/api/v1/loans?command=calculateLoanSchedule&tenantIdentifier=default").andReturn().asString(); + return from(json).get("periods"); + } + + private void verifyLoanRepaymentSchedule(ArrayList loanSchedule) { + System.out.println("--------------------VERIFYING THE PRINCIPAL DUES,INTEREST DUE AND DUE DATE--------------------------"); + + assertEquals("Checking for Due Date for 1st Month",new ArrayList(Arrays.asList(2011, 10, 20)),loanSchedule.get(1).get("dueDate")); + assertEquals("Checking for Principal Due for 1st Month", new Float("2911.49"), loanSchedule.get(1).get("principalOriginalDue")); + assertEquals("Checking for Interest Due for 1st Month",new Float("240.00"),loanSchedule.get(1).get("interestOriginalDue")); + + assertEquals("Checking for Due Date for 2nd Month",new ArrayList(Arrays.asList(2011,11,20)),loanSchedule.get(2).get("dueDate")); + assertEquals("Checking for Principal Due for 2nd Month", new Float("2969.72"), loanSchedule.get(2).get("principalDue")); + assertEquals("Checking for Interest Due for 2nd Month",new Float("181.77"),loanSchedule.get(2).get("interestOriginalDue")); + + assertEquals("Checking for Due Date for 3rd Month",new ArrayList(Arrays.asList(2011,12,20)),loanSchedule.get(3).get("dueDate")); + assertEquals("Checking for Principal Due for 3rd Month", new Float("3029.11"), loanSchedule.get(3).get("principalDue")); + assertEquals("Checking for Interest Due for 3rd Month",new Float("122.38"),loanSchedule.get(3).get("interestOriginalDue")); + + assertEquals("Checking for Due Date for 4th Month",new ArrayList(Arrays.asList(2012,1,20)),loanSchedule.get(4).get("dueDate")); + assertEquals("Checking for Principal Due for 4th Month", new Float("3089.68"), loanSchedule.get(4).get("principalDue")); + assertEquals("Checking for Interest Due for 4th Month",new Float("61.79"),loanSchedule.get(4).get("interestOriginalDue")); + } + + + private String getTestClientAsJSON() { + HashMap map = new HashMap(); + map.put("officeId", "1"); + map.put("firstname", randomNameGenerator("Client_FirstName_", 5)); + map.put("lastname", randomNameGenerator("Client_LastName_", 4)); + map.put("externalId", randomIDGenerator("ID_", 7)); + map.put("dateFormat", "dd MMMM yyyy"); + map.put("locale", "en"); + map.put("joinedDate", "04 March 2009"); + return new Gson().toJson(map); + } + + private String getTestLoanProductAsJSON(){ + HashMap map = new HashMap(); + map.put("name", randomNameGenerator("LOAN_PRODUCT_", 6)); + map.put("currencyCode", "INR"); + map.put("locale", "en_GB"); + map.put("digitsAfterDecimal", "2"); + map.put("principal", "12,000.00"); + map.put("numberOfRepayments", "4"); + map.put("repaymentEvery", "1"); + map.put("repaymentFrequencyType", "2"); + map.put("interestRatePerPeriod", "2"); + map.put("interestRateFrequencyType", "2"); + map.put("amortizationType", "1"); + map.put("interestType", "0"); + map.put("interestCalculationPeriodType", "1"); + map.put("inArrearsTolerance", "0"); + map.put("transactionProcessingStrategyId", "1"); + map.put("accountingRule", "1") ; + return new Gson().toJson(map); + } + + private String getLoanApplicationBodyAsJSON(String clientID,String productID){ + HashMap map = new HashMap(); + map.put("dateFormat", "dd MMMM yyyy"); + map.put("locale", "en_GB"); + map.put("clientId", clientID); + map.put("productId", productID); + map.put("principal", "12,000.00"); + map.put("loanTermFrequency", "4"); + map.put("loanTermFrequencyType", "2"); + map.put("numberOfRepayments", "4"); + map.put("repaymentEvery", "1"); + map.put("repaymentFrequencyType", "2"); + map.put("interestRateFrequencyType", "2"); + map.put("interestRatePerPeriod", "2"); + map.put("amortizationType", "1"); + map.put("interestType", "0"); + map.put("interestCalculationPeriodType", "1"); + map.put("transactionProcessingStrategyId", "1"); + map.put("expectedDisbursementDate", "20 September 2011"); + map.put("submittedOnDate", "20 September 2011"); + return new Gson().toJson(map); + } + + private String getLoanCalculationBodyAsJSON(String productID){ + HashMap map = new HashMap(); + map.put("dateFormat", "dd MMMM yyyy"); + map.put("locale", "en_GB"); + map.put("productId", productID); + map.put("principal", "12,000.00"); + map.put("loanTermFrequency", "4"); + map.put("loanTermFrequencyType", "2"); + map.put("numberOfRepayments", "4"); + map.put("repaymentEvery", "1"); + map.put("repaymentFrequencyType", "2"); + map.put("interestRateFrequencyType", "2"); + map.put("interestRatePerPeriod", "2"); + map.put("amortizationType", "1"); + map.put("interestType", "0"); + map.put("interestCalculationPeriodType", "1"); + map.put("expectedDisbursementDate", "20 September 2011"); + map.put("transactionProcessingStrategyId", "1"); + return new Gson().toJson(map); + } + + public String randomIDGenerator(String prefix, int lenOfRandomSuffix) { + return randomStringGenerator(prefix,lenOfRandomSuffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + } + + public String randomNameGenerator(String prefix, int lenOfRandomSuffix) { + return randomStringGenerator(prefix,lenOfRandomSuffix, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + } + + private String randomStringGenerator(String prefix,int len, String sourceSetString) { + int lengthOfSource = sourceSetString.length(); + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) + sb.append((sourceSetString).charAt(rnd.nextInt(lengthOfSource))); + return (prefix+(sb.toString())); + } + + + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +