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;
@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
private List<Long> employees;
private long contractor;

@ -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();
ptran marked this conversation as resolved
Review

dont use var

dont use var
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<String, String> 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<MultiValueMap<String, String>> 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();
}
}