From fe75c845dbf1a0f1de70ab29c631f286fc7f1883 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 12:45:12 +0200 Subject: [PATCH 1/2] Implement --- requests/employee/getProjects.http | 3 + .../lf8_starter/employee/EmployeeService.java | 23 ++++- .../project/ProjectRepository.java | 6 ++ .../employee/GetProjectsFromEmployee.java | 39 +++++++++ .../project/GetProjectsFromEmployeeTest.java | 86 +++++++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 requests/employee/getProjects.http create mode 100644 src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java diff --git a/requests/employee/getProjects.http b/requests/employee/getProjects.http new file mode 100644 index 0000000..8689798 --- /dev/null +++ b/requests/employee/getProjects.http @@ -0,0 +1,3 @@ +GET http://localhost:8080/employees/315/projects +Authorization: Bearer {{auth_token}} +Content-Type: application/json \ No newline at end of file diff --git a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java index 907de80..5008cd9 100644 --- a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java +++ b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java @@ -1,7 +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; @@ -9,12 +13,18 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import java.util.ArrayList; + @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) { @@ -48,4 +58,15 @@ public class EmployeeService { throw new ResourceNotFoundException("Employee not found"); } } + + public ArrayList getProjects(Long employeeId) { + ArrayList projects = this.projectRepository.findAllByEmployeeId(employeeId); + ArrayList projectDtos = new ArrayList<>(); + + for (ProjectEntity project : projects) { + projectDtos.add(this.projectMapper.mapToGetDto(project)); + } + + return projectDtos; + } } diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java index 6f7e33f..3b9dfb0 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java @@ -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 { + @Query("SELECT p FROM ProjectEntity p WHERE :employeeId MEMBER OF p.employees") + ArrayList findAllByEmployeeId(@Param("employeeId") Long employeeId); } diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java b/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java new file mode 100644 index 0000000..baa4dea --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java @@ -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 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); + } +} diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java new file mode 100644 index 0000000..769bd69 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java @@ -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 map = new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("client_id", "employee-management-service"); + map.add("username", "user"); + map.add("password", "test"); + + HttpEntity> request = new HttpEntity<>(map, headers); + + ResponseEntity response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + + return Objects.requireNonNull(response.getBody()).get("access_token").toString(); + } +} -- 2.45.2 From 02d9fda6b72679653110a96a142c5acca8e3727b Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 12:48:22 +0200 Subject: [PATCH 2/2] QA --- .../java/de/szut/lf8_starter/employee/EmployeeService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java index 5008cd9..d126288 100644 --- a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java +++ b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java @@ -14,6 +14,7 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; +import java.util.List; @Service public class EmployeeService { @@ -59,7 +60,7 @@ public class EmployeeService { } } - public ArrayList getProjects(Long employeeId) { + public List getProjects(Long employeeId) { ArrayList projects = this.projectRepository.findAllByEmployeeId(employeeId); ArrayList projectDtos = new ArrayList<>(); -- 2.45.2