feat: CurrencyService outline & tests

This commit is contained in:
Piotr Dec 2024-05-24 01:22:34 +02:00
parent 27ff8e4e32
commit b07fa4cb65
Signed by: stawros
GPG key ID: F89F27AD8F881A91
3 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,9 @@
package eu.ztsh.wymiana.exception;
public class InsufficientFundsException extends RuntimeException {
public InsufficientFundsException() {
super("Insufficient funds");
}
}

View file

@ -0,0 +1,21 @@
package eu.ztsh.wymiana.service;
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;
@RequiredArgsConstructor
@Service
public class CurrencyService {
private final UserService userService;
private final NbpService nbpService;
private final InstanceValidator validator;
public User exchange(CurrencyExchangeRequest request) {
throw new UnsupportedOperationException("Not implemented yet");
}
}

View file

@ -0,0 +1,50 @@
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.InsufficientFundsException;
import eu.ztsh.wymiana.validation.InstanceValidator;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
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(USD_SELL);
Mockito.when(nbp.getBuyRate("USD")).thenReturn(USD_BUY);
currencyService = new CurrencyService(new UserService(userRepository, instanceValidator), nbp, instanceValidator);
}
@Test
void successTest() {
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);
}
@Test
void insufficientFundsTest() {
assertThatThrownBy(() -> currencyService.exchange(EntityCreator.exchangeRequest()
.from(PLN_SYMBOL)
.to(USD_SYMBOL)
.toBuy(PLN)
.build()))
.isInstanceOf(InsufficientFundsException.class);
}
}