feat: Implement feature to list all employees from a project (SCRUM-46) #36
@ -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}}
|
@ -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,7 @@
|
||||
package de.szut.lf8_starter.employee;
|
||||
|
||||
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
|
||||
import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@ -31,4 +33,19 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user