feat: Implement add employee to project route (SCRUM-2) #32

Merged
jank merged 8 commits from feature/add-employee-to-project into main 2024-10-23 08:39:26 +00:00
2 changed files with 44 additions and 47 deletions
Showing only changes of commit 36b5d6581d - Show all commits

@ -25,7 +25,7 @@ public class ProjectEntity {
private long leadingEmployee; private long leadingEmployee;
@ElementCollection @ElementCollection(fetch = FetchType.EAGER)
private List<Long> employees; private List<Long> employees;
private long contractor; private long contractor;

@ -1,83 +1,80 @@
package de.szut.lf8_starter.integration.project; 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.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService; import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; 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.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; 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.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
class AddEmployeeToProjectActionIntegrationTest { class AddEmployeeToProjectActionIntegrationTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @Autowired
private ProjectService projectService; private RestTemplate restTemplate;
@MockBean @Autowired
private EmployeeService employeeService; private ProjectRepository projectRepository;
@Test @Test
void testAddEmployeeToProject() throws Exception { void addEmployeeToProjectTest() throws Exception {
Long projectId = 1L; var project = new ProjectEntity();
ptran marked this conversation as resolved
Review

dont use var

dont use var
Long employeeId = 1L; project.setComment("comment");
String accessToken = "Bearer valid_token"; 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)); mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312)
when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(true); .header(HttpHeaders.AUTHORIZATION, getBearerToken())
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId)
.header(HttpHeaders.AUTHORIZATION, accessToken)
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
var updatedProject = projectRepository.findById(1L).get();
assert updatedProject.getEmployees().contains(312L);
} }
@Test private String getBearerToken() {
void testAddEmployeeToProject_ProjectNotFound() throws Exception { String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
Long projectId = 1L;
Long employeeId = 1L;
String accessToken = "Bearer valid_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) MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
.header(HttpHeaders.AUTHORIZATION, accessToken) map.add("grant_type", "password");
.contentType(MediaType.APPLICATION_JSON)) map.add("client_id", "employee-management-service");
.andExpect(status().isNotFound()); map.add("username", "user");
} map.add("password", "test");
@Test HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
void testAddEmployeeToProject_EmployeeNotFound() throws Exception {
Long projectId = 1L;
Long employeeId = 1L;
String accessToken = "Bearer valid_token";
ProjectEntity projectEntity = new ProjectEntity(); var response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
projectEntity.setId(projectId);
when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); return Objects.requireNonNull(response.getBody()).get("access_token").toString();
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());
} }
} }