From e5e96319c2d09e3189d0cde5ce889c47eb9f8da8 Mon Sep 17 00:00:00 2001 From: Anthony Liu Date: Sun, 7 Dec 2014 04:56:23 -0500 Subject: [PATCH] Integration Test Suite for Currencies API Includes tests for updates and for retrieves Formatting --- .../integrationtests/CurrenciesTest.java | 81 +++++++++++ .../common/CurrenciesHelper.java | 67 +++++++++ .../common/CurrencyDomain.java | 127 ++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CurrenciesTest.java create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrenciesHelper.java create mode 100644 mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrencyDomain.java diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CurrenciesTest.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CurrenciesTest.java new file mode 100644 index 000000000..b3fa3850e --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/CurrenciesTest.java @@ -0,0 +1,81 @@ +/** + * 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.util.ArrayList; +import java.util.Collections; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mifosplatform.integrationtests.common.CurrenciesHelper; +import org.mifosplatform.integrationtests.common.CurrencyDomain; +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; + +@SuppressWarnings({ "unused", "rawtypes" }) +public class CurrenciesTest { + + 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(); + } + + @Test + public void testCurrency() { + + CurrencyDomain currency = CurrenciesHelper.getCurrencybyCode(requestSpec, responseSpec, "USD"); + CurrencyDomain usd = CurrencyDomain.create("USD", "US Dollar", 2, "$", "currency.USD", "US Dollar ($)").build(); + + Assert.assertTrue(currency.getDecimalPlaces() >= 0); + Assert.assertNotNull(currency.getName()); + Assert.assertNotNull(currency.getDisplaySymbol()); + Assert.assertNotNull(currency.getDisplayLabel()); + Assert.assertNotNull(currency.getNameCode()); + + Assert.assertEquals(usd, currency); + } + + public void testCurrencySelection() { + + // Test updation + ArrayList currenciestoUpdate = new ArrayList(); + currenciestoUpdate.add("KES"); + currenciestoUpdate.add("BND"); + currenciestoUpdate.add("LBP"); + currenciestoUpdate.add("GHC"); + currenciestoUpdate.add("USD"); + + ArrayList currenciesOutput = CurrenciesHelper.updateSelectedCurrencies(this.requestSpec, this.responseSpec, + currenciestoUpdate); + Assert.assertNotNull(currenciesOutput); + + Assert.assertEquals("Verifying Do Outputed Currencies Match after Updation", currenciestoUpdate, currenciesOutput); + + // Test that output matches updation + ArrayList currenciesBeforeUpdate = new ArrayList(); + for (String e : currenciestoUpdate) { + currenciesBeforeUpdate.add(CurrenciesHelper.getCurrencybyCode(requestSpec, responseSpec, e)); + } + Collections.sort(currenciesBeforeUpdate); + + ArrayList currenciesAfterUpdate = CurrenciesHelper.getSelectedCurrencies(requestSpec, responseSpec); + Assert.assertNotNull(currenciesAfterUpdate); + + Assert.assertEquals("Verifying Do Selected Currencies Match after Updation", currenciesBeforeUpdate, currenciesAfterUpdate); + } +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrenciesHelper.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrenciesHelper.java new file mode 100644 index 000000000..8e3e7544b --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrenciesHelper.java @@ -0,0 +1,67 @@ +/** + * 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.ArrayList; +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({ "unused", "rawtypes", "unchecked" }) +public class CurrenciesHelper { + + private static final String CURRENCIES_URL = "/mifosng-provider/api/v1/currencies"; + + public static ArrayList getAllCurrencies(final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final String GET_ALL_CURRENCIES_URL = CURRENCIES_URL + "?" + Utils.TENANT_IDENTIFIER; + System.out.println("------------------------ RETRIEVING ALL CURRENCIES -------------------------"); + final HashMap response = Utils.performServerGet(requestSpec, responseSpec, GET_ALL_CURRENCIES_URL, ""); + ArrayList selectedCurrencyOptions = (ArrayList) response.get("selectedCurrencyOptions"); + ArrayList currencyOptions = (ArrayList) response.get("currencyOptions"); + currencyOptions.addAll(selectedCurrencyOptions); + final String jsonData = new Gson().toJson(new ArrayList(selectedCurrencyOptions)); + return new Gson().fromJson(jsonData, new TypeToken>() {}.getType()); + } + + public static ArrayList getSelectedCurrencies(final RequestSpecification requestSpec, + final ResponseSpecification responseSpec) { + final String GET_ALL_SELECTED_CURRENCIES_URL = CURRENCIES_URL + "?fields=selectedCurrencyOptions" + "&" + Utils.TENANT_IDENTIFIER; + System.out.println("------------------------ RETRIEVING ALL SELECTED CURRENCIES -------------------------"); + final HashMap response = Utils.performServerGet(requestSpec, responseSpec, GET_ALL_SELECTED_CURRENCIES_URL, ""); + final String jsonData = new Gson().toJson(response.get("selectedCurrencyOptions")); + return new Gson().fromJson(jsonData, new TypeToken>() {}.getType()); + } + + public static CurrencyDomain getCurrencybyCode(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final String code) { + ArrayList currenciesList = getAllCurrencies(requestSpec, responseSpec); + for (CurrencyDomain e : currenciesList) { + if (e.getCode().equals(code)) return e; + } + return null; + } + + public static ArrayList updateSelectedCurrencies(final RequestSpecification requestSpec, + final ResponseSpecification responseSpec, final ArrayList currencies) { + final String CURRENCIES_UPDATE_URL = CURRENCIES_URL + "?" + Utils.TENANT_IDENTIFIER; + System.out.println("---------------------------------UPDATE SELECTED CURRENCIES LIST---------------------------------------------"); + return (ArrayList) Utils.performServerPut(requestSpec, responseSpec, CURRENCIES_UPDATE_URL, currenciesToJSON(currencies), + "currencies"); + } + + private static String currenciesToJSON(final ArrayList currencies) { + HashMap map = new HashMap<>(); + for (String e : currencies) { + map.put("currencies", e); + } + System.out.println("map : " + map); + return new Gson().toJson(map); + } +} diff --git a/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrencyDomain.java b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrencyDomain.java new file mode 100644 index 000000000..2a4a5dfcb --- /dev/null +++ b/mifosng-provider/src/integrationTest/java/org/mifosplatform/integrationtests/common/CurrencyDomain.java @@ -0,0 +1,127 @@ +/** + * 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 com.google.gson.Gson; + +public class CurrencyDomain implements Comparable { + + public static class Builder { + + private String code; + private String name; + private int decimalPlaces; + private String displaySymbol; + private String nameCode; + private String displayLabel; + + private Builder(final String code, final String name, final int decimalPlaces, final String displaySymbol, final String nameCode, + final String displayLabel) { + this.code = code; + this.name = name; + this.decimalPlaces = decimalPlaces; + this.displaySymbol = displaySymbol; + this.nameCode = nameCode; + this.displayLabel = displayLabel; + } + + public CurrencyDomain build() { + return new CurrencyDomain(this.code, this.name, this.decimalPlaces, this.displaySymbol, this.nameCode, this.displayLabel); + } + } + + private String code; + private String name; + private int decimalPlaces; + private String displaySymbol; + private String nameCode; + private String displayLabel; + + CurrencyDomain() { + super(); + } + + private CurrencyDomain(final String code, final String name, final int decimalPlaces, final String displaySymbol, + final String nameCode, final String displayLabel) { + super(); + this.code = code; + this.name = name; + this.decimalPlaces = decimalPlaces; + this.displaySymbol = displaySymbol; + this.nameCode = nameCode; + this.displayLabel = displayLabel; + } + + public String getCode() { + return this.code; + } + + public int getDecimalPlaces() { + return this.decimalPlaces; + } + + public String getDisplaySymbol() { + return this.displaySymbol; + } + + public String getNameCode() { + return this.nameCode; + } + + public String getDisplayLabel() { + return this.displayLabel; + } + + public String getName() { + return this.name; + } + + 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 String code, final String name, final int decimalPlaces, final String displaySymbol, + final String nameCode, final String displayLabel) { + return new Builder(code, name, decimalPlaces, displaySymbol, nameCode, displayLabel); + } + + @Override + public int hashCode() { + int hash = 1; + + if (this.name != null) hash += this.name.hashCode(); + if (this.code != null) hash += this.code.hashCode(); + if (this.decimalPlaces >= 0) hash += this.decimalPlaces; + if (this.displaySymbol != null) hash += this.displaySymbol.hashCode(); + if (this.nameCode != null) hash += this.nameCode.hashCode(); + if (this.displayLabel != null) hash += this.displayLabel.hashCode(); + + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { return true; } + + if (!(obj instanceof CurrencyDomain)) return false; + + CurrencyDomain cd = (CurrencyDomain) obj; + + if (this.name.equals(cd.name) && this.code.equals(cd.code) && this.decimalPlaces == cd.decimalPlaces + && this.displaySymbol.equals(cd.displaySymbol) && this.nameCode.equals(cd.nameCode) + && this.displayLabel.equals(cd.displayLabel)) return true; + return false; + } + + @Override + public int compareTo(CurrencyDomain cd) { + return this.name.compareTo(cd.getName()); + } +}