Compare commits
1 Commits
6035c73828
...
d24b6b446d
Author | SHA1 | Date | |
---|---|---|---|
d24b6b446d |
@ -1,12 +0,0 @@
|
|||||||
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"
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
DELETE https://employee.szut.dev/employees/310
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
@ -1,2 +0,0 @@
|
|||||||
GET https://employee.szut.dev/employees
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
@ -1,2 +0,0 @@
|
|||||||
GET https://employee.szut.dev/employees/312
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
@ -1,2 +0,0 @@
|
|||||||
POST http://localhost:8080/projects/1/employees/312
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
@ -1,34 +0,0 @@
|
|||||||
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,7 +25,7 @@ public class ProjectEntity {
|
|||||||
|
|
||||||
private long leadingEmployee;
|
private long leadingEmployee;
|
||||||
|
|
||||||
@ElementCollection(fetch = FetchType.EAGER)
|
@ElementCollection
|
||||||
private List<Long> employees;
|
private List<Long> employees;
|
||||||
|
|
||||||
private long contractor;
|
private long contractor;
|
||||||
|
@ -25,8 +25,10 @@ public class ProjectService {
|
|||||||
return projectRepository.findById(id);
|
return projectRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(ProjectEntity project) {
|
public ProjectEntity update(ProjectEntity project) {
|
||||||
this.projectRepository.save(project);
|
this.projectRepository.save(project);
|
||||||
|
|
||||||
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Long id) {
|
public void delete(Long id) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
package de.szut.lf8_starter.project.action;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
import de.szut.lf8_starter.project.ProjectEntity;
|
||||||
import de.szut.lf8_starter.project.ProjectMapper;
|
import de.szut.lf8_starter.project.ProjectMapper;
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
package de.szut.lf8_starter.project.action;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectMapper;
|
import de.szut.lf8_starter.project.ProjectMapper;
|
||||||
import de.szut.lf8_starter.project.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
package de.szut.lf8_starter.project.action;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
import de.szut.lf8_starter.project.ProjectEntity;
|
||||||
import de.szut.lf8_starter.project.ProjectMapper;
|
import de.szut.lf8_starter.project.ProjectMapper;
|
||||||
@ -11,10 +11,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
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;
|
import java.util.Optional;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
package de.szut.lf8_starter.project.action;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
import de.szut.lf8_starter.project.ProjectEntity;
|
||||||
import de.szut.lf8_starter.project.ProjectMapper;
|
import de.szut.lf8_starter.project.ProjectMapper;
|
@ -1,55 +0,0 @@
|
|||||||
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<Object> create(
|
|
||||||
@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().add(employeeId);
|
|
||||||
this.projectService.update(project.get());
|
|
||||||
|
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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<String> skillSet;
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
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<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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,5 @@
|
|||||||
package de.szut.lf8_starter.integration.project;
|
package de.szut.lf8_starter.integration.project;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
|
||||||
import de.szut.lf8_starter.project.ProjectRepository;
|
import de.szut.lf8_starter.project.ProjectRepository;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -12,12 +11,11 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
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;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -43,7 +41,7 @@ class CreateProjectActionTest {
|
|||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
final String contentAsString = this.mockMvc.perform(
|
final var contentAsString = this.mockMvc.perform(
|
||||||
post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
|
post("/projects").content(content).contentType(MediaType.APPLICATION_JSON)
|
||||||
)
|
)
|
||||||
.andExpect(status().isCreated())
|
.andExpect(status().isCreated())
|
||||||
@ -59,9 +57,9 @@ class CreateProjectActionTest {
|
|||||||
.getResponse()
|
.getResponse()
|
||||||
.getContentAsString();
|
.getContentAsString();
|
||||||
|
|
||||||
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
||||||
|
|
||||||
final Optional<ProjectEntity> project = this.projectRepository.findById(id);
|
final var project = this.projectRepository.findById(id);
|
||||||
assertThat(project.get().getName()).isEqualTo("name");
|
assertThat(project.get().getName()).isEqualTo("name");
|
||||||
assertThat(project.get().getLeadingEmployee()).isEqualTo(1);
|
assertThat(project.get().getLeadingEmployee()).isEqualTo(1);
|
||||||
assertThat(project.get().getContractor()).isEqualTo(4);
|
assertThat(project.get().getContractor()).isEqualTo(4);
|
||||||
|
@ -26,7 +26,7 @@ class GetProjectActionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getProjectTest() throws Exception {
|
void getProjectTest() throws Exception {
|
||||||
ProjectEntity project = new ProjectEntity();
|
var project = new ProjectEntity();
|
||||||
project.setId(1);
|
project.setId(1);
|
||||||
project.setComment("comment");
|
project.setComment("comment");
|
||||||
project.setContractor(1);
|
project.setContractor(1);
|
||||||
@ -54,6 +54,6 @@ class GetProjectActionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
|
void getProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
|
||||||
this.mockMvc.perform(get("/projects/1111")).andExpect(status().isNotFound());
|
this.mockMvc.perform(get("/projects/2")).andExpect(status().isNotFound());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ class ProjectFindAllSuccessTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAllProjectsTest() throws Exception {
|
void findAllProjectsTest() throws Exception {
|
||||||
ProjectEntity project = new ProjectEntity();
|
var project = new ProjectEntity();
|
||||||
project.setId(1);
|
project.setId(1);
|
||||||
project.setComment("comment");
|
project.setComment("comment");
|
||||||
project.setContractor(1);
|
project.setContractor(1);
|
||||||
|
@ -25,7 +25,7 @@ class RemoveProjectActionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteProjectTest() throws Exception {
|
void deleteProjectTest() throws Exception {
|
||||||
ProjectEntity project = new ProjectEntity();
|
var project = new ProjectEntity();
|
||||||
project.setComment("comment");
|
project.setComment("comment");
|
||||||
project.setContractor(1);
|
project.setContractor(1);
|
||||||
project.setContractorName("contractorName");
|
project.setContractorName("contractorName");
|
||||||
|
@ -13,7 +13,6 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
@ -56,7 +55,7 @@ class UpdateProjectActionTest {
|
|||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
final String contentAsString = this.mockMvc.perform(
|
final var contentAsString = this.mockMvc.perform(
|
||||||
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
@ -72,9 +71,9 @@ class UpdateProjectActionTest {
|
|||||||
.getResponse()
|
.getResponse()
|
||||||
.getContentAsString();
|
.getContentAsString();
|
||||||
|
|
||||||
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
||||||
|
|
||||||
final Optional<ProjectEntity> existingProject = this.projectRepository.findById(id);
|
final var existingProject = this.projectRepository.findById(id);
|
||||||
assertThat(existingProject.get().getName()).isEqualTo("updatedName");
|
assertThat(existingProject.get().getName()).isEqualTo("updatedName");
|
||||||
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
|
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(2);
|
||||||
assertThat(existingProject.get().getContractor()).isEqualTo(6);
|
assertThat(existingProject.get().getContractor()).isEqualTo(6);
|
||||||
@ -104,7 +103,7 @@ class UpdateProjectActionTest {
|
|||||||
{}
|
{}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
final String contentAsString = this.mockMvc.perform(
|
final var contentAsString = this.mockMvc.perform(
|
||||||
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
put("/projects/1").content(content).contentType(MediaType.APPLICATION_JSON)
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
@ -121,9 +120,9 @@ class UpdateProjectActionTest {
|
|||||||
.getResponse()
|
.getResponse()
|
||||||
.getContentAsString();
|
.getContentAsString();
|
||||||
|
|
||||||
final long id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
|
||||||
|
|
||||||
final Optional<ProjectEntity> existingProject = this.projectRepository.findById(id);
|
final var existingProject = this.projectRepository.findById(id);
|
||||||
assertThat(existingProject.get().getName()).isEqualTo("name");
|
assertThat(existingProject.get().getName()).isEqualTo("name");
|
||||||
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1);
|
assertThat(existingProject.get().getLeadingEmployee()).isEqualTo(1);
|
||||||
assertThat(existingProject.get().getContractor()).isEqualTo(1);
|
assertThat(existingProject.get().getContractor()).isEqualTo(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user