Compare commits
No commits in common. "52ff4a67b3c8d0f900b68ac1f738f36edf309543" and "ca2a6c179562583f8510227dcef7b280ec8ebb36" have entirely different histories.
52ff4a67b3
...
ca2a6c1795
12 changed files with 13 additions and 499 deletions
|
@ -6,5 +6,5 @@ indent_size = 4
|
||||||
insert_final_newline = true
|
insert_final_newline = true
|
||||||
trim_trailing_whitespace = true
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
[{*.yaml,*.json}]
|
[*.yaml]
|
||||||
indent_size = 2
|
indent_size = 2
|
||||||
|
|
24
pom.xml
24
pom.xml
|
@ -27,10 +27,8 @@
|
||||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||||
|
|
||||||
<!-- dependencies -->
|
<!-- dependencies -->
|
||||||
<wiremock.version>3.5.4</wiremock.version>
|
|
||||||
|
|
||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
<jsonschema2pojo.version>1.2.1</jsonschema2pojo.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -79,12 +77,6 @@
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.wiremock</groupId>
|
|
||||||
<artifactId>wiremock-standalone</artifactId>
|
|
||||||
<version>${wiremock.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -93,22 +85,6 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.jsonschema2pojo</groupId>
|
|
||||||
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
|
|
||||||
<version>${jsonschema2pojo.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
|
|
||||||
<targetPackage>eu.ztsh.wymiana.model</targetPackage>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<goals>
|
|
||||||
<goal>generate</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,89 +0,0 @@
|
||||||
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;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NBP exchange rates service
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
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");
|
|
||||||
|
|
||||||
private final ConcurrentMap<String, RatesCache> cache = new ConcurrentHashMap<>(1);
|
|
||||||
|
|
||||||
public double getSellRate(String currency) {
|
|
||||||
return getCurrency(currency).sell();
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getBuyRate(String currency) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates date for data fetch.
|
|
||||||
* <p>
|
|
||||||
* Usually this would be today, but as rates are set only Mon-Fri, during weekends it is needed to fetch last friday rates.
|
|
||||||
*
|
|
||||||
* @return date for data fetch
|
|
||||||
*/
|
|
||||||
@VisibleForTesting
|
|
||||||
LocalDate getFetchDate() {
|
|
||||||
var today = LocalDate.now(clock);
|
|
||||||
return isWeekend(today) ? today.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)) : today;
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
Rates fetchData(String code, String date) {
|
|
||||||
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) {
|
|
||||||
return today.getDayOfWeek() == DayOfWeek.SATURDAY
|
|
||||||
|| today.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
private record RatesCache(LocalDate date, double buy, double sell) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -4,21 +4,12 @@ import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
import jakarta.validation.ConstraintValidator;
|
import jakarta.validation.ConstraintValidator;
|
||||||
import jakarta.validation.ConstraintValidatorContext;
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
public class ValidExchangeRequestValidator implements
|
public class ValidExchangeRequestValidator implements ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
|
||||||
ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(CurrencyExchangeRequest request,
|
public boolean isValid(CurrencyExchangeRequest currencyExchangeRequest,
|
||||||
ConstraintValidatorContext constraintValidatorContext) {
|
ConstraintValidatorContext constraintValidatorContext) {
|
||||||
if (request == null) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !request.from().equals(request.to())
|
|
||||||
&& !((request.toBuy() == null && request.toSell() == null)
|
|
||||||
|| (request.toBuy() != null && request.toSell() != null))
|
|
||||||
&& ((request.toBuy() != null && request.toBuy() >= 0)
|
|
||||||
|| (request.toSell() != null && request.toSell() >= 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
{
|
|
||||||
"$id": "https://api.nbp.pl/c/rates.json",
|
|
||||||
"$schema": "http://json-schema.org/draft/2020-12/schema",
|
|
||||||
"type": "object",
|
|
||||||
"def": {
|
|
||||||
"rate": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"no": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"effectiveDate": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"bid": {
|
|
||||||
"type": "number"
|
|
||||||
},
|
|
||||||
"ask": {
|
|
||||||
"type": "number"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"no",
|
|
||||||
"effectiveDate",
|
|
||||||
"bid",
|
|
||||||
"ask"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"table": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"currency": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"code": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"rates": {
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "#/def/rate"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"table",
|
|
||||||
"currency",
|
|
||||||
"code",
|
|
||||||
"rates"
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -2,9 +2,6 @@ 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.UserCreateRequest;
|
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -19,12 +16,6 @@ public class EntityCreator {
|
||||||
public static String NAME = "Janina";
|
public static String NAME = "Janina";
|
||||||
public static String SURNAME = "Kowalska";
|
public static String SURNAME = "Kowalska";
|
||||||
public static double PLN = 20.10;
|
public static double PLN = 20.10;
|
||||||
public static double USD_SELL = 5.18;
|
|
||||||
public static double USD_BUY = 5.08;
|
|
||||||
public static String PLN_SYMBOL = "PLN";
|
|
||||||
public static String USD_SYMBOL = "USD";
|
|
||||||
public static double BUY_RATE = 3.8804;
|
|
||||||
public static double SELL_RATE = 3.9572;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,24 +30,6 @@ public class EntityCreator {
|
||||||
.pln(Constants.PLN);
|
.pln(Constants.PLN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CurrencyExchangeRequest.CurrencyExchangeRequestBuilder exchangeRequest() {
|
|
||||||
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 {
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
package eu.ztsh.wymiana;
|
|
||||||
|
|
||||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
|
||||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
|
||||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
|
||||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
|
||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
|
||||||
|
|
||||||
public class WireMockExtension implements BeforeAllCallback, AfterEachCallback, ExtensionContext.Store.CloseableResource {
|
|
||||||
|
|
||||||
public static final String baseUrl = "http://localhost:38080";
|
|
||||||
|
|
||||||
public static void response(String endpoint, int status, String body) {
|
|
||||||
configureFor(38080);
|
|
||||||
stubFor(get(urlEqualTo(endpoint))
|
|
||||||
.willReturn(WireMock.status(status)
|
|
||||||
.withHeader("Content-Type", "application/json")
|
|
||||||
.withBody(body)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void verifyGet(int count, String url) {
|
|
||||||
verify(exactly(count), getRequestedFor(urlEqualTo(url)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final WireMockServer wireMockServer = new WireMockServer(38080);
|
|
||||||
private boolean started;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void beforeAll(ExtensionContext extensionContext) throws Exception {
|
|
||||||
if (!started) {
|
|
||||||
wireMockServer.start();
|
|
||||||
started = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void afterEach(ExtensionContext extensionContext) throws Exception {
|
|
||||||
wireMockServer.listAllStubMappings().getMappings().forEach(wireMockServer::removeStub);
|
|
||||||
wireMockServer.findAllUnmatchedRequests().forEach(System.out::println);
|
|
||||||
wireMockServer.resetRequests();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws Throwable {
|
|
||||||
wireMockServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,117 +0,0 @@
|
||||||
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 eu.ztsh.wymiana.exception.NoDataException;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.EnumSource;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.web.client.RestClient;
|
|
||||||
|
|
||||||
import java.time.Clock;
|
|
||||||
import java.time.DayOfWeek;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.Month;
|
|
||||||
import java.time.ZoneId;
|
|
||||||
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 {
|
|
||||||
|
|
||||||
private static final ZoneId zone = ZoneId.of("Europe/Warsaw");
|
|
||||||
private static final LocalDate today = LocalDate.of(2024, Month.MAY, 12); // Sunday
|
|
||||||
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;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void prepare() {
|
|
||||||
clock = Mockito.mock(Clock.class);
|
|
||||||
Mockito.when(clock.getZone()).thenReturn(zone);
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void prepareTest() {
|
|
||||||
nbpService = new NbpService(clock, restClient);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DisplayName("Check if fetch date is calculated properly: weekdays")
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DisplayName("Check if fetch date is calculated properly: weekends")
|
|
||||||
@ParameterizedTest
|
|
||||||
@EnumSource(value = DayOfWeek.class, names = {"SATURDAY", "SUNDAY"})
|
|
||||||
void getFetchDateOnWeekendTest(DayOfWeek dayOfWeek) {
|
|
||||||
updateClock(dayOfWeek);
|
|
||||||
assertThat(nbpService.getFetchDate()).isEqualTo(LocalDate.of(2024, Month.MAY, 10));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DisplayName("Fetch rates straight from server")
|
|
||||||
@Test
|
|
||||||
void getWithoutCacheTest() 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)));
|
|
||||||
try {
|
|
||||||
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
|
||||||
} finally {
|
|
||||||
WireMockExtension.verifyGet(1, url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@DisplayName("Fetch rates from cache")
|
|
||||||
@Test
|
|
||||||
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(1, url);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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");
|
|
||||||
assertThatThrownBy(() -> nbpService.getSellRate("usb")).isInstanceOf(NoDataException.class);
|
|
||||||
WireMockExtension.verifyGet(1, url);
|
|
||||||
}
|
|
||||||
|
|
||||||
private LocalDate updateClock(DayOfWeek dayOfWeek) {
|
|
||||||
var date = today.with(TemporalAdjusters.previousOrSame(dayOfWeek));
|
|
||||||
Mockito.when(clock.instant()).thenReturn(date.atStartOfDay(zone).toInstant());
|
|
||||||
return LocalDate.from(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,16 +1,8 @@
|
||||||
package eu.ztsh.wymiana.validation;
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
|
||||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import static eu.ztsh.wymiana.EntityCreator.Constants.*;
|
|
||||||
|
|
||||||
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
||||||
|
|
||||||
|
@ -21,108 +13,42 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Valid request with buy value specified")
|
@DisplayName("Valid request with buy value specified")
|
||||||
void validRequestWithBuyTest() {
|
void validRequestWithBuyTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toBuy(USD_BUY)
|
|
||||||
.build()).isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Valid request with sell value specified")
|
@DisplayName("Valid request with sell value specified")
|
||||||
void validRequestWithSellTest() {
|
void validRequestWithSellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Disabled("Already validated (has field annotation)")
|
@Test
|
||||||
@DisplayName("Invalid PESEL value")
|
@DisplayName("Invalid PESEL value")
|
||||||
@ParameterizedTest
|
void invalidPeselTest() {
|
||||||
@MethodSource
|
|
||||||
void invalidPeselTest(String pesel) {
|
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.pesel(pesel)
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("From and To have same value")
|
@DisplayName("From and To have same value")
|
||||||
void sameFromToTest() {
|
void sameFromToTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(USD_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Empty amounts")
|
@DisplayName("Empty amounts")
|
||||||
void emptyBuySellTest() {
|
void emptyBuySellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Disabled("Already validated (has field annotation)")
|
|
||||||
@Test
|
|
||||||
@DisplayName("Empty 'from' value")
|
|
||||||
void emptyFromTest() {
|
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Disabled("Already validated (has field annotation)")
|
|
||||||
@Test
|
|
||||||
@DisplayName("Empty 'to' value")
|
|
||||||
void emptyToTest() {
|
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Both Buy and Sell params filled in")
|
@DisplayName("Both Buy and Sell params filled in")
|
||||||
void bothFilledBuySellTest() {
|
void bothFilledBuySellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toBuy(USD_BUY)
|
|
||||||
.toSell(USD_SELL)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Negative buy amount value")
|
@DisplayName("Negative amount value")
|
||||||
void negativeBuyAmountTest() {
|
void negativeAmountTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toBuy(-1.0)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Negative sell amount value")
|
|
||||||
void negativeSellAmountTest() {
|
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
|
||||||
.from(PLN_SYMBOL)
|
|
||||||
.to(USD_SYMBOL)
|
|
||||||
.toSell(-1.0)
|
|
||||||
.build()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Stream<String> invalidPeselTest() {
|
|
||||||
return Stream.of("INVALID", PESEL.replace('6', '7'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue