test: NbpServiceTest invocation fixed

This commit is contained in:
Piotr Dec 2024-05-23 22:58:55 +02:00
parent 4a2338d699
commit 157a1ebe68
Signed by: stawros
GPG key ID: F89F27AD8F881A91
2 changed files with 17 additions and 8 deletions

View file

@ -0,0 +1,9 @@
package eu.ztsh.wymiana.exception;
public class NoDataException extends RuntimeException {
public NoDataException(String code, String date) {
super("No data for code %s and date %s".formatted(code, date));
}
}

View file

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.ztsh.wymiana.EntityCreator;
import eu.ztsh.wymiana.WireMockExtension;
import eu.ztsh.wymiana.exception.NoDataException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@ -23,6 +24,7 @@ import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ExtendWith(WireMockExtension.class)
class NbpServiceTest {
@ -76,28 +78,26 @@ 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)));
nbpService.fetchData(EntityCreator.Constants.USD_SYMBOL, date);
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
WireMockExtension.verifyGet(1, url);
}
@DisplayName("Fetch rates from cache")
@Test
void getWithCacheTest() throws JsonProcessingException {
void getWithCacheTest() {
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);
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
WireMockExtension.verifyGet(0, url);
}
@DisplayName("Support 404")
@DisplayName("Support 404: invalid currency or no data")
@Test
void getInvalidCurrencyTest() {
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);
assertThatThrownBy(() -> nbpService.getSellRate("usb")).isInstanceOf(NoDataException.class);
WireMockExtension.verifyGet(1, url);
}