test: NbpService#getFetchDate tests

This commit is contained in:
Piotr Dec 2024-05-23 21:08:26 +02:00
parent 2e4ca845f8
commit 310a4c4087
Signed by: stawros
GPG key ID: F89F27AD8F881A91
4 changed files with 92 additions and 6 deletions

View file

@ -0,0 +1,16 @@
package eu.ztsh.wymiana.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Clock;
@Configuration
public class ClockConfiguration {
@Bean
public Clock clock() {
return Clock.systemDefaultZone();
}
}

View file

@ -1,17 +1,22 @@
package eu.ztsh.wymiana.service; package eu.ztsh.wymiana.service;
import eu.ztsh.wymiana.model.Rates; import eu.ztsh.wymiana.model.Rates;
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 java.time.Clock;
import java.time.LocalDate; import java.time.LocalDate;
/** /**
* NBP exchange rates service * NBP exchange rates service
*/ */
@Service @Service
@RequiredArgsConstructor
public class NbpService { public class NbpService {
private final Clock clock;
public double getSellRate(String currency) { public double getSellRate(String currency) {
throw new UnsupportedOperationException("Not implemented yet"); throw new UnsupportedOperationException("Not implemented yet");
} }

View file

@ -2,6 +2,8 @@ package eu.ztsh.wymiana;
import eu.ztsh.wymiana.data.entity.CurrencyEntity; import eu.ztsh.wymiana.data.entity.CurrencyEntity;
import eu.ztsh.wymiana.data.entity.UserEntity; import eu.ztsh.wymiana.data.entity.UserEntity;
import eu.ztsh.wymiana.model.Rate;
import eu.ztsh.wymiana.model.Rates;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest; import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import eu.ztsh.wymiana.web.model.UserCreateRequest; import eu.ztsh.wymiana.web.model.UserCreateRequest;
@ -41,6 +43,19 @@ public class EntityCreator {
return CurrencyExchangeRequest.builder().pesel(Constants.PESEL); return CurrencyExchangeRequest.builder().pesel(Constants.PESEL);
} }
public static Rates rates(String date) {
var rates = new Rates();
rates.setTable("C");
rates.setCurrency("dolar amerykański");
rates.setCode("USD");
var rate = new Rate();
rate.setNo("096/C/NBP/2024");
rate.setEffectiveDate(date);
rate.setBid(Constants.BUY_RATE);
rate.setAsk(Constants.SELL_RATE);
rates.setRates(List.of(rate));
return rates;
}
public static class UserEntityBuilder { public static class UserEntityBuilder {

View file

@ -1,32 +1,82 @@
package eu.ztsh.wymiana.service; package eu.ztsh.wymiana.service;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.Mockito;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import static org.assertj.core.api.Assertions.assertThat;
class NbpServiceTest { class NbpServiceTest {
@Test private static final ZoneId zone = ZoneId.of("Europe/Warsaw");
void getFetchDateOnWorkingDayTest() { private static final LocalDate today = LocalDate.of(2024, Month.MAY, 12); // Sunday
private static Clock clock;
private NbpService nbpService;
@BeforeAll
static void prepare() {
clock = Mockito.mock(Clock.class);
Mockito.when(clock.getZone()).thenReturn(zone);
} }
@Test @BeforeEach
void getFetchDateOnWeekendTest() { void prepareTest() {
nbpService = new NbpService(clock);
}
@ParameterizedTest
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY","SUNDAY"}, mode = EnumSource.Mode.EXCLUDE)
void getFetchDateOnWorkingDayTest(DayOfWeek dayOfWeek) {
updateClock(dayOfWeek);
assertThat(nbpService.getFetchDate()).isEqualTo(
switch (dayOfWeek) {
case MONDAY -> LocalDate.of(2024, Month.MAY, 6);
case TUESDAY -> LocalDate.of(2024, Month.MAY, 7);
case WEDNESDAY -> LocalDate.of(2024, Month.MAY, 8);
case THURSDAY -> LocalDate.of(2024, Month.MAY, 9);
case FRIDAY -> LocalDate.of(2024, Month.MAY, 10);
default -> null;
}
);
}
@ParameterizedTest
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY","SUNDAY"})
void getFetchDateOnWeekendTest(DayOfWeek dayOfWeek) {
updateClock(dayOfWeek);
assertThat(nbpService.getFetchDate()).isEqualTo(LocalDate.of(2024, Month.MAY, 10));
} }
@Test @Test
void getWithoutCacheTest() { void getWithoutCacheTest() {
throw new UnsupportedOperationException("Not implemented yet");
} }
@Test @Test
void getWithCacheTest() { void getWithCacheTest() {
throw new UnsupportedOperationException("Not implemented yet");
} }
@Test @Test
void getInvalidCurrencyTest() { void getInvalidCurrencyTest() {
throw new UnsupportedOperationException("Not implemented yet");
}
private void updateClock(DayOfWeek dayOfWeek) {
Mockito.when(clock.instant()).thenReturn(
today.with(TemporalAdjusters.previousOrSame(dayOfWeek))
.atStartOfDay(zone)
.toInstant());
} }
} }