Skip to content

Commit

Permalink
Refactor TestCurrencyConverter
Browse files Browse the repository at this point in the history
Make it possible to support multiple currencies. Plus some simplification.
  • Loading branch information
chirlu authored and buchen committed Mar 31, 2023
1 parent c447331 commit 74ff6c5
Showing 1 changed file with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import name.abuchen.portfolio.money.CurrencyUnit;
import name.abuchen.portfolio.money.ExchangeRate;
import name.abuchen.portfolio.money.ExchangeRateTimeSeries;
import name.abuchen.portfolio.money.MonetaryException;
import name.abuchen.portfolio.money.Money;
import name.abuchen.portfolio.money.impl.ExchangeRateTimeSeriesImpl;
import name.abuchen.portfolio.money.impl.InverseExchangeRateTimeSeries;

@SuppressWarnings("nls")
public class TestCurrencyConverter implements CurrencyConverter
{
private static ExchangeRateTimeSeriesImpl EUR_USD = null; // NOSONAR
private static InverseExchangeRateTimeSeries USD_EUR = null; // NOSONAR

static
{
Expand All @@ -34,20 +33,20 @@ public class TestCurrencyConverter implements CurrencyConverter
EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-14"), BigDecimal.valueOf(1.1775).setScale(10)));
EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-15"), BigDecimal.valueOf(1.1708).setScale(10)));
EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-16"), BigDecimal.valueOf(1.1588).setScale(10)));

USD_EUR = new InverseExchangeRateTimeSeries(EUR_USD);
}

private final String termCurrency;
private final ExchangeRateTimeSeries series;

public TestCurrencyConverter()
{
this(CurrencyUnit.EUR, new InverseExchangeRateTimeSeries(EUR_USD));
this(CurrencyUnit.EUR);
}

public TestCurrencyConverter(String currencyCode, ExchangeRateTimeSeries series)
public TestCurrencyConverter(String currencyCode)
{
this.termCurrency = currencyCode;
this.series = series;
}

@Override
Expand All @@ -56,31 +55,20 @@ public String getTermCurrency()
return termCurrency;
}

@Override
public Money convert(LocalDate date, Money amount)
{
try
{
return CurrencyConverter.super.convert(date, amount);
}
catch (MonetaryException e)
{
// testing: any other currency will be converted 1:1
if (!amount.getCurrencyCode().equals(series.getBaseCurrency()))
return Money.of(termCurrency, amount.getAmount());
else
throw e;
}
}

@Override
public ExchangeRate getRate(LocalDate date, String currencyCode)
{
if (termCurrency.equals(currencyCode))
return new ExchangeRate(date, BigDecimal.ONE);

if (!currencyCode.equals(series.getBaseCurrency()))
throw new MonetaryException();
ExchangeRateTimeSeries series;
if (currencyCode.equals("USD") && termCurrency.equals("EUR"))
series = USD_EUR;
else if (currencyCode.equals("EUR") && termCurrency.equals("USD"))
series = EUR_USD;
else
// testing: any other currency will be converted 1:1
return new ExchangeRate(date, BigDecimal.ONE);

return series.lookupRate(date).orElseThrow(IllegalArgumentException::new);
}
Expand All @@ -91,11 +79,9 @@ public CurrencyConverter with(String currencyCode)
if (currencyCode.equals(termCurrency))
return this;

if (currencyCode.equals(CurrencyUnit.EUR))
return new TestCurrencyConverter();

if (currencyCode.equals("USD"))
return new TestCurrencyConverter("USD", EUR_USD);
if (currencyCode.equals(CurrencyUnit.EUR)
|| currencyCode.equals(CurrencyUnit.USD))
return new TestCurrencyConverter(currencyCode);

return null;
}
Expand Down

0 comments on commit 74ff6c5

Please sign in to comment.