use of javax.money.convert.ConversionQuery in project jsr354-ri by JavaMoney.
the class IMFHistoricRateProviderTest method shouldReturnErrorWhenDoesNotFindTheExchangeRate.
@Test(expectedExceptions = MonetaryException.class)
public void shouldReturnErrorWhenDoesNotFindTheExchangeRate() {
LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of().set(localDate).setTermCurrency(EURO).build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
currencyConversion.apply(money);
fail();
}
use of javax.money.convert.ConversionQuery in project jsr354-ri by JavaMoney.
the class IMFHistoricRateProviderTest method shouldReturnErrorWhenFindFromHistoricalUsingPeriod.
@Test(expectedExceptions = MonetaryException.class)
public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() {
LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(EURO).set(withDaysBetween(localDate.minusDays(1), localDate)).build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
currencyConversion.apply(money);
fail();
}
use of javax.money.convert.ConversionQuery in project jsr354-ri by JavaMoney.
the class ECBHistoric90RateProviderTest method shouldSetTimeInLocalDateTime2.
@Test
public void shouldSetTimeInLocalDateTime2() {
LocalDate localDate = LocalDate.now().minusDays(90).with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
use of javax.money.convert.ConversionQuery in project jsr354-ri-bp by JavaMoney.
the class ECBHistoric90RateProviderTest method shouldSetTimeInLocalDateTime.
@Test
public void shouldSetTimeInLocalDateTime() {
Calendar localDate = GregorianCalendar.getInstance();
localDate.add(Calendar.DAY_OF_YEAR, -20);
localDate.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
use of javax.money.convert.ConversionQuery in project jsr354-ri by JavaMoney.
the class ECBAbstractRateProvider method findExchangeRate.
private RateResult findExchangeRate(ConversionQuery conversionQuery) {
LocalDate[] dates = getQueryDates(conversionQuery);
if (dates == null) {
Comparator<LocalDate> comparator = Comparator.naturalOrder();
LocalDate date = this.rates.keySet().stream().sorted(comparator.reversed()).findFirst().orElseThrow(() -> new MonetaryException("There is not more recent exchange rate to rate on ECBRateProvider."));
return new RateResult(date, this.rates.get(date));
} else {
for (LocalDate localDate : dates) {
Map<String, ExchangeRate> targets = this.rates.get(localDate);
if (Objects.nonNull(targets)) {
return new RateResult(localDate, targets);
}
}
String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)).collect(Collectors.joining(","));
throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to rate on ECBRateProvider.");
}
}
Aggregations