Refactoring

This commit is contained in:
Priti
2013-04-01 19:32:57 +05:30
parent 3924fafe88
commit 513f6f2764
3 changed files with 129 additions and 109 deletions
@@ -13,7 +13,6 @@ import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.Gson;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.builder.ResponseSpecBuilder;
import com.jayway.restassured.http.ContentType;
@@ -42,7 +41,7 @@ public class ClientLoanIntegrationTest {
@Test
public void checkClientLoanCreateAndDisburseFlow(){
Integer clientID = Utils.createClient(requestSpec, responseSpec);
checkClientCreatedOnServer(clientID);
Utils.verifyClientCreatedOnServer(requestSpec, responseSpec, clientID);
Integer loanProductID = createLoanProduct();
applyForLoanApplication(clientID, loanProductID);
@@ -53,14 +52,6 @@ public class ClientLoanIntegrationTest {
}
private void checkClientCreatedOnServer(final 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 loanProductJSON = new LoanProductTestBuilder()
@@ -74,14 +65,10 @@ public class ClientLoanIntegrationTest {
.withInterestTypeAsDecliningBalance()
.withinterestRatePerPeriod("1")
.build();
String json = given().spec(requestSpec).body(loanProductJSON)
.expect().spec(responseSpec)
.when().post("/mifosng-provider/api/v1/loanproducts?tenantIdentifier=default")
.andReturn().asString();
return from(json).get("resourceId");
return Utils.getLoanProductId(requestSpec, responseSpec, loanProductJSON);
}
private void applyForLoanApplication(final Integer clientID, final Integer loanProductID) {
private Integer applyForLoanApplication(final Integer clientID, final Integer loanProductID) {
System.out.println("--------------------------------APPLYING FOR LOAN APPLICATION--------------------------------");
String loanApplicationJSON = new LoanApplicationTestBuilder()
.withPrincipal("12,000.00")
@@ -98,18 +85,13 @@ public class ClientLoanIntegrationTest {
.withExpectedDisbursementDate("20 September 2011")
.withSubmittedOnDate("20 September 2011")
.Build(clientID.toString(),loanProductID.toString());
given().spec(requestSpec).body(loanApplicationJSON)
.expect().spec(responseSpec)
.when().post("/mifosng-provider/api/v1/loans?tenantIdentifier=default");
return Utils.getLoanId(requestSpec, responseSpec, loanApplicationJSON);
}
private ArrayList getLoanRepaymentSchedule(final 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");
return Utils.getRepaymentSchedule(requestSpec,responseSpec,productID);
}
private void verifyLoanRepaymentSchedule(final ArrayList<HashMap> loanSchedule) {
@@ -134,29 +116,6 @@ public class ClientLoanIntegrationTest {
private String getLoanCalculationBodyAsJSON(final String productID){
HashMap<String, String> map = new HashMap<String, String>();
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);
}
}
@@ -59,7 +59,7 @@ public class LoanWithWaiveInterestAndWriteOff {
public void checkClientLoanCreateAndDisburseFlow(){
//CREATE CLIENT
Integer clientID = Utils.createClient(requestSpec, responseSpec, DATE_OF_JOINING);
verifyClientCreatedOnServer(clientID);
Utils.verifyClientCreatedOnServer(requestSpec, responseSpec, clientID);
//CREATE LOAN PRODUCT
Integer loanProductID = createLoanProduct();
@@ -113,15 +113,6 @@ public class LoanWithWaiveInterestAndWriteOff {
}
private HashMap writeOffLoan(String transactionDate, Integer loanID){
System.out.println("--------------------LOAN WRITTEN OFF ON "+transactionDate+"-------------------------------\n");
String json = given().spec(requestSpec).body(Utils.getWriteOffBodyAsJSON(transactionDate))
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loans/" + loanID + "/transactions?command=writeoff&tenantIdentifier=default")
.andReturn().asString();
HashMap response = from(json).get("changes");
return (HashMap)response.get("status");
}
private void waiveInterest(String transactionDate, Integer loanID){
System.out.println("--------------------Waive INTEREST On "+transactionDate+"-------------------------------\n");
@@ -155,41 +146,24 @@ public class LoanWithWaiveInterestAndWriteOff {
return from(json).get("status");
}
private HashMap undoApproval(Integer loanID){
System.out.println("-----------------------------------UNDO LOAN APPROVAL-----------------------------------------");
String json = given().spec(requestSpec).body("{'note':'UNDO APPROVAL'}")
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loans/" + loanID + "?command=undoApproval&tenantIdentifier=default")
.andReturn().asString();
HashMap response = from(json).get("changes");
return (HashMap)response.get("status");
}
private HashMap disburseLoan(Integer loanID){
System.out.println("-----------------------------------DISBURSE LOAN-----------------------------------------");
String json = given().spec(requestSpec).body(Utils.getDisburseLoanAsJSON(DISBURSEMENT_DATE))
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loans/" + loanID + "?command=disburse&tenantIdentifier=default")
.andReturn().asString();
HashMap response = from(json).get("changes");
return (HashMap)response.get("status");
}
private HashMap approveLoan(String approvalDate, Integer loanID){
System.out.println("-----------------------------------APPROVE LOAN-----------------------------------------");
String json =given().spec(requestSpec).body(Utils.getApproveLoanAsJSON(approvalDate))
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loans/"+ loanID +"?command=approve&tenantIdentifier=default")
.andReturn().asString();
HashMap response = from(json).get("changes");
return (HashMap)response.get("status");
return Utils.approveLoan(requestSpec,responseSpec,approvalDate,loanID);
}
private void verifyClientCreatedOnServer(final Integer generatedClientID) {
System.out.println("------------------------------CHECK CLIENT DETAILS------------------------------------\n");
given().spec(requestSpec)
.expect().spec(responseSpec).log().ifError()
.when().get("/mifosng-provider/api/v1/clients/" + generatedClientID + "?tenantIdentifier=default");
private HashMap undoApproval(Integer loanID){
System.out.println("-----------------------------------UNDO LOAN APPROVAL-----------------------------------------");
return Utils.undoApproval(requestSpec,responseSpec,loanID);
}
private HashMap disburseLoan(Integer loanID){
System.out.println("-----------------------------------DISBURSE LOAN-----------------------------------------");
return Utils.disburseLoan(requestSpec,responseSpec,loanID,DISBURSEMENT_DATE);
}
private HashMap writeOffLoan(String transactionDate, Integer loanID){
System.out.println("--------------------LOAN WRITTEN OFF ON "+transactionDate+"-------------------------------\n");
return Utils.writeOffLoan(requestSpec,responseSpec,transactionDate,loanID);
}
private Integer createLoanProduct() {
@@ -205,11 +179,7 @@ public class LoanWithWaiveInterestAndWriteOff {
.withAmortizationTypeAsEqualPrinciplePayment()
.withInterestTypeAsFlat()
.build();
String json = given().spec(requestSpec).body(loanProductJSON)
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loanproducts?tenantIdentifier=default")
.andReturn().asString();
return from(json).get("resourceId");
return Utils.getLoanProductId(requestSpec,responseSpec,loanProductJSON);
}
private Integer applyForLoanApplication(final Integer clientID, final Integer loanProductID) {
@@ -229,11 +199,7 @@ public class LoanWithWaiveInterestAndWriteOff {
.withExpectedDisbursementDate(EXPECTED_DISBURSAL_DATE)
.withSubmittedOnDate(LOAN_APPLICATION_SUBMISSION_DATE)
.Build(clientID.toString(), loanProductID.toString());
String json = given().spec(requestSpec).body(loanApplicationJSON)
.expect().spec(responseSpec).log().ifError()
.when().post("/mifosng-provider/api/v1/loans?tenantIdentifier=default")
.andReturn().asString();
return from(json).get("loanId");
return Utils.getLoanId(requestSpec,responseSpec,loanApplicationJSON);
}
}
@@ -5,6 +5,7 @@ import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
@@ -12,7 +13,15 @@ import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.post;
import static com.jayway.restassured.path.json.JsonPath.from;
import junit.framework.Assert;
public class Utils {
private static final String CREATE_LOAN_PRODUCT_URL ="/mifosng-provider/api/v1/loanproducts?tenantIdentifier=default";
private static final String LOGIN_URL = "/mifosng-provider/api/v1/authentication?username=mifos&password=password&tenantIdentifier=default";
private static final String CREATE_CLIENT_URL = "/mifosng-provider/api/v1/clients?tenantIdentifier=default";
private static final String APPLY_LOAN_URL = "/mifosng-provider/api/v1/loans?tenantIdentifier=default";
private static final String REPAYMENT_SCHEDULE_URL ="/mifosng-provider/api/v1/loans?command=calculateLoanSchedule&tenantIdentifier=default";
public static String randomStringGenerator(final String prefix, final int len, final String sourceSetString) {
int lengthOfSource = sourceSetString.length();
@@ -90,24 +99,110 @@ public class Utils {
return new Gson().toJson(map);
}
public static Integer createClient(RequestSpecification requestSpec, ResponseSpecification responseSpec) {
return createClient(requestSpec,responseSpec,"04 March 2012");
private static String getLoanCalculationBodyAsJSON(final String productID){
HashMap<String, String> map = new HashMap<String, String>();
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 static Integer createClient(RequestSpecification requestSpec, ResponseSpecification responseSpec,String dateOfJoining) {
System.out.println("---------------------------------CREATING A CLIENT---------------------------------------------");
String json = given().spec(requestSpec).body(getTestClientAsJSON(dateOfJoining))
.expect().spec(responseSpec)
.when().post("/mifosng-provider/api/v1/clients?tenantIdentifier=default")
.andReturn().asString();
return from(json).get("clientId");
}
public static String loginIntoServerAndGetBase64EncodedAuthenticationKey() {
System.out.println("-----------------------------------LOGIN-----------------------------------------");
String json = post("/mifosng-provider/api/v1/authentication?username=mifos&password=password&tenantIdentifier=default").asString();
String json = post(LOGIN_URL).asString();
return JsonPath.with(json).get("base64EncodedAuthenticationKey");
}
public static Integer createClient(RequestSpecification requestSpec, ResponseSpecification responseSpec) {
return createClient(requestSpec,responseSpec,"04 March 2012");
}
public static Integer createClient(RequestSpecification requestSpec, ResponseSpecification responseSpec,String dateOfJoining) {
System.out.println("---------------------------------CREATING A CLIENT---------------------------------------------");
return performServerPost(requestSpec,responseSpec,CREATE_CLIENT_URL,getTestClientAsJSON(dateOfJoining),"clientId");
}
public static void verifyClientCreatedOnServer(RequestSpecification requestSpec, ResponseSpecification responseSpec, Integer generatedClientID) {
System.out.println("------------------------------CHECK CLIENT DETAILS------------------------------------\n");
String CLIENT_URL="/mifosng-provider/api/v1/clients/" + generatedClientID + "?tenantIdentifier=default";
Integer responseClientID = performServerGet(requestSpec,responseSpec,CLIENT_URL,"id");
Assert.assertEquals(generatedClientID,responseClientID);
}
public static Integer getLoanProductId(RequestSpecification requestSpec, ResponseSpecification responseSpec, String loanProductJSON) {
return performServerPost(requestSpec, responseSpec, CREATE_LOAN_PRODUCT_URL, loanProductJSON, "resourceId");
}
public static Integer getLoanId(RequestSpecification requestSpec, ResponseSpecification responseSpec, String loanApplicationJSON) {
return performServerPost(requestSpec, responseSpec, APPLY_LOAN_URL, loanApplicationJSON, "loanId");
}
public static ArrayList getRepaymentSchedule(RequestSpecification requestSpec, ResponseSpecification responseSpec,Integer productID){
return performServerPost(requestSpec,responseSpec,REPAYMENT_SCHEDULE_URL,getLoanCalculationBodyAsJSON(productID.toString()),"periods");
}
public static HashMap approveLoan(RequestSpecification requestSpec, ResponseSpecification responseSpec,String approvalDate,Integer loanID){
String approveLoanURL ="/mifosng-provider/api/v1/loans/"+ loanID +"?command=approve&tenantIdentifier=default";
HashMap response = performServerPost(requestSpec,responseSpec,approveLoanURL,getApproveLoanAsJSON(approvalDate),"changes");
return (HashMap)response.get("status");
}
public static HashMap undoApproval(RequestSpecification requestSpec, ResponseSpecification responseSpec,Integer loanID){
String undoBodyJson = "{'note':'UNDO APPROVAL'}";
String approveLoanURL ="/mifosng-provider/api/v1/loans/"+ loanID +"?command=undoApproval&tenantIdentifier=default";
HashMap response = performServerPost(requestSpec,responseSpec,approveLoanURL,undoBodyJson,"changes");
return (HashMap)response.get("status");
}
public static HashMap disburseLoan(RequestSpecification requestSpec, ResponseSpecification responseSpec,Integer loanID,String date){
String disburseBodyJson =getDisburseLoanAsJSON(date);
String disburseLoanURL ="/mifosng-provider/api/v1/loans/" + loanID + "?command=disburse&tenantIdentifier=default";
HashMap response=performServerPost(requestSpec,responseSpec,disburseLoanURL,disburseBodyJson,"changes");
return (HashMap)response.get("status");
}
public static HashMap writeOffLoan(RequestSpecification requestSpec, ResponseSpecification responseSpec,String date,Integer loanID){
String writeOffJsonBody =getWriteOffBodyAsJSON(date);
String writeOffURL = "/mifosng-provider/api/v1/loans/" + loanID + "/transactions?command=writeoff&tenantIdentifier=default";
HashMap response = performServerPost(requestSpec,responseSpec,writeOffURL,writeOffJsonBody,"changes");
return (HashMap)response.get("status");
}
private static <T> T performServerPost(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String postURL, final String jsonBodyToSend, final String jsonAttributeToGetBack){
String json = given().spec(requestSpec).body(jsonBodyToSend)
.expect().spec(responseSpec).log().ifError()
.when().post(postURL)
.andReturn().asString();
return (T) from(json).get(jsonAttributeToGetBack);
}
private static Integer performServerGet(final RequestSpecification requestSpec, final ResponseSpecification responseSpec,
final String postURL, final String jsonAttributeToGetBack){
String json = given().spec(requestSpec)
.expect().spec(responseSpec).log().ifError()
.when().get(postURL)
.andReturn().asString();
return from(json).get(jsonAttributeToGetBack);
}
}