From 5c4bfdacbedfb97d026e0ad983ae7476af12b356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 23 Oct 2024 11:20:52 +0200 Subject: [PATCH] PMT-43: Add Separate Endpoint for Completing Project, because of Spring and Generation limitation --- api/pmt.yml | 27 +++++++++-- src/main/java/de/hmmh/pmt/ApiController.java | 14 ++++++ src/main/java/de/hmmh/pmt/util/Mapper.java | 1 - .../java/de/hmmh/pmt/project/EditTest.java | 48 +++++++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/test/java/de/hmmh/pmt/project/EditTest.java diff --git a/api/pmt.yml b/api/pmt.yml index 476a476..59837b3 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -65,9 +65,6 @@ components: plannedEnd: type: string format: date-time - end: - type: string - format: date-time CreatedProjectDTO: type: object properties: @@ -260,3 +257,27 @@ paths: type: string 500: $ref: "#/components/responses/InternalError" + /project/{id}/completed: + post: + operationId: "completeProject" + description: "Complete a specific project" + parameters: + - in: path + name: id + schema: + type: integer + format: int64 + required: true + responses: + 204: + description: "Completed" + 401: + $ref: "#/components/responses/Unauthorized" + 404: + description: "Project not found" + content: + text/plain: + schema: + type: string + 500: + $ref: "#/components/responses/InternalError" diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 577b292..0f90f1f 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -19,6 +19,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import java.time.LocalDateTime; import java.util.Optional; @Controller @@ -172,4 +173,17 @@ public class ApiController implements DefaultApi { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + + @Override + public ResponseEntity completeProject(Long id) { + Optional optionalProject = projectRepository.findById(id); + if (optionalProject.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + Project project = optionalProject.get(); + + project.setRealEnd(LocalDateTime.now()); + projectRepository.save(project); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } diff --git a/src/main/java/de/hmmh/pmt/util/Mapper.java b/src/main/java/de/hmmh/pmt/util/Mapper.java index c57ae46..0d1d0e9 100644 --- a/src/main/java/de/hmmh/pmt/util/Mapper.java +++ b/src/main/java/de/hmmh/pmt/util/Mapper.java @@ -38,7 +38,6 @@ public class Mapper { project.setAdministratorId(dto.getAdministratorId()); project.setStart(dto.getStart()); project.setPlannedEnd(dto.getPlannedEnd()); - project.setRealEnd(dto.getEnd()); return project; } } diff --git a/src/test/java/de/hmmh/pmt/project/EditTest.java b/src/test/java/de/hmmh/pmt/project/EditTest.java new file mode 100644 index 0000000..8542e9f --- /dev/null +++ b/src/test/java/de/hmmh/pmt/project/EditTest.java @@ -0,0 +1,48 @@ +package de.hmmh.pmt.project; + +import de.hmmh.pmt.IntegrationTest; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.dtos.CreateProjectDTO; +import de.hmmh.pmt.dtos.UpdateProjectDTO; +import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.Map; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class EditTest extends IntegrationTest { + + @Test + void successfullyRenamed() throws Exception { + when(this.mockEmployeeApi.findById(Mockito.anyLong())) + .thenReturn(new EmployeeResponseDTO()); + + Map allProjects = createTestProjectData(); + Project projectToEdit = allProjects.get("medical-research"); + + UpdateProjectDTO dto = new UpdateProjectDTO(); + dto.setName("Mafia Drug Ring"); + dto.setGoal(projectToEdit.getGoal()); + dto.setCustomerId(projectToEdit.getCustomerId()); + dto.setAdministratorId(projectToEdit.getAdministratorId()); + dto.setStart(projectToEdit.getStart()); + dto.setPlannedEnd(projectToEdit.getPlannedEnd()); + + this.mvc + .perform(getRequest(projectToEdit.getId(), dto)) + .andExpect(status().isNoContent()); + } + + private RequestBuilder getRequest(long id, UpdateProjectDTO dto) throws Exception { + return MockMvcRequestBuilders + .put(baseUri + "/project/" + id) + .content(this.objectMapper.writeValueAsString(dto)) + .contentType(MediaType.APPLICATION_JSON); + } +}