Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support and test creation of Swift accounts via API #5852

Merged
merged 4 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ protected final void verifyEmptyForm(File jsonForm, String paymentMethodId, Stri
Object.class);
assertNotNull(emptyForm);

// TODO remove 'false' condition to enable creation of SWIFT accounts in future PR.
if (false && paymentMethodId.equals("SWIFT_ID")) {
if (paymentMethodId.equals("SWIFT_ID")) {
assertEquals(getSwiftFormComments(), emptyForm.get(PROPERTY_NAME_JSON_COMMENTS));
} else {
assertEquals(PROPERTY_VALUE_JSON_COMMENTS, emptyForm.get(PROPERTY_NAME_JSON_COMMENTS));
Expand Down Expand Up @@ -216,24 +215,16 @@ protected final String getCompletedFormAsJsonString() {

protected final String getCommaDelimitedFiatCurrencyCodes(Collection<FiatCurrency> fiatCurrencies) {
return fiatCurrencies.stream()
.sorted(TradeCurrency::compareTo) // note: sorted by ccy name, not ccy code
.sorted(Comparator.comparing(TradeCurrency::getCode))
.map(c -> c.getCurrency().getCurrencyCode())
.collect(Collectors.joining(","));
}

protected final String getCommaDelimitedTradeCurrencyCodes(List<TradeCurrency> tradeCurrencies) {
return tradeCurrencies.stream()
.sorted(Comparator.comparing(TradeCurrency::getCode)) // sorted by code
.map(c -> c.getCode())
.collect(Collectors.joining(","));
}

protected final List<String> getSwiftFormComments() {
List<String> comments = new ArrayList<>();
comments.addAll(PROPERTY_VALUE_JSON_COMMENTS);
// List<String> wrappedSwiftComments = Res.getWrappedAsList("payment.swift.info", 110);
// comments.addAll(wrappedSwiftComments);
// comments.add("See https://bisq.wiki/SWIFT");
List<String> wrappedSwiftComments = Res.getWrappedAsList("payment.swift.info.account", 110);
comments.addAll(wrappedSwiftComments);
return comments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.apitest.method.payment;

import bisq.core.locale.FiatCurrency;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.AdvancedCashAccount;
import bisq.core.payment.AliPayAccount;
Expand All @@ -43,6 +44,7 @@
import bisq.core.payment.SepaAccount;
import bisq.core.payment.SepaInstantAccount;
import bisq.core.payment.SpecificBanksAccount;
import bisq.core.payment.SwiftAccount;
import bisq.core.payment.SwishAccount;
import bisq.core.payment.TransferwiseAccount;
import bisq.core.payment.USPostalMoneyOrderAccount;
Expand All @@ -53,12 +55,14 @@
import bisq.core.payment.payload.CashDepositAccountPayload;
import bisq.core.payment.payload.SameBankAccountPayload;
import bisq.core.payment.payload.SpecificBanksAccountPayload;
import bisq.core.payment.payload.SwiftAccountPayload;

import io.grpc.StatusRuntimeException;

import java.io.File;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -79,6 +83,7 @@
import static bisq.cli.table.builder.TableType.PAYMENT_ACCOUNT_TBL;
import static bisq.core.locale.CurrencyUtil.*;
import static bisq.core.payment.payload.PaymentMethod.*;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -806,7 +811,6 @@ public void testCreateSpecificBanksAccount(TestInfo testInfo) {
print(paymentAccount);
}

/*
@Test
public void testCreateSwiftAccount(TestInfo testInfo) {
// https://www.theswiftcodes.com
Expand All @@ -816,7 +820,8 @@ public void testCreateSwiftAccount(TestInfo testInfo) {
PROPERTY_NAME_BANK_SWIFT_CODE);
COMPLETED_FORM_MAP.put(PROPERTY_NAME_PAYMENT_METHOD_ID, SWIFT_ID);
COMPLETED_FORM_MAP.put(PROPERTY_NAME_ACCOUNT_NAME, "IT Swift Acct w/ DE Intermediary");
String allFiatCodes = getCommaDelimitedFiatCurrencyCodes(getAllSortedFiatCurrencies());
Collection<FiatCurrency> swiftCurrenciesSortedByCode = getAllSortedFiatCurrencies(comparing(TradeCurrency::getCode));
String allFiatCodes = getCommaDelimitedFiatCurrencyCodes(swiftCurrenciesSortedByCode);
COMPLETED_FORM_MAP.put(PROPERTY_NAME_TRADE_CURRENCIES, allFiatCodes);
COMPLETED_FORM_MAP.put(PROPERTY_NAME_SELECTED_TRADE_CURRENCY, EUR);
COMPLETED_FORM_MAP.put(PROPERTY_NAME_BANK_SWIFT_CODE, "PASCITMMFIR");
Expand All @@ -839,7 +844,7 @@ public void testCreateSwiftAccount(TestInfo testInfo) {
String jsonString = getCompletedFormAsJsonString(getSwiftFormComments());
SwiftAccount paymentAccount = (SwiftAccount) createPaymentAccount(aliceClient, jsonString);
verifyUserPayloadHasPaymentAccountWithId(aliceClient, paymentAccount.getId());
verifyAccountTradeCurrencies(getAllSortedFiatCurrencies(), paymentAccount);
verifyAccountTradeCurrencies(swiftCurrenciesSortedByCode, paymentAccount);
assertEquals(COMPLETED_FORM_MAP.get(PROPERTY_NAME_SELECTED_TRADE_CURRENCY),
paymentAccount.getSelectedTradeCurrency().getCode());
verifyCommonFormEntries(paymentAccount);
Expand All @@ -863,7 +868,6 @@ public void testCreateSwiftAccount(TestInfo testInfo) {
assertEquals(COMPLETED_FORM_MAP.get(PROPERTY_NAME_SALT), paymentAccount.getSaltAsHex());
print(paymentAccount);
}
*/

@Test
public void testCreateSwishAccount(TestInfo testInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testCreatePaymentAccount(TestInfo testInfo) {
test.testCreateSepaInstantAccount(testInfo);
test.testCreateSepaAccount(testInfo);
test.testCreateSpecificBanksAccount(testInfo);
// test.testCreateSwiftAccount(testInfo);
test.testCreateSwiftAccount(testInfo);
test.testCreateSwishAccount(testInfo);

test.testCreateTransferwiseAccountWith1TradeCurrency(testInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import bisq.core.locale.Country;
import bisq.core.locale.FiatCurrency;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.CountryBasedPaymentAccount;
import bisq.core.payment.MoneyGramAccount;
Expand All @@ -31,8 +32,6 @@
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import java.util.ArrayList;
Expand Down Expand Up @@ -62,6 +61,7 @@
import static java.util.Collections.unmodifiableMap;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.capitalize;

@Slf4j
class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
Expand Down Expand Up @@ -130,16 +130,13 @@ private void writeComments(JsonWriter out, PaymentAccount account) throws IOExce
for (String s : JSON_COMMENTS) {
out.value(s);
}
/*
if (account.isSwiftAccount()) {
if (account.hasPaymentMethodWithId("SWIFT_ID")) {
// Add extra comments for more complex swift account form.
List<String> wrappedSwiftComments = Res.getWrappedAsList("payment.swift.info", 110);
List<String> wrappedSwiftComments = Res.getWrappedAsList("payment.swift.info.account", 110);
for (String line : wrappedSwiftComments) {
out.value(line);
}
out.value("See https://bisq.wiki/SWIFT");
}
*/
out.endArray();
}

Expand Down Expand Up @@ -170,7 +167,7 @@ private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) {
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
});
Expand All @@ -189,7 +186,7 @@ private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) {
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand All @@ -206,7 +203,7 @@ private void writeSelectedTradeCurrencyField(JsonWriter out, PaymentAccount acco
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand Down Expand Up @@ -319,7 +316,7 @@ private String nextStringOrNull(JsonReader in) {
}
} catch (IOException ex) {
String errMsg = "cannot see next string in json reader";
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand All @@ -335,7 +332,7 @@ private Long nextLongOrNull(JsonReader in) {
}
} catch (IOException ex) {
String errMsg = "cannot see next long in json reader";
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand Down Expand Up @@ -394,8 +391,10 @@ else if (account.hasPaymentMethodWithId(PAYSERA_ID))
return getTradeCurrenciesInList(currencyCodes, getAllPayseraCurrencies());
else if (account.hasPaymentMethodWithId(REVOLUT_ID))
return getTradeCurrenciesInList(currencyCodes, getAllRevolutCurrencies());
/*else if (account.hasPaymentMethodWithId(SWIFT_ID))
return getTradeCurrenciesInList(currencyCodes, new ArrayList<>(getAllSortedFiatCurrencies()));*/
else if (account.hasPaymentMethodWithId(SWIFT_ID))
return getTradeCurrenciesInList(currencyCodes,
new ArrayList<>(getAllSortedFiatCurrencies(
comparing(TradeCurrency::getCode))));
else if (account.hasPaymentMethodWithId(TRANSFERWISE_ID))
return getTradeCurrenciesInList(currencyCodes, getAllTransferwiseCurrencies());
else if (account.hasPaymentMethodWithId(UPHOLD_ID))
Expand Down Expand Up @@ -468,7 +467,7 @@ private boolean didReadCountryField(JsonReader in, PaymentAccount account, Strin
} else {
String errMsg = format("cannot set the country on a %s",
paymentAccountType.getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".");
log.error(capitalize(errMsg) + ".");
throw new IllegalStateException("programmer error: " + errMsg);
}

Expand All @@ -489,7 +488,7 @@ private Class<? extends PaymentAccountPayload> getPaymentAccountPayloadType() {
} catch (Exception ex) {
String errMsg = format("cannot get the payload class for %s",
paymentAccountType.getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand All @@ -506,7 +505,7 @@ private PaymentAccount initNewPaymentAccount() {
| InvocationTargetException ex) {
String errMsg = format("cannot instantiate a new %s",
paymentAccountType.getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex);
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/bisq/core/locale/CurrencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ public static void setBaseCurrencyCode(String baseCurrencyCode) {
}

public static Collection<FiatCurrency> getAllSortedFiatCurrencies() {
return fiatCurrencyMapSupplier.get().values();
return fiatCurrencyMapSupplier.get().values(); // sorted by currency name
}

public static Collection<FiatCurrency> getAllSortedFiatCurrencies(Comparator comparator) {
return (List<FiatCurrency>) getAllSortedFiatCurrencies().stream()
.sorted(comparator) // sorted by comparator param
.collect(Collectors.toList());
}

private static Map<String, FiatCurrency> createFiatCurrencyMap() {
Expand Down
17 changes: 9 additions & 8 deletions core/src/main/java/bisq/core/payment/SwiftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.FiatCurrency;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.payload.SwiftAccountPayload;
import bisq.core.payment.payload.PaymentAccountPayload;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.payment.payload.SwiftAccountPayload;

import java.util.Comparator;
import java.util.List;
Expand All @@ -34,6 +34,7 @@
public final class SwiftAccount extends PaymentAccount {
public SwiftAccount() {
super(PaymentMethod.SWIFT);
selectAllTradeCurrencies();
}

@Override
Expand All @@ -45,13 +46,6 @@ public SwiftAccountPayload getPayload() {
return ((SwiftAccountPayload) this.paymentAccountPayload);
}

public void selectAllTradeCurrencies() {
List<FiatCurrency> currencyCodesSorted = CurrencyUtil.getAllSortedFiatCurrencies().stream()
.sorted(Comparator.comparing(TradeCurrency::getCode))
.collect(Collectors.toList());
tradeCurrencies.addAll(currencyCodesSorted);
}

public String getMessageForBuyer() {
return "payment.swift.info.buyer";
}
Expand All @@ -63,4 +57,11 @@ public String getMessageForSeller() {
public String getMessageForAccountCreation() {
return "payment.swift.info.account";
}

private void selectAllTradeCurrencies() {
List<FiatCurrency> currencyCodesSorted = CurrencyUtil.getAllSortedFiatCurrencies().stream()
.sorted(Comparator.comparing(TradeCurrency::getCode))
.collect(Collectors.toList());
tradeCurrencies.addAll(currencyCodesSorted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public SwiftForm(PaymentAccount paymentAccount,

@Override
public void addFormForAddAccount() {
((SwiftAccount) paymentAccount).selectAllTradeCurrencies();
gridRowFrom = gridRow + 1;
addFieldsForBankEdit(true, this::setBankSwiftCode, this::setBankName, this::setBankBranch, this::setBankAddress);
addFieldsForBankEdit(false, this::setIntermediarySwiftCode, this::setIntermediaryName, this::setIntermediaryBranch, this::setIntermediaryAddress);
Expand Down