From 36b5d6581d993c25d73e5f176546783b6f4bd059 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:09:34 +0200 Subject: [PATCH] Fix tests --- .../lf8_starter/project/ProjectEntity.java | 2 +- .../project/AddEmployeeToProjectTest.java | 89 +++++++++---------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java b/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java index d9c35a1..6aa7127 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java @@ -25,7 +25,7 @@ public class ProjectEntity { private long leadingEmployee; - @ElementCollection + @ElementCollection(fetch = FetchType.EAGER) private List employees; private long contractor; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java index b8a79ff..d5a918a 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -1,83 +1,80 @@ package de.szut.lf8_starter.integration.project; -import de.szut.lf8_starter.employee.EmployeeService; import de.szut.lf8_starter.project.ProjectEntity; -import de.szut.lf8_starter.project.ProjectService; +import de.szut.lf8_starter.project.ProjectRepository; 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.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; -import java.util.Optional; +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Objects; -import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) class AddEmployeeToProjectActionIntegrationTest { - @Autowired private MockMvc mockMvc; - @MockBean - private ProjectService projectService; + @Autowired + private RestTemplate restTemplate; - @MockBean - private EmployeeService employeeService; + @Autowired + private ProjectRepository projectRepository; @Test - void testAddEmployeeToProject() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + void addEmployeeToProjectTest() throws Exception { + var project = new ProjectEntity(); + project.setComment("comment"); + project.setContractor(1); + project.setContractorName("contractorName"); + project.setEndDate(LocalDate.of(2024, 1, 1)); + project.setLeadingEmployee(1); + project.setName("name"); + project.setStartDate(LocalDate.of(2021, 1, 1)); + project.setEmployees(List.of(1L, 2L, 3L)); + this.projectRepository.save(project); - ProjectEntity projectEntity = new ProjectEntity(); - projectEntity.setId(projectId); - when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); - when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(true); - - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) + mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); + + var updatedProject = projectRepository.findById(1L).get(); + + assert updatedProject.getEmployees().contains(312L); } - @Test - void testAddEmployeeToProject_ProjectNotFound() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + private String getBearerToken() { + String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; - when(projectService.findById(projectId)).thenReturn(Optional.empty()); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); - } + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("client_id", "employee-management-service"); + map.add("username", "user"); + map.add("password", "test"); - @Test - void testAddEmployeeToProject_EmployeeNotFound() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + HttpEntity> request = new HttpEntity<>(map, headers); - ProjectEntity projectEntity = new ProjectEntity(); - projectEntity.setId(projectId); + var response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); - when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); - when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(false); - - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); + return Objects.requireNonNull(response.getBody()).get("access_token").toString(); } } \ No newline at end of file