Add tests
Some checks failed
Build PR / Build and analyze (pull_request) Failing after 1m5s
Quality Check / Tests (pull_request) Failing after 1m7s
Quality Check / Checkstyle Main (pull_request) Successful in 23s

This commit is contained in:
Phan Huy Tran 2024-10-23 14:14:58 +02:00
parent 9a123d1e76
commit 33b3bde1fa

@ -45,17 +45,97 @@ class AddEmployeeToProjectActionIntegrationTest {
project.setEmployees(List.of(1L, 2L, 3L)); project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project); this.projectRepository.save(project);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken()) .header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(1L).get(); ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
assert updatedProject.getEmployees().contains(312L); assert updatedProject.getEmployees().contains(312L);
} }
@Test
void addEmployeeToPastProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setComment("past project");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(1991, 1, 1));
project.setLeadingEmployee(1);
project.setName("past project");
project.setStartDate(LocalDate.of(1990, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
assert updatedProject.getEmployees().contains(312L);
}
@Test
void addEmployeeToFutureProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setComment("future project");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2101, 1, 1));
project.setLeadingEmployee(1);
project.setName("future project");
project.setStartDate(LocalDate.of(2100, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
assert updatedProject.getEmployees().contains(312L);
}
@Test
void addEmployeeToProjectWithOverlappingDatesTest() throws Exception {
ProjectEntity project1 = new ProjectEntity();
project1.setComment("project 1");
project1.setContractor(1);
project1.setContractorName("contractorName");
project1.setEndDate(LocalDate.of(2023, 1, 1));
project1.setLeadingEmployee(1);
project1.setName("project 1");
project1.setStartDate(LocalDate.of(2022, 1, 1));
project1.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project1);
ProjectEntity project2 = new ProjectEntity();
project2.setComment("project 2");
project2.setContractor(1);
project2.setContractorName("contractorName");
project2.setEndDate(LocalDate.of(2023, 6, 1));
project2.setLeadingEmployee(1);
project2.setName("project 2");
project2.setStartDate(LocalDate.of(2022, 6, 1));
project2.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project2);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project1.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project2.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isConflict());
}
private String getBearerToken() { private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";