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..6f805a234 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/FundsIntegrationTest.java @@ -0,0 +1,333 @@ +/** + * 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 static org.hamcrest.Matchers.*; +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; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +import java.util.*; + +/** + * Funds Integration Test for checking Funds Application. + */ +public class FundsIntegrationTest { + + private ResponseSpecification statusOkResponseSpec; + 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.statusOkResponseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + } + + @Test + public void testCreateFund() { + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + } + + @Test + public void testCreateFundWithEmptyName() { + FundsHelper fh = FundsHelper + .create(null) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + String jsonData = fh.toJSON(); + + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); + Assert.assertNull(fundID); + } + + @Test + public void testCreateFundWithEmptyExternalId() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(null) + .build(); + String jsonData = fh.toJSON(); + + final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec); + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + FundsHelper fh2 = FundsHelper + .create(fh.getName()) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + jsonData = fh2.toJSON(); + + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); + final Long fundID2 = createFund(jsonData, this.requestSpec, responseSpec); + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + FundsHelper fh2 = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(fh.getExternalId()) + .build(); + jsonData = fh2.toJSON(); + + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); + final Long fundID2 = createFund(jsonData, this.requestSpec, responseSpec); + Assert.assertNull(fundID2); + } + + @Test + public void testCreateFundWithInvalidName() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 120)) + .externalId(Utils.randomNameGenerator("fund-", 5)) + .build(); + String jsonData = fh.toJSON(); + + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); + Assert.assertNull(fundID); + } + + @Test + public void testCreateFundWithInvalidExternalId() { + FundsHelper fh = FundsHelper + .create(Utils.randomNameGenerator("", 10)) + .externalId(Utils.randomNameGenerator("fund-", 120)) + .build(); + String jsonData = fh.toJSON(); + + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(400).build(); + final Long fundID = createFund(jsonData, this.requestSpec, responseSpec); + Assert.assertNull(fundID); + } + + @Test + public void testRetrieveFund() { + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + jsonData = FundsResourceHandler.retrieveFund(fundID, this.requestSpec, this.statusOkResponseSpec); + FundsHelper fh2 = FundsHelper.fromJSON(jsonData); + + assertEquals(fh.getName(), fh2.getName()); + } + + @Test + public void testRetrieveAllFunds() { + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + List fhList = FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.statusOkResponseSpec); + + Assert.assertNotNull(fhList); + Assert.assertThat(fhList.size(), greaterThanOrEqualTo(1)); + Assert.assertThat(fhList, hasItem(fh)); + } + + @Test + public void testRetrieveUnknownFund() { + 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"); + } + + @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.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.statusOkResponseSpec); + + Assert.assertEquals(newName, fh2.getName()); + Assert.assertEquals(newExternalId, fh2.getExternalId()); + } + + @Test + public void testUpdateUnknownFund() { + String newName = Utils.randomNameGenerator("", 10); + String newExternalId = Utils.randomNameGenerator("fund-", 5); + ResponseSpecification responseSpec = new ResponseSpecBuilder().expectStatusCode(404).build(); + FundsHelper fh = FundsResourceHandler.updateFund(Long.MAX_VALUE, newName, newExternalId, this.requestSpec, + responseSpec); + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + 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, + responseSpec); + + Assert.assertNull(fh2); + } + + @Test + public void testUpdateFundWithNewExternalId() { + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + String newExternalId = Utils.randomNameGenerator("fund-", 5); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, null, newExternalId, this.requestSpec, this.statusOkResponseSpec); + + Assert.assertEquals(newExternalId, fh2.getExternalId()); + } + + @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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + 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, + responseSpec); + + Assert.assertNull(fh2); + } + + @Test + public void testUpdateFundWithNewName() { + 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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + String newName = Utils.randomNameGenerator("", 10); + FundsHelper fh2 = FundsResourceHandler.updateFund(fundID, newName, null, this.requestSpec, this.statusOkResponseSpec); + + Assert.assertEquals(newName, fh2.getName()); + } + + @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.statusOkResponseSpec); + Assert.assertNotNull(fundID); + + 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.statusOkResponseSpec); + 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, + final RequestSpecification requestSpec, + final ResponseSpecification 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/FundsHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java new file mode 100644 index 000000000..cc05dd869 --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsHelper.java @@ -0,0 +1,98 @@ +/** + * 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 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, this.externalId); + } + + } + + private String name; + private String externalId; + private Long resourceId; + + FundsHelper() { + super(); + } + + private FundsHelper(final String name, + final String externalId) { + super(); + this.name = name; + this.externalId = externalId; + } + + public String getName() { + return this.name; + } + + public String getExternalId() { + return this.externalId; + } + + public Long getResourceId() { + return this.resourceId; + } + + public String toJSON() { + return new Gson().toJson(this); + } + + public static FundsHelper fromJSON(final String jsonData) { + return new Gson().fromJson(jsonData, FundsHelper.class); + } + + public static Builder create(final String name) { + return new Builder(name); + } + + @Override + public int hashCode() { + if (this.name != null) { + return this.name.hashCode(); + } + return super.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 new file mode 100644 index 000000000..e27e1377c --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/funds/FundsResourceHandler.java @@ -0,0 +1,58 @@ +/** + * 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.common.reflect.TypeToken; +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; + List> list = Utils.performServerGet(requestSpec, responseSpec, URL, ""); + final String jsonData = new Gson().toJson(list); + return new Gson().fromJson(jsonData, new TypeToken>(){}.getType()); + } + + public static String retrieveFund(final Long fundID, + final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final String URL = FUNDS_URL + "/" + fundID + "?" + Utils.TENANT_IDENTIFIER; + 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) { + 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"); + final String jsonData = new Gson().toJson(response); + return new Gson().fromJson(jsonData, FundsHelper.class); + } + +}