diff --git a/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java index 6b0c3b5..8319011 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java @@ -46,7 +46,7 @@ public class CreateProjectAction { for (Long employeeId : createProjectDto.getEmployees()) { if (!this.employeeService.employeeExists(accessToken, employeeId)) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); + return new ResponseEntity<>("Employee with ID: " + employeeId + " not found", HttpStatus.NOT_FOUND); } for (GetProjectDto getProjectDto : this.employeeService.getProjects(employeeId)) { 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..3efc563 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 @@ -7,6 +7,7 @@ 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.http.*; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -22,7 +23,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) -class AddEmployeeToProjectActionIntegrationTest { +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +class AddEmployeeToProjectTest { @Autowired private MockMvc mockMvc; @@ -56,6 +58,87 @@ class AddEmployeeToProjectActionIntegrationTest { 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.setPlannedEndDate(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.setPlannedEndDate(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.setPlannedEndDate(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.setPlannedEndDate(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"; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java index c5f87cc..881b71a 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java @@ -3,12 +3,12 @@ package de.szut.lf8_starter.integration.project; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectRepository; import org.json.JSONObject; -import org.junit.jupiter.api.BeforeEach; 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.http.*; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -28,6 +28,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class CreateProjectActionTest { @Autowired private MockMvc mockMvc; @@ -36,11 +37,6 @@ class CreateProjectActionTest { @Autowired private RestTemplate restTemplate; - @BeforeEach - public void clear() { - this.projectRepository.deleteAll(); - } - @Test void createProjectTest() throws Exception { String content = """ diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java index 985fd2f..8b73e06 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java @@ -7,6 +7,7 @@ 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.http.*; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -23,6 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class GetEmployeesFromProjectTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java index 1b6ed3f..c3d2235 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java @@ -6,6 +6,7 @@ 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.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; @@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class GetProjectActionTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java index 769bd69..f7282ee 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java @@ -7,6 +7,7 @@ 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.http.*; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -23,6 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class GetProjectsFromEmployeeTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java deleted file mode 100644 index bda3c51..0000000 --- a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java +++ /dev/null @@ -1,32 +0,0 @@ -package de.szut.lf8_starter.integration.project; - -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.web.client.TestRestTemplate; -import org.springframework.test.web.servlet.MockMvc; - - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@AutoConfigureMockMvc(addFilters = true) -class ProjectFindAllNotAuthenticated { - @Autowired - private ProjectRepository projectRepository; - - @Autowired - private MockMvc mockMvc; - - @Autowired - private TestRestTemplate restTemplate; - - @Test - void findAllProjects() throws Exception { - this.mockMvc.perform(get("/projects")) - .andExpect(status().isUnauthorized()); - } -} diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java index ff3a642..e424a92 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java @@ -6,6 +6,7 @@ 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.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; @@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class ProjectFindAllSuccessTest { @Autowired private ProjectRepository projectRepository; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/RemoveEmployeeFromProjectIntegrationTest.java b/src/test/java/de/szut/lf8_starter/integration/project/RemoveEmployeeFromProjectIntegrationTest.java index 7e55dd8..4008ddb 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/RemoveEmployeeFromProjectIntegrationTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/RemoveEmployeeFromProjectIntegrationTest.java @@ -7,6 +7,7 @@ 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.http.*; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -17,11 +18,12 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class RemoveEmployeeFromProjectIntegrationTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java index c2c47dd..0dd94fe 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java @@ -6,6 +6,7 @@ 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.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; @@ -17,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class RemoveProjectActionTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java index a38e233..74d889b 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java @@ -8,6 +8,7 @@ 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.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; @@ -23,6 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) class UpdateProjectActionTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties new file mode 100644 index 0000000..f45162f --- /dev/null +++ b/src/test/resources/application-test.properties @@ -0,0 +1,6 @@ +spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.hibernate.ddl-auto=create-drop \ No newline at end of file