Compare commits

...

4 Commits
v1.7.0 ... main

Author SHA1 Message Date
5f6caa7fc2 feat: Implement feature to acknowledge project time frames when assigning employees to projects (SCRUM-7 SCRUM-2) (!38)
All checks were successful
Build / Build and analyze (push) Successful in 1m34s
Quality Check / Tests (push) Successful in 1m0s
Quality Check / Checkstyle Main (push) Successful in 25s
Release / Release (push) Successful in 43s
Co-authored-by: Phan Huy Tran <p.tran@neusta.de>
Reviewed-on: #38
Co-authored-by: Phan Huy Tran <ptran@noreply.localhost>
Co-committed-by: Phan Huy Tran <ptran@noreply.localhost>
2024-10-25 05:01:10 +00:00
97e211946f feat: Implement feature to get all projects from an employee (SCRUM-31) (!37)
All checks were successful
Quality Check / Checkstyle Main (push) Successful in 37s
Build / Build and analyze (push) Successful in 1m48s
Release / Release (push) Successful in 37s
Quality Check / Tests (push) Successful in 49s
Co-authored-by: Phan Huy Tran <p.tran@neusta.de>
Reviewed-on: #37
Reviewed-by: Jan Gleytenhoover <krisellp9@gmail.com>
2024-10-23 10:50:49 +00:00
202805e3dd feat: Implement feature to list all employees from a project (SCRUM-46) (!36)
All checks were successful
Quality Check / Tests (push) Successful in 1m8s
Quality Check / Checkstyle Main (push) Successful in 37s
Build / Build and analyze (push) Successful in 1m48s
Release / Release (push) Successful in 39s
Co-authored-by: Phan Huy Tran <p.tran@neusta.de>
Reviewed-on: #36
2024-10-23 10:19:33 +00:00
76b0201cf2 feat(employee): add action to remove employee from project (SCRUM-23) (!35)
All checks were successful
Quality Check / Tests (push) Successful in 1m7s
Quality Check / Checkstyle Main (push) Successful in 35s
Build / Build and analyze (push) Successful in 1m45s
Release / Release (push) Successful in 36s
Reviewed-on: #35
Reviewed-by: Phan Huy Tran <ptran@noreply.localhost>
Co-authored-by: Jan Klattenhoff <jan@kjan.de>
Co-committed-by: Jan Klattenhoff <jan@kjan.de>
2024-10-23 09:51:12 +00:00
38 changed files with 890 additions and 82 deletions

@ -3,10 +3,13 @@ Authorization: Bearer {{auth_token}}
Content-Type: application/json
{
"firstName": "Jan",
"lastName": "Klattenhoff",
"street": "Pirolweg 17",
"firstName": "Jan1",
"lastName": "Klattenhoff1",
"street": "Pirolweg 171",
"postcode": "27777",
"city": "Gandakersee",
"phone": "017684984816"
"city": "Gandakersee1",
"phone": "0176849848161",
"skillSet": [
"1"
]
}

@ -1,2 +1,2 @@
GET https://employee.szut.dev/employees/312
GET https://employee.szut.dev/employees/315
Authorization: Bearer {{auth_token}}

@ -0,0 +1,3 @@
GET http://localhost:8080/employees/315/projects
Authorization: Bearer {{auth_token}}
Content-Type: application/json

@ -6,10 +6,10 @@ Content-Type: application/json
{
"name": "name",
"leading_employee": 1,
"employees": [2, 3],
"employees": [312],
"contractor": 4,
"contractor_name": "Peter File",
"comment": "goal of project",
"start_date": "01.01.2000",
"planned_end_date": "01.01.2001"
"start_date": "02.01.2020",
"planned_end_date": "01.01.2023"
}

@ -0,0 +1,2 @@
GET http://localhost:8080/projects/2/employees
Authorization: Bearer {{auth_token}}

@ -1,5 +1,11 @@
package de.szut.lf8_starter.employee;
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.mapper.ProjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@ -7,12 +13,19 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Service
public class EmployeeService {
private final RestTemplate restTemplate;
private final ProjectRepository projectRepository;
private final ProjectMapper projectMapper;
public EmployeeService(RestTemplate restTemplate) {
public EmployeeService(RestTemplate restTemplate, ProjectRepository projectRepository, ProjectMapper projectMapper) {
this.restTemplate = restTemplate;
this.projectRepository = projectRepository;
this.projectMapper = projectMapper;
}
public boolean employeeExists(String accessToken, Long employeeId) {
@ -31,4 +44,30 @@ public class EmployeeService {
return true;
}
public GetEmployeeDto getEmployee(String accessToken, Long employeeId) {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken.replace("Bearer ", ""));
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
String url = "https://employee.szut.dev/employees/" + employeeId;
try {
return restTemplate.exchange(url, HttpMethod.GET, requestEntity, GetEmployeeDto.class).getBody();
} catch (HttpClientErrorException.NotFound e) {
throw new ResourceNotFoundException("Employee not found");
}
}
public List<GetProjectDto> getProjects(Long employeeId) {
ArrayList<ProjectEntity> projects = this.projectRepository.findAllByEmployeeId(employeeId);
ArrayList<GetProjectDto> projectDtos = new ArrayList<>();
for (ProjectEntity project : projects) {
projectDtos.add(this.projectMapper.mapToGetDto(project));
}
return projectDtos;
}
}

@ -30,7 +30,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorDetails> handleAllOtherExceptions(Exception ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getClass() + " " + ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ -1,8 +1,14 @@
package de.szut.lf8_starter.project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
@Repository
public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> {
@Query("SELECT p FROM ProjectEntity p WHERE :employeeId MEMBER OF p.employees")
ArrayList<ProjectEntity> findAllByEmployeeId(@Param("employeeId") Long employeeId);
}

@ -1,5 +1,6 @@
package de.szut.lf8_starter.project;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import org.springframework.stereotype.Service;
import java.util.List;
@ -32,4 +33,18 @@ public class ProjectService {
public void delete(Long id) {
this.projectRepository.deleteById(id);
}
public boolean isOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
return isDateRangeOverlapping(getProjectDto, existingProjectEntity) || isDateEqual(getProjectDto, existingProjectEntity);
}
private boolean isDateRangeOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
return getProjectDto.getStartDate().isBefore(existingProjectEntity.getPlannedEndDate())
&& getProjectDto.getPlannedEndDate().isAfter(existingProjectEntity.getStartDate());
}
private boolean isDateEqual(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
return getProjectDto.getStartDate().isEqual(existingProjectEntity.getStartDate())
|| getProjectDto.getPlannedEndDate().isEqual(existingProjectEntity.getPlannedEndDate());
}
}

@ -1,10 +1,11 @@
package de.szut.lf8_starter.project.action.crud;
import de.szut.lf8_starter.employee.EmployeeService;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.CreateProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.mapper.ProjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
@ -12,15 +13,18 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/projects")
public class CreateProjectAction {
private final EmployeeService employeeService;
private final ProjectService projectService;
private final ProjectMapper projectMapper;
public CreateProjectAction(ProjectService projectService, ProjectMapper mappingService) {
public CreateProjectAction(EmployeeService employeeService, ProjectService projectService, ProjectMapper mappingService) {
this.employeeService = employeeService;
this.projectService = projectService;
this.projectMapper = mappingService;
}
@ -29,14 +33,31 @@ public class CreateProjectAction {
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "created project", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))}),
@ApiResponse(responseCode = "400", description = "invalid JSON posted", content = @Content),
@ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
@ApiResponse(responseCode = "401", description = "not authorized", content = @Content),
@ApiResponse(responseCode = "409", description = "Project dates conflict", content = @Content)
})
@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public GetProjectDto create(@RequestBody @Valid CreateProjectDto createProjectDto) {
ProjectEntity projectEntity = this.projectMapper.mapCreateDtoToEntity(createProjectDto);
public ResponseEntity<Object> create(
@RequestBody @Valid CreateProjectDto createProjectDto,
@RequestHeader("Authorization") String accessToken
) {
ProjectEntity project = this.projectMapper.mapCreateDtoToEntity(createProjectDto);
projectEntity = this.projectService.create(projectEntity);
for (Long employeeId : createProjectDto.getEmployees()) {
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
return new ResponseEntity<>("Employee with ID: " + employeeId + " not found", HttpStatus.NOT_FOUND);
}
return this.projectMapper.mapToGetDto(projectEntity);
for (GetProjectDto getProjectDto : this.employeeService.getProjects(employeeId)) {
if (projectService.isOverlapping(getProjectDto, project)) {
return new ResponseEntity<>("Project dates conflict with an existing project for Employee with ID: " + employeeId, HttpStatus.CONFLICT);
}
}
}
this.projectService.create(project);
return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.CREATED);
}
}

@ -1,8 +1,8 @@
package de.szut.lf8_starter.project.action.crud;
import de.szut.lf8_starter.project.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.mapper.ProjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;

@ -1,9 +1,9 @@
package de.szut.lf8_starter.project.action.crud;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.mapper.ProjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;

@ -1,15 +1,18 @@
package de.szut.lf8_starter.project.action;
package de.szut.lf8_starter.project.action.crud;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;

@ -1,10 +1,10 @@
package de.szut.lf8_starter.project.action.crud;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.UpdateProjectDto;
import de.szut.lf8_starter.project.mapper.ProjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;

@ -3,6 +3,7 @@ package de.szut.lf8_starter.project.action.employee;
import de.szut.lf8_starter.employee.EmployeeService;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@ -29,7 +30,8 @@ public class AddEmployeeToProjectAction {
@Operation(summary = "Add an employee to a project")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Employee added to project"),
@ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content)
@ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content),
@ApiResponse(responseCode = "409", description = "Project dates conflict", content = @Content)
})
@PostMapping("/projects/{projectId}/employees/{employeeId}")
public ResponseEntity<Object> create(
@ -37,18 +39,26 @@ public class AddEmployeeToProjectAction {
@PathVariable Long employeeId,
@RequestHeader("Authorization") String accessToken
) {
Optional<ProjectEntity> project = this.projectService.findById(projectId);
Optional<ProjectEntity> optionalProject = this.projectService.findById(projectId);
if (project.isEmpty()) {
if (optionalProject.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
ProjectEntity projectEntity = optionalProject.get();
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
project.get().getEmployees().add(employeeId);
this.projectService.update(project.get());
for (GetProjectDto getProjectDto : this.employeeService.getProjects(employeeId)) {
if (this.projectService.isOverlapping(getProjectDto, projectEntity)) {
return new ResponseEntity<>("Project dates conflict with an existing project for Employee with ID: " + employeeId, HttpStatus.CONFLICT);
}
}
projectEntity.getEmployees().add(employeeId);
this.projectService.update(projectEntity);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@ -0,0 +1,51 @@
package de.szut.lf8_starter.project.action.employee;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.employee.GetEmployeesFromProjectDto;
import de.szut.lf8_starter.project.mapper.EmployeeFromProjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
public class GetEmployeeFromProjectAction {
private final ProjectService projectService;
private final EmployeeFromProjectMapper employeeFromProjectMapper;
public GetEmployeeFromProjectAction(ProjectService projectService, EmployeeFromProjectMapper employeeFromProjectMapper) {
this.projectService = projectService;
this.employeeFromProjectMapper = employeeFromProjectMapper;
}
@Operation(summary = "Get all employees from project")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Outputs projects with employees"),
@ApiResponse(responseCode = "404", description = "Project not found", content = @Content)
})
@GetMapping("/projects/{projectId}/employees")
public ResponseEntity<Object> create(
@PathVariable Long projectId,
@RequestHeader("Authorization") String accessToken
) {
Optional<ProjectEntity> project = projectService.findById(projectId);
if (project.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
GetEmployeesFromProjectDto getEmployeesFromProjectDto = this.employeeFromProjectMapper
.mapToGetEmployeesFromProjectDto(project.get(), accessToken);
return new ResponseEntity<>(getEmployeesFromProjectDto, HttpStatus.OK);
}
}

@ -0,0 +1,39 @@
package de.szut.lf8_starter.project.action.employee;
import de.szut.lf8_starter.employee.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GetProjectsFromEmployee {
private final EmployeeService employeeService;
public GetProjectsFromEmployee(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@Operation(summary = "Get all projects from employee")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Get all projects from employee", content = @Content),
@ApiResponse(responseCode = "404", description = "Employee not found", content = @Content)
})
@GetMapping("/employees/{employeeId}/projects")
public ResponseEntity<Object> create(
@PathVariable Long employeeId,
@RequestHeader("Authorization") String accessToken
) {
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(this.employeeService.getProjects(employeeId), HttpStatus.OK);
}
}

@ -0,0 +1,52 @@
package de.szut.lf8_starter.project.action.employee;
import de.szut.lf8_starter.employee.EmployeeService;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
public class RemoveEmployeeFromProjectAction {
private final ProjectService projectService;
private final EmployeeService employeeService;
public RemoveEmployeeFromProjectAction(ProjectService projectService, EmployeeService employeeService) {
this.projectService = projectService;
this.employeeService = employeeService;
}
@Operation(summary = "Remove an employee from a project")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Employee removed from project"),
@ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content)
})
@DeleteMapping("/projects/{projectId}/employees/{employeeId}")
public ResponseEntity<Object> remove(
@PathVariable Long projectId,
@PathVariable Long employeeId,
@RequestHeader("Authorization") String accessToken
) {
Optional<ProjectEntity> project = this.projectService.findById(projectId);
if (project.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
project.get().getEmployees().remove(employeeId);
this.projectService.update(project.get());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

@ -1,4 +1,4 @@
package de.szut.lf8_starter.project.dto;
package de.szut.lf8_starter.project.dto.employee;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@ -0,0 +1,11 @@
package de.szut.lf8_starter.project.dto.employee;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class GetEmployeeDto {
private long id;
private GetSkillDto[] skillSet;
}

@ -0,0 +1,17 @@
package de.szut.lf8_starter.project.dto.employee;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
@Getter
@Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GetEmployeesFromProjectDto {
private long projectId;
private String projectDescription;
private ArrayList<GetEmployeeDto> employees;
}

@ -0,0 +1,13 @@
package de.szut.lf8_starter.project.dto.employee;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GetSkillDto {
private String skill;
}

@ -1,4 +1,4 @@
package de.szut.lf8_starter.project.dto;
package de.szut.lf8_starter.project.dto.project;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

@ -1,4 +1,4 @@
package de.szut.lf8_starter.project.dto;
package de.szut.lf8_starter.project.dto.project;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

@ -1,4 +1,4 @@
package de.szut.lf8_starter.project.dto;
package de.szut.lf8_starter.project.dto.project;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

@ -0,0 +1,35 @@
package de.szut.lf8_starter.project.mapper;
import de.szut.lf8_starter.employee.EmployeeService;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto;
import de.szut.lf8_starter.project.dto.employee.GetEmployeesFromProjectDto;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class EmployeeFromProjectMapper {
private final EmployeeService employeeService;
public EmployeeFromProjectMapper(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public GetEmployeesFromProjectDto mapToGetEmployeesFromProjectDto(ProjectEntity projectEntity, String accessToken) {
GetEmployeesFromProjectDto getEmployeesFromProjectDto = new GetEmployeesFromProjectDto();
getEmployeesFromProjectDto.setProjectId(projectEntity.getId());
getEmployeesFromProjectDto.setProjectDescription(projectEntity.getComment());
ArrayList<GetEmployeeDto> employeeDtos = new ArrayList<>();
for (long id : projectEntity.getEmployees()) {
GetEmployeeDto getEmployeeDto = this.employeeService.getEmployee(accessToken, id);
employeeDtos.add(getEmployeeDto);
}
getEmployeesFromProjectDto.setEmployees(employeeDtos);
return getEmployeesFromProjectDto;
}
}

@ -1,8 +1,9 @@
package de.szut.lf8_starter.project;
package de.szut.lf8_starter.project.mapper;
import de.szut.lf8_starter.project.dto.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.dto.project.CreateProjectDto;
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import de.szut.lf8_starter.project.dto.project.UpdateProjectDto;
import org.springframework.stereotype.Service;
@Service

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -22,7 +23,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
class AddEmployeeToProjectActionIntegrationTest {
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class AddEmployeeToProjectTest {
@Autowired
private MockMvc mockMvc;
@ -56,6 +58,87 @@ class AddEmployeeToProjectActionIntegrationTest {
assert updatedProject.getEmployees().contains(312L);
}
@Test
void addEmployeeToPastProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setComment("past project");
project.setContractor(1);
project.setContractorName("contractorName");
project.setPlannedEndDate(LocalDate.of(1991, 1, 1));
project.setLeadingEmployee(1);
project.setName("past project");
project.setStartDate(LocalDate.of(1990, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
assert updatedProject.getEmployees().contains(312L);
}
@Test
void addEmployeeToFutureProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setComment("future project");
project.setContractor(1);
project.setContractorName("contractorName");
project.setPlannedEndDate(LocalDate.of(2101, 1, 1));
project.setLeadingEmployee(1);
project.setName("future project");
project.setStartDate(LocalDate.of(2100, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(project.getId()).get();
assert updatedProject.getEmployees().contains(312L);
}
@Test
void addEmployeeToProjectWithOverlappingDatesTest() throws Exception {
ProjectEntity project1 = new ProjectEntity();
project1.setComment("project 1");
project1.setContractor(1);
project1.setContractorName("contractorName");
project1.setPlannedEndDate(LocalDate.of(2023, 1, 1));
project1.setLeadingEmployee(1);
project1.setName("project 1");
project1.setStartDate(LocalDate.of(2022, 1, 1));
project1.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project1);
ProjectEntity project2 = new ProjectEntity();
project2.setComment("project 2");
project2.setContractor(1);
project2.setContractorName("contractorName");
project2.setPlannedEndDate(LocalDate.of(2023, 6, 1));
project2.setLeadingEmployee(1);
project2.setName("project 2");
project2.setStartDate(LocalDate.of(2022, 6, 1));
project2.setEmployees(List.of(1L, 2L, 3L));
this.projectRepository.save(project2);
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project1.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", project2.getId(), 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isConflict());
}
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";

@ -7,11 +7,17 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@ -22,11 +28,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class CreateProjectActionTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Autowired
private RestTemplate restTemplate;
@Test
void createProjectTest() throws Exception {
@ -34,7 +43,7 @@ class CreateProjectActionTest {
{
"name": "name",
"leading_employee": 1,
"employees": [2, 3],
"employees": [312],
"contractor": 4,
"contractor_name": "Peter File",
"comment": "goal of project",
@ -44,12 +53,15 @@ class CreateProjectActionTest {
""";
final String contentAsString = this.mockMvc.perform(
post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
post("/projects")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isCreated())
.andExpect(jsonPath("name", is("name")))
.andExpect(jsonPath("leading_employee", is(1)))
.andExpect(jsonPath("employees", is(Arrays.asList(2, 3))))
.andExpect(jsonPath("employees", is(List.of(312))))
.andExpect(jsonPath("contractor", is(4)))
.andExpect(jsonPath("contractor_name", is("Peter File")))
.andExpect(jsonPath("comment", is("goal of project")))
@ -70,4 +82,155 @@ class CreateProjectActionTest {
assertThat(project.get().getStartDate()).isEqualTo(LocalDate.of(2000, 1, 1));
assertThat(project.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2001, 1, 1));
}
@Test
void createProjectShouldReturnConflictResponseOnConflictingProjects() throws Exception {
String content = """
{
"name": "name",
"leading_employee": 1,
"employees": [312],
"contractor": 4,
"contractor_name": "Peter File",
"comment": "goal of project",
"start_date": "01.01.2000",
"planned_end_date": "01.01.2001"
}
""";
this.mockMvc.perform(
post("/projects")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isCreated());
this.mockMvc.perform(
post("/projects")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isConflict());
}
@Test
void createProjectWithPastStartDate() throws Exception {
String content = """
{
"name": "past project",
"leading_employee": 1,
"employees": [312],
"contractor": 4,
"contractor_name": "Past Contractor",
"comment": "past project",
"start_date": "01.01.1990",
"planned_end_date": "01.01.1991"
}
""";
this.mockMvc.perform(
post("/projects")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isCreated())
.andExpect(jsonPath("name", is("past project")))
.andExpect(jsonPath("start_date", is("01.01.1990")))
.andExpect(jsonPath("planned_end_date", is("01.01.1991")));
}
@Test
void createProjectWithFutureStartDate() throws Exception {
String content = """
{
"name": "future project",
"leading_employee": 1,
"employees": [312],
"contractor": 4,
"contractor_name": "Future Contractor",
"comment": "future project",
"start_date": "01.01.2100",
"planned_end_date": "01.01.2101"
}
""";
this.mockMvc.perform(
post("/projects")
.content(content)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isCreated())
.andExpect(jsonPath("name", is("future project")))
.andExpect(jsonPath("start_date", is("01.01.2100")))
.andExpect(jsonPath("planned_end_date", is("01.01.2101")));
}
@Test
void createProjectWithOverlappingDates() throws Exception {
String content1 = """
{
"name": "project 1",
"leading_employee": 1,
"employees": [312],
"contractor": 4,
"contractor_name": "Contractor 1",
"comment": "project 1",
"start_date": "01.01.2022",
"planned_end_date": "01.01.2023"
}
""";
String content2 = """
{
"name": "project 2",
"leading_employee": 1,
"employees": [312],
"contractor": 4,
"contractor_name": "Contractor 2",
"comment": "project 2",
"start_date": "01.06.2022",
"planned_end_date": "01.06.2023"
}
""";
this.mockMvc.perform(
post("/projects")
.content(content1)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isCreated());
this.mockMvc.perform(
post("/projects")
.content(content2)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
)
.andExpect(status().isConflict());
}
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", "password");
map.add("client_id", "employee-management-service");
map.add("username", "user");
map.add("password", "test");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
}

@ -0,0 +1,84 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
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;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class GetEmployeesFromProjectTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Autowired
private RestTemplate restTemplate;
@Test
void getEmployeesFromProjectTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setId(1);
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2024, 1, 1));
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
project.setEmployees(List.of(315L));
this.projectRepository.save(project);
this.mockMvc.perform(get("/projects/1/employees")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
).andExpect(status().isOk())
.andExpect(jsonPath("project_id").value(1))
.andExpect(jsonPath("project_description").value("comment"))
.andExpect(jsonPath("employees[0].id").value(315))
.andExpect(jsonPath("employees[0].skillSet[0].skill").value("Product Owner"));
}
@Test
void getEmployeesFromProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
this.mockMvc.perform(get("/projects/1111/employees")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
).andExpect(status().isNotFound());
}
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", "password");
map.add("client_id", "employee-management-service");
map.add("username", "user");
map.add("password", "test");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
}

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class GetProjectActionTest {
@Autowired
private MockMvc mockMvc;

@ -0,0 +1,88 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
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;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class GetProjectsFromEmployeeTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Autowired
private RestTemplate restTemplate;
@Test
void getProjectsFromEmployeeTest() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setId(1);
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2024, 1, 1));
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
project.setEmployees(List.of(315L));
this.projectRepository.save(project);
this.mockMvc.perform(get("/employees/315/projects")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
).andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1))
.andExpect(jsonPath("$[0].name").value("name"))
.andExpect(jsonPath("$[0].leading_employee").value(1))
.andExpect(jsonPath("$[0].employees[0]").value(315))
.andExpect(jsonPath("$[0].contractor").value(1))
.andExpect(jsonPath("$[0].contractor_name").value("contractorName"))
.andExpect(jsonPath("$[0].comment").value("comment"))
.andExpect(jsonPath("$[0].start_date").value("01.01.2021"));
}
@Test
void GetProjectsFromEmployeeShouldReturnNotFoundResponseWhenEmployeeIsNotFound() throws Exception {
this.mockMvc.perform(get("/employees/1111/projects")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
).andExpect(status().isNotFound());
}
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", "password");
map.add("client_id", "employee-management-service");
map.add("username", "user");
map.add("password", "test");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
}

@ -1,32 +0,0 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(addFilters = true)
class ProjectFindAllNotAuthenticated {
@Autowired
private ProjectRepository projectRepository;
@Autowired
private MockMvc mockMvc;
@Autowired
private TestRestTemplate restTemplate;
@Test
void findAllProjects() throws Exception {
this.mockMvc.perform(get("/projects"))
.andExpect(status().isUnauthorized());
}
}

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
@ -18,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class ProjectFindAllSuccessTest {
@Autowired
private ProjectRepository projectRepository;

@ -0,0 +1,87 @@
package de.szut.lf8_starter.integration.project;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class RemoveEmployeeFromProjectIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private RestTemplate restTemplate;
@Autowired
private ProjectRepository projectRepository;
@Test
void removeEmployeeFromProject() throws Exception {
ProjectEntity project = new ProjectEntity();
project.setComment("comment");
project.setContractor(1);
project.setContractorName("contractorName");
project.setEndDate(LocalDate.of(2024, 1, 1));
project.setLeadingEmployee(1);
project.setName("name");
project.setStartDate(LocalDate.of(2021, 1, 1));
project.setEmployees(List.of(1L, 2L, 3L, 312L));
this.projectRepository.save(project);
mockMvc.perform(delete("/projects/{projectId}/employees/{employeeId}", 1, 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
ProjectEntity updatedProject = projectRepository.findById(1L).get();
assert !updatedProject.getEmployees().contains(312L);
}
private String getBearerToken() {
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", "password");
map.add("client_id", "employee-management-service");
map.add("username", "user");
map.add("password", "test");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
}
@Test
void removeEmployeeShouldReturn404IfProjectNotFound() throws Exception {
mockMvc.perform(delete("/projects/{projectId}/employees/{employeeId}", 1987234987, 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
}

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
@ -17,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class RemoveProjectActionTest {
@Autowired
private MockMvc mockMvc;

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
@ -23,6 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class UpdateProjectActionTest {
@Autowired
private MockMvc mockMvc;
@ -136,6 +138,6 @@ class UpdateProjectActionTest {
@Test
void updateProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
this.mockMvc.perform(put("/projects/2").content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
this.mockMvc.perform(put("/projects/98723498798").content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}
}

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop