ExchangeRatesServiceImpl implements ExchangeRatesService { @Override public Map<Currency, BigDecimal> getCurrentRates() { if (container == null || !container.getDate().equals(LocalDate.now())) { container = client.getRates(Currency.getBase()); log.info("exchange rates has been updated: {}", container); } return ImmutableMap.of( Currency.EUR, container.getRates().get(Currency.EUR.name()), Currency.RUB, container.getRates().get(Currency.RUB.name()), Currency.USD, BigDecimal.ONE ); }  @Override Map<Currency, BigDecimal> getCurrentRates(); @Override BigDecimal convert(Currency from, Currency to, BigDecimal amount);  }
@Test public void shouldNotRequestRatesWhenTodaysContainerAlreadyExists() { ExchangeRatesContainer container = new ExchangeRatesContainer(); container.setRates(ImmutableMap.of( Currency.EUR.name(), new BigDecimal("0.8"), Currency.RUB.name(), new BigDecimal("80") )); when(client.getRates(Currency.getBase())).thenReturn(container); ratesService.getCurrentRates(); ratesService.getCurrentRates(); verify(client, times(1)).getRates(Currency.getBase()); }
@Test public void shouldReturnCurrentRatesWhenContainerIsEmptySoFar() { ExchangeRatesContainer container = new ExchangeRatesContainer(); container.setRates(ImmutableMap.of( Currency.EUR.name(), new BigDecimal("0.8"), Currency.RUB.name(), new BigDecimal("80") )); when(client.getRates(Currency.getBase())).thenReturn(container); Map<Currency, BigDecimal> result = ratesService.getCurrentRates(); verify(client, times(1)).getRates(Currency.getBase()); assertEquals(container.getRates().get(Currency.EUR.name()), result.get(Currency.EUR)); assertEquals(container.getRates().get(Currency.RUB.name()), result.get(Currency.RUB)); assertEquals(BigDecimal.ONE, result.get(Currency.USD)); }