fix: NbpService cache & tests fixes
This commit is contained in:
parent
1ae5dd9957
commit
52ff4a67b3
2 changed files with 28 additions and 7 deletions
|
@ -13,6 +13,8 @@ import java.time.DayOfWeek;
|
|||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* NBP exchange rates service
|
||||
|
@ -26,12 +28,30 @@ public class NbpService {
|
|||
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
|
||||
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
private final ConcurrentMap<String, RatesCache> cache = new ConcurrentHashMap<>(1);
|
||||
|
||||
public double getSellRate(String currency) {
|
||||
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getAsk();
|
||||
return getCurrency(currency).sell();
|
||||
}
|
||||
|
||||
public double getBuyRate(String currency) {
|
||||
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getBid();
|
||||
return getCurrency(currency).buy();
|
||||
}
|
||||
|
||||
private synchronized RatesCache getCurrency(String currency) {
|
||||
var today = getFetchDate();
|
||||
var cacheObject = cache.get(currency);
|
||||
if (cacheObject == null || cacheObject.date().isBefore(today)) {
|
||||
var fresh = fetchData(currency, dtf.format(today));
|
||||
var rate = fresh.getRates().get(0);
|
||||
cacheObject = new RatesCache(
|
||||
LocalDate.parse(rate.getEffectiveDate(), dtf),
|
||||
rate.getBid(),
|
||||
rate.getAsk()
|
||||
);
|
||||
cache.put(fresh.getCode(), cacheObject);
|
||||
}
|
||||
return cacheObject;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,7 +2,6 @@ package eu.ztsh.wymiana.service;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import eu.ztsh.wymiana.EntityCreator;
|
||||
import eu.ztsh.wymiana.WireMockExtension;
|
||||
import eu.ztsh.wymiana.exception.NoDataException;
|
||||
|
@ -81,20 +80,22 @@ class NbpServiceTest {
|
|||
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
||||
try {
|
||||
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||
assertThat(nbpService.getBuyRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.BUY_RATE);
|
||||
} finally {
|
||||
WireMockExtension.verifyGet(2, url);
|
||||
WireMockExtension.verifyGet(1, url);
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Fetch rates from cache")
|
||||
@Test
|
||||
void getWithCacheTest() {
|
||||
void getWithCacheTest() throws JsonProcessingException {
|
||||
var date = dtf.format(updateClock(DayOfWeek.FRIDAY));
|
||||
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
||||
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
||||
// save to cache
|
||||
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||
// get from cache
|
||||
assertThat(nbpService.getBuyRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.BUY_RATE);
|
||||
WireMockExtension.verifyGet(0, url);
|
||||
WireMockExtension.verifyGet(1, url);
|
||||
}
|
||||
|
||||
@DisplayName("Support 404: invalid currency or no data")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue