feat: Implement add employee to project route (SCRUM-2) #32
@ -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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -25,10 +25,8 @@ public class ProjectService {
|
|||||||
return projectRepository.findById(id);
|
return projectRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectEntity update(ProjectEntity project) {
|
public void update(ProjectEntity project) {
|
||||||
this.projectRepository.save(project);
|
this.projectRepository.save(project);
|
||||||
|
|
||||||
return project;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Long id) {
|
public void delete(Long id) {
|
||||||
|
@ -1,27 +1,36 @@
|
|||||||
package de.szut.lf8_starter.project.action.employee;
|
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.ProjectEntity;
|
||||||
import de.szut.lf8_starter.project.ProjectService;
|
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.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class AddEmployeeToProjectAction {
|
public class AddEmployeeToProjectAction {
|
||||||
private final ProjectService projectService;
|
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.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}")
|
@PostMapping("/projects/{projectId}/employees/{employeeId}")
|
||||||
public ResponseEntity<Object> create(
|
public ResponseEntity<Object> create(
|
||||||
@PathVariable Long projectId,
|
@PathVariable Long projectId,
|
||||||
@ -34,15 +43,12 @@ public class AddEmployeeToProjectAction {
|
|||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
|
||||||
headers.setBearerAuth(accessToken.replace("Bearer ", ""));
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
project.get().getEmployees().add(employeeId);
|
||||||
|
this.projectService.update(project.get());
|
||||||
String url = "https://employee.szut.dev/employees/" + employeeId;
|
|
||||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
|
|
||||||
|
|
||||||
System.out.println("Employee Data: " + response.getBody());
|
|
||||||
|
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
package de.szut.lf8_starter.project.dto;
|
package de.szut.lf8_starter.project.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
import java.util.List;
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class BearerTokenResponseDto {
|
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||||
private String accessToken;
|
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<String> skillSet;
|
||||||
}
|
}
|
@ -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();
|
||||||
ptran marked this conversation as resolved
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user
dont use var