Compare commits
No commits in common. "master" and "v/1.0.0" have entirely different histories.
29 changed files with 181 additions and 605 deletions
9
pom.xml
9
pom.xml
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
<groupId>eu.ztsh</groupId>
|
<groupId>eu.ztsh</groupId>
|
||||||
<artifactId>wymiana-walut</artifactId>
|
<artifactId>wymiana-walut</artifactId>
|
||||||
<version>1.1.0</version>
|
<version>1.0.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- encoding -->
|
<!-- encoding -->
|
||||||
|
@ -28,7 +28,6 @@
|
||||||
|
|
||||||
<!-- dependencies -->
|
<!-- dependencies -->
|
||||||
<wiremock.version>3.5.4</wiremock.version>
|
<wiremock.version>3.5.4</wiremock.version>
|
||||||
<openapi.version>2.5.0</openapi.version>
|
|
||||||
|
|
||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
<jsonschema2pojo.version>1.2.1</jsonschema2pojo.version>
|
<jsonschema2pojo.version>1.2.1</jsonschema2pojo.version>
|
||||||
|
@ -63,11 +62,6 @@
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springdoc</groupId>
|
|
||||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
|
||||||
<version>${openapi.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Database -->
|
<!-- Database -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -116,7 +110,6 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
|
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
|
||||||
<targetPackage>eu.ztsh.wymiana.model</targetPackage>
|
<targetPackage>eu.ztsh.wymiana.model</targetPackage>
|
||||||
<useBigDecimals>true</useBigDecimals>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
|
|
202
readme.md
202
readme.md
|
@ -1,202 +0,0 @@
|
||||||
# Wymiana walut
|
|
||||||
|
|
||||||
Prosty mikroserwis stworzony na potrzeby rekrutacji
|
|
||||||
|
|
||||||
## Założenia
|
|
||||||
|
|
||||||
- PESEL jedynym identyfikatorem
|
|
||||||
- Jedno konto na PESEL
|
|
||||||
- Użytkownik pełnoletni
|
|
||||||
- Wymiana walut na podstawie tabeli NBP
|
|
||||||
- Baza danych in-memory
|
|
||||||
|
|
||||||
## Interfejsy
|
|
||||||
|
|
||||||
- stworzenie konta
|
|
||||||
|
|
||||||
⇒ POST `/api/user`
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"surname": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"pesel": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"initial": {
|
|
||||||
"type": "number",
|
|
||||||
"description": "początkowy stan konta w domyślnej walucie"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"surname",
|
|
||||||
"pesel",
|
|
||||||
"initial"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
⇐ 204/400/409/500
|
|
||||||
|
|
||||||
- pobranie informacji o koncie
|
|
||||||
|
|
||||||
⇒ GET `/api/user/{pesel}`
|
|
||||||
|
|
||||||
⇐ 200/400/404/500
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"def": {
|
|
||||||
"currency": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"symbol": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"amount": {
|
|
||||||
"type": "number"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"symbol",
|
|
||||||
"amount"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"surname": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"pesel": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"currencies": {
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "#/def/currency"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"surname",
|
|
||||||
"pesel",
|
|
||||||
"currencies"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
- zlecenie wymiany walut
|
|
||||||
|
|
||||||
⇒ POST `/api/exchange/{pesel}`
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"pesel": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"from": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "waluta źródłowa"
|
|
||||||
},
|
|
||||||
"to": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "waluta docelowa"
|
|
||||||
},
|
|
||||||
"toBuy": {
|
|
||||||
"type": "number",
|
|
||||||
"description": "ilość do zakupu"
|
|
||||||
},
|
|
||||||
"toSell": {
|
|
||||||
"type": "number",
|
|
||||||
"description": "ilość do sprzedaży"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"required": [
|
|
||||||
"pesel",
|
|
||||||
"from",
|
|
||||||
"to",
|
|
||||||
"toBuy"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"required": [
|
|
||||||
"pesel",
|
|
||||||
"from",
|
|
||||||
"to",
|
|
||||||
"toSell"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
⇐ 200/400/404/500
|
|
||||||
|
|
||||||
patrz: pobranie informacji o koncie
|
|
||||||
|
|
||||||
## Architektura
|
|
||||||
|
|
||||||
```mermaid
|
|
||||||
flowchart LR
|
|
||||||
actor["fa:fa-person"]
|
|
||||||
subgraph NBP
|
|
||||||
tabC["Tabela C"]
|
|
||||||
end
|
|
||||||
subgraph proces
|
|
||||||
subgraph spring-boot
|
|
||||||
core
|
|
||||||
subgraph endpoint
|
|
||||||
user
|
|
||||||
exchange
|
|
||||||
end
|
|
||||||
end
|
|
||||||
hsqldb
|
|
||||||
end
|
|
||||||
|
|
||||||
actor <--> user
|
|
||||||
actor <--> exchange
|
|
||||||
endpoint <--> core
|
|
||||||
core <--> tabC
|
|
||||||
core <-- hsql . port --> hsqldb
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Konfiguracja
|
|
||||||
|
|
||||||
Aplikacja posiada dostosowaną konfigurację, która obejmuje własności:
|
|
||||||
|
|
||||||
### `hsqldb`
|
|
||||||
|
|
||||||
Konfiguracja bazy danych
|
|
||||||
|
|
||||||
| Nazwa | Opis | Typ | Wartość domyślna |
|
|
||||||
|-------|------------------|--------|------------------|
|
|
||||||
| name | host bazy danych | string | db |
|
|
||||||
| port | port bazy danych | int | 9090 |
|
|
||||||
|
|
||||||
### `nbp`
|
|
||||||
|
|
||||||
Konfiguracja połączenia z Narodowym Bankiem Polskim
|
|
||||||
|
|
||||||
| Nazwa | Opis | Typ | Wartość domyślna |
|
|
||||||
|---------|--------------|--------|-------------------|
|
|
||||||
| baseurl | host API nbp | string | http://api.nbp.pl |
|
|
||||||
|
|
||||||
### `currency`
|
|
||||||
|
|
||||||
Konfiguracja walut
|
|
||||||
|
|
||||||
| Nazwa | Opis | Typ | Wartość domyślna |
|
|
||||||
|---------|-----------------------------------------|--------|------------------|
|
|
||||||
| initial | waluta początkowa przy zakładaniu konta | string | PLN |
|
|
|
@ -1,9 +0,0 @@
|
||||||
package eu.ztsh.wymiana.config;
|
|
||||||
|
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
@ConfigurationProperties("currency")
|
|
||||||
public record CurrencyProperties(Symbol initial) {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +1,12 @@
|
||||||
package eu.ztsh.wymiana.data.entity;
|
package eu.ztsh.wymiana.data.entity;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.EnumType;
|
|
||||||
import jakarta.persistence.Enumerated;
|
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -22,8 +17,7 @@ public class CurrencyEntity {
|
||||||
@Id
|
@Id
|
||||||
String pesel;
|
String pesel;
|
||||||
@Id
|
@Id
|
||||||
@Enumerated(EnumType.STRING)
|
String symbol;
|
||||||
Symbol symbol;
|
Double amount;
|
||||||
BigDecimal amount;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package eu.ztsh.wymiana.exception;
|
package eu.ztsh.wymiana.exception;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
|
|
||||||
public class NoDataException extends RuntimeException {
|
public class NoDataException extends RuntimeException {
|
||||||
|
|
||||||
public NoDataException(Symbol code, String date) {
|
public NoDataException(String code, String date) {
|
||||||
super("No data for code %s and date %s".formatted(code, date));
|
super("No data for code %s and date %s".formatted(code, date));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package eu.ztsh.wymiana.model;
|
package eu.ztsh.wymiana.model;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
public record Currency(String symbol, double amount) {
|
||||||
|
|
||||||
public record Currency(Symbol symbol, BigDecimal amount) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package eu.ztsh.wymiana.model;
|
|
||||||
|
|
||||||
public enum Symbol {
|
|
||||||
PLN,
|
|
||||||
USD
|
|
||||||
}
|
|
|
@ -2,6 +2,6 @@ package eu.ztsh.wymiana.model;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public record User(String name, String surname, String pesel, Map<Symbol, Currency> currencies) {
|
public record User(String name, String surname, String pesel, Map<String, Currency> currencies) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
package eu.ztsh.wymiana.model;
|
|
||||||
|
|
||||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
public class UserCreateRequestConfiguredWrapper {
|
|
||||||
|
|
||||||
private final UserCreateRequest request;
|
|
||||||
private final Symbol initialSymbol;
|
|
||||||
|
|
||||||
public String name() {
|
|
||||||
return request.name();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String surname() {
|
|
||||||
return request.surname();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String pesel() {
|
|
||||||
return request.pesel();
|
|
||||||
}
|
|
||||||
|
|
||||||
public BigDecimal initial() {
|
|
||||||
return request.initial();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Symbol initialSymbol() {
|
|
||||||
return initialSymbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
private UserCreateRequestConfiguredWrapper(Builder builder) {
|
|
||||||
this.request = builder.request;
|
|
||||||
this.initialSymbol = builder.initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Builder wrap(UserCreateRequest request) {
|
|
||||||
return new Builder().withRequest(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Builder {
|
|
||||||
|
|
||||||
private UserCreateRequest request;
|
|
||||||
private Symbol initial;
|
|
||||||
|
|
||||||
private Builder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
private Builder withRequest(UserCreateRequest request) {
|
|
||||||
this.request = request;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder withInitial(Symbol initial) {
|
|
||||||
this.initial = initial;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserCreateRequestConfiguredWrapper build() {
|
|
||||||
return new UserCreateRequestConfiguredWrapper(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -4,7 +4,6 @@ import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
||||||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||||
import eu.ztsh.wymiana.model.Currency;
|
import eu.ztsh.wymiana.model.Currency;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.model.User;
|
import eu.ztsh.wymiana.model.User;
|
||||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
|
@ -29,56 +28,59 @@ public class CurrencyService {
|
||||||
public User exchange(CurrencyExchangeRequest request) {
|
public User exchange(CurrencyExchangeRequest request) {
|
||||||
validator.validate(request);
|
validator.validate(request);
|
||||||
return userService.get(request.pesel()).map(user -> {
|
return userService.get(request.pesel()).map(user -> {
|
||||||
if (!request.from().equals(Symbol.PLN) && !request.to().equals(Symbol.PLN)) {
|
if (!request.from().equalsIgnoreCase("PLN") && !request.to().equalsIgnoreCase("PLN")) {
|
||||||
throw new ExchangeFailedException("Either 'from' or 'to' has to be PLN");
|
throw new ExchangeFailedException("Either 'from' or 'to' has to be PLN");
|
||||||
}
|
}
|
||||||
|
// As we support only USD now, we need to limit second parameter too
|
||||||
|
// Begin: unlock other currencies
|
||||||
|
if (!request.from().equalsIgnoreCase("USD") && !request.to().equalsIgnoreCase("USD")) {
|
||||||
|
throw new ExchangeFailedException("Either 'from' or 'to' has to be USD");
|
||||||
|
}
|
||||||
|
// End: unlock other currencies
|
||||||
|
|
||||||
var from = user.currencies().get(request.from());
|
var from = user.currencies().get(request.from().toUpperCase());
|
||||||
if (from == null) {
|
if (from == null) {
|
||||||
// There is no currency 'from' opened so no need to check if user has funds to exchange
|
// There is no currency 'from' opened so no need to check if user has funds to exchange
|
||||||
throw new InsufficientFundsException();
|
throw new InsufficientFundsException();
|
||||||
}
|
}
|
||||||
var exchanged = performExchange(from,
|
var exchanged = performExchange(from,
|
||||||
Optional.ofNullable(user.currencies().get(request.to())).orElse(create(request.to())),
|
Optional.ofNullable(user.currencies().get(request.to().toUpperCase())).orElse(create(request.to())),
|
||||||
Optional.ofNullable(request.toSell()).orElse(BigDecimal.ZERO),
|
Optional.ofNullable(request.toSell()).orElse(0D),
|
||||||
Optional.ofNullable(request.toBuy()).orElse(BigDecimal.ZERO));
|
Optional.ofNullable(request.toBuy()).orElse(0D));
|
||||||
user.currencies().putAll(exchanged);
|
user.currencies().putAll(exchanged);
|
||||||
return userService.update(user);
|
return userService.update(user);
|
||||||
})
|
})
|
||||||
.orElseThrow(() -> new UserNotFoundException(request));
|
.orElseThrow(() -> new UserNotFoundException(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Currency create(Symbol symbol) {
|
private Currency create(String symbol) {
|
||||||
return new Currency(symbol, BigDecimal.ZERO);
|
// TODO: check if supported - now limited to PLN <-> USD
|
||||||
|
return new Currency(symbol.toUpperCase(), 0D);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Symbol, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
private Map<String, Currency> performExchange(Currency from, Currency to, double toSell, double toBuy) {
|
||||||
BigDecimal exchangeRate;
|
double exchangeRate;
|
||||||
BigDecimal neededFromAmount;
|
double neededFromAmount;
|
||||||
BigDecimal requestedToAmount;
|
double requestedToAmount;
|
||||||
if (from.symbol().equals(Symbol.PLN)) {
|
if (from.symbol().equalsIgnoreCase("PLN")) {
|
||||||
exchangeRate = nbpService.getSellRate(to.symbol());
|
exchangeRate = nbpService.getSellRate(to.symbol());
|
||||||
neededFromAmount = round(toBuy.signum() != 0 ? toBuy.multiply(exchangeRate) : toSell);
|
neededFromAmount = round(toBuy != 0 ? toBuy * exchangeRate : toSell);
|
||||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : divide(toSell, exchangeRate));
|
requestedToAmount = round(toBuy != 0 ? toBuy : toSell / exchangeRate);
|
||||||
} else {
|
} else {
|
||||||
exchangeRate = nbpService.getBuyRate(from.symbol());
|
exchangeRate = nbpService.getBuyRate(from.symbol());
|
||||||
neededFromAmount = round(toBuy.signum() != 0 ? divide(toBuy, exchangeRate) : toSell);
|
neededFromAmount = round(toBuy != 0 ? toBuy / exchangeRate : toSell);
|
||||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : toSell.multiply(exchangeRate));
|
requestedToAmount = round(toBuy != 0 ? toBuy : toSell * exchangeRate);
|
||||||
}
|
}
|
||||||
if (neededFromAmount.compareTo(from.amount()) > 0) {
|
if (neededFromAmount > from.amount()) {
|
||||||
throw new InsufficientFundsException();
|
throw new InsufficientFundsException();
|
||||||
}
|
}
|
||||||
var newFrom = new Currency(from.symbol(), from.amount().subtract(neededFromAmount));
|
var newFrom = new Currency(from.symbol(), from.amount() - neededFromAmount);
|
||||||
var newTo = new Currency(to.symbol(), to.amount().add(requestedToAmount));
|
var newTo = new Currency(to.symbol(), to.amount() + requestedToAmount);
|
||||||
return Stream.of(newFrom, newTo).collect(Collectors.toMap(Currency::symbol, currency -> currency));
|
return Stream.of(newFrom, newTo).collect(Collectors.toMap(Currency::symbol, currency -> currency));
|
||||||
}
|
}
|
||||||
|
|
||||||
private BigDecimal round(BigDecimal input) {
|
private double round(double input) {
|
||||||
return input.setScale(2, RoundingMode.HALF_UP);
|
return BigDecimal.valueOf(input).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||||
}
|
|
||||||
|
|
||||||
private BigDecimal divide(BigDecimal input, BigDecimal division) {
|
|
||||||
return input.setScale(2, RoundingMode.HALF_UP).divide(division, RoundingMode.HALF_UP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,12 @@ package eu.ztsh.wymiana.service;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.exception.NoDataException;
|
import eu.ztsh.wymiana.exception.NoDataException;
|
||||||
import eu.ztsh.wymiana.model.Rates;
|
import eu.ztsh.wymiana.model.Rates;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.assertj.core.util.VisibleForTesting;
|
import org.assertj.core.util.VisibleForTesting;
|
||||||
import org.springframework.http.HttpStatusCode;
|
import org.springframework.http.HttpStatusCode;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
@ -30,28 +28,28 @@ public class NbpService {
|
||||||
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
|
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
|
||||||
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
|
||||||
private final ConcurrentMap<Symbol, RatesCache> cache = new ConcurrentHashMap<>(1);
|
private final ConcurrentMap<String, RatesCache> cache = new ConcurrentHashMap<>(1);
|
||||||
|
|
||||||
public BigDecimal getSellRate(Symbol currency) {
|
public double getSellRate(String currency) {
|
||||||
return getCurrency(currency).sell();
|
return getCurrency(currency.toUpperCase()).sell();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BigDecimal getBuyRate(Symbol currency) {
|
public double getBuyRate(String currency) {
|
||||||
return getCurrency(currency).buy();
|
return getCurrency(currency.toUpperCase()).buy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized RatesCache getCurrency(Symbol currency) {
|
private synchronized RatesCache getCurrency(String currency) {
|
||||||
var today = getFetchDate();
|
var today = getFetchDate();
|
||||||
var cacheObject = cache.get(currency);
|
var cacheObject = cache.get(currency);
|
||||||
if (cacheObject == null || cacheObject.date().isBefore(today)) {
|
if (cacheObject == null || cacheObject.date().isBefore(today)) {
|
||||||
var fresh = fetchData(currency, dtf.format(today));
|
var fresh = fetchData(currency, dtf.format(today));
|
||||||
var rate = fresh.getRates().getFirst();
|
var rate = fresh.getRates().get(0);
|
||||||
cacheObject = new RatesCache(
|
cacheObject = new RatesCache(
|
||||||
LocalDate.parse(rate.getEffectiveDate(), dtf),
|
LocalDate.parse(rate.getEffectiveDate(), dtf),
|
||||||
rate.getBid(),
|
rate.getBid(),
|
||||||
rate.getAsk()
|
rate.getAsk()
|
||||||
);
|
);
|
||||||
cache.put(Symbol.valueOf(fresh.getCode().toUpperCase()), cacheObject);
|
cache.put(fresh.getCode(), cacheObject);
|
||||||
}
|
}
|
||||||
return cacheObject;
|
return cacheObject;
|
||||||
}
|
}
|
||||||
|
@ -70,8 +68,8 @@ public class NbpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Rates fetchData(Symbol code, String date) {
|
Rates fetchData(String code, String date) {
|
||||||
return restClient.get().uri(URI_PATTERN, code.name().toLowerCase(), date)
|
return restClient.get().uri(URI_PATTERN, code.toLowerCase(), date)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.onStatus(HttpStatusCode::is4xxClientError, ((request, response) -> {
|
.onStatus(HttpStatusCode::is4xxClientError, ((request, response) -> {
|
||||||
throw new NoDataException(code, date);
|
throw new NoDataException(code, date);
|
||||||
|
@ -84,7 +82,7 @@ public class NbpService {
|
||||||
|| today.getDayOfWeek() == DayOfWeek.SUNDAY;
|
|| today.getDayOfWeek() == DayOfWeek.SUNDAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private record RatesCache(LocalDate date, BigDecimal buy, BigDecimal sell) {
|
private record RatesCache(LocalDate date, double buy, double sell) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package eu.ztsh.wymiana.service;
|
package eu.ztsh.wymiana.service;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.config.CurrencyProperties;
|
|
||||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||||
import eu.ztsh.wymiana.model.User;
|
import eu.ztsh.wymiana.model.User;
|
||||||
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
|
|
||||||
import eu.ztsh.wymiana.util.UserMapper;
|
import eu.ztsh.wymiana.util.UserMapper;
|
||||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||||
|
@ -22,16 +20,13 @@ public class UserService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final InstanceValidator validator;
|
private final InstanceValidator validator;
|
||||||
private final CurrencyProperties currencyProperties;
|
|
||||||
|
|
||||||
public User create(UserCreateRequest request) {
|
public User create(UserCreateRequest request) {
|
||||||
validator.validate(request);
|
validator.validate(request);
|
||||||
if (userRepository.findById(request.pesel()).isPresent()) {
|
if (userRepository.findById(request.pesel()).isPresent()) {
|
||||||
throw new UserAlreadyExistsException(request);
|
throw new UserAlreadyExistsException(request);
|
||||||
}
|
}
|
||||||
return UserMapper.entityToPojo(userRepository.save(UserMapper.requestToEntity(
|
return UserMapper.entityToPojo(userRepository.save(UserMapper.requestToEntity(request)));
|
||||||
UserCreateRequestConfiguredWrapper.wrap(request).withInitial(currencyProperties.initial()).build()
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<User> get(@PESEL String pesel) {
|
public Optional<User> get(@PESEL String pesel) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package eu.ztsh.wymiana.util;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||||
import eu.ztsh.wymiana.model.Currency;
|
import eu.ztsh.wymiana.model.Currency;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -18,12 +17,12 @@ public class CurrencyMapper {
|
||||||
return new CurrencyEntity(pesel, pojo.symbol(), pojo.amount());
|
return new CurrencyEntity(pesel, pojo.symbol(), pojo.amount());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<Symbol, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||||
return values.stream().map(CurrencyMapper::entityToPojo)
|
return values.stream().map(CurrencyMapper::entityToPojo)
|
||||||
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<CurrencyEntity> pojoMapToEntities(Map<Symbol, Currency> currencies, String pesel) {
|
public static List<CurrencyEntity> pojoMapToEntities(Map<String, Currency> currencies, String pesel) {
|
||||||
return currencies.values().stream().map(entry -> pojoToEntity(entry, pesel)).toList();
|
return currencies.values().stream().map(entry -> pojoToEntity(entry, pesel)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,7 @@ package eu.ztsh.wymiana.util;
|
||||||
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.User;
|
import eu.ztsh.wymiana.model.User;
|
||||||
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
|
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||||
import eu.ztsh.wymiana.web.model.UserResponse;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -20,13 +19,9 @@ public class UserMapper {
|
||||||
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
|
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserResponse pojoToResponse(User pojo) {
|
public static UserEntity requestToEntity(UserCreateRequest request) {
|
||||||
return new UserResponse(pojo.name(), pojo.surname(), pojo.pesel(), pojo.currencies().values().stream().toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static UserEntity requestToEntity(UserCreateRequestConfiguredWrapper request) {
|
|
||||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||||
List.of(new CurrencyEntity(request.pesel(), request.initialSymbol(), request.initial())));
|
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserMapper() {
|
private UserMapper() {
|
||||||
|
|
|
@ -18,8 +18,8 @@ public class ValidExchangeRequestValidator implements
|
||||||
return (request.from() != null && !request.from().equals(request.to()))
|
return (request.from() != null && !request.from().equals(request.to()))
|
||||||
&& !((request.toBuy() == null && request.toSell() == null)
|
&& !((request.toBuy() == null && request.toSell() == null)
|
||||||
|| (request.toBuy() != null && request.toSell() != null))
|
|| (request.toBuy() != null && request.toSell() != null))
|
||||||
&& ((request.toBuy() != null && request.toBuy().signum() >= 0)
|
&& ((request.toBuy() != null && request.toBuy() >= 0)
|
||||||
|| (request.toSell() != null && request.toSell().signum() >= 0));
|
|| (request.toSell() != null && request.toSell() >= 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,9 @@ import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||||
import eu.ztsh.wymiana.service.CurrencyService;
|
import eu.ztsh.wymiana.service.CurrencyService;
|
||||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
import eu.ztsh.wymiana.web.model.UserResponse;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
@ -29,22 +22,6 @@ public class ExchangeController {
|
||||||
|
|
||||||
private final CurrencyService currencyService;
|
private final CurrencyService currencyService;
|
||||||
|
|
||||||
@Operation(summary = "Perform exchange")
|
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "200",
|
|
||||||
description = "Exchange performed successfully",
|
|
||||||
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
|
|
||||||
schema = @Schema(implementation = UserResponse.class))),
|
|
||||||
@ApiResponse(responseCode = "400",
|
|
||||||
description = "Insufficient funds",
|
|
||||||
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
|
|
||||||
@ApiResponse(responseCode = "404",
|
|
||||||
description = "User not found",
|
|
||||||
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
|
|
||||||
@ApiResponse(responseCode = "500",
|
|
||||||
description = "Another error has occurred",
|
|
||||||
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE))
|
|
||||||
})
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<Object> exchange(@Valid @RequestBody CurrencyExchangeRequest request) {
|
public ResponseEntity<Object> exchange(@Valid @RequestBody CurrencyExchangeRequest request) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -2,21 +2,14 @@ package eu.ztsh.wymiana.web.controller;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||||
|
import eu.ztsh.wymiana.model.User;
|
||||||
import eu.ztsh.wymiana.service.UserService;
|
import eu.ztsh.wymiana.service.UserService;
|
||||||
import eu.ztsh.wymiana.util.UserMapper;
|
|
||||||
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
||||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||||
import eu.ztsh.wymiana.web.model.UserResponse;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import jakarta.validation.ValidationException;
|
import jakarta.validation.ValidationException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -30,23 +23,14 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@Validated
|
@Validated
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "/api/user", produces = "application/json")
|
@RequestMapping(path = "/api/user", produces = "application/json")
|
||||||
@Tag(name="User management", description = "Create or get user")
|
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@Operation(summary = "Get user by PESEL")
|
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "200", description = "User found"),
|
|
||||||
@ApiResponse(responseCode = "400", description = "Request not valid", content = @Content),
|
|
||||||
@ApiResponse(responseCode = "404", description = "User not found", content = @Content),
|
|
||||||
@ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content)
|
|
||||||
})
|
|
||||||
@GetMapping("{pesel}")
|
@GetMapping("{pesel}")
|
||||||
public ResponseEntity<UserResponse> get(@PathVariable("pesel") String pesel) {
|
public ResponseEntity<User> get(@PathVariable("pesel") String pesel) {
|
||||||
try {
|
try {
|
||||||
return userService.get(pesel)
|
return userService.get(pesel)
|
||||||
.map(UserMapper::pojoToResponse)
|
|
||||||
.map(ResponseEntity::ok)
|
.map(ResponseEntity::ok)
|
||||||
.orElseThrow(() -> new UserNotFoundException(pesel));
|
.orElseThrow(() -> new UserNotFoundException(pesel));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -58,13 +42,6 @@ public class UserController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "Create user")
|
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "204", description = "User created", content = @Content),
|
|
||||||
@ApiResponse(responseCode = "400", description = "Request not valid", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
|
|
||||||
@ApiResponse(responseCode = "409", description = "User already exists", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
|
|
||||||
@ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE))
|
|
||||||
})
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<String> create(@Valid @RequestBody UserCreateRequest request) {
|
public ResponseEntity<String> create(@Valid @RequestBody UserCreateRequest request) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,22 +1,18 @@
|
||||||
package eu.ztsh.wymiana.web.model;
|
package eu.ztsh.wymiana.web.model;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.validation.ValidExchangeRequest;
|
import eu.ztsh.wymiana.validation.ValidExchangeRequest;
|
||||||
import jakarta.validation.constraints.Min;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import org.hibernate.validator.constraints.pl.PESEL;
|
import org.hibernate.validator.constraints.pl.PESEL;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
@ValidExchangeRequest
|
@ValidExchangeRequest
|
||||||
public record CurrencyExchangeRequest(
|
public record CurrencyExchangeRequest(
|
||||||
@NotNull @PESEL String pesel,
|
@PESEL String pesel,
|
||||||
@NotNull Symbol from,
|
@NotNull String from,
|
||||||
@NotNull Symbol to,
|
@NotNull String to,
|
||||||
@Min(0) BigDecimal toBuy,
|
Double toBuy,
|
||||||
@Min(0) BigDecimal toSell
|
Double toSell
|
||||||
) {
|
) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,11 @@ import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import org.hibernate.validator.constraints.pl.PESEL;
|
import org.hibernate.validator.constraints.pl.PESEL;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
public record UserCreateRequest(
|
public record UserCreateRequest(
|
||||||
@NotNull String name,
|
@NotNull String name,
|
||||||
@NotNull String surname,
|
@NotNull String surname,
|
||||||
@PESEL @Adult @NotNull String pesel,
|
@PESEL @Adult String pesel,
|
||||||
@NotNull @Min(0) BigDecimal initial) {
|
@Min(0) double pln) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
package eu.ztsh.wymiana.web.model;
|
|
||||||
|
|
||||||
import eu.ztsh.wymiana.model.Currency;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public record UserResponse(String name, String surname, String pesel, List<Currency> currencies) {
|
|
||||||
|
|
||||||
}
|
|
|
@ -4,8 +4,6 @@ hsqldb:
|
||||||
|
|
||||||
nbp:
|
nbp:
|
||||||
baseurl: "http://api.nbp.pl"
|
baseurl: "http://api.nbp.pl"
|
||||||
currency:
|
|
||||||
initial: PLN
|
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
datasource:
|
datasource:
|
||||||
|
@ -16,9 +14,6 @@ spring:
|
||||||
jpa:
|
jpa:
|
||||||
hibernate:
|
hibernate:
|
||||||
ddl-auto: create
|
ddl-auto: create
|
||||||
jackson:
|
|
||||||
mapper:
|
|
||||||
ACCEPT_CASE_INSENSITIVE_ENUMS: true
|
|
||||||
|
|
||||||
management:
|
management:
|
||||||
endpoints:
|
endpoints:
|
||||||
|
|
|
@ -5,13 +5,10 @@ import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||||
import eu.ztsh.wymiana.model.Currency;
|
import eu.ztsh.wymiana.model.Currency;
|
||||||
import eu.ztsh.wymiana.model.Rate;
|
import eu.ztsh.wymiana.model.Rate;
|
||||||
import eu.ztsh.wymiana.model.Rates;
|
import eu.ztsh.wymiana.model.Rates;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.model.User;
|
import eu.ztsh.wymiana.model.User;
|
||||||
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;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.RoundingMode;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -27,11 +24,13 @@ public class EntityCreator {
|
||||||
public static String ANOTHER_PESEL = "08280959342";
|
public static String ANOTHER_PESEL = "08280959342";
|
||||||
public static String NAME = "Janina";
|
public static String NAME = "Janina";
|
||||||
public static String SURNAME = "Kowalska";
|
public static String SURNAME = "Kowalska";
|
||||||
public static BigDecimal PLN = createScaled(20.10, 2);
|
public static double PLN = 20.10;
|
||||||
public static BigDecimal USD_SELL = createScaled(5.18, 2);
|
public static double USD_SELL = 5.18;
|
||||||
public static BigDecimal USD_BUY = createScaled(5.08, 2);
|
public static double USD_BUY = 5.08;
|
||||||
public static BigDecimal BUY_RATE = createScaled(3.8804, 4);
|
public static String PLN_SYMBOL = "PLN";
|
||||||
public static BigDecimal SELL_RATE = createScaled(3.9572, 4);
|
public static String USD_SYMBOL = "USD";
|
||||||
|
public static double BUY_RATE = 3.8804;
|
||||||
|
public static double SELL_RATE = 3.9572;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,29 +39,25 @@ public class EntityCreator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User user() {
|
public static User user() {
|
||||||
return user(Constants.PLN, BigDecimal.ZERO);
|
return user(Constants.PLN, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User user(BigDecimal pln, BigDecimal usd) {
|
public static User user(double pln, double usd) {
|
||||||
Map<Symbol, Currency> currencies = new HashMap<>();
|
Map<String, Currency> currencies = new HashMap<>();
|
||||||
if (pln.signum() > 0) {
|
if (pln > 0) {
|
||||||
currencies.put(Symbol.PLN, new Currency(Symbol.PLN, pln));
|
currencies.put("PLN", new Currency("PLN", pln));
|
||||||
}
|
}
|
||||||
if (usd.signum() > 0) {
|
if (usd > 0) {
|
||||||
currencies.put(Symbol.USD, new Currency(Symbol.USD, usd));
|
currencies.put("USD", new Currency("USD", usd));
|
||||||
}
|
}
|
||||||
return new User(Constants.NAME, Constants.SURNAME, Constants.PESEL, currencies);
|
return new User(Constants.NAME, Constants.SURNAME, Constants.PESEL, currencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserCreateRequest.UserCreateRequestBuilder userRequest() {
|
public static UserCreateRequest.UserCreateRequestBuilder userRequest() {
|
||||||
return userRequest(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static UserCreateRequest.UserCreateRequestBuilder userRequest(Double initial) {
|
|
||||||
return UserCreateRequest.builder().name(Constants.NAME)
|
return UserCreateRequest.builder().name(Constants.NAME)
|
||||||
.surname(Constants.SURNAME)
|
.surname(Constants.SURNAME)
|
||||||
.pesel(Constants.PESEL)
|
.pesel(Constants.PESEL)
|
||||||
.initial(initial != null ? createScaled(initial, 2) : Constants.PLN);
|
.pln(Constants.PLN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CurrencyExchangeRequest.CurrencyExchangeRequestBuilder exchangeRequest() {
|
public static CurrencyExchangeRequest.CurrencyExchangeRequestBuilder exchangeRequest() {
|
||||||
|
@ -73,7 +68,7 @@ public class EntityCreator {
|
||||||
var rates = new Rates();
|
var rates = new Rates();
|
||||||
rates.setTable("C");
|
rates.setTable("C");
|
||||||
rates.setCurrency("dolar amerykański");
|
rates.setCurrency("dolar amerykański");
|
||||||
rates.setCode(Symbol.USD.toString());
|
rates.setCode("USD");
|
||||||
var rate = new Rate();
|
var rate = new Rate();
|
||||||
rate.setNo("096/C/NBP/2024");
|
rate.setNo("096/C/NBP/2024");
|
||||||
rate.setEffectiveDate(date);
|
rate.setEffectiveDate(date);
|
||||||
|
@ -88,8 +83,8 @@ public class EntityCreator {
|
||||||
String name;
|
String name;
|
||||||
String surname;
|
String surname;
|
||||||
String pesel;
|
String pesel;
|
||||||
BigDecimal pln = BigDecimal.valueOf(-1);
|
double pln = -1;
|
||||||
BigDecimal usd = BigDecimal.valueOf(-1);
|
double usd = -1;
|
||||||
|
|
||||||
public UserEntityBuilder name(String name) {
|
public UserEntityBuilder name(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -107,34 +102,26 @@ public class EntityCreator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntityBuilder pln(double pln) {
|
public UserEntityBuilder pln(double pln) {
|
||||||
return this.pln(BigDecimal.valueOf(pln));
|
this.pln = pln;
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntityBuilder pln(BigDecimal pln) {
|
|
||||||
this.pln = pln.setScale(2, RoundingMode.HALF_UP);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntityBuilder usd(double usd) {
|
public UserEntityBuilder usd(double usd) {
|
||||||
return this.usd(BigDecimal.valueOf(usd));
|
this.usd = usd;
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntityBuilder usd(BigDecimal usd) {
|
|
||||||
this.usd = usd.setScale(2, RoundingMode.HALF_UP);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity build() {
|
public UserEntity build() {
|
||||||
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
|
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
|
||||||
List<CurrencyEntity> currencies = new ArrayList<>();
|
List<CurrencyEntity> currencies = new ArrayList<>();
|
||||||
if (pln.signum() > -1) {
|
if (pln > -1) {
|
||||||
currencies.add(new CurrencyEntity(nonnulPesel, Symbol.PLN, pln));
|
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
|
||||||
}
|
}
|
||||||
if (usd.signum() > -1) {
|
if (usd > -1) {
|
||||||
currencies.add(new CurrencyEntity(nonnulPesel, Symbol.USD, usd));
|
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
|
||||||
}
|
}
|
||||||
if (currencies.isEmpty()) {
|
if (currencies.isEmpty()) {
|
||||||
currencies.add(new CurrencyEntity(nonnulPesel, Symbol.PLN, Constants.PLN));
|
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", Constants.PLN));
|
||||||
}
|
}
|
||||||
return new UserEntity(
|
return new UserEntity(
|
||||||
nonnulPesel,
|
nonnulPesel,
|
||||||
|
@ -146,8 +133,4 @@ public class EntityCreator {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BigDecimal createScaled(double input, int scale) {
|
|
||||||
return BigDecimal.valueOf(input).setScale(scale, RoundingMode.HALF_UP);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,6 @@ public abstract class RepositoryBasedTest {
|
||||||
.isNotEmpty()
|
.isNotEmpty()
|
||||||
.get()
|
.get()
|
||||||
.usingRecursiveComparison()
|
.usingRecursiveComparison()
|
||||||
.ignoringCollectionOrder()
|
|
||||||
.isEqualTo(entity);
|
.isEqualTo(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,10 @@ package eu.ztsh.wymiana.service;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.RepositoryBasedTest;
|
import eu.ztsh.wymiana.RepositoryBasedTest;
|
||||||
import eu.ztsh.wymiana.config.CurrencyProperties;
|
|
||||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||||
|
import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
||||||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||||
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
|
@ -17,8 +16,6 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static eu.ztsh.wymiana.EntityCreator.Constants.*;
|
import static eu.ztsh.wymiana.EntityCreator.Constants.*;
|
||||||
|
@ -30,14 +27,12 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
private final CurrencyService currencyService;
|
private final CurrencyService currencyService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public CurrencyServiceTest(UserRepository userRepository, InstanceValidator instanceValidator,
|
public CurrencyServiceTest(UserRepository userRepository, InstanceValidator instanceValidator) {
|
||||||
CurrencyProperties currencyProperties) {
|
|
||||||
super(userRepository);
|
super(userRepository);
|
||||||
var nbp = Mockito.mock(NbpService.class);
|
var nbp = Mockito.mock(NbpService.class);
|
||||||
Mockito.when(nbp.getSellRate(Symbol.USD)).thenReturn(SELL_RATE);
|
Mockito.when(nbp.getSellRate("USD")).thenReturn(SELL_RATE);
|
||||||
Mockito.when(nbp.getBuyRate(Symbol.USD)).thenReturn(BUY_RATE);
|
Mockito.when(nbp.getBuyRate("USD")).thenReturn(BUY_RATE);
|
||||||
currencyService = new CurrencyService(new UserService(userRepository, instanceValidator, currencyProperties),
|
currencyService = new CurrencyService(new UserService(userRepository, instanceValidator), nbp, instanceValidator);
|
||||||
nbp, instanceValidator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -46,12 +41,12 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().build();
|
var entity = EntityCreator.userEntity().build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(PLN)
|
.toSell(PLN)
|
||||||
.build());
|
.build());
|
||||||
assertThat(result.currencies())
|
assertThat(result.currencies())
|
||||||
.matches(map -> map.get(Symbol.PLN).amount().signum() == 0 && Objects.equals(map.get(Symbol.USD).amount(), USD_BUY), "USD 5.08");
|
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
|
||||||
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
|
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
|
||||||
expect(expected);
|
expect(expected);
|
||||||
}
|
}
|
||||||
|
@ -62,12 +57,12 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().build();
|
var entity = EntityCreator.userEntity().build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(USD_BUY)
|
.toBuy(USD_BUY)
|
||||||
.build());
|
.build());
|
||||||
assertThat(result.currencies())
|
assertThat(result.currencies())
|
||||||
.matches(map -> map.get(Symbol.PLN).amount().signum() == 0 && Objects.equals(map.get(Symbol.USD).amount(), USD_BUY));
|
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
|
||||||
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
|
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
|
||||||
expect(expected);
|
expect(expected);
|
||||||
}
|
}
|
||||||
|
@ -78,12 +73,12 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().pln(-1).usd(USD_SELL).build();
|
var entity = EntityCreator.userEntity().pln(-1).usd(USD_SELL).build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.PLN)
|
.to(PLN_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build());
|
.build());
|
||||||
assertThat(result.currencies())
|
assertThat(result.currencies())
|
||||||
.matches(map -> Objects.equals(map.get(Symbol.PLN).amount(), PLN) && map.get(Symbol.USD).amount().signum() == 0, "PLN 20.10");
|
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
|
||||||
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
|
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
|
||||||
expect(expected);
|
expect(expected);
|
||||||
}
|
}
|
||||||
|
@ -94,12 +89,12 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().pln(-1).usd(USD_SELL).build();
|
var entity = EntityCreator.userEntity().pln(-1).usd(USD_SELL).build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.PLN)
|
.to(PLN_SYMBOL)
|
||||||
.toBuy(PLN)
|
.toBuy(PLN)
|
||||||
.build());
|
.build());
|
||||||
assertThat(result.currencies())
|
assertThat(result.currencies())
|
||||||
.matches(map -> Objects.equals(map.get(Symbol.PLN).amount(), PLN) && map.get(Symbol.USD).amount().signum() == 0, "PLN 20.10");
|
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
|
||||||
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
|
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
|
||||||
expect(expected);
|
expect(expected);
|
||||||
}
|
}
|
||||||
|
@ -110,8 +105,8 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().build();
|
var entity = EntityCreator.userEntity().build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var request = EntityCreator.exchangeRequest()
|
var request = EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.PLN)
|
.to(PLN_SYMBOL)
|
||||||
.toBuy(PLN)
|
.toBuy(PLN)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(request))
|
assertThatThrownBy(() -> currencyService.exchange(request))
|
||||||
|
@ -121,17 +116,17 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@Transactional
|
@Transactional
|
||||||
@Test
|
@Test
|
||||||
void doubleExchangeTest() {
|
void doubleExchangeTest() {
|
||||||
var initialValue = BigDecimal.valueOf(100);
|
var initialValue = 100;
|
||||||
var entity = EntityCreator.userEntity().pln(initialValue).build();
|
var entity = EntityCreator.userEntity().pln(initialValue).build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
currencyService.exchange(EntityCreator.exchangeRequest()
|
var result1 = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(USD_BUY)
|
.toBuy(USD_BUY)
|
||||||
.build());
|
.build());
|
||||||
currencyService.exchange(EntityCreator.exchangeRequest()
|
var result2 = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.PLN)
|
.to(PLN_SYMBOL)
|
||||||
.toSell(USD_BUY)
|
.toSell(USD_BUY)
|
||||||
.build());
|
.build());
|
||||||
var resultOptional = userRepository.findById(entity.getPesel());
|
var resultOptional = userRepository.findById(entity.getPesel());
|
||||||
|
@ -140,8 +135,8 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var resultEntity = resultOptional.get();
|
var resultEntity = resultOptional.get();
|
||||||
assertThat(resultEntity.getCurrencies()
|
assertThat(resultEntity.getCurrencies()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(c -> c.getSymbol().equals(Symbol.PLN))
|
.filter(c -> c.getSymbol().equalsIgnoreCase("PLN"))
|
||||||
.findFirst()).isNotEmpty().get().matches(currencyEntity -> currencyEntity.getAmount().compareTo(initialValue) < 0);
|
.findFirst()).isNotEmpty().get().matches(currencyEntity -> currencyEntity.getAmount() < initialValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -150,8 +145,8 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
var entity = EntityCreator.userEntity().build();
|
var entity = EntityCreator.userEntity().build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var request = EntityCreator.exchangeRequest()
|
var request = EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(PLN)
|
.toBuy(PLN)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(request))
|
assertThatThrownBy(() -> currencyService.exchange(request))
|
||||||
|
@ -164,8 +159,8 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
void invalidPeselTest(String pesel) {
|
void invalidPeselTest(String pesel) {
|
||||||
var entity = EntityCreator.exchangeRequest()
|
var entity = EntityCreator.exchangeRequest()
|
||||||
.pesel(pesel)
|
.pesel(pesel)
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||||
|
@ -176,8 +171,8 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@Test
|
@Test
|
||||||
void notExistingUserTest() {
|
void notExistingUserTest() {
|
||||||
var entity = EntityCreator.exchangeRequest()
|
var entity = EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||||
|
@ -188,7 +183,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@DisplayName("Empty 'from' value")
|
@DisplayName("Empty 'from' value")
|
||||||
void emptyFromTest() {
|
void emptyFromTest() {
|
||||||
var entity = EntityCreator.exchangeRequest()
|
var entity = EntityCreator.exchangeRequest()
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||||
|
@ -200,7 +195,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@DisplayName("Empty 'to' value")
|
@DisplayName("Empty 'to' value")
|
||||||
void emptyToTest() {
|
void emptyToTest() {
|
||||||
var entity = EntityCreator.exchangeRequest()
|
var entity = EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build();
|
.build();
|
||||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||||
|
|
|
@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.WireMockExtension;
|
import eu.ztsh.wymiana.WireMockExtension;
|
||||||
import eu.ztsh.wymiana.exception.NoDataException;
|
import eu.ztsh.wymiana.exception.NoDataException;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
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.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
@ -80,7 +79,7 @@ class NbpServiceTest {
|
||||||
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
||||||
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
||||||
try {
|
try {
|
||||||
assertThat(nbpService.getSellRate(Symbol.USD)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||||
} finally {
|
} finally {
|
||||||
WireMockExtension.verifyGet(1, url);
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
@ -93,9 +92,9 @@ class NbpServiceTest {
|
||||||
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
||||||
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
WireMockExtension.response(url, 200, new ObjectMapper().writeValueAsString(EntityCreator.rates(date)));
|
||||||
// save to cache
|
// save to cache
|
||||||
assertThat(nbpService.getSellRate(Symbol.USD)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
assertThat(nbpService.getSellRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.SELL_RATE);
|
||||||
// get from cache
|
// get from cache
|
||||||
assertThat(nbpService.getBuyRate(Symbol.USD)).isEqualTo(EntityCreator.Constants.BUY_RATE);
|
assertThat(nbpService.getBuyRate(EntityCreator.Constants.USD_SYMBOL)).isEqualTo(EntityCreator.Constants.BUY_RATE);
|
||||||
WireMockExtension.verifyGet(1, url);
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,9 +102,9 @@ class NbpServiceTest {
|
||||||
@Test
|
@Test
|
||||||
void getInvalidCurrencyTest() {
|
void getInvalidCurrencyTest() {
|
||||||
var date = dtf.format(updateClock(DayOfWeek.FRIDAY));
|
var date = dtf.format(updateClock(DayOfWeek.FRIDAY));
|
||||||
var url = "/api/exchangerates/rates/c/usd/%s/".formatted(date);
|
var url = "/api/exchangerates/rates/c/usb/%s/".formatted(date);
|
||||||
WireMockExtension.response(url, 404, "404 NotFound - Not Found - Brak danych");
|
WireMockExtension.response(url, 404, "404 NotFound - Not Found - Brak danych");
|
||||||
assertThatThrownBy(() -> nbpService.getSellRate(Symbol.USD)).isInstanceOf(NoDataException.class);
|
assertThatThrownBy(() -> nbpService.getSellRate("usb")).isInstanceOf(NoDataException.class);
|
||||||
WireMockExtension.verifyGet(1, url);
|
WireMockExtension.verifyGet(1, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package eu.ztsh.wymiana.service;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.RepositoryBasedTest;
|
import eu.ztsh.wymiana.RepositoryBasedTest;
|
||||||
import eu.ztsh.wymiana.config.CurrencyProperties;
|
|
||||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||||
import eu.ztsh.wymiana.util.UserMapper;
|
import eu.ztsh.wymiana.util.UserMapper;
|
||||||
|
@ -22,10 +21,9 @@ class UserServiceTest extends RepositoryBasedTest {
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UserServiceTest(UserRepository userRepository, InstanceValidator instanceValidator,
|
public UserServiceTest(UserRepository userRepository, InstanceValidator instanceValidator) {
|
||||||
CurrencyProperties currencyProperties) {
|
|
||||||
super(userRepository);
|
super(userRepository);
|
||||||
userService = new UserService(userRepository, instanceValidator, currencyProperties);
|
userService = new UserService(userRepository, instanceValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -44,22 +42,23 @@ class UserServiceTest extends RepositoryBasedTest {
|
||||||
var second = EntityCreator.userRequest().build();
|
var second = EntityCreator.userRequest().build();
|
||||||
userService.create(first);
|
userService.create(first);
|
||||||
assertThatThrownBy(() -> userService.create(second))
|
assertThatThrownBy(() -> userService.create(second))
|
||||||
.isInstanceOf(UserAlreadyExistsException.class)
|
.isInstanceOf(UserAlreadyExistsException.class)
|
||||||
.hasMessage("User with PESEL %s already exists".formatted(EntityCreator.Constants.PESEL));
|
.hasMessage("User with PESEL %s already exists".formatted(EntityCreator.Constants.PESEL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Try to create another user with same PESEL")
|
@DisplayName("Try to create another user with same PESEL")
|
||||||
void createDuplicatedPeselTest() {
|
void createDuplicatedPeselTest() {
|
||||||
var first = EntityCreator.userRequest().build();
|
var first = EntityCreator.userRequest().build();
|
||||||
var second = EntityCreator.userRequest(30.30)
|
var second = EntityCreator.userRequest()
|
||||||
.name("Jan")
|
.name("Jan")
|
||||||
.surname("Kowalski")
|
.surname("Kowalski")
|
||||||
.build();
|
.pln(30.30)
|
||||||
|
.build();
|
||||||
userService.create(first);
|
userService.create(first);
|
||||||
assertThatThrownBy(() -> userService.create(second))
|
assertThatThrownBy(() -> userService.create(second))
|
||||||
.isInstanceOf(UserAlreadyExistsException.class)
|
.isInstanceOf(UserAlreadyExistsException.class)
|
||||||
.hasMessage("User with PESEL %s already exists".formatted(EntityCreator.Constants.PESEL));
|
.hasMessage("User with PESEL %s already exists".formatted(EntityCreator.Constants.PESEL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -67,8 +66,8 @@ class UserServiceTest extends RepositoryBasedTest {
|
||||||
void youngUserTest() {
|
void youngUserTest() {
|
||||||
var request = EntityCreator.userRequest().pesel("").build();
|
var request = EntityCreator.userRequest().pesel("").build();
|
||||||
assertThatThrownBy(() -> userService.create(request))
|
assertThatThrownBy(() -> userService.create(request))
|
||||||
.isInstanceOf(ValidationFailedException.class)
|
.isInstanceOf(ValidationFailedException.class)
|
||||||
.hasMessageContaining(Adult.MESSAGE);
|
.hasMessageContaining(Adult.MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -80,10 +79,10 @@ class UserServiceTest extends RepositoryBasedTest {
|
||||||
var userOptional = userService.get(EntityCreator.Constants.PESEL);
|
var userOptional = userService.get(EntityCreator.Constants.PESEL);
|
||||||
var expected = UserMapper.entityToPojo(entity);
|
var expected = UserMapper.entityToPojo(entity);
|
||||||
assertThat(userOptional)
|
assertThat(userOptional)
|
||||||
.isNotEmpty()
|
.isNotEmpty()
|
||||||
.get()
|
.get()
|
||||||
.usingRecursiveComparison()
|
.usingRecursiveComparison()
|
||||||
.isEqualTo(expected);
|
.isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package eu.ztsh.wymiana.util;
|
package eu.ztsh.wymiana.util;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
import eu.ztsh.wymiana.model.Currency;
|
||||||
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
|
import eu.ztsh.wymiana.model.User;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
class UserMapperTest {
|
class UserMapperTest {
|
||||||
|
@ -29,9 +31,7 @@ class UserMapperTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void requestToEntityTest() {
|
void requestToEntityTest() {
|
||||||
var request = UserCreateRequestConfiguredWrapper.wrap(EntityCreator.userRequest().build())
|
var request = EntityCreator.userRequest().build();
|
||||||
.withInitial(Symbol.PLN)
|
|
||||||
.build() ;
|
|
||||||
var expected = EntityCreator.userEntity().build();
|
var expected = EntityCreator.userEntity().build();
|
||||||
assertThat(UserMapper.requestToEntity(request))
|
assertThat(UserMapper.requestToEntity(request))
|
||||||
.usingRecursiveComparison()
|
.usingRecursiveComparison()
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
package eu.ztsh.wymiana.validation;
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
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 java.math.BigDecimal;
|
import static eu.ztsh.wymiana.EntityCreator.Constants.PLN_SYMBOL;
|
||||||
|
|
||||||
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_BUY;
|
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_BUY;
|
||||||
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_SELL;
|
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_SELL;
|
||||||
|
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_SYMBOL;
|
||||||
|
|
||||||
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
||||||
|
|
||||||
|
@ -21,8 +20,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Valid request with buy value specified")
|
@DisplayName("Valid request with buy value specified")
|
||||||
void validRequestWithBuyTest() {
|
void validRequestWithBuyTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(USD_BUY)
|
.toBuy(USD_BUY)
|
||||||
.build()).isTrue();
|
.build()).isTrue();
|
||||||
}
|
}
|
||||||
|
@ -31,8 +30,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Valid request with sell value specified")
|
@DisplayName("Valid request with sell value specified")
|
||||||
void validRequestWithSellTest() {
|
void validRequestWithSellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build()).isTrue();
|
.build()).isTrue();
|
||||||
}
|
}
|
||||||
|
@ -41,8 +40,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("From and To have same value")
|
@DisplayName("From and To have same value")
|
||||||
void sameFromToTest() {
|
void sameFromToTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build()).isFalse();
|
.build()).isFalse();
|
||||||
}
|
}
|
||||||
|
@ -51,8 +50,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Empty amounts")
|
@DisplayName("Empty amounts")
|
||||||
void emptyBuySellTest() {
|
void emptyBuySellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.build()).isFalse();
|
.build()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,8 +59,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Both Buy and Sell params filled in")
|
@DisplayName("Both Buy and Sell params filled in")
|
||||||
void bothFilledBuySellTest() {
|
void bothFilledBuySellTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(USD_BUY)
|
.toBuy(USD_BUY)
|
||||||
.toSell(USD_SELL)
|
.toSell(USD_SELL)
|
||||||
.build()).isFalse();
|
.build()).isFalse();
|
||||||
|
@ -71,9 +70,9 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Negative buy amount value")
|
@DisplayName("Negative buy amount value")
|
||||||
void negativeBuyAmountTest() {
|
void negativeBuyAmountTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toBuy(BigDecimal.valueOf(-1.0))
|
.toBuy(-1.0)
|
||||||
.build()).isFalse();
|
.build()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,9 +80,9 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
||||||
@DisplayName("Negative sell amount value")
|
@DisplayName("Negative sell amount value")
|
||||||
void negativeSellAmountTest() {
|
void negativeSellAmountTest() {
|
||||||
assertThatValidation(EntityCreator.exchangeRequest()
|
assertThatValidation(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(BigDecimal.valueOf(-1.0))
|
.toSell(-1.0)
|
||||||
.build()).isFalse();
|
.build()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import eu.ztsh.wymiana.EntityCreator;
|
import eu.ztsh.wymiana.EntityCreator;
|
||||||
import eu.ztsh.wymiana.WireMockExtension;
|
import eu.ztsh.wymiana.WireMockExtension;
|
||||||
import eu.ztsh.wymiana.model.Symbol;
|
|
||||||
import eu.ztsh.wymiana.util.UserMapper;
|
import eu.ztsh.wymiana.util.UserMapper;
|
||||||
import org.junit.jupiter.api.ClassOrderer;
|
import org.junit.jupiter.api.ClassOrderer;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
@ -19,7 +18,6 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
@ -82,7 +80,7 @@ class ApplicationIntegrationTests {
|
||||||
void createUserTest() {
|
void createUserTest() {
|
||||||
webTestClient.post()
|
webTestClient.post()
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.userRequest(100D).build())
|
.bodyValue(EntityCreator.userRequest().pln(100).build())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isNoContent();
|
.expectStatus().isNoContent();
|
||||||
}
|
}
|
||||||
|
@ -116,7 +114,7 @@ class ApplicationIntegrationTests {
|
||||||
.uri(endpoint.concat("/").concat(PESEL))
|
.uri(endpoint.concat("/").concat(PESEL))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isOk()
|
.expectStatus().isOk()
|
||||||
.expectBody().json(asJson(UserMapper.pojoToResponse(UserMapper.entityToPojo(EntityCreator.userEntity().pln(100).build()))));
|
.expectBody().json(asJson(UserMapper.entityToPojo(EntityCreator.userEntity().pln(100).build())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -154,8 +152,8 @@ class ApplicationIntegrationTests {
|
||||||
webTestClient.post()
|
webTestClient.post()
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.exchangeRequest()
|
.bodyValue(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(PLN)
|
.toSell(PLN)
|
||||||
.build())
|
.build())
|
||||||
.exchange()
|
.exchange()
|
||||||
|
@ -171,12 +169,12 @@ class ApplicationIntegrationTests {
|
||||||
200,
|
200,
|
||||||
asJson(EntityCreator.rates(date))
|
asJson(EntityCreator.rates(date))
|
||||||
);
|
);
|
||||||
var expected = asJson(EntityCreator.user(BigDecimal.valueOf(100).subtract(PLN), USD_BUY));
|
var expected = asJson(EntityCreator.user(100 - PLN, USD_BUY));
|
||||||
webTestClient.post()
|
webTestClient.post()
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.exchangeRequest()
|
.bodyValue(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(PLN)
|
.toSell(PLN)
|
||||||
.build())
|
.build())
|
||||||
.exchange()
|
.exchange()
|
||||||
|
@ -192,8 +190,8 @@ class ApplicationIntegrationTests {
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.exchangeRequest()
|
.bodyValue(EntityCreator.exchangeRequest()
|
||||||
.pesel(ANOTHER_PESEL)
|
.pesel(ANOTHER_PESEL)
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(PLN)
|
.toSell(PLN)
|
||||||
.build())
|
.build())
|
||||||
.exchange()
|
.exchange()
|
||||||
|
@ -208,8 +206,8 @@ class ApplicationIntegrationTests {
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.exchangeRequest()
|
.bodyValue(EntityCreator.exchangeRequest()
|
||||||
.pesel(INVALID_PESEL)
|
.pesel(INVALID_PESEL)
|
||||||
.from(Symbol.PLN)
|
.from(PLN_SYMBOL)
|
||||||
.to(Symbol.USD)
|
.to(USD_SYMBOL)
|
||||||
.toSell(PLN)
|
.toSell(PLN)
|
||||||
.build())
|
.build())
|
||||||
.exchange()
|
.exchange()
|
||||||
|
@ -222,34 +220,14 @@ class ApplicationIntegrationTests {
|
||||||
webTestClient.post()
|
webTestClient.post()
|
||||||
.uri(endpoint)
|
.uri(endpoint)
|
||||||
.bodyValue(EntityCreator.exchangeRequest()
|
.bodyValue(EntityCreator.exchangeRequest()
|
||||||
.from(Symbol.USD)
|
.from(USD_SYMBOL)
|
||||||
.to(Symbol.PLN)
|
.to(PLN_SYMBOL)
|
||||||
.toBuy(PLN)
|
.toBuy(PLN)
|
||||||
.build())
|
.build())
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus().isBadRequest();
|
.expectStatus().isBadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("03.6: Perform valid money exchange with lower case currency symbols")
|
|
||||||
void ignoreCaseTest() {
|
|
||||||
var date = getTodayOrLastFriday();
|
|
||||||
var expected = asJson(EntityCreator.user(BigDecimal.valueOf(100).subtract(PLN.multiply(BigDecimal.TWO)), USD_BUY.multiply(BigDecimal.TWO)));
|
|
||||||
webTestClient.post()
|
|
||||||
.uri(endpoint)
|
|
||||||
.header("Content-Type", "application/json")
|
|
||||||
.bodyValue(asJson(EntityCreator.exchangeRequest()
|
|
||||||
.from(Symbol.PLN)
|
|
||||||
.to(Symbol.USD)
|
|
||||||
.toSell(PLN)
|
|
||||||
.build()).replace("USD", "usd").replace("PLN", "pln"))
|
|
||||||
.exchange()
|
|
||||||
.expectStatus().isOk()
|
|
||||||
.expectBody().json(expected);
|
|
||||||
// cache
|
|
||||||
WireMockExtension.verifyGet(0, URI_PATTERN.formatted(date));
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getTodayOrLastFriday() {
|
private String getTodayOrLastFriday() {
|
||||||
var today = LocalDate.now();
|
var today = LocalDate.now();
|
||||||
if (today.getDayOfWeek() == DayOfWeek.SATURDAY
|
if (today.getDayOfWeek() == DayOfWeek.SATURDAY
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue