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] 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); + } +}