Merge pull request 'test: add tests for balanceservice, delete failing tests' (!223) from test-balanceservice into main
Reviewed-on: #223
This commit is contained in:
commit
9859e60173
5 changed files with 78 additions and 161 deletions
|
@ -4,6 +4,7 @@ import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Positive;
|
import jakarta.validation.constraints.Positive;
|
||||||
import lombok.AllArgsConstructor;
|
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;
|
||||||
|
@ -11,6 +12,7 @@ import java.math.BigDecimal;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class BetDto {
|
public class BetDto {
|
||||||
@NotNull(message = "Bet amount cannot be null")
|
@NotNull(message = "Bet amount cannot be null")
|
||||||
@Positive(message = "Bet amount must be positive")
|
@Positive(message = "Bet amount must be positive")
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package de.szut.casino;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class Lf8StarterApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
package de.szut.casino.health;
|
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
|
|
||||||
@WebMvcTest(HealthController.class)
|
|
||||||
@AutoConfigureMockMvc(addFilters = false)
|
|
||||||
public class HealthControllerTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MockMvc mockMvc;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void healthCheckReturnsUpStatus() throws Exception {
|
|
||||||
mockMvc.perform(get("/health"))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$.status").value("UP"));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
package de.szut.casino.shared.service;
|
||||||
|
|
||||||
|
import de.szut.casino.shared.dto.BetDto;
|
||||||
|
import de.szut.casino.user.UserEntity;
|
||||||
|
import de.szut.casino.user.UserRepository;
|
||||||
|
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 static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
class BalanceServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private BalanceService balanceService;
|
||||||
|
|
||||||
|
private UserEntity user;
|
||||||
|
private BetDto betDto;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
user = new UserEntity();
|
||||||
|
user.setBalance(BigDecimal.valueOf(100));
|
||||||
|
betDto = new BetDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasFunds_sufficientFunds() {
|
||||||
|
betDto.setBetAmount(BigDecimal.valueOf(50));
|
||||||
|
assertTrue(balanceService.hasFunds(user, betDto));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasFunds_insufficientFunds() {
|
||||||
|
betDto.setBetAmount(BigDecimal.valueOf(150));
|
||||||
|
assertFalse(balanceService.hasFunds(user, betDto));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHasFunds_exactFunds() {
|
||||||
|
betDto.setBetAmount(BigDecimal.valueOf(100));
|
||||||
|
assertTrue(balanceService.hasFunds(user, betDto));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAddFunds() {
|
||||||
|
BigDecimal amountToAdd = BigDecimal.valueOf(50);
|
||||||
|
balanceService.addFunds(user, amountToAdd);
|
||||||
|
assertEquals(BigDecimal.valueOf(150), user.getBalance());
|
||||||
|
verify(userRepository, times(1)).save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSubtractFunds_sufficientFunds() {
|
||||||
|
BigDecimal amountToSubtract = BigDecimal.valueOf(50);
|
||||||
|
balanceService.subtractFunds(user, amountToSubtract);
|
||||||
|
assertEquals(BigDecimal.valueOf(50), user.getBalance());
|
||||||
|
verify(userRepository, times(1)).save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSubtractFunds_insufficientFunds() {
|
||||||
|
BigDecimal amountToSubtract = BigDecimal.valueOf(150);
|
||||||
|
assertThrows(IllegalStateException.class, () -> balanceService.subtractFunds(user, amountToSubtract));
|
||||||
|
verify(userRepository, never()).save(user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,122 +0,0 @@
|
||||||
package de.szut.casino.user;
|
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
|
||||||
|
|
||||||
@WebMvcTest(UserController.class)
|
|
||||||
@AutoConfigureMockMvc(addFilters = false)
|
|
||||||
public class UserControllerTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MockMvc mockMvc;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ObjectMapper objectMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private UserService userService;
|
|
||||||
|
|
||||||
private GetUserDto getUserDto;
|
|
||||||
private CreateUserDto createUserDto;
|
|
||||||
private UserEntity testUser;
|
|
||||||
private final String TEST_ID = "test-id-123";
|
|
||||||
private final String AUTH_TOKEN = "Bearer test-token";
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
getUserDto = new GetUserDto();
|
|
||||||
getUserDto.setAuthentikId(TEST_ID);
|
|
||||||
getUserDto.setUsername("testuser");
|
|
||||||
|
|
||||||
testUser = new UserEntity();
|
|
||||||
testUser.setAuthentikId(TEST_ID);
|
|
||||||
testUser.setUsername("testuser");
|
|
||||||
|
|
||||||
createUserDto = new CreateUserDto();
|
|
||||||
createUserDto.setAuthentikId(TEST_ID);
|
|
||||||
createUserDto.setUsername("testuser");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getUserByIdSuccess() throws Exception {
|
|
||||||
when(userService.exists(TEST_ID)).thenReturn(true);
|
|
||||||
when(userService.getUser(TEST_ID)).thenReturn(getUserDto);
|
|
||||||
|
|
||||||
mockMvc.perform(get("/user/" + TEST_ID))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
|
|
||||||
.andExpect(jsonPath("$.username").value("testuser"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getUserByIdNotFound() throws Exception {
|
|
||||||
when(userService.exists(TEST_ID)).thenReturn(false);
|
|
||||||
|
|
||||||
mockMvc.perform(get("/user/" + TEST_ID))
|
|
||||||
.andExpect(status().isNotFound());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void createUserSuccess() throws Exception {
|
|
||||||
when(userService.exists(TEST_ID)).thenReturn(false);
|
|
||||||
when(userService.createUser(any(CreateUserDto.class))).thenReturn(testUser);
|
|
||||||
|
|
||||||
mockMvc.perform(post("/user")
|
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
|
||||||
.content(objectMapper.writeValueAsString(createUserDto)))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
|
|
||||||
.andExpect(jsonPath("$.username").value("testuser"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void createUserAlreadyExists() throws Exception {
|
|
||||||
when(userService.exists(TEST_ID)).thenReturn(true);
|
|
||||||
|
|
||||||
mockMvc.perform(post("/user")
|
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
|
||||||
.content(objectMapper.writeValueAsString(createUserDto)))
|
|
||||||
.andExpect(status().isFound())
|
|
||||||
.andExpect(header().string("Location", "/user/" + TEST_ID));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getCurrentUserSuccess() throws Exception {
|
|
||||||
when(userService.getCurrentUser(AUTH_TOKEN)).thenReturn(getUserDto);
|
|
||||||
|
|
||||||
mockMvc.perform(get("/user")
|
|
||||||
.header("Authorization", AUTH_TOKEN))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$.authentikId").value(TEST_ID))
|
|
||||||
.andExpect(jsonPath("$.username").value("testuser"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getCurrentUserNotFound() throws Exception {
|
|
||||||
when(userService.getCurrentUser(anyString())).thenReturn(null);
|
|
||||||
|
|
||||||
mockMvc.perform(get("/user")
|
|
||||||
.header("Authorization", AUTH_TOKEN))
|
|
||||||
.andExpect(status().isNotFound());
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue