Search in sources :

Example 1 with ConversionQuery

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();
}
Also used : ConversionQuery(javax.money.convert.ConversionQuery) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with ConversionQuery

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();
}
Also used : ConversionQuery(javax.money.convert.ConversionQuery) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with ConversionQuery

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);
}
Also used : ConversionQuery(javax.money.convert.ConversionQuery) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with ConversionQuery

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);
}
Also used : ConversionQuery(javax.money.convert.ConversionQuery) MonetaryAmount(javax.money.MonetaryAmount) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) CurrencyConversion(javax.money.convert.CurrencyConversion) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with ConversionQuery

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.");
    }
}
Also used : LoaderService(org.javamoney.moneta.spi.LoaderService) CurrencyUnit(javax.money.CurrencyUnit) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ProviderContext(javax.money.convert.ProviderContext) Level(java.util.logging.Level) LoaderListener(org.javamoney.moneta.spi.LoaderService.LoaderListener) Map(java.util.Map) SAXParser(javax.xml.parsers.SAXParser) ExchangeRate(javax.money.convert.ExchangeRate) AbstractRateProvider(org.javamoney.moneta.spi.AbstractRateProvider) Monetary(javax.money.Monetary) MathContext(java.math.MathContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MonetaryException(javax.money.MonetaryException) ConversionQuery(javax.money.convert.ConversionQuery) ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) CurrencyConversionException(javax.money.convert.CurrencyConversionException) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Comparator(java.util.Comparator) Bootstrap(javax.money.spi.Bootstrap) InputStream(java.io.InputStream) ExchangeRate(javax.money.convert.ExchangeRate) LocalDate(java.time.LocalDate) MonetaryException(javax.money.MonetaryException)

Aggregations

ConversionQuery (javax.money.convert.ConversionQuery)11 MonetaryAmount (javax.money.MonetaryAmount)10 CurrencyConversion (javax.money.convert.CurrencyConversion)10 BeforeTest (org.testng.annotations.BeforeTest)10 Test (org.testng.annotations.Test)10 LocalDate (java.time.LocalDate)9 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 InputStream (java.io.InputStream)1 MathContext (java.math.MathContext)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1