Compare commits

...

2 commits

Author SHA1 Message Date
49244526ef PMT-40: Write Tests for Endpoint Implementation
All checks were successful
Quality Check / Validate OAS (push) Successful in 52s
Quality Check / Validate OAS (pull_request) Successful in 1m6s
Quality Check / Linting (push) Successful in 2m8s
Quality Check / Linting (pull_request) Successful in 2m14s
Quality Check / Testing (push) Successful in 2m30s
Quality Check / Static Analysis (push) Successful in 2m34s
Quality Check / Testing (pull_request) Successful in 2m26s
Quality Check / Static Analysis (pull_request) Successful in 2m29s
2024-10-24 10:31:09 +02:00
6b430ae251 PMT-40: Fix Endpoint 2024-10-24 10:30:56 +02:00
2 changed files with 84 additions and 7 deletions

View file

@ -241,16 +241,19 @@ public class ApiController implements DefaultApi {
@Override
public ResponseEntity<EmployeeProjectsDTO> getAListOfAllProjectsFromASpecificEmployee(Long id) {
ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(id);
if (employeeRecord.status() != HttpStatus.OK) {
return new ResponseEntity<>(employeeRecord.status());
}
List<Allocation> allocationsByEmployee = allocationRepository.findAllByEmployeeId(id);
Set<Project> projects = allocationsByEmployee.stream()
.map(Allocation::getProject)
.collect(Collectors.toSet());
EmployeeProjectsDTO response = new EmployeeProjectsDTO();
for (Project project : projects) {
response.addProjectsItem(mapper.mapProject(project));
response.setProjects(new ArrayList<>());
for (Allocation allocation : allocationsByEmployee) {
response.addProjectsItem(mapper.mapProject(allocation.getProject()));
}
return new ResponseEntity<>(HttpStatus.OK);
return ResponseEntity.ok(response);
}
}

View file

@ -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<String, Project> allProjects = createTestProjectData();
Map<String, Allocation> allAllocations = createTestAllocationData(allProjects);
List<Allocation> 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());
}
}