National Bank of Romania’s Exchange Rate meets Java Money

What this means?

It means that we provide a Java SPI extension for using the exchange rates of National Bank of Romania with Java Money (JSR 354) API.

By adding this Exchange Rate extension to your project classpath, it will automatically enable the capability to use the current year’s RON exchange rate.

Example usage

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);
}

Where you can find it

The source code is available on Github https://github.com/albertlr/javamoney-lib