From 49244526ef625ff6b4eecae913766535e66bca17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Thu, 24 Oct 2024 10:31:09 +0200 Subject: [PATCH] PMT-40: Write Tests for Endpoint Implementation --- .../project/GetAllProjectsByEmployeeTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/de/hmmh/pmt/project/GetAllProjectsByEmployeeTest.java diff --git a/src/test/java/de/hmmh/pmt/project/GetAllProjectsByEmployeeTest.java b/src/test/java/de/hmmh/pmt/project/GetAllProjectsByEmployeeTest.java new file mode 100644 index 0000000..99d681a --- /dev/null +++ b/src/test/java/de/hmmh/pmt/project/GetAllProjectsByEmployeeTest.java @@ -0,0 +1,74 @@ +package de.hmmh.pmt.project; + +import de.hmmh.pmt.IntegrationTest; +import de.hmmh.pmt.db.Allocation; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; + +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasSize; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class GetAllProjectsByEmployeeTest extends IntegrationTest { + + @Test + void successfullyGetAllProjectsByEmployee() throws Exception { + when(this.mockEmployeeApi.findById(Mockito.anyLong())) + .thenReturn(new EmployeeResponseDTO()); + + Map allProjects = createTestProjectData(); + Map allAllocations = createTestAllocationData(allProjects); + + List allocations = allAllocations + .values() + .stream() + .filter(allocation -> allocation.getEmployeeId().equals(TEST_EMPLOYEE_A_ID)) + .toList(); + + mvc + .perform(get(baseUri + "/employye/" + TEST_EMPLOYEE_A_ID + "/projects")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.projects", hasSize(allocations.size()))) + .andExpect(jsonPath("$.projects[*].id").exists()) + .andExpect(jsonPath("$.projects[*].name").exists()) + .andExpect(jsonPath("$.projects[*].goal").exists()) + .andExpect(jsonPath("$.projects[*].customerId").exists()) + .andExpect(jsonPath("$.projects[*].administratorId").exists()) + .andExpect(jsonPath("$.projects[*].plannedEnd").exists()) + .andExpect(jsonPath("$.projects[*].realEnd").exists()); + } + + @Test + void shouldReturnEmptyListWhenNoAllocationsFound() throws Exception { + when(this.mockEmployeeApi.findById(Mockito.anyLong())) + .thenReturn(new EmployeeResponseDTO()); + + createTestProjectData(); + + mvc + .perform(get(baseUri + "/employye/" + TEST_EMPLOYEE_A_ID + "/projects")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.projects", empty())); + } + + @Test + void shouldReturnNotFoundWhenEmployeeDoesNotExist() throws Exception { + when(this.mockEmployeeApi.findById(Mockito.anyLong())) + .thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)); + + mvc + .perform(get(baseUri + "/employye/0/projects")) + .andExpect(status().isNotFound()); + } +}