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>
This commit is contained in:
Phan Huy Tran 2024-10-23 10:50:49 +00:00
parent 202805e3dd
commit 97e211946f
5 changed files with 157 additions and 1 deletions

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

@ -1,7 +1,11 @@
package de.szut.lf8_starter.employee; package de.szut.lf8_starter.employee;
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException; 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.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.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -9,12 +13,19 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Service @Service
public class EmployeeService { public class EmployeeService {
private final RestTemplate restTemplate; 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.restTemplate = restTemplate;
this.projectRepository = projectRepository;
this.projectMapper = projectMapper;
} }
public boolean employeeExists(String accessToken, Long employeeId) { public boolean employeeExists(String accessToken, Long employeeId) {
@ -48,4 +59,15 @@ public class EmployeeService {
throw new ResourceNotFoundException("Employee not found"); 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;
}
} }

@ -1,8 +1,14 @@
package de.szut.lf8_starter.project; package de.szut.lf8_starter.project;
import org.springframework.data.jpa.repository.JpaRepository; 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 org.springframework.stereotype.Repository;
import java.util.ArrayList;
@Repository @Repository
public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> { 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);
} }

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