Merge pull request #1231 from AJLiu/develop

closed
This commit is contained in:
Markus Geiß
2014-12-30 22:57:43 +01:00
3 changed files with 310 additions and 0 deletions
@@ -0,0 +1,85 @@
/**
* 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;
import java.sql.Timestamp;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mifosplatform.integrationtests.common.CenterDomain;
import org.mifosplatform.integrationtests.common.CenterHelper;
import org.mifosplatform.integrationtests.common.OfficeHelper;
import org.mifosplatform.integrationtests.common.StaffHelper;
import org.mifosplatform.integrationtests.common.Utils;
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.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
public class CenterIntegrationTest {
private RequestSpecification requestSpec;
private ResponseSpecification responseSpec;
@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();
}
@Test
public void testBasicCenterCreation() {
OfficeHelper oh = new OfficeHelper(requestSpec, responseSpec);
int officeId = oh.createOffice("01 July 2007");
String name = "TestBasicCreation" + new Timestamp(new java.util.Date().getTime());
int resourceId = CenterHelper.createCenter(name, officeId, requestSpec, responseSpec);
CenterDomain center = CenterHelper.retrieveByID(resourceId, requestSpec, responseSpec);
Assert.assertTrue(center.getName().equals(name));
Assert.assertTrue(center.getOfficeId() == officeId);
Assert.assertTrue(center.isActive() == false);
}
@SuppressWarnings("unchecked")
@Test
public void testFullCenterCreation() {
int officeId = new OfficeHelper(requestSpec, responseSpec).createOffice("01 July 2007");
String name = "TestFullCreation" + new Timestamp(new java.util.Date().getTime());
String externalId = Utils.randomStringGenerator("ID_", 7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
int staffId = StaffHelper.createStaff(requestSpec, responseSpec);
int[] groupMembers = new int[3];
for (int i = 0; i < groupMembers.length; i++) {
final HashMap<String, String> map = new HashMap<>();
map.put("officeId", "" + officeId);
map.put("name", Utils.randomStringGenerator("Group_Name_", 5));
map.put("externalId", Utils.randomStringGenerator("ID_", 7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
map.put("dateFormat", "dd MMMM yyyy");
map.put("locale", "en");
map.put("active", "true");
map.put("activationDate", "04 March 2011");
groupMembers[i] = Utils.performServerPost(requestSpec, responseSpec, "/mifosng-provider/api/v1/groups?"
+ Utils.TENANT_IDENTIFIER, new Gson().toJson(map), "groupId");
}
int resourceId = CenterHelper.createCenter(name, officeId, externalId, staffId, groupMembers, requestSpec, responseSpec);
CenterDomain center = CenterHelper.retrieveByID(resourceId, requestSpec, responseSpec);
Assert.assertTrue(center.getName().equals(name));
Assert.assertTrue(center.getOfficeId() == officeId);
Assert.assertTrue(center.getExternalId().equals(externalId));
Assert.assertTrue(center.getStaffId() == staffId);
Assert.assertTrue(center.isActive() == false);
}
}
@@ -0,0 +1,160 @@
/**
* 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.common;
import java.util.HashMap;
import com.google.gson.Gson;
public class CenterDomain {
public static class Builder {
private int id;
private HashMap status;
private boolean active;
private String name;
private String externalId;
private int staffId;
private int officeId;
private String officeName;
private String hierarchy;
private Builder(final int id, final int statusid, final String statuscode, final String statusvalue, final boolean active,
final String name, final String externalId, final int staffId, final int officeID, final String officeName,
final String hierarchy) {
this.id = id;
this.status = new HashMap();
this.status.put("id", statusid);
this.status.put("code", statuscode);
this.status.put("value", statusvalue);
this.active = active;
this.name = name;
this.externalId = externalId;
this.staffId = staffId;
this.officeId = officeID;
this.officeName = officeName;
this.hierarchy = hierarchy;
}
public CenterDomain build() {
return new CenterDomain(this.id, (int) this.status.get("id"), (String) this.status.get("code"),
(String) this.status.get("value"), this.active, this.name, this.externalId, this.staffId, this.officeId,
this.officeName, this.hierarchy);
}
}
private int id;
private HashMap status;
private boolean active;
private String name;
private String externalId;
private int staffId;
private int officeId;
private String officeName;
private String hierarchy;
CenterDomain() {
super();
}
private CenterDomain(final int id, final int statusid, final String statuscode, final String statusvalue, final boolean active,
final String name, final String externalId, final int staffId, final int officeID, final String officeName,
final String hierarchy) {
this.id = id;
this.status = new HashMap();
this.status.put("id", statusid);
this.status.put("code", statuscode);
this.status.put("value", statusvalue);
this.active = active;
this.name = name;
this.externalId = externalId;
this.staffId = staffId;
this.officeId = officeID;
this.officeName = officeName;
this.hierarchy = hierarchy;
}
public String toJSON() {
return new Gson().toJson(this);
}
public static CurrencyDomain fromJSON(final String jsonData) {
return new Gson().fromJson(jsonData, CurrencyDomain.class);
}
public static Builder create(final int id, final int statusid, final String statuscode, final String statusvalue, final boolean active,
final String name, final String externalId, final int staffId, final int officeID, final String officeName,
final String hierarchy) {
return new Builder(id, statusid, statuscode, statusvalue, active, name, externalId, staffId, officeID, officeName, hierarchy);
}
public String getExternalId() {
return this.externalId;
}
public int getStaffId() {
return this.staffId;
}
public int getId() {
return this.id;
}
public HashMap getStatus() {
return this.status;
}
public boolean isActive() {
return this.active;
}
public String getName() {
return this.name;
}
public int getOfficeId() {
return this.officeId;
}
public String getOfficeName() {
return this.officeName;
}
public String getHierarchy() {
return this.hierarchy;
}
@Override
public int hashCode() {
int hash = 1;
if (this.id >= 0) hash += this.id;
if (this.status != null) {
if ((int) this.status.get("id") >= 0) hash += (int) this.status.get("id");
if ((String) this.status.get("code") != null) hash += this.status.get("code").hashCode();
if ((String) this.status.get("value") != null) hash += this.status.get("value").hashCode();
}
if (this.name != null) hash += this.name.hashCode();
if (this.officeId >= 0) hash += this.officeId;
if (this.officeName != null) hash += this.officeName.hashCode();
if (this.hierarchy != null) hash += this.hierarchy.hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == this) { return true; }
if (!(obj instanceof CenterDomain)) return false;
CenterDomain cd = (CenterDomain) obj;
if (this.hashCode() == cd.hashCode()) return true;
return false;
}
}
@@ -0,0 +1,65 @@
/**
* 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.common;
import java.util.HashMap;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jayway.restassured.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CenterHelper {
private static final String CENTERS_URL = "/mifosng-provider/api/v1/centers";
public static CenterDomain retrieveByID(int id, final RequestSpecification requestSpec, final ResponseSpecification responseSpec) {
final String GET_CENTER_BY_ID_URL = CENTERS_URL + "/" + id + "?" + Utils.TENANT_IDENTIFIER;
System.out.println("------------------------ RETRIEVING CENTER AT " + id + "-------------------------");
final String jsonData = new Gson().toJson(Utils.performServerGet(requestSpec, responseSpec, GET_CENTER_BY_ID_URL, ""));
System.out.print(jsonData);
return new Gson().fromJson(jsonData, new TypeToken<CenterDomain>() {}.getType());
}
public static int createCenter(final String name, final int officeId, final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
return createCenter(name, officeId, null, -1, null, null, requestSpec, responseSpec);
}
public static int createCenter(final String name, final int officeId, final String activationDate,
final RequestSpecification requestSpec, final ResponseSpecification responseSpec) {
return createCenter(name, officeId, null, -1, null, activationDate, requestSpec, responseSpec);
}
public static int createCenter(final String name, final int officeId, final String externalId, final int staffId,
final int[] groupMembers, final RequestSpecification requestSpec, final ResponseSpecification responseSpec) {
return createCenter(name, officeId, externalId, staffId, groupMembers, null, requestSpec, responseSpec);
}
public static int createCenter(final String name, final int officeId, final String externalId, final int staffId,
final int[] groupMembers, final String activationDate, final RequestSpecification requestSpec,
final ResponseSpecification responseSpec) {
final String CREATE_CENTER_URL = CENTERS_URL + "?" + Utils.TENANT_IDENTIFIER;
HashMap hm = new HashMap();
hm.put("name", name);
hm.put("officeId", officeId);
hm.put("active", false);
if (externalId != null) hm.put("externalId", externalId);
if (staffId != -1) hm.put("staffId", staffId);
if (groupMembers != null) hm.put("groupMembers", groupMembers);
if (activationDate != null) {
hm.put("active", true);
hm.put("locale", "en");
hm.put("dateFormat", "dd MMM yyyy");
hm.put("activationDate", activationDate);
}
System.out.println("------------------------CREATING CENTER-------------------------");
return Utils.performServerPost(requestSpec, responseSpec, CREATE_CENTER_URL, new Gson().toJson(hm), "resourceId");
}
}