Merge pull request 'add test for backend' (!42) from tests/backend into main
Reviewed-on: https://git.simonis.lol/projects/casino/pulls/42 Reviewed-by: Huy <ptran@noreply@simonis.lol>
This commit is contained in:
commit
f2bd399c05
3 changed files with 180 additions and 0 deletions
|
@ -36,6 +36,38 @@ jobs:
|
||||||
working-directory: ./backend
|
working-directory: ./backend
|
||||||
run: gradle --stop
|
run: gradle --stop
|
||||||
|
|
||||||
|
test:
|
||||||
|
name: "Test"
|
||||||
|
runs-on: "vps-4"
|
||||||
|
container:
|
||||||
|
image: "cimg/openjdk:22.0-node"
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
- name: Setup Java 22
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
with:
|
||||||
|
distribution: "temurin"
|
||||||
|
java-version: "22"
|
||||||
|
- name: "Cache Gradle dependencies"
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches
|
||||||
|
~/.gradle/wrapper
|
||||||
|
key: gradle-${{ runner.os }}-common
|
||||||
|
restore-keys: |
|
||||||
|
gradle-${{ runner.os }}-
|
||||||
|
- name: "Prepare Gradle"
|
||||||
|
working-directory: ./backend
|
||||||
|
run: gradle clean
|
||||||
|
- name: "Test"
|
||||||
|
working-directory: ./backend
|
||||||
|
run: gradle test
|
||||||
|
- name: "Stop Gradle"
|
||||||
|
working-directory: ./backend
|
||||||
|
run: gradle --stop
|
||||||
|
|
||||||
eslint:
|
eslint:
|
||||||
name: eslint
|
name: eslint
|
||||||
runs-on: vps-4
|
runs-on: vps-4
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
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,122 @@
|
||||||
|
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.setKeycloakId(TEST_ID);
|
||||||
|
getUserDto.setUsername("testuser");
|
||||||
|
|
||||||
|
testUser = new UserEntity();
|
||||||
|
testUser.setKeycloakId(TEST_ID);
|
||||||
|
testUser.setUsername("testuser");
|
||||||
|
|
||||||
|
createUserDto = new CreateUserDto();
|
||||||
|
createUserDto.setKeycloakId(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("$.keycloakId").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("$.keycloakId").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("$.keycloakId").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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue