PMT-40: alle Projekte eines Bestimmten Mitarbeiters abfragen #18

Merged
SZUT-Dominik merged 4 commits from story/PMT-40-alle-projekte-eines-bestimmten into trunk 2024-10-24 09:02:40 +00:00
4 changed files with 161 additions and 0 deletions

View file

@ -112,6 +112,31 @@ components:
plannedEnd:
type: string
format: date-time
Project:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
goal:
type: string
customerId:
type: integer
format: int64
administratorId:
type: integer
format: int64
start:
type: string
format: date-time
plannedEnd:
type: string
format: date-time
realEnd:
type: string
format: date-time
AddEmployeeDTO:
type: object
properties:
@ -160,6 +185,13 @@ components:
type: array
items:
$ref: "#/components/schemas/Employee"
EmployeeProjectsDTO:
type: object
properties:
projects:
type: array
items:
$ref: '#/components/schemas/Project'
responses:
Unauthorized:
description: "Unauthorized"
@ -416,3 +448,27 @@ paths:
$ref: '#/components/responses/NotFound'
500:
$ref: "#/components/responses/InternalError"
/employye/{id}/projects:
get:
description: "getAllProjects"
operationId: "Get a List of all Projects from a specific Employee"
parameters:
- in: path
name: id
schema:
type: integer
format: int64
required: true
responses:
200:
content:
application/json:
schema:
$ref: "#/components/schemas/EmployeeProjectsDTO"
description: 'Get a List of all Projects from a specific Employee'
401:
$ref: "#/components/responses/Unauthorized"
404:
$ref: '#/components/responses/NotFound'
500:
$ref: '#/components/responses/InternalError'

View file

@ -3,6 +3,7 @@ package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.db.*;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.dtos.*;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
@ -237,4 +238,22 @@ public class ApiController implements DefaultApi {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Override
public ResponseEntity<EmployeeProjectsDTO> getAListOfAllProjectsFromASpecificEmployee(Long id) {
ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(id);
if (employeeRecord.status() != HttpStatus.OK) {
return new ResponseEntity<>(employeeRecord.status());
SZUT-Rajbir marked this conversation as resolved Outdated

Doppeltes anlegen des response DTOs.

Codeflow kann hier auf jedenfall verbessert werden

Doppeltes anlegen des response DTOs. Codeflow kann hier auf jedenfall verbessert werden
}
List<Allocation> allocationsByEmployee = allocationRepository.findAllByEmployeeId(id);
EmployeeProjectsDTO response = new EmployeeProjectsDTO();
response.setProjects(new ArrayList<>());
for (Allocation allocation : allocationsByEmployee) {
response.addProjectsItem(mapper.mapProject(allocation.getProject()));
}
return ResponseEntity.ok(response);
}
}

View file

@ -82,4 +82,16 @@ public class Mapper {
project.setPlannedEnd(dto.getPlannedEnd());
return project;
}
public de.hmmh.pmt.dtos.Project mapProject(Project project){
de.hmmh.pmt.dtos.Project dto = new de.hmmh.pmt.dtos.Project();
dto.setId(project.getId());
dto.setName(project.getName());
dto.setGoal(project.getGoal());
dto.setCustomerId(project.getCustomerId());
dto.setAdministratorId(project.getAdministratorId());
dto.setStart(project.getStart());
dto.setPlannedEnd(project.getPlannedEnd());
dto.setRealEnd(project.getRealEnd());
return dto;
}
}

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());
}
}