From 2f6222ff0010c454a2146644eec76366d0b86265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Tue, 22 Oct 2024 14:32:09 +0200 Subject: [PATCH 1/7] PMT-27: implement endpoint --- api/pmt.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/api/pmt.yml b/api/pmt.yml index e2318d9..b0af215 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -210,3 +210,39 @@ paths: type: string 500: $ref: "#/components/responses/InternalError" + + /project/{id}/employee/{employeeId}: + delete: + operationId: "removeEmployeeFromProject" + description: "Removes an employee from a Project" + parameters: + - in: path + name: id + schema: + type: integer + format: int64 + required: true + - in: path + name: employeeId + schema: + type: integer + format: int64 + required: true + responses: + 204: + description: "Deletes the employee from the Project" + 401: + $ref: "#/components/responses/Unauthorized" + 404: + description: "Employee not found" + content: + text/plain: + schema: + type: string + 409: + $ref: "#/components/responses/Conflict" + 500: + $ref: "#/components/responses/InternalError" + 503: + $ref: "#/components/responses/ServiceUnavailable" + From 967946bee2a734f6bde05aac832ea14f99b54355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Tue, 22 Oct 2024 14:36:24 +0200 Subject: [PATCH 2/7] PMT-27: implement removeEmployeeFromProject method --- src/main/java/de/hmmh/pmt/ApiController.java | 31 +++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 44205b5..e4e8ac1 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -1,12 +1,11 @@ package de.hmmh.pmt; import com.fasterxml.jackson.databind.ObjectMapper; -import de.hmmh.pmt.db.Allocation; -import de.hmmh.pmt.db.AllocationRepository; -import de.hmmh.pmt.db.Project; -import de.hmmh.pmt.db.ProjectRepository; + +import de.hmmh.pmt.db.*; import de.hmmh.pmt.dtos.*; import de.hmmh.pmt.employee.ApiClientFactory; +import de.hmmh.pmt.employee.api.EmployeeControllerApi; import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.util.Mapper; @@ -143,4 +142,28 @@ public class ApiController implements DefaultApi { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + + @Override + public ResponseEntity removeEmployeeFromProject(Long id, Long employeeId){ + if (!projectRepository.existsById(id)) { + return ResponseEntity.notFound().build(); + } + + EmployeeResponseDTO employee; + try { + employee = apiClientFactory.getEmployeeApi().findById(employeeId); + } catch (HttpClientErrorException exception) { + return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND) + ? HttpStatus.NOT_FOUND + : HttpStatus.SERVICE_UNAVAILABLE); + } catch (RestClientException exception) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + Allocation allocation = allocationRepository.findById(id); + if (allocation.getEmployeeId().equals(employeeId)) { + allocationRepository.delete(allocation); + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } From e6d3854fbdd0cc292041872789b7510cb69d3ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 23 Oct 2024 11:09:41 +0200 Subject: [PATCH 3/7] PMT-27-added TestData --- src/test/java/de/hmmh/pmt/IntegrationTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/de/hmmh/pmt/IntegrationTest.java b/src/test/java/de/hmmh/pmt/IntegrationTest.java index 91852a4..65e98f0 100644 --- a/src/test/java/de/hmmh/pmt/IntegrationTest.java +++ b/src/test/java/de/hmmh/pmt/IntegrationTest.java @@ -164,6 +164,13 @@ public abstract class IntegrationTest { allocation1ToOverlapA.setEmployeeId(TEST_EMPLOYEE_A_ID); allocation1ToOverlapA.setRole(TEST_QUALIFICATION_A_ID); allocations.put("1>overlap-a", allocation1ToOverlapA); + + + Allocation allocation1ToSpaceStation = new Allocation(); + allocation1ToSpaceStation.setProject(allProjects.get("space-station")); + allocation1ToSpaceStation.setEmployeeId(TEST_EMPLOYEE_A_ID); + allocation1ToSpaceStation.setRole(TEST_QUALIFICATION_A_ID); + allocations.put("1>space-station", allocation1ToSpaceStation); allocationRepository.saveAllAndFlush(allocations.values()); return allocations; From 8dff301457a79885f9783178aa5de684ba696a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 23 Oct 2024 11:10:40 +0200 Subject: [PATCH 4/7] PMT-27-added Tests for Responses --- .../hmmh/pmt/project/DeleteEmployeeTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java diff --git a/src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java b/src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java new file mode 100644 index 0000000..294dd90 --- /dev/null +++ b/src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java @@ -0,0 +1,59 @@ +package de.hmmh.pmt.project; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.web.client.HttpClientErrorException; + +import de.hmmh.pmt.IntegrationTest; +import de.hmmh.pmt.db.Allocation; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.dtos.AddEmployeeDTO; + +public class DeleteEmployeeTest extends IntegrationTest { + @Test + void shouldNotDeleteEmployeeWhenProjectIsNotFound() throws Exception { + mvc + .perform(delete(baseUri + "/project/1")) + .andExpect(status().isNotFound()) + ; + } + + @Test + void shouldNotDeleteEmployeeWhenEmployeeIsNotAllocated() throws Exception { + when(this.mockEmployeeApi.findById(Mockito.anyLong())) + .thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)); + + Map allProjects = createTestProjectData(); + + this.mvc + .perform(getRequest(allProjects.get("research-lab").getId(), 0L)) + .andExpect(status().isNotFound()); + } + + @Test + void employeeSuccessfullyDeletedFromProject() throws Exception { + Map allProjects = createTestProjectData(); + Map allAllocations = createTestAllocationData(allProjects); + Allocation allocation = allAllocations.get("1>space-station"); + + this.mvc + .perform(getRequest(allocation.getProjectId(),allocation.getEmployeeId())) + .andExpect(status().isNoContent()); + + } + + private RequestBuilder getRequest(Long projectId, Long employeeId) { + return MockMvcRequestBuilders + .delete(baseUri + "/project/" + projectId + "/employee/" + employeeId); + } +} From 2ca9cfbadb1dece1dd0860ba42276f71c1161ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 23 Oct 2024 11:11:58 +0200 Subject: [PATCH 5/7] PMT-27-code fix and cleanup Controller --- src/main/java/de/hmmh/pmt/ApiController.java | 21 ++++---------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index e4e8ac1..5d2a8f4 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -145,25 +145,12 @@ public class ApiController implements DefaultApi { @Override public ResponseEntity removeEmployeeFromProject(Long id, Long employeeId){ - if (!projectRepository.existsById(id)) { - return ResponseEntity.notFound().build(); + Optional allocation = allocationRepository.findById(new AllocationId(id, employeeId)); + if (allocation.isEmpty()){ + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - EmployeeResponseDTO employee; - try { - employee = apiClientFactory.getEmployeeApi().findById(employeeId); - } catch (HttpClientErrorException exception) { - return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND) - ? HttpStatus.NOT_FOUND - : HttpStatus.SERVICE_UNAVAILABLE); - } catch (RestClientException exception) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - - Allocation allocation = allocationRepository.findById(id); - if (allocation.getEmployeeId().equals(employeeId)) { - allocationRepository.delete(allocation); - } + allocationRepository.delete(allocation.get()); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } From ccaf234d286fd5dce7341e60ef488c5f94c5b97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 23 Oct 2024 11:13:06 +0200 Subject: [PATCH 6/7] PMT-27-added Method to AllocationRepository --- src/main/java/de/hmmh/pmt/db/AllocationRepository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java index dcbc9d3..f10e765 100644 --- a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java +++ b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java @@ -7,4 +7,5 @@ import java.util.List; public interface AllocationRepository extends JpaRepository { List findAllByEmployeeId(Long employeeId); + List findAllByProjectId(Long projectId); } From 60ee2e9bb2fee1064fc8c1e0186624fbeecfc7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Wed, 23 Oct 2024 11:27:37 +0200 Subject: [PATCH 7/7] PMT-27-fixed 404 ref --- api/pmt.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/api/pmt.yml b/api/pmt.yml index b0af215..0e803c0 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -234,15 +234,10 @@ paths: 401: $ref: "#/components/responses/Unauthorized" 404: - description: "Employee not found" - content: - text/plain: - schema: - type: string + $ref: "#/components/responses/NotFound" 409: $ref: "#/components/responses/Conflict" 500: $ref: "#/components/responses/InternalError" 503: $ref: "#/components/responses/ServiceUnavailable" -