diff --git a/requests/employee/createEmployee.http b/requests/employee/createEmployee.http new file mode 100644 index 0000000..a170fe2 --- /dev/null +++ b/requests/employee/createEmployee.http @@ -0,0 +1,12 @@ +POST https://employee.szut.dev/employees +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{ + "firstName": "Jan", + "lastName": "Klattenhoff", + "street": "Pirolweg 17", + "postcode": "27777", + "city": "Gandakersee", + "phone": "017684984816" +} \ No newline at end of file diff --git a/requests/employee/deleteEmployee.http b/requests/employee/deleteEmployee.http new file mode 100644 index 0000000..cf1d827 --- /dev/null +++ b/requests/employee/deleteEmployee.http @@ -0,0 +1,2 @@ +DELETE https://employee.szut.dev/employees/310 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/employee/getAllEmployees.http b/requests/employee/getAllEmployees.http new file mode 100644 index 0000000..c3911be --- /dev/null +++ b/requests/employee/getAllEmployees.http @@ -0,0 +1,2 @@ +GET https://employee.szut.dev/employees +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/employee/getEmployee.http b/requests/employee/getEmployee.http new file mode 100644 index 0000000..eddf4d9 --- /dev/null +++ b/requests/employee/getEmployee.http @@ -0,0 +1,2 @@ +GET https://employee.szut.dev/employees/312 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/project/addEmployeeToProject.http b/requests/project/addEmployeeToProject.http new file mode 100644 index 0000000..981839a --- /dev/null +++ b/requests/project/addEmployeeToProject.http @@ -0,0 +1,2 @@ +POST http://localhost:8080/projects/1/employees/312 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/createProject.http b/requests/project/createProject.http similarity index 100% rename from requests/createProject.http rename to requests/project/createProject.http diff --git a/requests/getAllProjects.http b/requests/project/getAllProjects.http similarity index 100% rename from requests/getAllProjects.http rename to requests/project/getAllProjects.http diff --git a/requests/getProject.http b/requests/project/getProject.http similarity index 100% rename from requests/getProject.http rename to requests/project/getProject.http diff --git a/requests/updateProject.http b/requests/project/updateProject.http similarity index 100% rename from requests/updateProject.http rename to requests/project/updateProject.http diff --git a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java new file mode 100644 index 0000000..a78f76b --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java @@ -0,0 +1,34 @@ +package de.szut.lf8_starter.employee; + +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +@Service +public class EmployeeService { + private final RestTemplate restTemplate; + + public EmployeeService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public boolean employeeExists(String accessToken, Long employeeId) { + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken.replace("Bearer ", "")); + + HttpEntity requestEntity = new HttpEntity<>(headers); + + String url = "https://employee.szut.dev/employees/" + employeeId; + + try { + restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); + } catch (HttpClientErrorException.NotFound e) { + return false; + } + + return true; + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java b/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java index d9c35a1..6aa7127 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectEntity.java @@ -25,7 +25,7 @@ public class ProjectEntity { private long leadingEmployee; - @ElementCollection + @ElementCollection(fetch = FetchType.EAGER) private List employees; private long contractor; diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java index 31efbec..17afcea 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -25,10 +25,8 @@ public class ProjectService { return projectRepository.findById(id); } - public ProjectEntity update(ProjectEntity project) { + public void update(ProjectEntity project) { this.projectRepository.save(project); - - return project; } public void delete(Long id) { diff --git a/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java similarity index 97% rename from src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java index 3e636b5..f21646d 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java @@ -1,4 +1,4 @@ -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.ProjectMapper; diff --git a/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java similarity index 97% rename from src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java index 039a104..5fb346a 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectService; diff --git a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java similarity index 83% rename from src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java index d8e4207..eba31c1 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java @@ -1,4 +1,4 @@ -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.ProjectMapper; @@ -11,7 +11,10 @@ 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.GetMapping; +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; diff --git a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java similarity index 95% rename from src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java index 9c174e3..29a4ad5 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java @@ -1,4 +1,4 @@ -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.ProjectMapper; diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java new file mode 100644 index 0000000..1e14ae0 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java @@ -0,0 +1,55 @@ +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; + +@RestController +public class AddEmployeeToProjectAction { + private final ProjectService projectService; + private final EmployeeService employeeService; + + public AddEmployeeToProjectAction(ProjectService projectService, EmployeeService employeeService) { + this.projectService = projectService; + this.employeeService = employeeService; + } + + @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) + }) + @PostMapping("/projects/{projectId}/employees/{employeeId}") + public ResponseEntity create( + @PathVariable Long projectId, + @PathVariable Long employeeId, + @RequestHeader("Authorization") String accessToken + ) { + Optional 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().add(employeeId); + this.projectService.update(project.get()); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java b/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java new file mode 100644 index 0000000..05909e1 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java @@ -0,0 +1,22 @@ +package de.szut.lf8_starter.project.dto; + +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +public class EmployeeDto { + private long id; + private String firstName; + private String lastName; + private String street; + private String postcode; + private String city; + private String phone; + private List skillSet; +} diff --git a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java new file mode 100644 index 0000000..4193e77 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -0,0 +1,77 @@ +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.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +class AddEmployeeToProjectActionIntegrationTest { + @Autowired + private MockMvc mockMvc; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private ProjectRepository projectRepository; + + @Test + void addEmployeeToProjectTest() 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)); + this.projectRepository.save(project); + + + mockMvc.perform(post("/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 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(); + } +} diff --git a/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java index af10c64..27fe186 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/CreateProjectActionTest.java @@ -1,5 +1,6 @@ package de.szut.lf8_starter.integration.project; +import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectRepository; import org.json.JSONObject; import org.junit.jupiter.api.Test; @@ -11,11 +12,12 @@ import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; import java.util.Arrays; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @@ -41,9 +43,9 @@ class CreateProjectActionTest { } """; - final var contentAsString = this.mockMvc.perform( - post("/projects").content(content).contentType(MediaType.APPLICATION_JSON) - ) + final String contentAsString = this.mockMvc.perform( + post("/projects").content(content).contentType(MediaType.APPLICATION_JSON) + ) .andExpect(status().isCreated()) .andExpect(jsonPath("name", is("name"))) .andExpect(jsonPath("leading_employee", is(1))) @@ -57,15 +59,15 @@ class CreateProjectActionTest { .getResponse() .getContentAsString(); - final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); + final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); - final var project = this.projectRepository.findById(id); - assertThat(project.get().getName()).isEqualTo("name"); - assertThat(project.get().getLeadingEmployee()).isEqualTo(1); - assertThat(project.get().getContractor()).isEqualTo(4); - assertThat(project.get().getContractorName()).isEqualTo("Peter File"); - assertThat(project.get().getComment()).isEqualTo("goal of project"); - assertThat(project.get().getStartDate()).isEqualTo(LocalDate.of(2000, 1, 1)); - assertThat(project.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2001, 1, 1)); + final Optional project = this.projectRepository.findById(id); + assertThat(project.get().getName()).isEqualTo("name"); + assertThat(project.get().getLeadingEmployee()).isEqualTo(1); + assertThat(project.get().getContractor()).isEqualTo(4); + assertThat(project.get().getContractorName()).isEqualTo("Peter File"); + assertThat(project.get().getComment()).isEqualTo("goal of project"); + assertThat(project.get().getStartDate()).isEqualTo(LocalDate.of(2000, 1, 1)); + assertThat(project.get().getPlannedEndDate()).isEqualTo(LocalDate.of(2001, 1, 1)); } } diff --git a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java index 751d002..1b6ed3f 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/GetProjectActionTest.java @@ -26,7 +26,7 @@ class GetProjectActionTest { @Test void getProjectTest() throws Exception { - var project = new ProjectEntity(); + ProjectEntity project = new ProjectEntity(); project.setId(1); project.setComment("comment"); project.setContractor(1); @@ -54,6 +54,6 @@ class GetProjectActionTest { @Test void getProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { - this.mockMvc.perform(get("/projects/2")).andExpect(status().isNotFound()); + this.mockMvc.perform(get("/projects/1111")).andExpect(status().isNotFound()); } } diff --git a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java index 3fa8bd8..ff3a642 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/ProjectFindAllSuccessTest.java @@ -27,7 +27,7 @@ class ProjectFindAllSuccessTest { @Test void findAllProjectsTest() throws Exception { - var project = new ProjectEntity(); + ProjectEntity project = new ProjectEntity(); project.setId(1); project.setComment("comment"); project.setContractor(1); @@ -36,7 +36,7 @@ class ProjectFindAllSuccessTest { project.setLeadingEmployee(1); project.setName("name"); project.setStartDate(LocalDate.of(2021, 1, 1)); - project.setEmployees(List.of(1L,2L,3L)); + project.setEmployees(List.of(1L, 2L, 3L)); this.projectRepository.save(project); this.mockMvc.perform(get("/projects")) diff --git a/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java index 93285ab..c2c47dd 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/RemoveProjectActionTest.java @@ -25,7 +25,7 @@ class RemoveProjectActionTest { @Test void deleteProjectTest() throws Exception { - var project = new ProjectEntity(); + ProjectEntity project = new ProjectEntity(); project.setComment("comment"); project.setContractor(1); project.setContractorName("contractorName"); diff --git a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java index 4e53c8c..e03a680 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java @@ -13,6 +13,7 @@ import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; import java.util.Arrays; import java.util.List; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; @@ -55,7 +56,7 @@ class UpdateProjectActionTest { } """; - final var contentAsString = this.mockMvc.perform( + final String contentAsString = this.mockMvc.perform( put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON) ) .andExpect(status().isOk()) @@ -71,9 +72,9 @@ class UpdateProjectActionTest { .getResponse() .getContentAsString(); - final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); + final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); - final var existingProject = this.projectRepository.findById(id); + final Optional existingProject = this.projectRepository.findById(id); assertThat(existingProject.get().getName()).isEqualTo("updatedName"); assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2); assertThat(existingProject.get().getContractor()).isEqualTo(6); @@ -103,13 +104,13 @@ class UpdateProjectActionTest { {} """; - final var contentAsString = this.mockMvc.perform( + final String contentAsString = this.mockMvc.perform( put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON) ) .andExpect(status().isOk()) .andExpect(jsonPath("name", is("name"))) .andExpect(jsonPath("leading_employee", is(1))) - .andExpect(jsonPath("employees", is(List.of(1,2,3)))) + .andExpect(jsonPath("employees", is(List.of(1, 2, 3)))) .andExpect(jsonPath("contractor", is(1))) .andExpect(jsonPath("contractor_name", is("contractorName"))) .andExpect(jsonPath("comment", is("comment"))) @@ -120,9 +121,9 @@ class UpdateProjectActionTest { .getResponse() .getContentAsString(); - final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); + final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); - final var existingProject = this.projectRepository.findById(id); + final Optional existingProject = this.projectRepository.findById(id); assertThat(existingProject.get().getName()).isEqualTo("name"); assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1); assertThat(existingProject.get().getContractor()).isEqualTo(1);