Skip to content

Commit

Permalink
Enhance exchange-rates client with the ability to request specific rates
Browse files Browse the repository at this point in the history
Added overloaded get-rates method with 'symbols' request parameter,
which allows limiting response to specific currencies.
Updated existing get-rates method with default implementation, which
retrieves rates only for known system currencies (currency enum values).
  • Loading branch information
solo-yolo committed Jul 10, 2018
1 parent 447ebc6 commit 54c0efd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.Collection;

@FeignClient(url = "${rates.url}", name = "rates-client", fallback = ExchangeRatesClientFallback.class)
public interface ExchangeRatesClient {

default ExchangeRatesContainer getRates(Currency base) {
return getRates(base, Arrays.asList(Currency.values()));
}

@RequestMapping(method = RequestMethod.GET, value = "/latest")
ExchangeRatesContainer getRates(@RequestParam("base") Currency base);
ExchangeRatesContainer getRates(
@RequestParam("base") Currency base,
@RequestParam("symbols") Collection<Currency> currencies);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.piggymetrics.statistics.domain.ExchangeRatesContainer;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;

@Component
public class ExchangeRatesClientFallback implements ExchangeRatesClient {

@Override
public ExchangeRatesContainer getRates(Currency base) {
public ExchangeRatesContainer getRates(Currency base, Collection<Currency> currencies) {
ExchangeRatesContainer container = new ExchangeRatesContainer();
container.setBase(Currency.getBase());
container.setRates(Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.time.LocalDate;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -30,8 +31,24 @@ public void shouldRetrieveExchangeRates() {
assertEquals(container.getBase(), Currency.getBase());

assertNotNull(container.getRates());
assertEquals(Currency.values().length, container.getRates().size());
assertNotNull(container.getRates().get(Currency.USD.name()));
assertNotNull(container.getRates().get(Currency.EUR.name()));
assertNotNull(container.getRates().get(Currency.RUB.name()));
}

@Test
public void shouldRetrieveExchangeRatesForSpecifiedCurrency() {

Currency requestedCurrency = Currency.EUR;
ExchangeRatesContainer container = client.getRates(Currency.getBase(), Collections.singleton(requestedCurrency));

assertEquals(container.getDate(), LocalDate.now());
assertEquals(container.getBase(), Currency.getBase());

assertNotNull(container.getRates());
assertEquals(1, container.getRates().size());
assertNotNull(container.getRates().get(requestedCurrency.name()));
}

}

0 comments on commit 54c0efd

Please sign in to comment.