fix: Double to BigDecimal

This commit is contained in:
Piotr Dec 2024-06-10 23:52:47 +02:00
parent b11607088a
commit f42dcce74b
Signed by: stawros
GPG key ID: F89F27AD8F881A91
15 changed files with 93 additions and 57 deletions

View file

@ -9,6 +9,8 @@ import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import eu.ztsh.wymiana.web.model.UserCreateRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -24,13 +26,13 @@ public class EntityCreator {
public static String ANOTHER_PESEL = "08280959342";
public static String NAME = "Janina";
public static String SURNAME = "Kowalska";
public static double PLN = 20.10;
public static double USD_SELL = 5.18;
public static double USD_BUY = 5.08;
public static BigDecimal PLN = createScaled(20.10, 2);
public static BigDecimal USD_SELL = createScaled(5.18, 2);
public static BigDecimal USD_BUY = createScaled(5.08, 2);
public static String PLN_SYMBOL = "PLN";
public static String USD_SYMBOL = "USD";
public static double BUY_RATE = 3.8804;
public static double SELL_RATE = 3.9572;
public static BigDecimal BUY_RATE = createScaled(3.8804, 4);
public static BigDecimal SELL_RATE = createScaled(3.9572, 4);
}
@ -39,25 +41,29 @@ public class EntityCreator {
}
public static User user() {
return user(Constants.PLN, 0);
return user(Constants.PLN, BigDecimal.ZERO);
}
public static User user(double pln, double usd) {
public static User user(BigDecimal pln, BigDecimal usd) {
Map<String, Currency> currencies = new HashMap<>();
if (pln > 0) {
if (pln.signum() > 0) {
currencies.put("PLN", new Currency("PLN", pln));
}
if (usd > 0) {
if (usd.signum() > 0) {
currencies.put("USD", new Currency("USD", usd));
}
return new User(Constants.NAME, Constants.SURNAME, Constants.PESEL, currencies);
}
public static UserCreateRequest.UserCreateRequestBuilder userRequest() {
return userRequest(null);
}
public static UserCreateRequest.UserCreateRequestBuilder userRequest(Double initial) {
return UserCreateRequest.builder().name(Constants.NAME)
.surname(Constants.SURNAME)
.pesel(Constants.PESEL)
.pln(Constants.PLN);
.initial(initial != null ? createScaled(initial, 2) : Constants.PLN);
}
public static CurrencyExchangeRequest.CurrencyExchangeRequestBuilder exchangeRequest() {
@ -83,8 +89,8 @@ public class EntityCreator {
String name;
String surname;
String pesel;
double pln = -1;
double usd = -1;
BigDecimal pln = BigDecimal.valueOf(-1);
BigDecimal usd = BigDecimal.valueOf(-1);
public UserEntityBuilder name(String name) {
this.name = name;
@ -102,22 +108,30 @@ public class EntityCreator {
}
public UserEntityBuilder pln(double pln) {
this.pln = pln;
return this.pln(BigDecimal.valueOf(pln));
}
public UserEntityBuilder pln(BigDecimal pln) {
this.pln = pln.setScale(2, RoundingMode.HALF_UP);
return this;
}
public UserEntityBuilder usd(double usd) {
this.usd = usd;
return this.usd(BigDecimal.valueOf(usd));
}
public UserEntityBuilder usd(BigDecimal usd) {
this.usd = usd.setScale(2, RoundingMode.HALF_UP);
return this;
}
public UserEntity build() {
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
List<CurrencyEntity> currencies = new ArrayList<>();
if (pln > -1) {
if (pln.signum() > -1) {
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
}
if (usd > -1) {
if (usd.signum() > -1) {
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
}
if (currencies.isEmpty()) {
@ -133,4 +147,8 @@ public class EntityCreator {
}
private static BigDecimal createScaled(double input, int scale) {
return BigDecimal.valueOf(input).setScale(scale, RoundingMode.HALF_UP);
}
}

View file

@ -16,6 +16,8 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.stream.Stream;
import static eu.ztsh.wymiana.EntityCreator.Constants.*;
@ -46,7 +48,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
.toSell(PLN)
.build());
assertThat(result.currencies())
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
.matches(map -> map.get(PLN_SYMBOL).amount().signum() == 0 && Objects.equals(map.get(USD_SYMBOL).amount(), USD_BUY), "USD 5.08");
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
expect(expected);
}
@ -62,7 +64,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
.toBuy(USD_BUY)
.build());
assertThat(result.currencies())
.matches(map -> map.get(PLN_SYMBOL).amount() == 0 && map.get(USD_SYMBOL).amount() == USD_BUY);
.matches(map -> map.get(PLN_SYMBOL).amount().signum() == 0 && Objects.equals(map.get(USD_SYMBOL).amount(), USD_BUY));
var expected = EntityCreator.userEntity().pln(0).usd(USD_BUY).build();
expect(expected);
}
@ -78,7 +80,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
.toSell(USD_SELL)
.build());
assertThat(result.currencies())
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
.matches(map -> Objects.equals(map.get(PLN_SYMBOL).amount(), PLN) && map.get(USD_SYMBOL).amount().signum() == 0, "PLN 20.10");
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
expect(expected);
}
@ -94,7 +96,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
.toBuy(PLN)
.build());
assertThat(result.currencies())
.matches(map -> map.get(PLN_SYMBOL).amount() == PLN && map.get(USD_SYMBOL).amount() == 0);
.matches(map -> Objects.equals(map.get(PLN_SYMBOL).amount(), PLN) && map.get(USD_SYMBOL).amount().signum() == 0, "PLN 20.10");
var expected = EntityCreator.userEntity().pln(PLN).usd(0).build();
expect(expected);
}
@ -116,7 +118,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
@Transactional
@Test
void doubleExchangeTest() {
var initialValue = 100;
var initialValue = BigDecimal.valueOf(100);
var entity = EntityCreator.userEntity().pln(initialValue).build();
userRepository.save(entity);
var result1 = currencyService.exchange(EntityCreator.exchangeRequest()
@ -136,7 +138,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
assertThat(resultEntity.getCurrencies()
.stream()
.filter(c -> c.getSymbol().equalsIgnoreCase("PLN"))
.findFirst()).isNotEmpty().get().matches(currencyEntity -> currencyEntity.getAmount() < initialValue);
.findFirst()).isNotEmpty().get().matches(currencyEntity -> currencyEntity.getAmount().compareTo(initialValue) < 0);
}
@Transactional

View file

@ -50,10 +50,9 @@ class UserServiceTest extends RepositoryBasedTest {
@DisplayName("Try to create another user with same PESEL")
void createDuplicatedPeselTest() {
var first = EntityCreator.userRequest().build();
var second = EntityCreator.userRequest()
var second = EntityCreator.userRequest(30.30)
.name("Jan")
.surname("Kowalski")
.pln(30.30)
.build();
userService.create(first);
assertThatThrownBy(() -> userService.create(second))

View file

@ -5,6 +5,8 @@ import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import org.junit.jupiter.api.DisplayName;
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_SELL;
@ -72,7 +74,7 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
assertThatValidation(EntityCreator.exchangeRequest()
.from(PLN_SYMBOL)
.to(USD_SYMBOL)
.toBuy(-1.0)
.toBuy(BigDecimal.valueOf(-1.0))
.build()).isFalse();
}
@ -82,7 +84,7 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
assertThatValidation(EntityCreator.exchangeRequest()
.from(PLN_SYMBOL)
.to(USD_SYMBOL)
.toSell(-1.0)
.toSell(BigDecimal.valueOf(-1.0))
.build()).isFalse();
}

View file

@ -18,6 +18,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@ -80,7 +81,7 @@ class ApplicationIntegrationTests {
void createUserTest() {
webTestClient.post()
.uri(endpoint)
.bodyValue(EntityCreator.userRequest().pln(100).build())
.bodyValue(EntityCreator.userRequest(100D).build())
.exchange()
.expectStatus().isNoContent();
}
@ -169,7 +170,7 @@ class ApplicationIntegrationTests {
200,
asJson(EntityCreator.rates(date))
);
var expected = asJson(EntityCreator.user(100 - PLN, USD_BUY));
var expected = asJson(EntityCreator.user(BigDecimal.valueOf(100).subtract(PLN), USD_BUY));
webTestClient.post()
.uri(endpoint)
.bodyValue(EntityCreator.exchangeRequest()