feat: NbpService & tests implementation
This commit is contained in:
parent
9e69a47b3e
commit
1ae5dd9957
2 changed files with 20 additions and 5 deletions
|
@ -1,14 +1,17 @@
|
|||
package eu.ztsh.wymiana.service;
|
||||
|
||||
import eu.ztsh.wymiana.exception.NoDataException;
|
||||
import eu.ztsh.wymiana.model.Rates;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.assertj.core.util.VisibleForTesting;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
|
||||
/**
|
||||
|
@ -21,13 +24,14 @@ public class NbpService {
|
|||
private final Clock clock;
|
||||
private final RestClient restClient;
|
||||
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
|
||||
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
public double getSellRate(String currency) {
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getAsk();
|
||||
}
|
||||
|
||||
public double getBuyRate(String currency) {
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
return fetchData(currency, dtf.format(getFetchDate())).getRates().get(0).getBid();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -45,7 +49,12 @@ public class NbpService {
|
|||
|
||||
@VisibleForTesting
|
||||
Rates fetchData(String code, String date) {
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
return restClient.get().uri(URI_PATTERN, code.toLowerCase(), date)
|
||||
.retrieve()
|
||||
.onStatus(HttpStatusCode::is4xxClientError, ((request, response) -> {
|
||||
throw new NoDataException(code, date);
|
||||
}))
|
||||
.body(Rates.class);
|
||||
}
|
||||
|
||||
private static boolean isWeekend(LocalDate today) {
|
||||
|
|
|
@ -2,6 +2,7 @@ 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;
|
||||
|
@ -78,8 +79,12 @@ class NbpServiceTest {
|
|||
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)));
|
||||
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||
WireMockExtension.verifyGet(1, url);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Fetch rates from cache")
|
||||
|
@ -88,6 +93,7 @@ class NbpServiceTest {
|
|||
var date = dtf.format(updateClock(DayOfWeek.FRIDAY));
|
||||
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
||||
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||
assertThat(nbpService.getBuyRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.BUY_RATE);
|
||||
WireMockExtension.verifyGet(0, url);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue