A while ago I needed a standard way to use monetary exchange rate from the National Bank of Romania in a standard way.
The standard way is to use Java Money (JSR 354) .. but this API does not include exchange rate for every bank .. by default has only Yahoo Exchange Rate and the exchange rate of Federal Reserve Bank.
As this was built to be a generally available API .. luckily is delivered with an extendable SPI .. so I did extended this SPI and added support to load the exchange rate of the National Bank of Romania.
Example using National Bank of Romania’s exchange rate on a given date:
private static final CurrencyUnit EURO = Monetary.getCurrency("EUR");
private static final CurrencyUnit ROMANIAN_LEI = Monetary.getCurrency("RON");
private ExchangeRateProvider provider;
@BeforeTest
public void setup() throws InterruptedException {
provider = MonetaryConversions.getExchangeRateProvider("NBR");
Thread.sleep(1_000L); // allow the exchange rate provider to load the exchange rate for the current year
}
@Test
public void shouldConvertEuroToLeiOnKnownDate() {
// 2018-01-03 - <Rate currency="EUR">4.6412</Rate>
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(ROMANIAN_LEI)
.set(LocalDate.class, LocalDate.of(2018, 1, 3))
.build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount hundredEuro = Money.of(BigDecimal.valueOf(100), EURO);
MonetaryAmount result = currencyConversion.apply(hundredEuro);
MonetaryAmount fourSixFourPointOneTwoRon = Money.of(BigDecimal.valueOf(464.12d), ROMANIAN_LEI);
log.info("money {} converted as {}", hundredEuro, result);
assertEquals(result.getCurrency(), ROMANIAN_LEI);
assertEquals(result, fourSixFourPointOneTwoRon);
}
private static final CurrencyUnit HUNGARIAN_FORINT = Monetary.getCurrency("HUF");
private static final CurrencyUnit ROMANIAN_LEI = Monetary.getCurrency("RON");
private ExchangeRateProvider provider;
@BeforeTest
public void setup() throws InterruptedException {
provider = MonetaryConversions.getExchangeRateProvider("NBR");
Thread.sleep(1_000L); // allow the exchange rate provider to load the exchange rate for the current year
}
@Test
public void shouldConvertForintToLeiOnKnownDate() {
// 2018-01-03 - <Rate currency="HUF" multiplier="100">1.5010</Rate>
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(ROMANIAN_LEI)
.set(LocalDate.class, LocalDate.of(2018, 1, 3))
.build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount hundredHuf = Money.of(BigDecimal.valueOf(100), HUNGARIAN_FORINT);
MonetaryAmount result = currencyConversion.apply(hundredHuf);
MonetaryAmount onePointFiftyTenRon = Money.of(BigDecimal.valueOf(1.5010d), ROMANIAN_LEI);
log.info("money {} converted as {}", hundredHuf, result);
assertEquals(result.getCurrency(), ROMANIAN_LEI);
assertEquals(result, onePointFiftyTenRon);
}
Example using National Bank of Romania with the exchange rate from today:
private static final CurrencyUnit HUNGARIAN_FORINT = Monetary.getCurrency("HUF");
private static final CurrencyUnit ROMANIAN_LEI = Monetary.getCurrency("RON");
private ExchangeRateProvider provider;
@BeforeTest
public void setup() throws InterruptedException {
provider = MonetaryConversions.getExchangeRateProvider("NBR");
Thread.sleep(1_000L); // allow the exchange rate provider to load the exchange rate for the current year
}
@Test
public void shouldConvertLeiToForint() {
CurrencyConversion currencyConversion = provider.getCurrencyConversion(HUNGARIAN_FORINT);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, ROMANIAN_LEI);
MonetaryAmount result = currencyConversion.apply(money);
log.info("money {} converted as {}", money, result);
assertEquals(result.getCurrency(), HUNGARIAN_FORINT);
assertTrue(result.getNumber().doubleValue() > 0);
}
Comments (0)