diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java index abfb03179..6bec406c6 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -40,6 +40,18 @@ public class FundsIntegrationTest { this.responseSpec = new ResponseSpecBuilder().build(); } + @Test + public void testCreateFaultyFund() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 120)) + .externalId(Utils.randomNameGenerator("fund-", 120)) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNull(fundID); + } + @Test public void testRetrieveFund() { FundsHelper fh = FundsHelper @@ -82,10 +94,43 @@ public class FundsIntegrationTest { assertEquals(map.get("userMessageGlobalisationCode"), "error.msg.resource.not.found"); } + @Test + public void testUpdateFund() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNotNull(fundID); + + String newName = Utils.randomNameGenerator("", 10); + String newExternalId = Utils.randomNameGenerator("fund-", 5); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, this.responseSpec); + + Assert.assertEquals(newName, fh2.getName()); + Assert.assertNull(fh2.getExternalId()); + } + + @Test + public void testUpdateUnknownFund() { + String newName = Utils.randomNameGenerator("", 10); + String newExternalId = Utils.randomNameGenerator("fund-", 5); + FundsHelper fh = FundsResourceHandler.updateFund(Long.MAX_VALUE, newName, newExternalId, this.requestSpec, this.responseSpec); + Assert.assertNull(fh); + } + private Long createFund(final String fundJSON, final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { - return new Long(String.valueOf(FundsResourceHandler.createFund(fundJSON, requestSpec, responseSpec))); + String fundId = String.valueOf(FundsResourceHandler.createFund(fundJSON, requestSpec, responseSpec)); + if (fundId.equals("null")) { + // Invalid JSON data parameters + return null; + } + + return new Long(fundId); } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java index 9e25df5b8..9c8f5dd99 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java @@ -29,7 +29,7 @@ public class FundsResourceHandler { final ResponseSpecification responseSpec) { final String URL = FUNDS_URL + "?" + Utils.TENANT_IDENTIFIER; List> list = Utils.performServerGet(requestSpec, responseSpec, URL, ""); - String jsonData = new Gson().toJson(list); + final String jsonData = new Gson().toJson(list); return new Gson().fromJson(jsonData, new TypeToken>(){}.getType()); } @@ -37,8 +37,29 @@ public class FundsResourceHandler { final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { final String URL = FUNDS_URL + "/" + fundID + "?" + Utils.TENANT_IDENTIFIER; - HashMap map = Utils.performServerGet(requestSpec, responseSpec, URL, ""); - return new Gson().toJson(map); + final HashMap response = Utils.performServerGet(requestSpec, responseSpec, URL, ""); + return new Gson().toJson(response); + } + + public static FundsHelper updateFund(final Long fundID, + final String newName, + final String newExternalId, + final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final HashMap map = new HashMap<>(); + if (!newName.isEmpty()) { + map.put("name", newName); + } else if (!newExternalId.isEmpty()) { + // to test that the externalId cannot be updated + map.put("externalId", newExternalId); + } + + String updateJSON = new Gson().toJson(map); + + final String URL = FUNDS_URL + "/" + fundID + "?" + Utils.TENANT_IDENTIFIER; + final HashMap response = Utils.performServerPut(requestSpec, responseSpec, URL, updateJSON, "changes"); + final String jsonData = new Gson().toJson(response); + return new Gson().fromJson(jsonData, FundsHelper.class); } }