diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 76ac9aa..6ebfb08 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -35,6 +35,38 @@ jobs: - name: "Stop Gradle" working-directory: ./backend 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: name: eslint diff --git a/backend/src/test/java/de/szut/casino/health/HealthControllerTest.java b/backend/src/test/java/de/szut/casino/health/HealthControllerTest.java new file mode 100644 index 0000000..1214c7a --- /dev/null +++ b/backend/src/test/java/de/szut/casino/health/HealthControllerTest.java @@ -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")); + } +} \ No newline at end of file diff --git a/backend/src/test/java/de/szut/casino/user/UserControllerTest.java b/backend/src/test/java/de/szut/casino/user/UserControllerTest.java new file mode 100644 index 0000000..2addb43 --- /dev/null +++ b/backend/src/test/java/de/szut/casino/user/UserControllerTest.java @@ -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()); + } +} \ No newline at end of file