Merge pull request 'CurrencyService implementation' from currency-service into master
This commit is contained in:
commit
56162ad895
12 changed files with 367 additions and 58 deletions
|
@ -0,0 +1,13 @@
|
|||
package eu.ztsh.wymiana.exception;
|
||||
|
||||
public class ExchangeFailedException extends RuntimeException {
|
||||
|
||||
public ExchangeFailedException() {
|
||||
this("An exchange error has occurred");
|
||||
}
|
||||
|
||||
public ExchangeFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package eu.ztsh.wymiana.exception;
|
||||
|
||||
public class InsufficientFundsException extends RuntimeException {
|
||||
|
||||
public InsufficientFundsException() {
|
||||
super("Insufficient funds");
|
||||
}
|
||||
|
||||
}
|
85
src/main/java/eu/ztsh/wymiana/service/CurrencyService.java
Normal file
85
src/main/java/eu/ztsh/wymiana/service/CurrencyService.java
Normal file
|
@ -0,0 +1,85 @@
|
|||
package eu.ztsh.wymiana.service;
|
||||
|
||||
import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
||||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class CurrencyService {
|
||||
|
||||
private final UserService userService;
|
||||
private final NbpService nbpService;
|
||||
private final InstanceValidator validator;
|
||||
|
||||
public User exchange(CurrencyExchangeRequest request) {
|
||||
validator.validate(request);
|
||||
return userService.get(request.pesel()).map(user -> {
|
||||
if (!request.from().equalsIgnoreCase("PLN") && !request.to().equalsIgnoreCase("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().toUpperCase());
|
||||
if (from == null) {
|
||||
// There is no currency 'from' opened so no need to check if user has funds to exchange
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
var exchanged = performExchange(from,
|
||||
Optional.ofNullable(user.currencies().get(request.to().toUpperCase())).orElse(create(request.to())),
|
||||
Optional.ofNullable(request.toSell()).orElse(0D),
|
||||
Optional.ofNullable(request.toBuy()).orElse(0D));
|
||||
user.currencies().putAll(exchanged);
|
||||
return userService.update(user);
|
||||
})
|
||||
.orElseThrow(ExchangeFailedException::new);
|
||||
}
|
||||
|
||||
private Currency create(String symbol) {
|
||||
// TODO: check if supported - now limited to PLN <-> USD
|
||||
return new Currency(symbol.toUpperCase(), 0D);
|
||||
}
|
||||
|
||||
private Map<String, Currency> performExchange(Currency from, Currency to, double toSell, double toBuy) {
|
||||
double exchangeRate;
|
||||
double neededFromAmount;
|
||||
double requestedToAmount;
|
||||
if (from.symbol().equalsIgnoreCase("PLN")) {
|
||||
exchangeRate = nbpService.getSellRate(to.symbol());
|
||||
neededFromAmount = round(toBuy != 0 ? toBuy * exchangeRate : toSell);
|
||||
requestedToAmount = round(toBuy != 0 ? toBuy : toSell / exchangeRate);
|
||||
} else {
|
||||
exchangeRate = nbpService.getBuyRate(from.symbol());
|
||||
neededFromAmount = round(toBuy != 0 ? toBuy / exchangeRate : toSell);
|
||||
requestedToAmount = round(toBuy != 0 ? toBuy : toSell * exchangeRate);
|
||||
}
|
||||
if (neededFromAmount > from.amount()) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
var newFrom = new Currency(from.symbol(), from.amount() - neededFromAmount);
|
||||
var newTo = new Currency(to.symbol(), to.amount() + requestedToAmount);
|
||||
return Stream.of(newFrom, newTo).collect(Collectors.toMap(Currency::symbol, currency -> currency));
|
||||
}
|
||||
|
||||
private double round(double input) {
|
||||
return BigDecimal.valueOf(input).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -31,11 +31,11 @@ public class NbpService {
|
|||
private final ConcurrentMap<String, RatesCache> cache = new ConcurrentHashMap<>(1);
|
||||
|
||||
public double getSellRate(String currency) {
|
||||
return getCurrency(currency).sell();
|
||||
return getCurrency(currency.toUpperCase()).sell();
|
||||
}
|
||||
|
||||
public double getBuyRate(String currency) {
|
||||
return getCurrency(currency).buy();
|
||||
return getCurrency(currency.toUpperCase()).buy();
|
||||
}
|
||||
|
||||
private synchronized RatesCache getCurrency(String currency) {
|
||||
|
|
|
@ -30,4 +30,8 @@ public class UserService {
|
|||
return userRepository.findById(pesel).map(UserMapper::entityToPojo);
|
||||
}
|
||||
|
||||
public User update(User user) {
|
||||
return UserMapper.entityToPojo(userRepository.save(UserMapper.pojoToEntity(user)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,11 +13,19 @@ public class CurrencyMapper {
|
|||
return new Currency(entity.getSymbol(), entity.getAmount());
|
||||
}
|
||||
|
||||
public static CurrencyEntity pojoToEntity(Currency pojo, String pesel) {
|
||||
return new CurrencyEntity(pesel, pojo.symbol(), pojo.amount());
|
||||
}
|
||||
|
||||
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||
return values.stream().map(CurrencyMapper::entityToPojo)
|
||||
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
||||
}
|
||||
|
||||
public static List<CurrencyEntity> pojoMapToEntities(Map<String, Currency> currencies, String pesel) {
|
||||
return currencies.values().stream().map(entry -> pojoToEntity(entry, pesel)).toList();
|
||||
}
|
||||
|
||||
private CurrencyMapper() {
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,11 @@ public class UserMapper {
|
|||
CurrencyMapper.entitiesToPojoMap(entity.getCurrencies()));
|
||||
}
|
||||
|
||||
public static UserEntity pojoToEntity(User pojo) {
|
||||
return new UserEntity(pojo.pesel(), pojo.name(), pojo.surname(),
|
||||
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
|
||||
}
|
||||
|
||||
public static UserEntity requestToEntity(UserCreateRequest request) {
|
||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));
|
||||
|
|
|
@ -14,7 +14,8 @@ public class ValidExchangeRequestValidator implements
|
|||
return false;
|
||||
}
|
||||
|
||||
return !request.from().equals(request.to())
|
||||
// Apart from @NotNull annotation we need to check if request.from() != null to avoid NPE in equals
|
||||
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.toBuy() >= 0)
|
||||
|
|
|
@ -62,8 +62,8 @@ public class EntityCreator {
|
|||
String name;
|
||||
String surname;
|
||||
String pesel;
|
||||
double pln;
|
||||
double usd;
|
||||
double pln = -1;
|
||||
double usd = -1;
|
||||
|
||||
public UserEntityBuilder name(String name) {
|
||||
this.name = name;
|
||||
|
@ -93,10 +93,10 @@ public class EntityCreator {
|
|||
public UserEntity build() {
|
||||
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
|
||||
List<CurrencyEntity> currencies = new ArrayList<>();
|
||||
if (pln > 0) {
|
||||
if (pln > -1) {
|
||||
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
|
||||
}
|
||||
if (usd > 0) {
|
||||
if (usd > -1) {
|
||||
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
|
||||
}
|
||||
if (currencies.isEmpty()) {
|
||||
|
|
209
src/test/java/eu/ztsh/wymiana/service/CurrencyServiceTest.java
Normal file
209
src/test/java/eu/ztsh/wymiana/service/CurrencyServiceTest.java
Normal file
|
@ -0,0 +1,209 @@
|
|||
package eu.ztsh.wymiana.service;
|
||||
|
||||
import eu.ztsh.wymiana.EntityCreator;
|
||||
import eu.ztsh.wymiana.RepositoryBasedTest;
|
||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||
import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
||||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||
import eu.ztsh.wymiana.validation.ValidationFailedException;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static eu.ztsh.wymiana.EntityCreator.Constants.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class CurrencyServiceTest extends RepositoryBasedTest {
|
||||
|
||||
private final CurrencyService currencyService;
|
||||
|
||||
@Autowired
|
||||
public CurrencyServiceTest(UserRepository userRepository, InstanceValidator instanceValidator) {
|
||||
super(userRepository);
|
||||
var nbp = Mockito.mock(NbpService.class);
|
||||
Mockito.when(nbp.getSellRate("USD")).thenReturn(SELL_RATE);
|
||||
Mockito.when(nbp.getBuyRate("USD")).thenReturn(BUY_RATE);
|
||||
currencyService = new CurrencyService(new UserService(userRepository, instanceValidator), nbp, instanceValidator);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void plnToUsdToSellSuccessTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
userRepository.save(entity);
|
||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(PLN)
|
||||
.build());
|
||||
assertThat(result.currencies())
|
||||
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
|
||||
var expected = EntityCreator.user().pln(0).usd(USD_BUY).build();
|
||||
expect(expected);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void plnToUsdToBuySuccessTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
userRepository.save(entity);
|
||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toBuy(USD_BUY)
|
||||
.build());
|
||||
assertThat(result.currencies())
|
||||
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
|
||||
var expected = EntityCreator.user().pln(0).usd(USD_BUY).build();
|
||||
expect(expected);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void usdToPlnToSellSuccessTest() {
|
||||
var entity = EntityCreator.user().pln(-1).usd(USD_SELL).build();
|
||||
userRepository.save(entity);
|
||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(USD_SYMBOL)
|
||||
.to(PLN_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build());
|
||||
assertThat(result.currencies())
|
||||
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
|
||||
var expected = EntityCreator.user().pln(PLN).usd(0).build();
|
||||
expect(expected);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void usdToPlnToBuySuccessTest() {
|
||||
var entity = EntityCreator.user().pln(-1).usd(USD_SELL).build();
|
||||
userRepository.save(entity);
|
||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(USD_SYMBOL)
|
||||
.to(PLN_SYMBOL)
|
||||
.toBuy(PLN)
|
||||
.build());
|
||||
assertThat(result.currencies())
|
||||
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
|
||||
var expected = EntityCreator.user().pln(PLN).usd(0).build();
|
||||
expect(expected);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void usdToPlnNoUsdCurrencyTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
userRepository.save(entity);
|
||||
var request = EntityCreator.exchangeRequest()
|
||||
.from(USD_SYMBOL)
|
||||
.to(PLN_SYMBOL)
|
||||
.toBuy(PLN)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(request))
|
||||
.isInstanceOf(InsufficientFundsException.class);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void doubleExchangeTest() {
|
||||
var initialValue = 100;
|
||||
var entity = EntityCreator.user().pln(initialValue).build();
|
||||
userRepository.save(entity);
|
||||
var result1 = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toBuy(USD_BUY)
|
||||
.build());
|
||||
var result2 = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||
.from(USD_SYMBOL)
|
||||
.to(PLN_SYMBOL)
|
||||
.toSell(USD_BUY)
|
||||
.build());
|
||||
var resultOptional = userRepository.findById(entity.getPesel());
|
||||
assertThat(resultOptional)
|
||||
.isNotEmpty();
|
||||
var resultEntity = resultOptional.get();
|
||||
assertThat(resultEntity.getCurrencies()
|
||||
.stream()
|
||||
.filter(c -> c.getSymbol().equalsIgnoreCase("PLN"))
|
||||
.findFirst()).isNotEmpty().get().matches(currencyEntity -> currencyEntity.getAmount() < initialValue);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void insufficientFundsTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
userRepository.save(entity);
|
||||
var request = EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toBuy(PLN)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(request))
|
||||
.isInstanceOf(InsufficientFundsException.class);
|
||||
}
|
||||
|
||||
@DisplayName("Invalid PESEL value")
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void invalidPeselTest(String pesel) {
|
||||
var entity = EntityCreator.exchangeRequest()
|
||||
.pesel(pesel)
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||
.isInstanceOf(ValidationFailedException.class)
|
||||
.hasMessageContaining("PESEL");
|
||||
}
|
||||
|
||||
@Test
|
||||
void notExistingUserTest() {
|
||||
var entity = EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||
.isInstanceOf(ExchangeFailedException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty 'from' value")
|
||||
void emptyFromTest() {
|
||||
var entity = EntityCreator.exchangeRequest()
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||
.isInstanceOf(ValidationFailedException.class)
|
||||
.hasMessageContaining("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty 'to' value")
|
||||
void emptyToTest() {
|
||||
var entity = EntityCreator.exchangeRequest()
|
||||
.from(PLN_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build();
|
||||
assertThatThrownBy(() -> currencyService.exchange(entity))
|
||||
.isInstanceOf(ValidationFailedException.class)
|
||||
.hasMessageContaining("null");
|
||||
}
|
||||
|
||||
private static Stream<String> invalidPeselTest() {
|
||||
return Stream.of("INVALID", PESEL.replace('6', '7'));
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,20 @@ class UserMapperTest {
|
|||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pojoToEntityTest() {
|
||||
var entity = new User(
|
||||
EntityCreator.Constants.NAME,
|
||||
EntityCreator.Constants.SURNAME,
|
||||
EntityCreator.Constants.PESEL,
|
||||
Map.of("PLN", new Currency("PLN", EntityCreator.Constants.PLN))
|
||||
);
|
||||
var expected = EntityCreator.user().build();
|
||||
assertThat(UserMapper.pojoToEntity(entity))
|
||||
.usingRecursiveComparison()
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestToEntityTest() {
|
||||
var request = EntityCreator.userRequest().build();
|
||||
|
|
|
@ -2,15 +2,13 @@ package eu.ztsh.wymiana.validation;
|
|||
|
||||
import eu.ztsh.wymiana.EntityCreator;
|
||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
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.*;
|
||||
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_SELL;
|
||||
import static eu.ztsh.wymiana.EntityCreator.Constants.USD_SYMBOL;
|
||||
|
||||
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
||||
|
||||
|
@ -38,19 +36,6 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.build()).isTrue();
|
||||
}
|
||||
|
||||
@Disabled("Already validated (has field annotation)")
|
||||
@DisplayName("Invalid PESEL value")
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void invalidPeselTest(String pesel) {
|
||||
assertThatValidation(EntityCreator.exchangeRequest()
|
||||
.pesel(pesel)
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("From and To have same value")
|
||||
void sameFromToTest() {
|
||||
|
@ -70,26 +55,6 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.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
|
||||
@DisplayName("Both Buy and Sell params filled in")
|
||||
void bothFilledBuySellTest() {
|
||||
|
@ -121,8 +86,4 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.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