From f477a190a787702deddd53be372a4f0d00dd1fd0 Mon Sep 17 00:00:00 2001 From: Anthony Liu Date: Sun, 4 Jan 2015 17:15:38 -0500 Subject: [PATCH] Center Integration Update and Deletion --- .../CenterIntegrationTest.java | 104 ++++++++++++++---- .../integrationtests/common/CenterDomain.java | 15 ++- .../integrationtests/common/CenterHelper.java | 37 ++++++- 3 files changed, 127 insertions(+), 29 deletions(-) diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CenterIntegrationTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CenterIntegrationTest.java index 997944a04..85819c422 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CenterIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CenterIntegrationTest.java @@ -41,8 +41,7 @@ public class CenterIntegrationTest { @Test public void testBasicCenterCreation() { - OfficeHelper oh = new OfficeHelper(requestSpec, responseSpec); - int officeId = oh.createOffice("01 July 2007"); + int officeId = new OfficeHelper(requestSpec, responseSpec).createOffice("01 July 2007"); String name = "TestBasicCreation" + new Timestamp(new java.util.Date().getTime()); int resourceId = CenterHelper.createCenter(name, officeId, requestSpec, responseSpec); @@ -54,7 +53,6 @@ public class CenterIntegrationTest { Assert.assertTrue(center.isActive() == false); } - @SuppressWarnings("unchecked") @Test public void testFullCenterCreation() { @@ -62,20 +60,7 @@ public class CenterIntegrationTest { 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 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[] groupMembers = generateGroupMembers(3, officeId); int resourceId = CenterHelper.createCenter(name, officeId, externalId, staffId, groupMembers, requestSpec, responseSpec); CenterDomain center = CenterHelper.retrieveByID(resourceId, requestSpec, responseSpec); @@ -85,12 +70,7 @@ public class CenterIntegrationTest { Assert.assertTrue(center.getExternalId().equals(externalId)); Assert.assertTrue(center.getStaffId() == staffId); Assert.assertTrue(center.isActive() == false); - int[] groupMemberList = new int[center.getGroupMembers().size()]; - for (int i = 0; i < groupMemberList.length; i++) { - groupMemberList[i] = ((Double) center.getGroupMembers().get(i).get("id")).intValue(); - } - - Assert.assertArrayEquals(groupMemberList, groupMembers); + Assert.assertArrayEquals(center.getGroupMembers(), groupMembers); } @Test @@ -120,9 +100,85 @@ public class CenterIntegrationTest { public void testVoidCenterRetrieval() { ArrayList arr = CenterHelper.listCenters(requestSpec, responseSpec); int id = arr.get(arr.size() - 1).getId() + 1; - ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); CenterDomain center = CenterHelper.retrieveByID(id, requestSpec, responseSpec); Assert.assertNotNull(center); } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testCenterUpdate() { + 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 = generateGroupMembers(3, officeId); + int resourceId = CenterHelper.createCenter(name, officeId, externalId, staffId, groupMembers, requestSpec, responseSpec); + + String newName = "TestCenterUpdateNew" + new Timestamp(new java.util.Date().getTime()); + String newExternalId = Utils.randomStringGenerator("newID_", 7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + int newStaffId = StaffHelper.createStaff(requestSpec, responseSpec); + int[] associateGroupMembers = generateGroupMembers(2, officeId); + + int[] associateResponse = CenterHelper.associateGroups(resourceId, associateGroupMembers, requestSpec, responseSpec); + Arrays.sort(associateResponse); + Arrays.sort(associateGroupMembers); + Assert.assertArrayEquals(associateResponse, associateGroupMembers); + + int[] newGroupMembers = new int[5]; + for (int i = 0; i < 5; i++) { + if (i < 3) { + newGroupMembers[i] = groupMembers[i]; + } else { + newGroupMembers[i] = associateGroupMembers[i % 3]; + } + } + + HashMap request = new HashMap(); + request.put("name", newName); + request.put("externalId", newExternalId); + request.put("staffId", newStaffId); + HashMap response = CenterHelper.updateCenter(resourceId, request, requestSpec, responseSpec); + Assert.assertNotNull(response); + Assert.assertEquals(newName, response.get("name")); + Assert.assertEquals(newExternalId, response.get("externalId")); + Assert.assertEquals(newStaffId, response.get("staffId")); + + CenterDomain center = CenterHelper.retrieveByID(resourceId, requestSpec, responseSpec); + Assert.assertNotNull(center); + Assert.assertEquals(newName, center.getName()); + Assert.assertEquals(newExternalId, center.getExternalId()); + Assert.assertEquals(newStaffId, center.getStaffId()); + Assert.assertArrayEquals(newGroupMembers, center.getGroupMembers()); + } + + @Test + public void testCenterDeletion() { + int officeId = new OfficeHelper(requestSpec, responseSpec).createOffice("01 July 2007"); + String name = "TestBasicCreation" + new Timestamp(new java.util.Date().getTime()); + int resourceId = CenterHelper.createCenter(name, officeId, requestSpec, responseSpec); + + CenterHelper.deleteCenter(resourceId, requestSpec, responseSpec); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); + CenterDomain center = CenterHelper.retrieveByID(resourceId, requestSpec, responseSpec); + Assert.assertNotNull(center); + } + + private int[] generateGroupMembers(int size, int officeId) { + int[] groupMembers = new int[size]; + for (int i = 0; i < groupMembers.length; i++) { + final HashMap 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"); + } + return groupMembers; + } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterDomain.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterDomain.java index 5a764554c..e9031eb05 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterDomain.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterDomain.java @@ -10,7 +10,7 @@ import java.util.HashMap; import com.google.gson.Gson; -public class CenterDomain { +public class CenterDomain implements Comparable { public static class Builder { @@ -134,8 +134,12 @@ public class CenterDomain { return this.hierarchy; } - public ArrayList getGroupMembers() { - return this.groupMembers; + public int[] getGroupMembers() { + int[] groupMemberList = new int[this.groupMembers.size()]; + for (int i = 0; i < groupMemberList.length; i++) { + groupMemberList[i] = ((Double) this.groupMembers.get(i).get("id")).intValue(); + } + return groupMemberList; } @Override @@ -168,4 +172,9 @@ public class CenterDomain { if (this.hashCode() == cd.hashCode()) return true; return false; } + + @Override + public int compareTo(CenterDomain cd) { + return ((Integer) this.id).compareTo(cd.getId()); + } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterHelper.java index 1f0351ba0..08df09043 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CenterHelper.java @@ -27,14 +27,14 @@ public class CenterHelper { public static ArrayList paginatedListCenters(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { - final String GET_CENTER = CENTERS_URL + "?paged=true&" + Utils.TENANT_IDENTIFIER; + final String GET_CENTER = CENTERS_URL + "?paged=true&limit=-1&" + Utils.TENANT_IDENTIFIER; System.out.println("------------------------ RETRIEVING CENTERS-------------------------"); final String jsonData = new Gson().toJson(Utils.performServerGet(requestSpec, responseSpec, GET_CENTER, "pageItems")); return new Gson().fromJson(jsonData, new TypeToken>() {}.getType()); } public static ArrayList listCenters(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { - final String GET_CENTER = CENTERS_URL + "?" + Utils.TENANT_IDENTIFIER; + final String GET_CENTER = CENTERS_URL + "?limit=-1&" + Utils.TENANT_IDENTIFIER; System.out.println("------------------------ RETRIEVING CENTERS-------------------------"); final String jsonData = new Gson().toJson(Utils.performServerGet(requestSpec, responseSpec, GET_CENTER, "")); return new Gson().fromJson(jsonData, new TypeToken>() {}.getType()); @@ -77,4 +77,37 @@ public class CenterHelper { System.out.println("------------------------CREATING CENTER-------------------------"); return Utils.performServerPost(requestSpec, responseSpec, CREATE_CENTER_URL, new Gson().toJson(hm), "resourceId"); } + + public static HashMap updateCenter(final int id, HashMap request, final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final String UPDATE_CENTER_URL = CENTERS_URL + "/" + id + "?" + Utils.TENANT_IDENTIFIER; + System.out.println("---------------------------------UPDATE CENTER AT " + id + "---------------------------------------------"); + HashMap hash = Utils.performServerPut(requestSpec, responseSpec, UPDATE_CENTER_URL, new Gson().toJson(request), + "changes"); + return hash; + } + + public static int[] associateGroups(final int id, final int[] groupMembers, final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final String ASSOCIATE_GROUP_CENTER_URL = CENTERS_URL + "/" + id + "?command=associateGroups&" + Utils.TENANT_IDENTIFIER; + HashMap groupMemberHashMap = new HashMap(); + groupMemberHashMap.put("groupMembers", groupMembers); + System.out.println("---------------------------------ASSOCIATING GROUPS AT " + id + "--------------------------------------------"); + HashMap hash = Utils.performServerPost(requestSpec, responseSpec, ASSOCIATE_GROUP_CENTER_URL, + new Gson().toJson(groupMemberHashMap), "changes"); + System.out.println(hash); + ArrayList arr = (ArrayList) hash.get("groupMembers"); + int[] ret = new int[arr.size()]; + for (int i = 0; i < ret.length; i++) { + ret[i] = Integer.parseInt(arr.get(i)); + } + return ret; + } + + public static void deleteCenter(final int id, final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { + final String DELETE_CENTER_URL = CENTERS_URL + "/" + id + "?" + Utils.TENANT_IDENTIFIER; + System.out.println("---------------------------------DELETING CENTER AT " + id + "--------------------------------------------"); + Utils.performServerDelete(requestSpec, responseSpec, DELETE_CENTER_URL, ""); + } + }