Compare commits
3 Commits
7092483809
...
78ac3d8610
Author | SHA1 | Date | |
---|---|---|---|
78ac3d8610 | |||
97e211946f | |||
202805e3dd |
@ -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}}
|
3
requests/employee/getProjects.http
Normal file
3
requests/employee/getProjects.http
Normal file
@ -0,0 +1,3 @@
|
||||
GET http://localhost:8080/employees/315/projects
|
||||
Authorization: Bearer {{auth_token}}
|
||||
Content-Type: application/json
|
@ -6,7 +6,7 @@ Content-Type: application/json
|
||||
{
|
||||
"name": "name",
|
||||
"leading_employee": 1,
|
||||
"employees": [2, 3],
|
||||
"employees": [315, 312],
|
||||
"contractor": 4,
|
||||
"contractor_name": "Peter File",
|
||||
"comment": "goal of project",
|
||||
|
2
requests/project/getEmployeesFromProject.http
Normal file
2
requests/project/getEmployeesFromProject.http
Normal file
@ -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);
|
||||
}
|
||||
|
@ -2,14 +2,17 @@ package de.szut.lf8_starter.project.action;
|
||||
|
||||
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.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;
|
||||
|
@ -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,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;
|
||||
|
51
src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java
Normal file
51
src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
39
src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java
Normal file
39
src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
17
src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java
Normal file
17
src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java
Normal file
@ -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
|
82
src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java
Normal file
82
src/test/java/de/szut/lf8_starter/integration/project/GetEmployeesFromProjectTest.java
Normal file
@ -0,0 +1,82 @@
|
||||
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.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)
|
||||
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();
|
||||
}
|
||||
}
|
86
src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java
Normal file
86
src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java
Normal file
@ -0,0 +1,86 @@
|
||||
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.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)
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user