test: coinflip service
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 53s
CI / Docker backend validation (pull_request) Successful in 54s
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 53s
CI / Docker backend validation (pull_request) Successful in 54s
This commit is contained in:
parent
9859e60173
commit
ce444c5e59
4 changed files with 85 additions and 2 deletions
|
@ -2,13 +2,16 @@ package de.szut.casino.coinflip;
|
||||||
|
|
||||||
import de.szut.casino.shared.dto.BetDto;
|
import de.szut.casino.shared.dto.BetDto;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
public class CoinflipDto extends BetDto {
|
public class CoinflipDto extends BetDto {
|
||||||
@NotNull(message = "chosen side cannot be null")
|
@NotNull(message = "chosen side cannot be null")
|
||||||
private CoinSide coinSide;
|
private CoinSide coinSide;
|
||||||
|
|
|
@ -9,11 +9,12 @@ import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CoinflipService {
|
public class CoinflipService {
|
||||||
private final Random random = new Random();
|
private final Random random;
|
||||||
private final BalanceService balanceService;
|
private final BalanceService balanceService;
|
||||||
|
|
||||||
public CoinflipService(BalanceService balanceService) {
|
public CoinflipService(BalanceService balanceService, Random random) {
|
||||||
this.balanceService = balanceService;
|
this.balanceService = balanceService;
|
||||||
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CoinflipResult play(UserEntity user, CoinflipDto coinflipDto) {
|
public CoinflipResult play(UserEntity user, CoinflipDto coinflipDto) {
|
||||||
|
|
15
backend/src/main/java/de/szut/casino/config/AppConfig.java
Normal file
15
backend/src/main/java/de/szut/casino/config/AppConfig.java
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package de.szut.casino.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class AppConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Random random() {
|
||||||
|
return new Random();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package de.szut.casino.coinflip;
|
||||||
|
|
||||||
|
import de.szut.casino.shared.service.BalanceService;
|
||||||
|
import de.szut.casino.user.UserEntity;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
class CoinflipServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private BalanceService balanceService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Random random;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private CoinflipService coinflipService;
|
||||||
|
|
||||||
|
private UserEntity user;
|
||||||
|
private CoinflipDto coinflipDto;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
user = new UserEntity();
|
||||||
|
user.setBalance(BigDecimal.valueOf(100));
|
||||||
|
coinflipDto = new CoinflipDto(BigDecimal.valueOf(10), CoinSide.HEAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPlay_userWins() {
|
||||||
|
when(random.nextBoolean()).thenReturn(true);
|
||||||
|
|
||||||
|
CoinflipResult result = coinflipService.play(user, coinflipDto);
|
||||||
|
|
||||||
|
assertTrue(result.isWin());
|
||||||
|
assertEquals(BigDecimal.valueOf(20), result.getPayout());
|
||||||
|
assertEquals(CoinSide.HEAD, result.getCoinSide());
|
||||||
|
verify(balanceService, times(1)).subtractFunds(user, BigDecimal.valueOf(10));
|
||||||
|
verify(balanceService, times(1)).addFunds(user, BigDecimal.valueOf(20));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPlay_userLoses() {
|
||||||
|
when(random.nextBoolean()).thenReturn(false);
|
||||||
|
|
||||||
|
CoinflipResult result = coinflipService.play(user, coinflipDto);
|
||||||
|
|
||||||
|
assertFalse(result.isWin());
|
||||||
|
assertEquals(BigDecimal.ZERO, result.getPayout());
|
||||||
|
assertEquals(CoinSide.TAILS, result.getCoinSide());
|
||||||
|
verify(balanceService, times(1)).subtractFunds(user, BigDecimal.valueOf(10));
|
||||||
|
verify(balanceService, never()).addFunds(any(), any());
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue