From 9a123d1e76b6882df5a1d5c7046bc5d0b7966566 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 14:11:57 +0200 Subject: [PATCH 1/2] Implement feature to acknowledge project time frames when assigning employees to projects --- requests/project/createProject.http | 6 +- .../lf8_starter/project/ProjectService.java | 15 ++ .../action/crud/CreateProjectAction.java | 33 +++- .../{ => crud}/RemoveProjectAction.java | 2 +- .../employee/AddEmployeeToProjectAction.java | 20 +- .../project/CreateProjectActionTest.java | 177 +++++++++++++++++- 6 files changed, 233 insertions(+), 20 deletions(-) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/RemoveProjectAction.java (97%) diff --git a/requests/project/createProject.http b/requests/project/createProject.http index 3338a2c..1e044b5 100644 --- a/requests/project/createProject.http +++ b/requests/project/createProject.http @@ -6,10 +6,10 @@ Content-Type: application/json { "name": "name", "leading_employee": 1, - "employees": [315, 312], + "employees": [312], "contractor": 4, "contractor_name": "Peter File", "comment": "goal of project", - "start_date": "01.01.2000", - "planned_end_date": "01.01.2001" + "start_date": "02.01.2020", + "planned_end_date": "01.01.2023" } \ No newline at end of file diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java index 17afcea..5a50454 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -1,5 +1,6 @@ package de.szut.lf8_starter.project; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; import org.springframework.stereotype.Service; import java.util.List; @@ -32,4 +33,18 @@ public class ProjectService { public void delete(Long id) { this.projectRepository.deleteById(id); } + + public boolean isOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) { + return isDateRangeOverlapping(getProjectDto, existingProjectEntity) || isDateEqual(getProjectDto, existingProjectEntity); + } + + private boolean isDateRangeOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) { + return getProjectDto.getStartDate().isBefore(existingProjectEntity.getPlannedEndDate()) + && getProjectDto.getPlannedEndDate().isAfter(existingProjectEntity.getStartDate()); + } + + private boolean isDateEqual(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) { + return getProjectDto.getStartDate().isEqual(existingProjectEntity.getStartDate()) + || getProjectDto.getPlannedEndDate().isEqual(existingProjectEntity.getPlannedEndDate()); + } } 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 1725ba8..6b0c3b5 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 @@ -1,5 +1,6 @@ package de.szut.lf8_starter.project.action.crud; +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.dto.project.CreateProjectDto; @@ -12,15 +13,18 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.Valid; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value = "/projects") public class CreateProjectAction { + private final EmployeeService employeeService; private final ProjectService projectService; private final ProjectMapper projectMapper; - public CreateProjectAction(ProjectService projectService, ProjectMapper mappingService) { + public CreateProjectAction(EmployeeService employeeService, ProjectService projectService, ProjectMapper mappingService) { + this.employeeService = employeeService; this.projectService = projectService; this.projectMapper = mappingService; } @@ -29,14 +33,31 @@ public class CreateProjectAction { @ApiResponses(value = { @ApiResponse(responseCode = "201", description = "created project", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))}), @ApiResponse(responseCode = "400", description = "invalid JSON posted", content = @Content), - @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)}) + @ApiResponse(responseCode = "401", description = "not authorized", content = @Content), + @ApiResponse(responseCode = "409", description = "Project dates conflict", content = @Content) + }) @PostMapping @ResponseStatus(code = HttpStatus.CREATED) - public GetProjectDto create(@RequestBody @Valid CreateProjectDto createProjectDto) { - ProjectEntity projectEntity = this.projectMapper.mapCreateDtoToEntity(createProjectDto); + public ResponseEntity create( + @RequestBody @Valid CreateProjectDto createProjectDto, + @RequestHeader("Authorization") String accessToken + ) { + ProjectEntity project = this.projectMapper.mapCreateDtoToEntity(createProjectDto); - projectEntity = this.projectService.create(projectEntity); + for (Long employeeId : createProjectDto.getEmployees()) { + if (!this.employeeService.employeeExists(accessToken, employeeId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } - return this.projectMapper.mapToGetDto(projectEntity); + for (GetProjectDto getProjectDto : this.employeeService.getProjects(employeeId)) { + if (projectService.isOverlapping(getProjectDto, project)) { + return new ResponseEntity<>("Project dates conflict with an existing project for Employee with ID: " + employeeId, HttpStatus.CONFLICT); + } + } + } + + this.projectService.create(project); + + return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.CREATED); } } diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/RemoveProjectAction.java similarity index 97% rename from src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/RemoveProjectAction.java index efbd48d..49aebab 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/RemoveProjectAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectService; diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java index 1e14ae0..d9862e8 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java @@ -3,6 +3,7 @@ package de.szut.lf8_starter.project.action.employee; 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.dto.project.GetProjectDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -29,7 +30,8 @@ public class AddEmployeeToProjectAction { @Operation(summary = "Add an employee to a project") @ApiResponses(value = { @ApiResponse(responseCode = "204", description = "Employee added to project"), - @ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content) + @ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content), + @ApiResponse(responseCode = "409", description = "Project dates conflict", content = @Content) }) @PostMapping("/projects/{projectId}/employees/{employeeId}") public ResponseEntity create( @@ -37,18 +39,26 @@ public class AddEmployeeToProjectAction { @PathVariable Long employeeId, @RequestHeader("Authorization") String accessToken ) { - Optional project = this.projectService.findById(projectId); + Optional optionalProject = this.projectService.findById(projectId); - if (project.isEmpty()) { + if (optionalProject.isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + ProjectEntity projectEntity = optionalProject.get(); + if (!this.employeeService.employeeExists(accessToken, employeeId)) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - project.get().getEmployees().add(employeeId); - this.projectService.update(project.get()); + for (GetProjectDto getProjectDto : this.employeeService.getProjects(employeeId)) { + if (this.projectService.isOverlapping(getProjectDto, projectEntity)) { + return new ResponseEntity<>("Project dates conflict with an existing project for Employee with ID: " + employeeId, HttpStatus.CONFLICT); + } + } + + projectEntity.getEmployees().add(employeeId); + this.projectService.update(projectEntity); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } 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 27fe186..c5f87cc 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,15 +3,21 @@ 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.MediaType; +import org.springframework.http.*; 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.time.LocalDate; -import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; @@ -27,6 +33,13 @@ class CreateProjectActionTest { private MockMvc mockMvc; @Autowired private ProjectRepository projectRepository; + @Autowired + private RestTemplate restTemplate; + + @BeforeEach + public void clear() { + this.projectRepository.deleteAll(); + } @Test void createProjectTest() throws Exception { @@ -34,7 +47,7 @@ class CreateProjectActionTest { { "name": "name", "leading_employee": 1, - "employees": [2, 3], + "employees": [312], "contractor": 4, "contractor_name": "Peter File", "comment": "goal of project", @@ -44,12 +57,15 @@ class CreateProjectActionTest { """; final String contentAsString = this.mockMvc.perform( - post("/projects").content(content).contentType(MediaType.APPLICATION_JSON) + post("/projects") + .content(content) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) ) .andExpect(status().isCreated()) .andExpect(jsonPath("name", is("name"))) .andExpect(jsonPath("leading_employee", is(1))) - .andExpect(jsonPath("employees", is(Arrays.asList(2, 3)))) + .andExpect(jsonPath("employees", is(List.of(312)))) .andExpect(jsonPath("contractor", is(4))) .andExpect(jsonPath("contractor_name", is("Peter File"))) .andExpect(jsonPath("comment", is("goal of project"))) @@ -70,4 +86,155 @@ class CreateProjectActionTest { assertThat(project.get().getStartDate()).isEqualTo(LocalDate.of(2000, 1, 1)); assertThat(project.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2001, 1, 1)); } + + @Test + void createProjectShouldReturnConflictResponseOnConflictingProjects() throws Exception { + String content = """ + { + "name": "name", + "leading_employee": 1, + "employees": [312], + "contractor": 4, + "contractor_name": "Peter File", + "comment": "goal of project", + "start_date": "01.01.2000", + "planned_end_date": "01.01.2001" + } + """; + + this.mockMvc.perform( + post("/projects") + .content(content) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isCreated()); + + this.mockMvc.perform( + post("/projects") + .content(content) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isConflict()); + } + + @Test + void createProjectWithPastStartDate() throws Exception { + String content = """ + { + "name": "past project", + "leading_employee": 1, + "employees": [312], + "contractor": 4, + "contractor_name": "Past Contractor", + "comment": "past project", + "start_date": "01.01.1990", + "planned_end_date": "01.01.1991" + } + """; + + this.mockMvc.perform( + post("/projects") + .content(content) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isCreated()) + .andExpect(jsonPath("name", is("past project"))) + .andExpect(jsonPath("start_date", is("01.01.1990"))) + .andExpect(jsonPath("planned_end_date", is("01.01.1991"))); + } + + @Test + void createProjectWithFutureStartDate() throws Exception { + String content = """ + { + "name": "future project", + "leading_employee": 1, + "employees": [312], + "contractor": 4, + "contractor_name": "Future Contractor", + "comment": "future project", + "start_date": "01.01.2100", + "planned_end_date": "01.01.2101" + } + """; + + this.mockMvc.perform( + post("/projects") + .content(content) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isCreated()) + .andExpect(jsonPath("name", is("future project"))) + .andExpect(jsonPath("start_date", is("01.01.2100"))) + .andExpect(jsonPath("planned_end_date", is("01.01.2101"))); + } + + @Test + void createProjectWithOverlappingDates() throws Exception { + String content1 = """ + { + "name": "project 1", + "leading_employee": 1, + "employees": [312], + "contractor": 4, + "contractor_name": "Contractor 1", + "comment": "project 1", + "start_date": "01.01.2022", + "planned_end_date": "01.01.2023" + } + """; + + String content2 = """ + { + "name": "project 2", + "leading_employee": 1, + "employees": [312], + "contractor": 4, + "contractor_name": "Contractor 2", + "comment": "project 2", + "start_date": "01.06.2022", + "planned_end_date": "01.06.2023" + } + """; + + this.mockMvc.perform( + post("/projects") + .content(content1) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isCreated()); + + this.mockMvc.perform( + post("/projects") + .content(content2) + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) + ) + .andExpect(status().isConflict()); + } + + private String getBearerToken() { + String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("client_id", "employee-management-service"); + map.add("username", "user"); + map.add("password", "test"); + + HttpEntity> request = new HttpEntity<>(map, headers); + + ResponseEntity response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + + return Objects.requireNonNull(response.getBody()).get("access_token").toString(); + } } + -- 2.45.2 From ec69f31b0693645f2de37bf8a9ac81ef8db7e309 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 15:29:36 +0200 Subject: [PATCH 2/2] PLS --- .../action/crud/CreateProjectAction.java | 2 +- .../project/AddEmployeeToProjectTest.java | 85 ++++++++++++++++++- .../project/CreateProjectActionTest.java | 8 +- .../project/GetEmployeesFromProjectTest.java | 2 + .../project/GetProjectActionTest.java | 2 + .../project/GetProjectsFromEmployeeTest.java | 2 + .../ProjectFindAllNotAuthenticated.java | 32 ------- .../project/ProjectFindAllSuccessTest.java | 2 + ...oveEmployeeFromProjectIntegrationTest.java | 4 +- .../project/RemoveProjectActionTest.java | 2 + .../project/UpdateProjectActionTest.java | 2 + .../resources/application-test.properties | 6 ++ 12 files changed, 108 insertions(+), 41 deletions(-) delete mode 100644 src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllNotAuthenticated.java create mode 100644 src/test/resources/application-test.properties 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 -- 2.45.2