From 790c2f4bc434dafb90084c043410c276920bf2f3 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 2 Oct 2024 12:43:07 +0200 Subject: [PATCH 1/8] WIP --- requests/employee/createEmployee.http | 12 +++++ requests/employee/deleteEmployee.http | 2 + requests/employee/getAllEmployees.http | 2 + requests/employee/getEmployee.http | 2 + requests/project/addEmployeeToProject.http | 2 + requests/{ => project}/createProject.http | 0 requests/{ => project}/getAllProjects.http | 0 requests/{ => project}/getProject.http | 0 requests/{ => project}/updateProject.http | 0 .../{ => crud}/CreateProjectAction.java | 2 +- .../{ => crud}/GetAllProjectsAction.java | 2 +- .../action/{ => crud}/GetProjectAction.java | 7 ++- .../{ => crud}/UpdateProjectAction.java | 2 +- .../employee/AddEmployeeToProjectAction.java | 49 +++++++++++++++++++ .../project/dto/BearerTokenResponseDto.java | 15 ++++++ 15 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 requests/employee/createEmployee.http create mode 100644 requests/employee/deleteEmployee.http create mode 100644 requests/employee/getAllEmployees.http create mode 100644 requests/employee/getEmployee.http create mode 100644 requests/project/addEmployeeToProject.http rename requests/{ => project}/createProject.http (100%) rename requests/{ => project}/getAllProjects.http (100%) rename requests/{ => project}/getProject.http (100%) rename requests/{ => project}/updateProject.http (100%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/CreateProjectAction.java (97%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/GetAllProjectsAction.java (97%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/GetProjectAction.java (83%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/UpdateProjectAction.java (95%) create mode 100644 src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java diff --git a/requests/employee/createEmployee.http b/requests/employee/createEmployee.http new file mode 100644 index 0000000..c9f51b4 --- /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": "yomom", + "lastName": "yomom", + "street": "yomom", + "postcode": "yomom", + "city": "yomom", + "phone": "yomom" +} \ 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/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..541f832 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java @@ -0,0 +1,49 @@ +package de.szut.lf8_starter.project.action.employee; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectService; +import org.springframework.http.*; +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 org.springframework.web.client.RestTemplate; + +import java.util.Optional; + +@RestController +public class AddEmployeeToProjectAction { + private final ProjectService projectService; + private final RestTemplate restTemplate; + + public AddEmployeeToProjectAction(ProjectService projectService, RestTemplate restTemplate) { + this.projectService = projectService; + this.restTemplate = restTemplate; + } + + + @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); + } + + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken.replace("Bearer ", "")); + + HttpEntity requestEntity = new HttpEntity<>(headers); + + String url = "https://employee.szut.dev/employees/" + employeeId; + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); + + System.out.println("Employee Data: " + response.getBody()); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java b/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java new file mode 100644 index 0000000..7bd40eb --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java @@ -0,0 +1,15 @@ +package de.szut.lf8_starter.project.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import lombok.Getter; +import lombok.Setter; + +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +@JsonIgnoreProperties(ignoreUnknown = true) +@Getter +@Setter +public class BearerTokenResponseDto { + private String accessToken; +} -- 2.45.2 From 9260e96df7fe9452df2fb85ce1a072bfda4e23ce Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 09:10:48 +0200 Subject: [PATCH 2/8] Done --- .../lf8_starter/employee/EmployeeService.java | 34 ++++++++ .../lf8_starter/project/ProjectService.java | 4 +- .../employee/AddEmployeeToProjectAction.java | 34 ++++---- ...TokenResponseDto.java => EmployeeDto.java} | 17 ++-- .../project/AddEmployeeToProjectTest.java | 83 +++++++++++++++++++ 5 files changed, 150 insertions(+), 22 deletions(-) create mode 100644 src/main/java/de/szut/lf8_starter/employee/EmployeeService.java rename src/main/java/de/szut/lf8_starter/project/dto/{BearerTokenResponseDto.java => EmployeeDto.java} (51%) create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java 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/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/employee/AddEmployeeToProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java index 541f832..1e14ae0 100644 --- 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 @@ -1,27 +1,36 @@ 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 org.springframework.http.*; +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 org.springframework.web.client.RestTemplate; import java.util.Optional; @RestController public class AddEmployeeToProjectAction { private final ProjectService projectService; - private final RestTemplate restTemplate; + private final EmployeeService employeeService; - public AddEmployeeToProjectAction(ProjectService projectService, RestTemplate restTemplate) { + public AddEmployeeToProjectAction(ProjectService projectService, EmployeeService employeeService) { this.projectService = projectService; - this.restTemplate = restTemplate; + 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, @@ -34,15 +43,12 @@ public class AddEmployeeToProjectAction { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - HttpHeaders headers = new HttpHeaders(); - headers.setBearerAuth(accessToken.replace("Bearer ", "")); + if (!this.employeeService.employeeExists(accessToken, employeeId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } - HttpEntity requestEntity = new HttpEntity<>(headers); - - String url = "https://employee.szut.dev/employees/" + employeeId; - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - - System.out.println("Employee Data: " + response.getBody()); + 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/BearerTokenResponseDto.java b/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java similarity index 51% rename from src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java rename to src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java index 7bd40eb..05909e1 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java @@ -1,15 +1,22 @@ package de.szut.lf8_starter.project.dto; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; import lombok.Getter; import lombok.Setter; -@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) -@JsonIgnoreProperties(ignoreUnknown = true) +import java.util.List; + @Getter @Setter -public class BearerTokenResponseDto { - private String accessToken; +@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..b8a79ff --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -0,0 +1,83 @@ +package de.szut.lf8_starter.integration.project; + +import de.szut.lf8_starter.employee.EmployeeService; +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectService; +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.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Optional; + +import static org.mockito.Mockito.when; +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; + + @MockBean + private ProjectService projectService; + + @MockBean + private EmployeeService employeeService; + + @Test + void testAddEmployeeToProject() throws Exception { + Long projectId = 1L; + Long employeeId = 1L; + String accessToken = "Bearer valid_token"; + + ProjectEntity projectEntity = new ProjectEntity(); + projectEntity.setId(projectId); + + when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); + when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(true); + + mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) + .header(HttpHeaders.AUTHORIZATION, accessToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + @Test + void testAddEmployeeToProject_ProjectNotFound() throws Exception { + Long projectId = 1L; + Long employeeId = 1L; + String accessToken = "Bearer valid_token"; + + when(projectService.findById(projectId)).thenReturn(Optional.empty()); + + mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) + .header(HttpHeaders.AUTHORIZATION, accessToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + + @Test + void testAddEmployeeToProject_EmployeeNotFound() throws Exception { + Long projectId = 1L; + Long employeeId = 1L; + String accessToken = "Bearer valid_token"; + + ProjectEntity projectEntity = new ProjectEntity(); + projectEntity.setId(projectId); + + when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); + when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(false); + + mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) + .header(HttpHeaders.AUTHORIZATION, accessToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } +} \ No newline at end of file -- 2.45.2 From 36b5d6581d993c25d73e5f176546783b6f4bd059 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:09:34 +0200 Subject: [PATCH 3/8] Fix tests --- .../lf8_starter/project/ProjectEntity.java | 2 +- .../project/AddEmployeeToProjectTest.java | 89 +++++++++---------- 2 files changed, 44 insertions(+), 47 deletions(-) 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/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java index b8a79ff..d5a918a 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -1,83 +1,80 @@ package de.szut.lf8_starter.integration.project; -import de.szut.lf8_starter.employee.EmployeeService; import de.szut.lf8_starter.project.ProjectEntity; -import de.szut.lf8_starter.project.ProjectService; +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.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; 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.util.Optional; +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Objects; -import static org.mockito.Mockito.when; 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; - @MockBean - private ProjectService projectService; + @Autowired + private RestTemplate restTemplate; - @MockBean - private EmployeeService employeeService; + @Autowired + private ProjectRepository projectRepository; @Test - void testAddEmployeeToProject() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + void addEmployeeToProjectTest() throws Exception { + var 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); - ProjectEntity projectEntity = new ProjectEntity(); - projectEntity.setId(projectId); - when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); - when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(true); - - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) + mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", 1, 312) + .header(HttpHeaders.AUTHORIZATION, getBearerToken()) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); + + var updatedProject = projectRepository.findById(1L).get(); + + assert updatedProject.getEmployees().contains(312L); } - @Test - void testAddEmployeeToProject_ProjectNotFound() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + private String getBearerToken() { + String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; - when(projectService.findById(projectId)).thenReturn(Optional.empty()); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); - } + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("grant_type", "password"); + map.add("client_id", "employee-management-service"); + map.add("username", "user"); + map.add("password", "test"); - @Test - void testAddEmployeeToProject_EmployeeNotFound() throws Exception { - Long projectId = 1L; - Long employeeId = 1L; - String accessToken = "Bearer valid_token"; + HttpEntity> request = new HttpEntity<>(map, headers); - ProjectEntity projectEntity = new ProjectEntity(); - projectEntity.setId(projectId); + var response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); - when(projectService.findById(projectId)).thenReturn(Optional.of(projectEntity)); - when(employeeService.employeeExists(accessToken, employeeId)).thenReturn(false); - - mockMvc.perform(post("/projects/{projectId}/employees/{employeeId}", projectId, employeeId) - .header(HttpHeaders.AUTHORIZATION, accessToken) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); + return Objects.requireNonNull(response.getBody()).get("access_token").toString(); } } \ No newline at end of file -- 2.45.2 From 09d2b9a3fb4eab43ebce3ea325992220afc89edd Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:13:03 +0200 Subject: [PATCH 4/8] Fix tests --- .../integration/project/GetProjectActionTest.java | 6 ++++++ 1 file changed, 6 insertions(+) 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..75fc944 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 @@ -2,6 +2,7 @@ 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.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -24,6 +25,11 @@ class GetProjectActionTest { @Autowired private ProjectRepository projectRepository; + @BeforeEach + void setUp() { + this.projectRepository.deleteAll(); + } + @Test void getProjectTest() throws Exception { var project = new ProjectEntity(); -- 2.45.2 From d30940326ceb68962ce0200570871723b3e3c78c Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:16:23 +0200 Subject: [PATCH 5/8] Fix tests --- .../integration/project/GetProjectActionTest.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) 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 75fc944..0b9eb83 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 @@ -2,7 +2,6 @@ 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.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -25,11 +24,6 @@ class GetProjectActionTest { @Autowired private ProjectRepository projectRepository; - @BeforeEach - void setUp() { - this.projectRepository.deleteAll(); - } - @Test void getProjectTest() throws Exception { var project = new ProjectEntity(); @@ -60,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()); } } -- 2.45.2 From 928920a359e7af194eb4a286459d69d6aad6b165 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:22:58 +0200 Subject: [PATCH 6/8] alter --- requests/employee/createEmployee.http | 12 ++++++------ .../project/AddEmployeeToProjectTest.java | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/requests/employee/createEmployee.http b/requests/employee/createEmployee.http index c9f51b4..a170fe2 100644 --- a/requests/employee/createEmployee.http +++ b/requests/employee/createEmployee.http @@ -3,10 +3,10 @@ Authorization: Bearer {{auth_token}} Content-Type: application/json { - "firstName": "yomom", - "lastName": "yomom", - "street": "yomom", - "postcode": "yomom", - "city": "yomom", - "phone": "yomom" + "firstName": "Jan", + "lastName": "Klattenhoff", + "street": "Pirolweg 17", + "postcode": "27777", + "city": "Gandakersee", + "phone": "017684984816" } \ No newline at end of file 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 index d5a918a..570687a 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -77,4 +77,4 @@ class AddEmployeeToProjectActionIntegrationTest { return Objects.requireNonNull(response.getBody()).get("access_token").toString(); } -} \ No newline at end of file +} -- 2.45.2 From cda04fa8a23087b919c02d2f4acc700b5b457720 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:34:24 +0200 Subject: [PATCH 7/8] Remove var --- .../project/AddEmployeeToProjectTest.java | 11 +++----- .../project/CreateProjectActionTest.java | 28 +++++++++---------- .../project/GetProjectActionTest.java | 2 +- .../project/ProjectFindAllSuccessTest.java | 4 +-- .../project/RemoveProjectActionTest.java | 2 +- .../project/UpdateProjectActionTest.java | 15 +++++----- 6 files changed, 30 insertions(+), 32 deletions(-) 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 index 570687a..4193e77 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/AddEmployeeToProjectTest.java @@ -6,10 +6,7 @@ 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.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; +import org.springframework.http.*; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -37,7 +34,7 @@ class AddEmployeeToProjectActionIntegrationTest { @Test void addEmployeeToProjectTest() throws Exception { - var project = new ProjectEntity(); + ProjectEntity project = new ProjectEntity(); project.setComment("comment"); project.setContractor(1); project.setContractorName("contractorName"); @@ -54,7 +51,7 @@ class AddEmployeeToProjectActionIntegrationTest { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); - var updatedProject = projectRepository.findById(1L).get(); + ProjectEntity updatedProject = projectRepository.findById(1L).get(); assert updatedProject.getEmployees().contains(312L); } @@ -73,7 +70,7 @@ class AddEmployeeToProjectActionIntegrationTest { HttpEntity> request = new HttpEntity<>(map, headers); - var response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + 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..6a340ca 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; @@ -12,10 +13,9 @@ import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; import java.util.Arrays; -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 +41,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 +57,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 ProjectEntity 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 0b9eb83..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); 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); -- 2.45.2 From fa607435cf430c6946e618489d79a7792d07e459 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 10:37:01 +0200 Subject: [PATCH 8/8] Fix --- .../integration/project/CreateProjectActionTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 6a340ca..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 @@ -12,7 +12,9 @@ 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; @@ -59,7 +61,7 @@ class CreateProjectActionTest { final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); - final ProjectEntity project = this.projectRepository.findById(id); + 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); -- 2.45.2