From 33b3bde1fab5e78aca5f67e1e0add0d0d763c8be Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Wed, 23 Oct 2024 14:14:58 +0200
Subject: [PATCH] Add tests
---
.../project/AddEmployeeToProjectTest.java | 88 ++++++++++++++++++-
1 file changed, 84 insertions(+), 4 deletions(-)
diff --git a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
index 4193e77..ab06823 100644
--- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
+++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java
@@ -45,17 +45,97 @@ class AddEmployeeToProjectActionIntegrationTest {
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
-
- mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312)
+ 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(1L).get();
+ ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
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() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
@@ -74,4 +154,4 @@ class AddEmployeeToProjectActionIntegrationTest {
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
-}
+}
\ No newline at end of file