feat: Implement add employee to project route (SCRUM-2) #32
@ -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
|
||||
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();
|
||||
ptran marked this conversation as resolved
Outdated
jank
commented
also here also here
|
||||
|
||||
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);
|
||||
ptran marked this conversation as resolved
Outdated
jank
commented
and here and here
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
dont use var