Improve testing patterns for funds retrieval

This commit is contained in:
Mohammed Nafees
2014-12-03 12:07:29 +05:30
parent 8f13a96f61
commit a51390ae37
3 changed files with 70 additions and 22 deletions
@@ -5,8 +5,10 @@
*/
package org.mifosplatform.integrationtests;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Before;
@@ -21,9 +23,7 @@ import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.*;
/**
* Funds Integration Test for checking Funds Application.
@@ -43,10 +43,13 @@ public class FundsIntegrationTest {
@Test
public void testRetrieveFund() {
FundsHelper fh = FundsHelper.create(Utils.randomNameGenerator("", 10)).build();
FundsHelper fh = FundsHelper
.create(Utils.randomNameGenerator("", 10))
.externalId(Utils.randomNameGenerator("fund-", 5))
.build();
String jsonData = fh.toJSON();
final Integer fundID = createFund(jsonData, this.requestSpec, this.responseSpec);
final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec);
Assert.assertNotNull(fundID);
jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.responseSpec);
@@ -57,28 +60,36 @@ public class FundsIntegrationTest {
@Test
public void testRetrieveAllFunds() {
FundsHelper fh = FundsHelper.create(Utils.randomNameGenerator("", 10)).build();
FundsHelper fh = FundsHelper
.create(Utils.randomNameGenerator("", 10))
.externalId(Utils.randomNameGenerator("fund-", 5))
.build();
String jsonData = fh.toJSON();
final Integer fundID = createFund(jsonData, this.requestSpec, this.responseSpec);
final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec);
Assert.assertNotNull(fundID);
List<HashMap> list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec);
List<HashMap<String, Object>> list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec);
Assert.assertNotNull(list);
Assert.assertThat(list.size(), greaterThanOrEqualTo(1));
jsonData = new Gson().toJson(list);
List<FundsHelper> fhList = new Gson().fromJson(jsonData, new TypeToken<List<FundsHelper>>(){}.getType());
Assert.assertThat(fhList, hasItem(fh));
}
@Test
public void testFundUnknown() {
String jsonData = FundsResourceHandler.retrieveFund(Integer.MAX_VALUE, this.requestSpec, this.responseSpec);
String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, this.responseSpec);
HashMap<String, String> map = new Gson().fromJson(jsonData, HashMap.class);
assertEquals(map.get("userMessageGlobalisationCode"), "error.msg.resource.not.found");
}
private Integer createFund(final String fundJSON,
final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
System.out.println("------------------ CREATING NEW FUND ------------------");
return FundsResourceHandler.createFund(fundJSON, requestSpec, responseSpec);
private Long createFund(final String fundJSON,
final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
return new Long(String.valueOf(FundsResourceHandler.createFund(fundJSON, requestSpec, responseSpec)));
}
}
@@ -12,34 +12,47 @@ public class FundsHelper {
public static class Builder {
private String name;
private String externalId;
private Builder(final String name) {
this.name = name;
}
public Builder externalId(final String externalId) {
this.externalId = externalId;
return this;
}
public FundsHelper build() {
return new FundsHelper(this.name);
return new FundsHelper(this.name, this.externalId);
}
}
private String name;
private Integer resourceId;
private String externalId;
private Long resourceId;
public FundsHelper() {
FundsHelper() {
super();
}
private FundsHelper(final String name) {
private FundsHelper(final String name,
final String externalId) {
super();
this.name = name;
this.externalId = externalId;
}
public String getName() {
return this.name;
}
public Integer getResourceID() {
public String getExternalId() {
return this.externalId;
}
public Long getResourceId() {
return this.resourceId;
}
@@ -55,4 +68,28 @@ public class FundsHelper {
return new Builder(name);
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof FundsHelper)) {
return false;
}
FundsHelper fh = (FundsHelper)o;
if (this.name.equals(fh.name)) {
return true;
}
return false;
}
}
@@ -24,13 +24,13 @@ public class FundsResourceHandler {
return Utils.performServerPost(requestSpec, responseSpec, CREATE_FUNDS_URL, fundJSON, "resourceId");
}
public static List<HashMap> retrieveAllFunds(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
public static List<HashMap<String, Object>> retrieveAllFunds(final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
final String URL = FUNDS_URL + "?" + Utils.TENANT_IDENTIFIER;
return Utils.performServerGet(requestSpec, responseSpec, URL, "");
}
public static String retrieveFund(final Integer fundID,
public static String retrieveFund(final Long fundID,
final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
final String URL = FUNDS_URL + "/" + fundID + "?" + Utils.TENANT_IDENTIFIER;