test: NbpServiceTest completed?
This commit is contained in:
parent
b2cbfad2ac
commit
b38a507ce9
4 changed files with 59 additions and 15 deletions
2
pom.xml
2
pom.xml
|
@ -81,7 +81,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.wiremock</groupId>
|
<groupId>org.wiremock</groupId>
|
||||||
<artifactId>wiremock</artifactId>
|
<artifactId>wiremock-standalone</artifactId>
|
||||||
<version>${wiremock.version}</version>
|
<version>${wiremock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package eu.ztsh.wymiana.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RestClientConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestClient restClient() {
|
||||||
|
return RestClient.builder()
|
||||||
|
.baseUrl("http://api.nbp.pl")
|
||||||
|
.defaultHeader("Accept", "application/json")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import eu.ztsh.wymiana.model.Rates;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.assertj.core.util.VisibleForTesting;
|
import org.assertj.core.util.VisibleForTesting;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
|
@ -18,6 +19,8 @@ import java.time.temporal.TemporalAdjusters;
|
||||||
public class NbpService {
|
public class NbpService {
|
||||||
|
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
private final RestClient restClient;
|
||||||
|
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
|
||||||
|
|
||||||
public double getSellRate(String currency) {
|
public double getSellRate(String currency) {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
throw new UnsupportedOperationException("Not implemented yet");
|
||||||
|
@ -41,7 +44,7 @@ public class NbpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Rates fetchData(String date) {
|
Rates fetchData(String code, String date) {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
throw new UnsupportedOperationException("Not implemented yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +52,7 @@ public class NbpService {
|
||||||
return today.getDayOfWeek() == DayOfWeek.SATURDAY
|
return today.getDayOfWeek() == DayOfWeek.SATURDAY
|
||||||
|| today.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|| today.getDayOfWeek() == DayOfWeek.SUNDAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private record RatesCache(LocalDate date, double buy, double sell) {
|
private record RatesCache(LocalDate date, double buy, double sell) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,36 @@
|
||||||
package eu.ztsh.wymiana.service;
|
package eu.ztsh.wymiana.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
|
import eu.ztsh.wymiana.WireMockExtension;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.EnumSource;
|
import org.junit.jupiter.params.provider.EnumSource;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.TemporalAdjusters;
|
import java.time.temporal.TemporalAdjusters;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ExtendWith(WireMockExtension.class)
|
||||||
class NbpServiceTest {
|
class NbpServiceTest {
|
||||||
|
|
||||||
private static final ZoneId zone = ZoneId.of("Europe/Warsaw");
|
private static final ZoneId zone = ZoneId.of("Europe/Warsaw");
|
||||||
private static final LocalDate today = LocalDate.of(2024, Month.MAY, 12); // Sunday
|
private static final LocalDate today = LocalDate.of(2024, Month.MAY, 12); // Sunday
|
||||||
private static Clock clock;
|
private static Clock clock;
|
||||||
|
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
private final RestClient restClient = RestClient.builder().baseUrl(WireMockExtension.baseUrl).build();
|
||||||
private NbpService nbpService;
|
private NbpService nbpService;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
|
@ -31,11 +41,11 @@ class NbpServiceTest {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void prepareTest() {
|
void prepareTest() {
|
||||||
nbpService = new NbpService(clock);
|
nbpService = new NbpService(clock, restClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY","SUNDAY"}, mode = EnumSource.Mode.EXCLUDE)
|
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY", "SUNDAY"}, mode = EnumSource.Mode.EXCLUDE)
|
||||||
void getFetchDateOnWorkingDayTest(DayOfWeek dayOfWeek) {
|
void getFetchDateOnWorkingDayTest(DayOfWeek dayOfWeek) {
|
||||||
updateClock(dayOfWeek);
|
updateClock(dayOfWeek);
|
||||||
assertThat(nbpService.getFetchDate()).isEqualTo(
|
assertThat(nbpService.getFetchDate()).isEqualTo(
|
||||||
|
@ -51,32 +61,44 @@ class NbpServiceTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY","SUNDAY"})
|
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY", "SUNDAY"})
|
||||||
void getFetchDateOnWeekendTest(DayOfWeek dayOfWeek) {
|
void getFetchDateOnWeekendTest(DayOfWeek dayOfWeek) {
|
||||||
updateClock(dayOfWeek);
|
updateClock(dayOfWeek);
|
||||||
assertThat(nbpService.getFetchDate()).isEqualTo(LocalDate.of(2024, Month.MAY, 10));
|
assertThat(nbpService.getFetchDate()).isEqualTo(LocalDate.of(2024, Month.MAY, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getWithoutCacheTest() {
|
void getWithoutCacheTest() throws JsonProcessingException {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
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)));
|
||||||
|
nbpService.fetchData(EntityCreator.Constants.USD_SYMBOL, date);
|
||||||
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getWithCacheTest() {
|
void getWithCacheTest() throws JsonProcessingException {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
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)));
|
||||||
|
nbpService.fetchData(EntityCreator.Constants.USD_SYMBOL, date);
|
||||||
|
nbpService.fetchData(EntityCreator.Constants.USD_SYMBOL, date);
|
||||||
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getInvalidCurrencyTest() {
|
void getInvalidCurrencyTest() {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
var date = dtf.format(updateClock(DayOfWeek.FRIDAY));
|
||||||
|
var url = "/api/exchangerates/rates/c/usb/%s/".formatted(date);
|
||||||
|
WireMockExtension.response(url, 404, "404 NotFound - Not Found - Brak danych");
|
||||||
|
nbpService.fetchData("usb", date);
|
||||||
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateClock(DayOfWeek dayOfWeek) {
|
private LocalDate updateClock(DayOfWeek dayOfWeek) {
|
||||||
Mockito.when(clock.instant()).thenReturn(
|
var date = today.with(TemporalAdjusters.previousOrSame(dayOfWeek));
|
||||||
today.with(TemporalAdjusters.previousOrSame(dayOfWeek))
|
Mockito.when(clock.instant()).thenReturn(date.atStartOfDay(zone).toInstant());
|
||||||
.atStartOfDay(zone)
|
return LocalDate.from(date);
|
||||||
.toInstant());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue