Compare commits

...

2 Commits

Author SHA1 Message Date
7092483809 chore(deps): update cimg/openjdk docker tag to v21.0.5-node
All checks were successful
Build PR / Build and analyze (pull_request) Successful in 1m17s
gitea-sonarqube-bot OK
Quality Check / Tests (pull_request) Successful in 1m18s
Quality Check / Checkstyle Main (pull_request) Successful in 1m2s
2024-10-23 10:01:34 +00:00
76b0201cf2 feat(employee): add action to remove employee from project (SCRUM-23) (!35)
All checks were successful
Quality Check / Tests (push) Successful in 1m7s
Quality Check / Checkstyle Main (push) Successful in 35s
Build / Build and analyze (push) Successful in 1m45s
Release / Release (push) Successful in 36s
Reviewed-on: #35
Reviewed-by: Phan Huy Tran <ptran@noreply.localhost>
Co-authored-by: Jan Klattenhoff <jan@kjan.de>
Co-committed-by: Jan Klattenhoff <jan@kjan.de>
2024-10-23 09:51:12 +00:00
4 changed files with 140 additions and 3 deletions

@ -11,7 +11,7 @@ jobs:
name: "Tests" name: "Tests"
runs-on: "ubuntu-latest" runs-on: "ubuntu-latest"
container: container:
image: "cimg/openjdk:21.0.2-node" image: "cimg/openjdk:21.0.5-node"
steps: steps:
- name: "Checkout" - name: "Checkout"
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -33,7 +33,7 @@ jobs:
name: "Checkstyle Main" name: "Checkstyle Main"
runs-on: "ubuntu-latest" runs-on: "ubuntu-latest"
container: container:
image: "cimg/openjdk:21.0.2-node" image: "cimg/openjdk:21.0.5-node"
steps: steps:
- name: "Checkout" - name: "Checkout"
uses: actions/checkout@v4 uses: actions/checkout@v4

@ -0,0 +1,52 @@
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.*;
import java.util.Optional;
@RestController
public class RemoveEmployeeFromProjectAction {
private final ProjectService projectService;
private final EmployeeService employeeService;
public RemoveEmployeeFromProjectAction(ProjectService projectService, EmployeeService employeeService) {
this.projectService = projectService;
this.employeeService = employeeService;
}
@Operation(summary = "Remove an employee from a project")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Employee removed from project"),
@ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content)
})
@DeleteMapping("/projects/{projectId}/employees/{employeeId}")
public ResponseEntity<Object> remove(
@PathVariable Long projectId,
@PathVariable Long employeeId,
@RequestHeader("Authorization") String accessToken
) {
Optional<ProjectEntity> 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().remove(employeeId);
this.projectService.update(project.get());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

@ -0,0 +1,85 @@
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.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
class RemoveEmployeeFromProjectIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private RestTemplate restTemplate;
@Autowired
private ProjectRepository projectRepository;
@Test
void removeEmployeeFromProject() 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, 312L));
this.projectRepository.save(project);
mockMvc.perform(delete("/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<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();
}
@Test
void removeEmployeeShouldReturn404IfProjectNotFound() throws Exception {
mockMvc.perform(delete("/projects/{projectId}/employees/{employeeId}", 1987234987, 312)
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
}

@ -136,6 +136,6 @@ class UpdateProjectActionTest {
@Test @Test
void updateProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { void updateProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
this.mockMvc.perform(put("/projects/2").content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound()); this.mockMvc.perform(put("/projects/98723498798").content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
} }
} }