Compare commits
19 commits
85e80e4a61
...
6b37fe7ae4
Author | SHA1 | Date | |
---|---|---|---|
6b37fe7ae4 | |||
65836b7772 | |||
8abec2433d | |||
224561f038 | |||
3af49e2466 | |||
5ca3603f65 | |||
0fb0e77b91 | |||
b9a1329a4e | |||
108a31b2fa | |||
b9d3170802 | |||
933ac666a8 | |||
9fe23f3e90 | |||
60ee2e9bb2 | |||
ccaf234d28 | |||
2ca9cfbadb | |||
8dff301457 | |||
e6d3854fbd | |||
967946bee2 | |||
2f6222ff00 |
13 changed files with 744 additions and 71 deletions
182
api/pmt.yml
182
api/pmt.yml
|
@ -46,6 +46,25 @@ components:
|
|||
plannedEnd:
|
||||
type: string
|
||||
format: date-time
|
||||
UpdateProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
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
|
||||
CreatedProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
|
@ -77,6 +96,45 @@ components:
|
|||
qualificationId:
|
||||
type: integer
|
||||
format: int64
|
||||
Employee:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
format: int64
|
||||
lastName:
|
||||
type: string
|
||||
firstName:
|
||||
type: string
|
||||
street:
|
||||
type: string
|
||||
postcode:
|
||||
maxLength: 5
|
||||
minLength: 5
|
||||
type: string
|
||||
city:
|
||||
type: string
|
||||
phone:
|
||||
type: string
|
||||
skillSet:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Qualification'
|
||||
Qualification:
|
||||
type: object
|
||||
properties:
|
||||
skill:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
format: int64
|
||||
ProjectEmployeesDTO:
|
||||
type: object
|
||||
properties:
|
||||
employees:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Employee"
|
||||
responses:
|
||||
Unauthorized:
|
||||
description: "Unauthorized"
|
||||
|
@ -153,8 +211,7 @@ paths:
|
|||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
|
||||
|
||||
|
||||
/project/{id}:
|
||||
post:
|
||||
operationId: "addEmployee"
|
||||
|
@ -167,10 +224,10 @@ paths:
|
|||
format: int64
|
||||
required: true
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/AddEmployeeDTO"
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/AddEmployeeDTO"
|
||||
responses:
|
||||
204:
|
||||
description: "Employee successfully added to the specific Project"
|
||||
|
@ -179,14 +236,43 @@ paths:
|
|||
404:
|
||||
$ref: "#/components/responses/NotFound"
|
||||
409:
|
||||
$ref: "#/components/responses/Conflict"
|
||||
$ref: "#/components/responses/Conflict"
|
||||
422:
|
||||
$ref: "#/components/responses/UnprocessableContent"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
put:
|
||||
operationId: "updateProject"
|
||||
description: "Updates a project"
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/UpdateProjectDTO"
|
||||
responses:
|
||||
204:
|
||||
description: "Project updated successfully"
|
||||
401:
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
404:
|
||||
$ref: "#/components/responses/NotFound"
|
||||
409:
|
||||
$ref: "#/components/responses/Conflict"
|
||||
422:
|
||||
$ref: "#/components/responses/UnprocessableContent"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
|
||||
delete:
|
||||
operationId: "deleteProject"
|
||||
description: "Delete a specific Project"
|
||||
|
@ -203,10 +289,80 @@ paths:
|
|||
401:
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
404:
|
||||
description: "Project not found"
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
$ref: "#/components/responses/NotFound"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
/project/{id}/completed:
|
||||
post:
|
||||
operationId: "completeProject"
|
||||
description: "Complete a specific project"
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
responses:
|
||||
204:
|
||||
description: "Completed"
|
||||
401:
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
404:
|
||||
$ref: "#/components/responses/NotFound"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
/project/{id}/employee/{employeeId}:
|
||||
delete:
|
||||
operationId: "removeEmployeeFromProject"
|
||||
description: "Removes an employee from a Project"
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
- in: path
|
||||
name: employeeId
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
responses:
|
||||
204:
|
||||
description: "Deletes the employee from the Project"
|
||||
401:
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
404:
|
||||
$ref: "#/components/responses/NotFound"
|
||||
409:
|
||||
$ref: "#/components/responses/Conflict"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
|
||||
/project/{id}/employees:
|
||||
get:
|
||||
description: "getAllEmployees"
|
||||
operationId: "Get a List of all Employees from a specific Project"
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
responses:
|
||||
200:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/ProjectEmployeesDTO"
|
||||
description: 'Get a List of all Employees from a specific Project '
|
||||
404:
|
||||
$ref: '#/components/responses/NotFound'
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
|
|
@ -1,27 +1,26 @@
|
|||
package de.hmmh.pmt;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.hmmh.pmt.db.Allocation;
|
||||
import de.hmmh.pmt.db.AllocationRepository;
|
||||
import de.hmmh.pmt.db.Project;
|
||||
import de.hmmh.pmt.db.ProjectRepository;
|
||||
|
||||
import de.hmmh.pmt.db.*;
|
||||
import de.hmmh.pmt.dtos.*;
|
||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||
import de.hmmh.pmt.oas.DefaultApi;
|
||||
import de.hmmh.pmt.util.ApiTools;
|
||||
import de.hmmh.pmt.util.Mapper;
|
||||
import de.hmmh.pmt.util.Validator;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
|
||||
|
@ -31,6 +30,10 @@ public class ApiController implements DefaultApi {
|
|||
@Autowired
|
||||
private ApiClientFactory apiClientFactory;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
@Autowired
|
||||
private ApiTools apiTools;
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
@Autowired
|
||||
AllocationRepository allocationRepository;
|
||||
|
@ -69,26 +72,61 @@ public class ApiController implements DefaultApi {
|
|||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> updateProject(Long id, UpdateProjectDTO body) {
|
||||
Optional<Project> optionalProject = projectRepository.findById(id);
|
||||
if (optionalProject.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
Project project = optionalProject.get();
|
||||
|
||||
if (!project.getName().equals(body.getName()) && projectRepository.existsByName(body.getName())) {
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
if (project.getAdministratorId().equals(body.getAdministratorId())) {
|
||||
HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status();
|
||||
if (status != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(status);
|
||||
}
|
||||
}
|
||||
|
||||
Project finalProject = mapper.map(project, body);
|
||||
if (!validator.isValidProject(project)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
if (validator.areAllocationTimeRangesOverlapping(
|
||||
project.getStart(),
|
||||
project.getPlannedEnd(),
|
||||
allocationRepository
|
||||
.findAllByProjectId(finalProject.getId())
|
||||
.stream()
|
||||
.map(Allocation::getEmployeeId)
|
||||
.flatMap(employeeId -> allocationRepository.findAllByEmployeeId(employeeId).stream())
|
||||
.filter(employeeAllocation -> employeeAllocation.getProjectId().equals(finalProject.getId()))
|
||||
.toList()
|
||||
)) {
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
projectRepository.save(project);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
|
||||
if (projectRepository.existsByName(body.getName())) {
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
try {
|
||||
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
|
||||
} catch (HttpClientErrorException exception) {
|
||||
return new ResponseEntity<>(
|
||||
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||
? HttpStatus.NOT_FOUND
|
||||
: HttpStatus.SERVICE_UNAVAILABLE
|
||||
);
|
||||
} catch (RestClientException exception) {
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status();
|
||||
if (status != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(status);
|
||||
}
|
||||
|
||||
Project project = mapper.map(body);
|
||||
if (!project.isValid()) {
|
||||
if (!validator.isValidProject(project)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
projectRepository.save(project);
|
||||
|
@ -105,16 +143,13 @@ public class ApiController implements DefaultApi {
|
|||
}
|
||||
Project project = optionalProject.get();
|
||||
|
||||
EmployeeResponseDTO employee;
|
||||
try {
|
||||
employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
|
||||
} catch (HttpClientErrorException exception) {
|
||||
return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||
? HttpStatus.NOT_FOUND
|
||||
: HttpStatus.SERVICE_UNAVAILABLE);
|
||||
} catch (RestClientException exception) {
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
||||
ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(body.getEmployeeId());
|
||||
if (employeeRecord.status() != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(employeeRecord.status());
|
||||
}
|
||||
EmployeeResponseDTO employee = employeeRecord.employee();
|
||||
|
||||
|
||||
if (employee.getSkillSet()
|
||||
.stream()
|
||||
|
@ -122,16 +157,11 @@ public class ApiController implements DefaultApi {
|
|||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
long start = project.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long plannedEnd = project.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
List<Allocation> allocations = allocationRepository.findAllByEmployeeId(body.getEmployeeId());
|
||||
if (allocations.stream()
|
||||
.map(Allocation::getProject)
|
||||
.anyMatch(allocatedProject -> {
|
||||
long allocatedStart = allocatedProject.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd);
|
||||
})) {
|
||||
if (validator.areAllocationTimeRangesOverlapping(
|
||||
project.getStart(),
|
||||
project.getPlannedEnd(),
|
||||
allocationRepository.findAllByEmployeeId(body.getEmployeeId())
|
||||
)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
|
@ -143,4 +173,56 @@ public class ApiController implements DefaultApi {
|
|||
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> completeProject(Long id) {
|
||||
Optional<Project> optionalProject = projectRepository.findById(id);
|
||||
if (optionalProject.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
Project project = optionalProject.get();
|
||||
|
||||
project.setRealEnd(LocalDateTime.now());
|
||||
projectRepository.save(project);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> removeEmployeeFromProject(Long id, Long employeeId){
|
||||
Optional<Allocation> allocation = allocationRepository.findById(new AllocationId(id, employeeId));
|
||||
if (allocation.isEmpty()){
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
allocationRepository.delete(allocation.get());
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<ProjectEmployeesDTO> getAListOfAllEmployeesFromASpecificProject(Long id) {
|
||||
if (!projectRepository.existsById(id)) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
ProjectEmployeesDTO dto = new ProjectEmployeesDTO();
|
||||
List<Allocation> allocationsByProject = allocationRepository.findAllByProjectId(id);
|
||||
if (allocationsByProject.isEmpty()) {
|
||||
return new ResponseEntity<>(dto, HttpStatus.OK);
|
||||
}
|
||||
|
||||
Set<Long> employeeIds = allocationsByProject.stream()
|
||||
.map(Allocation::getEmployeeId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
try {
|
||||
List<Employee> employees = apiClientFactory.getEmployeeApi().findAll1().stream()
|
||||
.filter(employeeResponseDTO -> employeeIds.contains(employeeResponseDTO.getId()))
|
||||
.map(mapper::map)
|
||||
.toList();
|
||||
dto.setEmployees(employees);
|
||||
return new ResponseEntity<>(dto, HttpStatus.OK);
|
||||
} catch (RestClientException exception) {
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,4 +7,5 @@ import java.util.List;
|
|||
public interface AllocationRepository extends JpaRepository<Allocation, AllocationId> {
|
||||
|
||||
List<Allocation> findAllByEmployeeId(Long employeeId);
|
||||
List<Allocation> findAllByProjectId(Long projectId);
|
||||
}
|
||||
|
|
|
@ -47,16 +47,4 @@ public class Project {
|
|||
private LocalDateTime plannedEnd;
|
||||
|
||||
private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword
|
||||
|
||||
|
||||
public boolean isValid() {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
Set<ConstraintViolation<Project>> violations = validator.validate(this);
|
||||
|
||||
return violations.isEmpty() &&
|
||||
plannedEnd.isAfter(start) &&
|
||||
(realEnd == null || realEnd.isAfter(start));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package de.hmmh.pmt.util;
|
||||
|
||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
|
||||
@Component
|
||||
public class ApiTools {
|
||||
|
||||
@Autowired
|
||||
private ApiClientFactory apiClientFactory;
|
||||
|
||||
public record CheckEmployeeRecord(EmployeeResponseDTO employee, HttpStatus status) {}
|
||||
public CheckEmployeeRecord checkEmployeeExists(long id) {
|
||||
EmployeeResponseDTO employee;
|
||||
try {
|
||||
employee =apiClientFactory.getEmployeeApi().findById(id);
|
||||
} catch (HttpClientErrorException exception) {
|
||||
return new CheckEmployeeRecord(
|
||||
null,
|
||||
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||
? HttpStatus.NOT_FOUND
|
||||
: HttpStatus.SERVICE_UNAVAILABLE
|
||||
);
|
||||
} catch (RestClientException exception) {
|
||||
return new CheckEmployeeRecord(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return new CheckEmployeeRecord(employee, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -3,8 +3,15 @@ package de.hmmh.pmt.util;
|
|||
import de.hmmh.pmt.db.Project;
|
||||
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||
import de.hmmh.pmt.dtos.Employee;
|
||||
import de.hmmh.pmt.dtos.Qualification ;
|
||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||
import de.hmmh.pmt.employee.dtos.QualificationGetDTO;
|
||||
import de.hmmh.pmt.dtos.UpdateProjectDTO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Mapper {
|
||||
public Project map(CreateProjectDTO dto) {
|
||||
|
@ -29,4 +36,40 @@ public class Mapper {
|
|||
dto.setPlannedEnd(project.getPlannedEnd());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public Employee map(EmployeeResponseDTO employeeResponseDTO) {
|
||||
Employee dto = new Employee();
|
||||
dto.setId(employeeResponseDTO.getId());
|
||||
dto.setLastName(employeeResponseDTO.getLastName());
|
||||
dto.setFirstName(employeeResponseDTO.getFirstName());
|
||||
dto.setStreet(employeeResponseDTO.getStreet());
|
||||
dto.setPostcode(employeeResponseDTO.getPostcode());
|
||||
dto.setCity(employeeResponseDTO.getCity());
|
||||
dto.setPhone(employeeResponseDTO.getPhone());
|
||||
|
||||
List<Qualification > skillSet = employeeResponseDTO.getSkillSet().stream()
|
||||
.map(this::map)
|
||||
.toList();
|
||||
|
||||
dto.setSkillSet(skillSet);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private Qualification map(QualificationGetDTO qualificationGetDTO) {
|
||||
Qualification dto = new Qualification ();
|
||||
dto.setId(qualificationGetDTO.getId());
|
||||
dto.setSkill(qualificationGetDTO.getSkill());
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
public Project map(Project project, UpdateProjectDTO dto) {
|
||||
project.setName(dto.getName());
|
||||
project.setGoal(dto.getGoal());
|
||||
project.setCustomerId(dto.getCustomerId());
|
||||
project.setAdministratorId(dto.getAdministratorId());
|
||||
project.setStart(dto.getStart());
|
||||
project.setPlannedEnd(dto.getPlannedEnd());
|
||||
return project;
|
||||
}
|
||||
}
|
||||
|
|
48
src/main/java/de/hmmh/pmt/util/Validator.java
Normal file
48
src/main/java/de/hmmh/pmt/util/Validator.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
package de.hmmh.pmt.util;
|
||||
|
||||
import de.hmmh.pmt.db.Allocation;
|
||||
import de.hmmh.pmt.db.Project;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class Validator {
|
||||
|
||||
public boolean isValidProject(Project project) {
|
||||
jakarta.validation.Validator validator = Validation
|
||||
.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
Set<ConstraintViolation<Project>> violations = validator.validate(project);
|
||||
|
||||
LocalDateTime start = project.getStart();
|
||||
LocalDateTime plannedEnd = project.getPlannedEnd();
|
||||
LocalDateTime realEnd = project.getRealEnd();
|
||||
|
||||
return violations.isEmpty() &&
|
||||
plannedEnd.isAfter(start) &&
|
||||
(realEnd == null || realEnd.isAfter(start));
|
||||
}
|
||||
|
||||
public boolean areAllocationTimeRangesOverlapping(
|
||||
LocalDateTime start,
|
||||
LocalDateTime plannedEnd,
|
||||
List<Allocation> allocations
|
||||
){
|
||||
long startUnix = start.toEpochSecond(ZoneOffset.UTC);
|
||||
long plannedEndUnix = plannedEnd.toEpochSecond(ZoneOffset.UTC);
|
||||
return allocations.stream()
|
||||
.map(Allocation::getProject)
|
||||
.anyMatch(allocatedProject -> {
|
||||
long allocatedStart = allocatedProject.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
return Math.max(startUnix, allocatedStart) <= Math.min(plannedEndUnix, allocatedPlannedEnd);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +1,7 @@
|
|||
<FindBugsFilter xmlns="https://raw.githubusercontent.com/spotbugs/spotbugs/4.8.6/spotbugs/etc/findbugsfilter.xsd">
|
||||
<Match>
|
||||
<!-- We Want This Exposure of Resources the Way it is for our usage -->
|
||||
<Class name="de.hmmh.pmt.employee.ApiClientFactory"/>
|
||||
<Bug code="M,V,EI"/>
|
||||
</Match>
|
||||
<Match>
|
||||
<Class name="de.hmmh.pmt.db.Allocation"/>
|
||||
<Bug code="M,V,EI,EI2"/>
|
||||
<Source name="~.*"/>
|
||||
<Bug code="EI,EI2"/> <!-- We don't care about these codes -->
|
||||
</Match>
|
||||
<Match>
|
||||
<!--Ignore Auto Generated Code -->
|
||||
|
|
|
@ -114,6 +114,15 @@ public abstract class IntegrationTest {
|
|||
medicalResearchProject.setPlannedEnd(LocalDateTime.of(2026, 8, 20, 9, 15));
|
||||
projects.put("medical-research", medicalResearchProject);
|
||||
|
||||
Project futureResearchProject = new Project();
|
||||
futureResearchProject.setName("What is the Future?");
|
||||
futureResearchProject.setGoal("Develop new Future (possibly through Genocide).");
|
||||
futureResearchProject.setCustomerId(95L);
|
||||
futureResearchProject.setAdministratorId(TEST_EMPLOYEE_A_ID);
|
||||
futureResearchProject.setStart(LocalDateTime.of(2077, 1, 12, 2, 0));
|
||||
futureResearchProject.setPlannedEnd(LocalDateTime.of(3000, 6, 19, 4, 0));
|
||||
projects.put("future-research", futureResearchProject);
|
||||
|
||||
Project overlappingProjectA = new Project();
|
||||
overlappingProjectA.setName("Overlap A");
|
||||
overlappingProjectA.setGoal("A Project That Overlaps with another one for Testing");
|
||||
|
@ -164,7 +173,31 @@ public abstract class IntegrationTest {
|
|||
allocation1ToOverlapA.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||
allocation1ToOverlapA.setRole(TEST_QUALIFICATION_A_ID);
|
||||
allocations.put("1>overlap-a", allocation1ToOverlapA);
|
||||
|
||||
|
||||
Allocation allocation1ToSpaceStation = new Allocation();
|
||||
allocation1ToSpaceStation.setProject(allProjects.get("space-station"));
|
||||
allocation1ToSpaceStation.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||
allocation1ToSpaceStation.setRole(TEST_QUALIFICATION_A_ID);
|
||||
allocations.put("1>space-station", allocation1ToSpaceStation);
|
||||
|
||||
Allocation allocation1ToAiResearch = new Allocation();
|
||||
allocation1ToAiResearch.setProject(allProjects.get("ai-research"));
|
||||
allocation1ToAiResearch.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||
allocation1ToAiResearch.setRole(TEST_QUALIFICATION_A_ID);
|
||||
allocations.put("1>ai-research", allocation1ToAiResearch);
|
||||
|
||||
Allocation allocation1ToMedical = new Allocation();
|
||||
allocation1ToMedical.setProject(allProjects.get("medical-research"));
|
||||
allocation1ToMedical.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||
allocation1ToMedical.setRole(TEST_QUALIFICATION_A_ID);
|
||||
allocations.put("1>medical-research", allocation1ToMedical);
|
||||
|
||||
Allocation allocation1ToFuture = new Allocation();
|
||||
allocation1ToFuture.setProject(allProjects.get("future-research"));
|
||||
allocation1ToFuture.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||
allocation1ToFuture.setRole(TEST_QUALIFICATION_A_ID);
|
||||
allocations.put("1>future-research", allocation1ToFuture);
|
||||
|
||||
allocationRepository.saveAllAndFlush(allocations.values());
|
||||
return allocations;
|
||||
}
|
||||
|
|
31
src/test/java/de/hmmh/pmt/project/CompleteTest.java
Normal file
31
src/test/java/de/hmmh/pmt/project/CompleteTest.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package de.hmmh.pmt.project;
|
||||
|
||||
import de.hmmh.pmt.IntegrationTest;
|
||||
import de.hmmh.pmt.db.Project;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class CompleteTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundWhenProjectDoesntExist() throws Exception {
|
||||
this.mvc
|
||||
.perform(post(baseUri + "/project/0/completed"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void successfullyComplete() throws Exception {
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToComplete = allProjects.get("medical-research");
|
||||
|
||||
this.mvc
|
||||
.perform(post(baseUri + "/project/" + projectToComplete.getId() + "/completed"))
|
||||
.andExpect(status().isNoContent());
|
||||
}
|
||||
}
|
59
src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java
Normal file
59
src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java
Normal file
|
@ -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<String, Project> allProjects = createTestProjectData();
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(allProjects.get("research-lab").getId(), 0L))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void employeeSuccessfullyDeletedFromProject() throws Exception {
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Map<String, Allocation> 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);
|
||||
}
|
||||
}
|
137
src/test/java/de/hmmh/pmt/project/EditTest.java
Normal file
137
src/test/java/de/hmmh/pmt/project/EditTest.java
Normal file
|
@ -0,0 +1,137 @@
|
|||
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.dtos.UpdateProjectDTO;
|
||||
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.MediaType;
|
||||
import org.springframework.test.web.servlet.RequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class EditTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void successfullyRenamed() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToEdit = allProjects.get("medical-research");
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
dto.setName("Mafia Drug Ring");
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(projectToEdit.getId(), dto))
|
||||
.andExpect(status().isNoContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundWhenProjectDoesntExist() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToEdit = allProjects.get("medical-research");
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(0L, dto))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundWhenNewAdministratorDoesntExist() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToEdit = allProjects.get("medical-research");
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(projectToEdit.getId(), dto))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnUnprocessableWhenNewTimestampsAreInvalid() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToEdit = allProjects.get("medical-research");
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
dto.setPlannedEnd(dto.getStart().minusDays(1));
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(projectToEdit.getId(), dto))
|
||||
.andExpect(status().isUnprocessableEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnConflictWhenUpdateWouldCreateNameConflict() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Project projectToEdit = allProjects.get("medical-research");
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
dto.setName(allProjects.get("renewable-energy").getName());
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(projectToEdit.getId(), dto))
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnConflictWhenAllocationsWouldConflictAfterUpdate() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
Map<String, Allocation> allAllocations = createTestAllocationData(allProjects);
|
||||
Project projectToEdit = allAllocations.get("1>future-research").getProject();
|
||||
|
||||
UpdateProjectDTO dto = getDto(projectToEdit);
|
||||
dto.setStart(LocalDateTime.of(1970, 1, 1, 0, 0));
|
||||
dto.setStart(LocalDateTime.of(3000, 1, 1, 0, 0));
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(projectToEdit.getId(), dto))
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
private UpdateProjectDTO getDto(Project project) {
|
||||
UpdateProjectDTO dto = new UpdateProjectDTO();
|
||||
dto.setName(project.getName());
|
||||
dto.setGoal(project.getGoal());
|
||||
dto.setCustomerId(project.getCustomerId());
|
||||
dto.setAdministratorId(project.getAdministratorId());
|
||||
dto.setStart(project.getStart());
|
||||
dto.setPlannedEnd(project.getPlannedEnd());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private RequestBuilder getRequest(long id, UpdateProjectDTO dto) throws Exception {
|
||||
return MockMvcRequestBuilders
|
||||
.put(baseUri + "/project/" + id)
|
||||
.content(this.objectMapper.writeValueAsString(dto))
|
||||
.contentType(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package de.hmmh.pmt.project;
|
||||
|
||||
import de.hmmh.pmt.IntegrationTest;
|
||||
import de.hmmh.pmt.db.Project;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.RestClientException;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class GetAllEmployeesByProjectTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundWhenProjectDoesNotExist() throws Exception {
|
||||
createTestProjectData();
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(50L))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnOkWhenProjectHasNoEmployees() throws Exception {
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(allProjects.get("research-lab").getId()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnListOfEmployeesWhenAllParametersAreValid() throws Exception {
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
createTestAllocationData(allProjects);
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(allProjects.get("ai-research").getId()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnInternalServer() throws Exception {
|
||||
when(this.mockEmployeeApi.findAll1())
|
||||
.thenThrow(new RestClientException("Internal Server Error"));
|
||||
|
||||
Map<String, Project> allProjects = createTestProjectData();
|
||||
createTestAllocationData(allProjects);
|
||||
|
||||
this.mvc
|
||||
.perform(getRequest(allProjects.get("ai-research").getId()))
|
||||
.andExpect(status().isInternalServerError());
|
||||
}
|
||||
|
||||
private RequestBuilder getRequest(Long projectId) {
|
||||
return MockMvcRequestBuilders
|
||||
.get(baseUri + "/project/" + projectId + "/employees")
|
||||
.contentType(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue