diff --git a/assets/src/main/java/bisq/asset/coins/REOSC.java b/assets/src/main/java/bisq/asset/coins/REOSC.java new file mode 100644 index 00000000000..382adc81e92 --- /dev/null +++ b/assets/src/main/java/bisq/asset/coins/REOSC.java @@ -0,0 +1,28 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.asset.coins; + +import bisq.asset.Coin; +import bisq.asset.EtherAddressValidator; + +public class REOSC extends Coin { + + public REOSC() { + super("REOSC Ecosystem", "REOSC", new EtherAddressValidator()); + } +} diff --git a/assets/src/main/resources/META-INF/services/bisq.asset.Asset b/assets/src/main/resources/META-INF/services/bisq.asset.Asset index d093cd01a27..4db7834ced9 100644 --- a/assets/src/main/resources/META-INF/services/bisq.asset.Asset +++ b/assets/src/main/resources/META-INF/services/bisq.asset.Asset @@ -36,6 +36,7 @@ bisq.asset.coins.PIVX bisq.asset.coins.PZDC bisq.asset.coins.QMCoin bisq.asset.coins.Radium +bisq.asset.coins.REOSC bisq.asset.coins.Ryo bisq.asset.coins.Siafund bisq.asset.coins.Spectrecoin diff --git a/assets/src/test/java/bisq/asset/coins/REOSCTest.java b/assets/src/test/java/bisq/asset/coins/REOSCTest.java new file mode 100644 index 00000000000..a70ec6e283d --- /dev/null +++ b/assets/src/test/java/bisq/asset/coins/REOSCTest.java @@ -0,0 +1,42 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.asset.coins; + +import bisq.asset.AbstractAssetTest; + +import org.junit.Test; + +public class REOSCTest extends AbstractAssetTest { + + public REOSCTest() { + super(new REOSC()); + } + + @Test + public void testValidAddresses() { + assertValidAddress("0x0d81d9e21bd7c5bb095535624dcb0759e64b3899"); + assertValidAddress("0d81d9e21bd7c5bb095535624dcb0759e64b3899"); + } + + @Test + public void testInvalidAddresses() { + assertInvalidAddress("0x65767ec6d4d3d18a200842352485cdc37cbf3a216"); + assertInvalidAddress("0x65767ec6d4d3d18a200842352485cdc37cbf3a2g"); + assertInvalidAddress("65767ec6d4d3d18a200842352485cdc37cbf3a2g"); + } +} diff --git a/core/src/main/java/bisq/core/locale/CurrencyUtil.java b/core/src/main/java/bisq/core/locale/CurrencyUtil.java deleted file mode 100644 index b62510df8e6..00000000000 --- a/core/src/main/java/bisq/core/locale/CurrencyUtil.java +++ /dev/null @@ -1,500 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.core.locale; - -import bisq.core.app.BisqEnvironment; -import bisq.core.btc.BaseCurrencyNetwork; -import bisq.core.dao.governance.asset.AssetService; - -import bisq.asset.Asset; -import bisq.asset.AssetRegistry; -import bisq.asset.Coin; -import bisq.asset.Token; -import bisq.asset.coins.BSQ; - -import bisq.common.app.DevEnv; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Currency; -import java.util.List; -import java.util.Locale; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import lombok.extern.slf4j.Slf4j; - -import static com.google.common.base.Preconditions.checkArgument; - -@Slf4j -public class CurrencyUtil { - - public static void setup() { - setBaseCurrencyCode(BisqEnvironment.getBaseCurrencyNetwork().getCurrencyCode()); - } - - private static final AssetRegistry assetRegistry = new AssetRegistry(); - - private static String baseCurrencyCode = "BTC"; - private static List allSortedFiatCurrencies; - private static List allSortedCryptoCurrencies; - - public static void setBaseCurrencyCode(String baseCurrencyCode) { - CurrencyUtil.baseCurrencyCode = baseCurrencyCode; - } - - public static List getAllSortedFiatCurrencies() { - if (Objects.isNull(allSortedFiatCurrencies)) - allSortedFiatCurrencies = createAllSortedFiatCurrenciesList(); - - return allSortedFiatCurrencies; - } - - private static List createAllSortedFiatCurrenciesList() { - return CountryUtil.getAllCountries().stream() - .map(country -> getCurrencyByCountryCode(country.code)) - .distinct() - .sorted(TradeCurrency::compareTo) - .collect(Collectors.toList()); - } - - public static List getMainFiatCurrencies() { - TradeCurrency defaultTradeCurrency = getDefaultTradeCurrency(); - List list = new ArrayList<>(); - // Top traded currencies - list.add(new FiatCurrency("USD")); - list.add(new FiatCurrency("EUR")); - list.add(new FiatCurrency("GBP")); - list.add(new FiatCurrency("CAD")); - list.add(new FiatCurrency("AUD")); - list.add(new FiatCurrency("RUB")); - list.add(new FiatCurrency("INR")); - - list.sort(TradeCurrency::compareTo); - - FiatCurrency defaultFiatCurrency = - defaultTradeCurrency instanceof FiatCurrency ? (FiatCurrency) defaultTradeCurrency : null; - if (defaultFiatCurrency != null && list.contains(defaultFiatCurrency)) { - //noinspection SuspiciousMethodCalls - list.remove(defaultTradeCurrency); - list.add(0, defaultFiatCurrency); - } - return list; - } - - public static List getAllSortedCryptoCurrencies() { - if (allSortedCryptoCurrencies == null) - allSortedCryptoCurrencies = createAllSortedCryptoCurrenciesList(); - return allSortedCryptoCurrencies; - } - - private static List createAllSortedCryptoCurrenciesList() { - return getSortedAssetStream() - .map(CurrencyUtil::assetToCryptoCurrency) - .collect(Collectors.toList()); - } - - public static Stream getSortedAssetStream() { - return assetRegistry.stream() - .filter(CurrencyUtil::assetIsNotBaseCurrency) - .filter(asset -> isNotBsqOrBsqTradingActivated(asset, BisqEnvironment.getBaseCurrencyNetwork(), DevEnv.isDaoTradingActivated())) - .filter(asset -> assetMatchesNetworkIfMainnet(asset, BisqEnvironment.getBaseCurrencyNetwork())) - .sorted(Comparator.comparing(Asset::getName)); - } - - public static List getMainCryptoCurrencies() { - final List result = new ArrayList<>(); - if (DevEnv.isDaoTradingActivated()) - result.add(new CryptoCurrency("BSQ", "BSQ")); - if (!baseCurrencyCode.equals("BTC")) - result.add(new CryptoCurrency("BTC", "Bitcoin")); - if (!baseCurrencyCode.equals("DASH")) - result.add(new CryptoCurrency("DASH", "Dash")); - result.add(new CryptoCurrency("DCR", "Decred")); - result.add(new CryptoCurrency("ETH", "Ether")); - result.add(new CryptoCurrency("ETC", "Ether Classic")); - result.add(new CryptoCurrency("GRC", "Gridcoin")); - if (!baseCurrencyCode.equals("LTC")) - result.add(new CryptoCurrency("LTC", "Litecoin")); - result.add(new CryptoCurrency("XMR", "Monero")); - result.add(new CryptoCurrency("MT", "Mycelium Token", true)); - result.add(new CryptoCurrency("NMC", "Namecoin")); - result.add(new CryptoCurrency("SC", "Siacoin")); - result.add(new CryptoCurrency("SF", "Siafund")); - result.add(new CryptoCurrency("UNO", "Unobtanium")); - result.add(new CryptoCurrency("ZEC", "Zcash")); - result.sort(TradeCurrency::compareTo); - - return result; - } - - // At OKPay you can exchange internally those currencies - public static List getAllAdvancedCashCurrencies() { - ArrayList currencies = new ArrayList<>(Arrays.asList( - new FiatCurrency("USD"), - new FiatCurrency("EUR"), - new FiatCurrency("GBP"), - new FiatCurrency("RUB"), - new FiatCurrency("UAH"), - new FiatCurrency("KZT"), - new FiatCurrency("BRL") - )); - currencies.sort(Comparator.comparing(TradeCurrency::getCode)); - return currencies; - } - - public static List getAllOKPayCurrencies() { - ArrayList currencies = new ArrayList<>(Arrays.asList( - new FiatCurrency("EUR"), - new FiatCurrency("USD"), - new FiatCurrency("GBP"), - new FiatCurrency("CHF"), - new FiatCurrency("RUB"), - new FiatCurrency("PLN"), - new FiatCurrency("JPY"), - new FiatCurrency("CAD"), - new FiatCurrency("AUD"), - new FiatCurrency("CZK"), - new FiatCurrency("NOK"), - new FiatCurrency("SEK"), - new FiatCurrency("DKK"), - new FiatCurrency("HRK"), - new FiatCurrency("HUF"), - new FiatCurrency("NZD"), - new FiatCurrency("RON"), - new FiatCurrency("TRY"), - new FiatCurrency("ZAR"), - new FiatCurrency("HKD"), - new FiatCurrency("CNY") - )); - currencies.sort(Comparator.comparing(TradeCurrency::getCode)); - return currencies; - } - - public static List getAllMoneyGramCurrencies() { - ArrayList currencies = new ArrayList<>(Arrays.asList( - new FiatCurrency("AED"), - new FiatCurrency("AUD"), - new FiatCurrency("BND"), - new FiatCurrency("CAD"), - new FiatCurrency("CHF"), - new FiatCurrency("CZK"), - new FiatCurrency("DKK"), - new FiatCurrency("EUR"), - new FiatCurrency("FJD"), - new FiatCurrency("GBP"), - new FiatCurrency("HKD"), - new FiatCurrency("HUF"), - new FiatCurrency("IDR"), - new FiatCurrency("ILS"), - new FiatCurrency("INR"), - new FiatCurrency("JPY"), - new FiatCurrency("KRW"), - new FiatCurrency("KWD"), - new FiatCurrency("LKR"), - new FiatCurrency("MAD"), - new FiatCurrency("MGA"), - new FiatCurrency("MXN"), - new FiatCurrency("MYR"), - new FiatCurrency("NOK"), - new FiatCurrency("NZD"), - new FiatCurrency("OMR"), - new FiatCurrency("PEN"), - new FiatCurrency("PGK"), - new FiatCurrency("PHP"), - new FiatCurrency("PKR"), - new FiatCurrency("PLN"), - new FiatCurrency("SAR"), - new FiatCurrency("SBD"), - new FiatCurrency("SCR"), - new FiatCurrency("SEK"), - new FiatCurrency("SGD"), - new FiatCurrency("THB"), - new FiatCurrency("TOP"), - new FiatCurrency("TRY"), - new FiatCurrency("TWD"), - new FiatCurrency("USD"), - new FiatCurrency("VND"), - new FiatCurrency("VUV"), - new FiatCurrency("WST"), - new FiatCurrency("XOF"), - new FiatCurrency("XPF"), - new FiatCurrency("ZAR") - )); - - currencies.sort(Comparator.comparing(TradeCurrency::getCode)); - return currencies; - } - - // https://support.uphold.com/hc/en-us/articles/202473803-Supported-currencies - public static List getAllUpholdCurrencies() { - ArrayList currencies = new ArrayList<>(Arrays.asList( - new FiatCurrency("USD"), - new FiatCurrency("EUR"), - new FiatCurrency("GBP"), - new FiatCurrency("CNY"), - new FiatCurrency("JPY"), - new FiatCurrency("CHF"), - new FiatCurrency("INR"), - new FiatCurrency("MXN"), - new FiatCurrency("AUD"), - new FiatCurrency("CAD"), - new FiatCurrency("HKD"), - new FiatCurrency("NZD"), - new FiatCurrency("SGD"), - new FiatCurrency("KES"), - new FiatCurrency("ILS"), - new FiatCurrency("DKK"), - new FiatCurrency("NOK"), - new FiatCurrency("SEK"), - new FiatCurrency("PLN"), - new FiatCurrency("ARS"), - new FiatCurrency("BRL"), - new FiatCurrency("AED"), - new FiatCurrency("PHP") - )); - - currencies.sort(Comparator.comparing(TradeCurrency::getCode)); - return currencies; - } - - //https://www.revolut.com/pa/faq#can-i-hold-multiple-currencies - public static List getAllRevolutCurrencies() { - ArrayList currencies = new ArrayList<>(Arrays.asList( - new FiatCurrency("USD"), - new FiatCurrency("GBP"), - new FiatCurrency("EUR"), - new FiatCurrency("PLN"), - new FiatCurrency("CHF"), - new FiatCurrency("DKK"), - new FiatCurrency("NOK"), - new FiatCurrency("SEK"), - new FiatCurrency("RON"), - new FiatCurrency("SGD"), - new FiatCurrency("HKD"), - new FiatCurrency("AUD"), - new FiatCurrency("NZD"), - new FiatCurrency("TRY"), - new FiatCurrency("ILS"), - new FiatCurrency("AED"), - new FiatCurrency("CAD"), - new FiatCurrency("HUF"), - new FiatCurrency("INR"), - new FiatCurrency("JPY"), - new FiatCurrency("MAD"), - new FiatCurrency("QAR"), - new FiatCurrency("THB"), - new FiatCurrency("ZAR") - )); - - currencies.sort(Comparator.comparing(TradeCurrency::getCode)); - return currencies; - } - - public static boolean isFiatCurrency(String currencyCode) { - try { - return currencyCode != null - && !currencyCode.isEmpty() - && !isCryptoCurrency(currencyCode) - && Currency.getInstance(currencyCode) != null; - } catch (Throwable t) { - return false; - } - } - - public static Optional getFiatCurrency(String currencyCode) { - return getAllSortedFiatCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny(); - } - - @SuppressWarnings("WeakerAccess") - /** - * We return true if it is BTC or any of our currencies available in the assetRegistry. - * For removed assets it would fail as they are not found but we don't want to conclude that they are fiat then. - * As the caller might not deal with the case that a currency can be neither a cryptoCurrency nor Fiat if not found - * we return true as well in case we have no fiat currency for the code. - * - * As we use a boolean result for isCryptoCurrency and isFiatCurrency we do not treat missing currencies correctly. - * To throw an exception might be an option but that will require quite a lot of code change, so we don't do that - * for the moment, but could be considered for the future. Another maybe better option is to introduce a enum which - * contains 3 entries (CryptoCurrency, Fiat, Undefined). - */ - public static boolean isCryptoCurrency(String currencyCode) { - // Some tests call that method with null values. Should be fixed in the tests but to not break them return false. - if (currencyCode == null) - return false; - - // BTC is not part of our assetRegistry so treat it extra here. Other old base currencies (LTC, DOGE, DASH) - // are not supported anymore so we can ignore that case. - if (currencyCode.equals("BTC")) - return true; - - // If we find the code in our assetRegistry we return true. - // It might be that an asset was removed from the assetsRegistry, we deal with such cases below by checking if - // it is a fiat currency - if (getCryptoCurrency(currencyCode).isPresent()) - return true; - - // In case the code is from a removed asset we cross check if there exist a fiat currency with that code, - // if we don't find a fiat currency we treat it as a crypto currency. - if (!getFiatCurrency(currencyCode).isPresent()) - return true; - - // If we would have found a fiat currency we return false - return false; - } - - public static Optional getCryptoCurrency(String currencyCode) { - return getAllSortedCryptoCurrencies().stream().filter(e -> e.getCode().equals(currencyCode)).findAny(); - } - - public static Optional getTradeCurrency(String currencyCode) { - Optional fiatCurrencyOptional = getFiatCurrency(currencyCode); - if (isFiatCurrency(currencyCode) && fiatCurrencyOptional.isPresent()) - return Optional.of(fiatCurrencyOptional.get()); - - Optional cryptoCurrencyOptional = getCryptoCurrency(currencyCode); - if (isCryptoCurrency(currencyCode) && cryptoCurrencyOptional.isPresent()) - return Optional.of(cryptoCurrencyOptional.get()); - - return Optional.empty(); - } - - public static FiatCurrency getCurrencyByCountryCode(String countryCode) { - if (countryCode.equals("XK")) - return new FiatCurrency("EUR"); - - Currency currency = Currency.getInstance(new Locale(LanguageUtil.getDefaultLanguage(), countryCode)); - return new FiatCurrency(currency.getCurrencyCode()); - } - - - public static String getNameByCode(String currencyCode) { - if (isCryptoCurrency(currencyCode)) { - // We might not find the name in case we have a call for a removed asset. - // If BTC is the code (used in tests) we also want return Bitcoin as name. - String btcOrRemovedAsset = "BTC".equals(currencyCode) ? "Bitcoin" : Res.get("shared.na"); - return getCryptoCurrency(currencyCode).map(TradeCurrency::getName).orElse(btcOrRemovedAsset); - } - try { - return Currency.getInstance(currencyCode).getDisplayName(); - } catch (Throwable t) { - log.debug("No currency name available " + t.getMessage()); - return currencyCode; - } - } - - public static Optional findCryptoCurrencyByName(String currencyName) { - return getAllSortedCryptoCurrencies().stream() - .filter(e -> e.getName().equals(currencyName)) - .findAny(); - } - - public static String getNameAndCode(String currencyCode) { - return getNameByCode(currencyCode) + " (" + currencyCode + ")"; - } - - public static TradeCurrency getDefaultTradeCurrency() { - return GlobalSettings.getDefaultTradeCurrency(); - } - - private static boolean assetIsNotBaseCurrency(Asset asset) { - return !assetMatchesCurrencyCode(asset, baseCurrencyCode); - } - - // TODO We handle assets of other types (Token, ERC20) as matching the network which is not correct. - // We should add support for network property in those tokens as well. - public static boolean assetMatchesNetwork(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork) { - return !(asset instanceof Coin) || - ((Coin) asset).getNetwork().name().equals(baseCurrencyNetwork.getNetwork()); - } - - // We only check for coins not other types of assets (TODO network check should be supported for all assets) - public static boolean assetMatchesNetworkIfMainnet(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork) { - return !(asset instanceof Coin) || - coinMatchesNetworkIfMainnet((Coin) asset, baseCurrencyNetwork); - } - - // We want all coins available also in testnet or regtest for testing purpose - public static boolean coinMatchesNetworkIfMainnet(Coin coin, BaseCurrencyNetwork baseCurrencyNetwork) { - boolean matchesNetwork = assetMatchesNetwork(coin, baseCurrencyNetwork); - return !baseCurrencyNetwork.isMainnet() || - matchesNetwork; - } - - private static CryptoCurrency assetToCryptoCurrency(Asset asset) { - return new CryptoCurrency(asset.getTickerSymbol(), asset.getName(), asset instanceof Token); - } - - private static boolean isNotBsqOrBsqTradingActivated(Asset asset, BaseCurrencyNetwork baseCurrencyNetwork, boolean daoTradingActivated) { - return !(asset instanceof BSQ) || - daoTradingActivated && assetMatchesNetwork(asset, baseCurrencyNetwork); - } - - public static boolean assetMatchesCurrencyCode(Asset asset, String currencyCode) { - return currencyCode.equals(asset.getTickerSymbol()); - } - - public static Optional findAsset(AssetRegistry assetRegistry, String currencyCode, - BaseCurrencyNetwork baseCurrencyNetwork, boolean daoTradingActivated) { - List assets = assetRegistry.stream() - .filter(asset -> assetMatchesCurrencyCode(asset, currencyCode)).collect(Collectors.toList()); - - // If we don't have the ticker symbol we throw an exception - if (!assets.stream().findFirst().isPresent()) - return Optional.empty(); - - if (currencyCode.equals("BSQ") && baseCurrencyNetwork.isMainnet() && !daoTradingActivated) - return Optional.empty(); - - // We check for exact match with network, e.g. BTC$TESTNET - Optional optionalAssetMatchesNetwork = assets.stream() - .filter(asset -> assetMatchesNetwork(asset, baseCurrencyNetwork)) - .findFirst(); - if (optionalAssetMatchesNetwork.isPresent()) - return optionalAssetMatchesNetwork; - - // In testnet or regtest we want to show all coins as well. Most coins have only Mainnet defined so we deliver - // that if no exact match was found in previous step - if (!baseCurrencyNetwork.isMainnet()) { - Optional optionalAsset = assets.stream().findFirst(); - checkArgument(optionalAsset.isPresent(), "optionalAsset must be present as we checked for " + - "not matching ticker symbols already above"); - return optionalAsset; - } - - // If we are in mainnet we need have a mainet asset defined. - throw new IllegalArgumentException("We are on mainnet and we could not find an asset with network type mainnet"); - } - - public static Optional findAsset(String tickerSymbol, BaseCurrencyNetwork baseCurrencyNetwork) { - return assetRegistry.stream() - .filter(asset -> asset.getTickerSymbol().equals(tickerSymbol)) - .filter(asset -> assetMatchesNetwork(asset, baseCurrencyNetwork)) - .findAny(); - } - - // Excludes all assets which got removed by DAO voting - public static List getActiveSortedCryptoCurrencies(AssetService assetService) { - return getAllSortedCryptoCurrencies().stream() - .filter(e -> e.getCode().equals("BSQ") || assetService.isActive(e.getCode())) - .collect(Collectors.toList()); - } -} diff --git a/core/src/test/java/bisq/core/user/PreferencesTest.java b/core/src/test/java/bisq/core/user/PreferencesTest.java deleted file mode 100644 index d5d25bfd6b6..00000000000 --- a/core/src/test/java/bisq/core/user/PreferencesTest.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.core.user; - -import bisq.core.app.BisqEnvironment; -import bisq.core.locale.CountryUtil; -import bisq.core.locale.CryptoCurrency; -import bisq.core.locale.CurrencyUtil; -import bisq.core.locale.FiatCurrency; -import bisq.core.locale.GlobalSettings; -import bisq.core.locale.Res; - -import bisq.common.storage.Storage; - -import javafx.collections.ObservableList; - -import java.util.ArrayList; -import java.util.Currency; -import java.util.List; -import java.util.Locale; - -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({Storage.class, PreferencesPayload.class, BisqEnvironment.class}) -@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"}) -public class PreferencesTest { - - private Preferences preferences; - private Storage storage; - private BisqEnvironment bisqEnvironment; - - @Before - public void setUp() { - final Locale en_US = new Locale("en", "US"); - Locale.setDefault(en_US); - GlobalSettings.setLocale(en_US); - Res.setBaseCurrencyCode("BTC"); - Res.setBaseCurrencyName("Bitcoin"); - - storage = mock(Storage.class); - bisqEnvironment = mock(BisqEnvironment.class); - - preferences = new Preferences(storage, bisqEnvironment, null, null, null, null, null, null); - } - - @Test - public void testAddFiatCurrency() { - final FiatCurrency usd = new FiatCurrency("USD"); - final FiatCurrency usd2 = new FiatCurrency("USD"); - final ObservableList fiatCurrencies = preferences.getFiatCurrenciesAsObservable(); - - preferences.addFiatCurrency(usd); - - assertEquals(1, fiatCurrencies.size()); - - preferences.addFiatCurrency(usd2); - - assertEquals(1, fiatCurrencies.size()); - } - - @Test - public void testGetUniqueListOfFiatCurrencies() { - PreferencesPayload payload = mock(PreferencesPayload.class); - - List fiatCurrencies = CurrencyUtil.getMainFiatCurrencies(); - final FiatCurrency usd = new FiatCurrency("USD"); - fiatCurrencies.add(usd); - - when(storage.initAndGetPersistedWithFileName(anyString(), anyLong())).thenReturn(payload); - when(payload.getUserLanguage()).thenReturn("en"); - when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry()); - when(payload.getPreferredTradeCurrency()).thenReturn(usd); - when(payload.getFiatCurrencies()).thenReturn(fiatCurrencies); - - preferences.readPersisted(); - - assertEquals(7, preferences.getFiatCurrenciesAsObservable().size()); - assertTrue(preferences.getFiatCurrenciesAsObservable().contains(usd)); - - } - - @Test - public void testGetUniqueListOfCryptoCurrencies() { - PreferencesPayload payload = mock(PreferencesPayload.class); - - List cryptoCurrencies = CurrencyUtil.getMainCryptoCurrencies(); - final CryptoCurrency dash = new CryptoCurrency("DASH", "Dash"); - cryptoCurrencies.add(dash); - - when(storage.initAndGetPersistedWithFileName(anyString(), anyLong())).thenReturn(payload); - when(payload.getUserLanguage()).thenReturn("en"); - when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry()); - when(payload.getPreferredTradeCurrency()).thenReturn(new FiatCurrency("USD")); - when(payload.getCryptoCurrencies()).thenReturn(cryptoCurrencies); - - preferences.readPersisted(); - - assertEquals(13, preferences.getCryptoCurrenciesAsObservable().size()); - assertTrue(preferences.getCryptoCurrenciesAsObservable().contains(dash)); - - } - - @Test - public void testUpdateOfPersistedFiatCurrenciesAfterLocaleChanged() { - PreferencesPayload payload = mock(PreferencesPayload.class); - - List fiatCurrencies = new ArrayList<>(); - final FiatCurrency usd = new FiatCurrency(Currency.getInstance("USD"), new Locale("de", "AT")); - fiatCurrencies.add(usd); - - assertEquals("US-Dollar (USD)", usd.getNameAndCode()); - - when(storage.initAndGetPersistedWithFileName(anyString(), anyLong())).thenReturn(payload); - when(payload.getUserLanguage()).thenReturn("en"); - when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry()); - when(payload.getPreferredTradeCurrency()).thenReturn(usd); - when(payload.getFiatCurrencies()).thenReturn(fiatCurrencies); - - preferences.readPersisted(); - - assertEquals("US Dollar (USD)",preferences.getFiatCurrenciesAsObservable().get(0).getNameAndCode()); - } - -}