From a15ce5ce8b606fc8474c671e3f7770ec388c6571 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Tue, 2 Dec 2014 13:39:32 +0530 Subject: [PATCH 1/9] Create integration test suite for Funds API --- .../FundsIntegrationTest.java | 33 ++++++++++++ .../common/funds/FundsHelper.java | 53 +++++++++++++++++++ .../common/funds/FundsResourceHandler.java | 15 ++++++ 3 files changed, 101 insertions(+) create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java new file mode 100644 index 000000000..5deac5140 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -0,0 +1,33 @@ +/** + * 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 org.junit.Before; +import org.mifosplatform.integrationtests.common.Utils; + +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; + +/** + * Funds Integration Test for checking Funds Application. + */ +public class FundsIntegrationTest { + + private ResponseSpecification responseSpec; + private RequestSpecification requestSpec; + + @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(); + } + +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java new file mode 100644 index 000000000..2fd3f24ec --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java @@ -0,0 +1,53 @@ +/** + * 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.funds; + +import com.google.gson.Gson; + +public class FundsHelper { + + public static class Builder { + + private String name; + + private Builder(final String name) { + this.name = name; + } + + public FundsHelper build() { + return new FundsHelper(this.name); + } + + } + + private String name; + + FundsHelper() { + super(); + } + + private FundsHelper(final String name) { + super(); + this.name = name; + } + + public String getName() { + return this.name; + } + + public String toJSON() { + return new Gson().toJson(this); + } + + public static FundsHelper fromJSON(final String jsonData) { + return new Gson().fromJson(jsonData, FundsHelper.class); + } + + public Builder create(final String name) { + return new Builder(name); + } + +} 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 new file mode 100644 index 000000000..4c133b20e --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java @@ -0,0 +1,15 @@ +/** + * 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.funds; + +import org.mifosplatform.integrationtests.common.Utils; + +public class FundsResourceHandler { + + private static final String FUNDS_URL = "/mifosng-provider/api/v1/funds"; + private static final String CREATE_FUNDS_URL = FUNDS_URL + "?" + Utils.TENANT_IDENTIFIER; + +} From 8f13a96f61a2d89d15ada74314db0cb88fc791f7 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Tue, 2 Dec 2014 21:26:16 +0530 Subject: [PATCH 2/9] Add test cases for funds retrieval --- .../FundsIntegrationTest.java | 53 ++++++++++++++++++- .../common/funds/FundsHelper.java | 9 +++- .../common/funds/FundsResourceHandler.java | 26 +++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) 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 5deac5140..bc78b77a2 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -5,8 +5,15 @@ */ package org.mifosplatform.integrationtests; +import static org.junit.Assert.assertEquals; + +import com.google.gson.Gson; +import org.junit.Assert; import org.junit.Before; +import org.junit.Test; import org.mifosplatform.integrationtests.common.Utils; +import org.mifosplatform.integrationtests.common.funds.FundsHelper; +import org.mifosplatform.integrationtests.common.funds.FundsResourceHandler; import com.jayway.restassured.builder.RequestSpecBuilder; import com.jayway.restassured.builder.ResponseSpecBuilder; @@ -14,6 +21,10 @@ 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; + /** * Funds Integration Test for checking Funds Application. */ @@ -27,7 +38,47 @@ public class FundsIntegrationTest { Utils.initializeRESTAssured(); this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); - this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.responseSpec = new ResponseSpecBuilder().build(); + } + + @Test + public void testRetrieveFund() { + FundsHelper fh = FundsHelper.create(Utils.randomNameGenerator("", 10)).build(); + String jsonData = fh.toJSON(); + + final Integer fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNotNull(fundID); + + jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.responseSpec); + FundsHelper fh2 = FundsHelper.fromJSON(jsonData); + + assertEquals(fh.getName(), fh2.getName()); + } + + @Test + public void testRetrieveAllFunds() { + FundsHelper fh = FundsHelper.create(Utils.randomNameGenerator("", 10)).build(); + String jsonData = fh.toJSON(); + + final Integer fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNotNull(fundID); + + List list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); + Assert.assertNotNull(list); + } + + @Test + public void testFundUnknown() { + String jsonData = FundsResourceHandler.retrieveFund(Integer.MAX_VALUE, this.requestSpec, this.responseSpec); + HashMap 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); } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java index 2fd3f24ec..f4fd4d968 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java @@ -24,8 +24,9 @@ public class FundsHelper { } private String name; + private Integer resourceId; - FundsHelper() { + public FundsHelper() { super(); } @@ -38,6 +39,10 @@ public class FundsHelper { return this.name; } + public Integer getResourceID() { + return this.resourceId; + } + public String toJSON() { return new Gson().toJson(this); } @@ -46,7 +51,7 @@ public class FundsHelper { return new Gson().fromJson(jsonData, FundsHelper.class); } - public Builder create(final String name) { + public static Builder create(final String name) { return new Builder(name); } 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 4c133b20e..c12cd569a 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 @@ -5,11 +5,37 @@ */ package org.mifosplatform.integrationtests.common.funds; +import com.google.gson.Gson; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; import org.mifosplatform.integrationtests.common.Utils; +import java.util.HashMap; +import java.util.List; + public class FundsResourceHandler { private static final String FUNDS_URL = "/mifosng-provider/api/v1/funds"; private static final String CREATE_FUNDS_URL = FUNDS_URL + "?" + Utils.TENANT_IDENTIFIER; + public static Integer createFund(final String fundJSON, + final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + return Utils.performServerPost(requestSpec, responseSpec, CREATE_FUNDS_URL, fundJSON, "resourceId"); + } + + public static List 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, + 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); + } + } From a51390ae371cd47c96915b7e9dfdb7a12b32038f Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 12:07:29 +0530 Subject: [PATCH 3/9] Improve testing patterns for funds retrieval --- .../FundsIntegrationTest.java | 39 +++++++++------ .../common/funds/FundsHelper.java | 47 +++++++++++++++++-- .../common/funds/FundsResourceHandler.java | 6 +-- 3 files changed, 70 insertions(+), 22 deletions(-) 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 bc78b77a2..80a6e8887 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -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 list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); + List> list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); + Assert.assertNotNull(list); + Assert.assertThat(list.size(), greaterThanOrEqualTo(1)); + + jsonData = new Gson().toJson(list); + List fhList = new Gson().fromJson(jsonData, new TypeToken>(){}.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 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))); } } diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java index f4fd4d968..2f8c79614 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java @@ -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; + } + } 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 c12cd569a..70fdd4795 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 @@ -24,13 +24,13 @@ public class FundsResourceHandler { return Utils.performServerPost(requestSpec, responseSpec, CREATE_FUNDS_URL, fundJSON, "resourceId"); } - public static List retrieveAllFunds(final RequestSpecification requestSpec, - final ResponseSpecification responseSpec) { + public static List> 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; From d0f214c834caaeb0719d02dc9343c4c2edb6aff6 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 12:21:49 +0530 Subject: [PATCH 4/9] Directly return list of funds helpers from retrieveAllFunds() --- .../integrationtests/FundsIntegrationTest.java | 10 +++------- .../common/funds/FundsResourceHandler.java | 7 +++++-- 2 files changed, 8 insertions(+), 9 deletions(-) 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 80a6e8887..abfb03179 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -8,7 +8,6 @@ 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; @@ -69,13 +68,10 @@ public class FundsIntegrationTest { final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); Assert.assertNotNull(fundID); - List> list = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); + List fhList = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); - Assert.assertNotNull(list); - Assert.assertThat(list.size(), greaterThanOrEqualTo(1)); - - jsonData = new Gson().toJson(list); - List fhList = new Gson().fromJson(jsonData, new TypeToken>(){}.getType()); + Assert.assertNotNull(fhList); + Assert.assertThat(fhList.size(), greaterThanOrEqualTo(1)); Assert.assertThat(fhList, hasItem(fh)); } 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 70fdd4795..9e25df5b8 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 @@ -5,6 +5,7 @@ */ package org.mifosplatform.integrationtests.common.funds; +import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.jayway.restassured.specification.RequestSpecification; import com.jayway.restassured.specification.ResponseSpecification; @@ -24,10 +25,12 @@ public class FundsResourceHandler { return Utils.performServerPost(requestSpec, responseSpec, CREATE_FUNDS_URL, fundJSON, "resourceId"); } - public static List> retrieveAllFunds(final RequestSpecification requestSpec, + public static List retrieveAllFunds(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { final String URL = FUNDS_URL + "?" + Utils.TENANT_IDENTIFIER; - return Utils.performServerGet(requestSpec, responseSpec, URL, ""); + List> list = Utils.performServerGet(requestSpec, responseSpec, URL, ""); + String jsonData = new Gson().toJson(list); + return new Gson().fromJson(jsonData, new TypeToken>(){}.getType()); } public static String retrieveFund(final Long fundID, From eb8c7103ae36a155b0f25a66b5453b7395b039ea Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 14:26:43 +0530 Subject: [PATCH 5/9] Create test cases to cover funds modifications, covering faulty behaviour --- .../FundsIntegrationTest.java | 47 ++++++++++++++++++- .../common/funds/FundsResourceHandler.java | 27 +++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) 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); } } From aaf7025d795e119298616548cc8b9234ea5f5727 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 14:47:47 +0530 Subject: [PATCH 6/9] Add test for duplicate creation --- .../FundsIntegrationTest.java | 25 +++++++++++++++---- .../common/funds/FundsResourceHandler.java | 11 ++------ 2 files changed, 22 insertions(+), 14 deletions(-) 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 6bec406c6..07cf09c50 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -37,7 +37,7 @@ public class FundsIntegrationTest { Utils.initializeRESTAssured(); this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); - this.responseSpec = new ResponseSpecBuilder().build(); + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); } @Test @@ -48,10 +48,25 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec.statusCode(400)); Assert.assertNull(fundID); } + @Test + public void testDuplicates() { + 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); + + final Long fundID2 = createFund(jsonData, this.requestSpec, this.responseSpec.statusCode(403)); + Assert.assertNull(fundID2); + } + @Test public void testRetrieveFund() { FundsHelper fh = FundsHelper @@ -89,7 +104,7 @@ public class FundsIntegrationTest { @Test public void testFundUnknown() { - String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, this.responseSpec); + String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, this.responseSpec.statusCode(404)); HashMap map = new Gson().fromJson(jsonData, HashMap.class); assertEquals(map.get("userMessageGlobalisationCode"), "error.msg.resource.not.found"); } @@ -110,14 +125,14 @@ public class FundsIntegrationTest { FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, this.responseSpec); Assert.assertEquals(newName, fh2.getName()); - Assert.assertNull(fh2.getExternalId()); + Assert.assertEquals(newExternalId, 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); + FundsHelper fh = FundsResourceHandler.updateFund(Long.MAX_VALUE, newName, newExternalId, this.requestSpec, this.responseSpec.statusCode(404)); Assert.assertNull(fh); } 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 9c8f5dd99..e27e1377c 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 @@ -46,15 +46,8 @@ public class FundsResourceHandler { 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); + FundsHelper fh = FundsHelper.create(newName).externalId(newExternalId).build(); + String updateJSON = new Gson().toJson(fh); final String URL = FUNDS_URL + "/" + fundID + "?" + Utils.TENANT_IDENTIFIER; final HashMap response = Utils.performServerPut(requestSpec, responseSpec, URL, updateJSON, "changes"); From 4dcb979b78139a08ff3fb41dddf945b4188c177c Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 17:48:51 +0530 Subject: [PATCH 7/9] Distribute testing logic in separate methods --- .../FundsIntegrationTest.java | 185 ++++++++++++++++-- .../common/funds/FundsHelper.java | 5 +- 2 files changed, 178 insertions(+), 12 deletions(-) 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 07cf09c50..139fce056 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -41,32 +41,107 @@ public class FundsIntegrationTest { } @Test - public void testCreateFaultyFund() { + public void testCreateFund() { FundsHelper fh = FundsHelper - .create(Utils.randomNameGenerator("", 120)) - .externalId(Utils.randomNameGenerator("fund-", 120)) + .create(Utils.randomNameGenerator("", 10)) + .externalId(Utils.randomNameGenerator("fund-", 5)) .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec.statusCode(400)); + final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNotNull(fundID); + } + + @Test + public void testCreateFundWithEmptyName() { + FundsHelper fh = FundsHelper + .create(null) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); Assert.assertNull(fundID); } @Test - public void testDuplicates() { + public void testCreateFundWithEmptyExternalId() { FundsHelper fh = FundsHelper - .create(Utils.randomNameGenerator("", 10)) - .externalId(Utils.randomNameGenerator("fund-", 5)) - .build(); + .create(Utils.randomNameGenerator("", 10)) + .externalId(null) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + Assert.assertNotNull(fundID); + } + + @Test + public void testCreateFundWithDuplicateName() { + 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); - final Long fundID2 = createFund(jsonData, this.requestSpec, this.responseSpec.statusCode(403)); + FundsHelper fh2 = FundsHelper + .create(fh.getName()) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + jsonData = fh2.toJSON(); + + final Long fundID2 = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(403).build()); Assert.assertNull(fundID2); } + @Test + public void testCreateFundWithDuplicateExternalId() { + 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); + + FundsHelper fh2 = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(fh.getExternalId()) + .build(); + jsonData = fh2.toJSON(); + + final Long fundID2 = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(403).build()); + Assert.assertNull(fundID2); + } + + @Test + public void testCreateFundWithInvalidName() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 120)) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); + Assert.assertNull(fundID); + } + + @Test + public void testCreateFundWithInvalidExternalId() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(Utils.randomNameGenerator("fund-", 120)) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); + Assert.assertNull(fundID); + } + @Test public void testRetrieveFund() { FundsHelper fh = FundsHelper @@ -103,7 +178,7 @@ public class FundsIntegrationTest { } @Test - public void testFundUnknown() { + public void testRetrieveUnknownFund() { String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, this.responseSpec.statusCode(404)); HashMap map = new Gson().fromJson(jsonData, HashMap.class); assertEquals(map.get("userMessageGlobalisationCode"), "error.msg.resource.not.found"); @@ -132,10 +207,98 @@ public class FundsIntegrationTest { 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.statusCode(404)); + FundsHelper fh = FundsResourceHandler.updateFund(Long.MAX_VALUE, newName, newExternalId, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(404).build()); Assert.assertNull(fh); } + @Test + public void testUpdateFundWithInvalidNewName() { + 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("", 120); + String newExternalId = Utils.randomNameGenerator("fund-", 5); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, + new ResponseSpecBuilder().expectStatusCode(400).build()); + + Assert.assertNull(fh2); + } + + @Test + public void testUpdateFundWithEmptyNewName() { + 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 newExternalId = Utils.randomNameGenerator("fund-", 5); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, newExternalId, this.requestSpec, this.responseSpec); + + Assert.assertNotNull(fh2); + } + + @Test + public void testUpdateFundWithInvalidNewExternalId() { + 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-", 120); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, + new ResponseSpecBuilder().expectStatusCode(400).build()); + + Assert.assertNull(fh2); + } + + @Test + public void testUpdateFundWithEmptyNewExternalId() { + 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); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, null, this.requestSpec, this.responseSpec); + + Assert.assertNotNull(fh2); + } + + @Test + public void testUpdateFundWithEmptyParams() { + 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); + + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, null, this.requestSpec, this.responseSpec); + + Assert.assertNotNull(fh2); + } + private Long createFund(final String fundJSON, final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java index 2f8c79614..cc05dd869 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java @@ -70,7 +70,10 @@ public class FundsHelper { @Override public int hashCode() { - return this.name.hashCode(); + if (this.name != null) { + return this.name.hashCode(); + } + return super.hashCode(); } @Override From c4db5b811a7043e84844c40d3240b351d8a3a091 Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 18:09:30 +0530 Subject: [PATCH 8/9] Change method names --- .../FundsIntegrationTest.java | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) 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 139fce056..adfe89228 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -60,7 +60,8 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); Assert.assertNull(fundID); } @@ -93,7 +94,8 @@ public class FundsIntegrationTest { .build(); jsonData = fh2.toJSON(); - final Long fundID2 = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(403).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); + final Long fundID2 = createFund(jsonData, this.requestSpec, responseSpec); Assert.assertNull(fundID2); } @@ -114,7 +116,8 @@ public class FundsIntegrationTest { .build(); jsonData = fh2.toJSON(); - final Long fundID2 = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(403).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); + final Long fundID2 = createFund(jsonData, this.requestSpec, responseSpec); Assert.assertNull(fundID2); } @@ -126,7 +129,8 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); Assert.assertNull(fundID); } @@ -138,7 +142,8 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, new ResponseSpecBuilder().expectStatusCode(400).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); Assert.assertNull(fundID); } @@ -207,7 +212,9 @@ public class FundsIntegrationTest { 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, new ResponseSpecBuilder().expectStatusCode(404).build()); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); + FundsHelper fh = FundsResourceHandler.updateFund(Long.MAX_VALUE, newName, newExternalId, this.requestSpec, + responseSpec); Assert.assertNull(fh); } @@ -224,14 +231,15 @@ public class FundsIntegrationTest { String newName = Utils.randomNameGenerator("", 120); String newExternalId = Utils.randomNameGenerator("fund-", 5); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, - new ResponseSpecBuilder().expectStatusCode(400).build()); + responseSpec); Assert.assertNull(fh2); } @Test - public void testUpdateFundWithEmptyNewName() { + public void testUpdateFundWithNewExternalId() { FundsHelper fh = FundsHelper .create(Utils.randomNameGenerator("", 10)) .externalId(Utils.randomNameGenerator("fund-", 5)) @@ -244,7 +252,7 @@ public class FundsIntegrationTest { String newExternalId = Utils.randomNameGenerator("fund-", 5); FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, newExternalId, this.requestSpec, this.responseSpec); - Assert.assertNotNull(fh2); + Assert.assertEquals(newExternalId, fh2.getExternalId()); } @Test @@ -260,14 +268,15 @@ public class FundsIntegrationTest { String newName = Utils.randomNameGenerator("", 10); String newExternalId = Utils.randomNameGenerator("fund-", 120); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, - new ResponseSpecBuilder().expectStatusCode(400).build()); + responseSpec); Assert.assertNull(fh2); } @Test - public void testUpdateFundWithEmptyNewExternalId() { + public void testUpdateFundWithNewName() { FundsHelper fh = FundsHelper .create(Utils.randomNameGenerator("", 10)) .externalId(Utils.randomNameGenerator("fund-", 5)) @@ -280,7 +289,7 @@ public class FundsIntegrationTest { String newName = Utils.randomNameGenerator("", 10); FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, null, this.requestSpec, this.responseSpec); - Assert.assertNotNull(fh2); + Assert.assertEquals(newName, fh2.getName()); } @Test @@ -296,7 +305,16 @@ public class FundsIntegrationTest { FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, null, this.requestSpec, this.responseSpec); - Assert.assertNotNull(fh2); + Assert.assertNull(fh2.getName()); + Assert.assertNull(fh2.getExternalId()); + + // assert that there was no change in + // the name and external ID of the fund + jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.responseSpec); + FundsHelper fh3 = new Gson().fromJson(jsonData, FundsHelper.class); + + Assert.assertEquals(fh.getName(), fh3.getName()); + Assert.assertEquals(fh.getExternalId(), fh3.getExternalId()); } private Long createFund(final String fundJSON, From 8ad5324fb67a01aa58ea0f50b0e1a37259632b4e Mon Sep 17 00:00:00 2001 From: Mohammed Nafees Date: Wed, 3 Dec 2014 18:21:22 +0530 Subject: [PATCH 9/9] Rename global response spec to be more clear --- .../FundsIntegrationTest.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) 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 adfe89228..6f805a234 100644 --- a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -29,7 +29,7 @@ import java.util.*; */ public class FundsIntegrationTest { - private ResponseSpecification responseSpec; + private ResponseSpecification statusOkResponseSpec; private RequestSpecification requestSpec; @Before @@ -37,7 +37,7 @@ public class FundsIntegrationTest { Utils.initializeRESTAssured(); this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); - this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.statusOkResponseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); } @Test @@ -48,7 +48,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); } @@ -73,7 +73,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); } @@ -85,7 +85,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); FundsHelper fh2 = FundsHelper @@ -107,7 +107,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); FundsHelper fh2 = FundsHelper @@ -155,10 +155,10 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); - jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.responseSpec); + jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.statusOkResponseSpec); FundsHelper fh2 = FundsHelper.fromJSON(jsonData); assertEquals(fh.getName(), fh2.getName()); @@ -172,10 +172,10 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); - List fhList = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.responseSpec); + List fhList = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fhList); Assert.assertThat(fhList.size(), greaterThanOrEqualTo(1)); @@ -184,7 +184,8 @@ public class FundsIntegrationTest { @Test public void testRetrieveUnknownFund() { - String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, this.responseSpec.statusCode(404)); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); + String jsonData = FundsResourceHandler.retrieveFund(Long.MAX_VALUE, this.requestSpec, responseSpec); HashMap map = new Gson().fromJson(jsonData, HashMap.class); assertEquals(map.get("userMessageGlobalisationCode"), "error.msg.resource.not.found"); } @@ -197,12 +198,12 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); 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); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, newExternalId, this.requestSpec, this.statusOkResponseSpec); Assert.assertEquals(newName, fh2.getName()); Assert.assertEquals(newExternalId, fh2.getExternalId()); @@ -226,7 +227,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); String newName = Utils.randomNameGenerator("", 120); @@ -246,11 +247,11 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); String newExternalId = Utils.randomNameGenerator("fund-", 5); - FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, newExternalId, this.requestSpec, this.responseSpec); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, newExternalId, this.requestSpec, this.statusOkResponseSpec); Assert.assertEquals(newExternalId, fh2.getExternalId()); } @@ -263,7 +264,7 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); String newName = Utils.randomNameGenerator("", 10); @@ -283,11 +284,11 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); String newName = Utils.randomNameGenerator("", 10); - FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, null, this.requestSpec, this.responseSpec); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, null, this.requestSpec, this.statusOkResponseSpec); Assert.assertEquals(newName, fh2.getName()); } @@ -300,17 +301,17 @@ public class FundsIntegrationTest { .build(); String jsonData = fh.toJSON(); - final Long fundID = createFund(jsonData, this.requestSpec, this.responseSpec); + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); Assert.assertNotNull(fundID); - FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, null, this.requestSpec, this.responseSpec); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, null, this.requestSpec, this.statusOkResponseSpec); Assert.assertNull(fh2.getName()); Assert.assertNull(fh2.getExternalId()); // assert that there was no change in // the name and external ID of the fund - jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.responseSpec); + jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.statusOkResponseSpec); FundsHelper fh3 = new Gson().fromJson(jsonData, FundsHelper.class); Assert.assertEquals(fh.getName(), fh3.getName());