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