Compare commits
2 commits
096c25e33b
...
49244526ef
Author | SHA1 | Date | |
---|---|---|---|
49244526ef | |||
6b430ae251 |
2 changed files with 84 additions and 7 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue