Compare commits
4 commits
967946bee2
...
ccaf234d28
Author | SHA1 | Date | |
---|---|---|---|
ccaf234d28 | |||
2ca9cfbadb | |||
8dff301457 | |||
e6d3854fbd |
4 changed files with 71 additions and 17 deletions
|
@ -145,25 +145,12 @@ public class ApiController implements DefaultApi {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<Void> removeEmployeeFromProject(Long id, Long employeeId){
|
public ResponseEntity<Void> removeEmployeeFromProject(Long id, Long employeeId){
|
||||||
if (!projectRepository.existsById(id)) {
|
Optional<Allocation> allocation = allocationRepository.findById(new AllocationId(id, employeeId));
|
||||||
return ResponseEntity.notFound().build();
|
if (allocation.isEmpty()){
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
EmployeeResponseDTO employee;
|
allocationRepository.delete(allocation.get());
|
||||||
try {
|
|
||||||
employee = apiClientFactory.getEmployeeApi().findById(employeeId);
|
|
||||||
} catch (HttpClientErrorException exception) {
|
|
||||||
return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
|
||||||
? HttpStatus.NOT_FOUND
|
|
||||||
: HttpStatus.SERVICE_UNAVAILABLE);
|
|
||||||
} catch (RestClientException exception) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
Allocation allocation = allocationRepository.findById(id);
|
|
||||||
if (allocation.getEmployeeId().equals(employeeId)) {
|
|
||||||
allocationRepository.delete(allocation);
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,4 +7,5 @@ import java.util.List;
|
||||||
public interface AllocationRepository extends JpaRepository<Allocation, AllocationId> {
|
public interface AllocationRepository extends JpaRepository<Allocation, AllocationId> {
|
||||||
|
|
||||||
List<Allocation> findAllByEmployeeId(Long employeeId);
|
List<Allocation> findAllByEmployeeId(Long employeeId);
|
||||||
|
List<Allocation> findAllByProjectId(Long projectId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,6 +164,13 @@ public abstract class IntegrationTest {
|
||||||
allocation1ToOverlapA.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
allocation1ToOverlapA.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||||
allocation1ToOverlapA.setRole(TEST_QUALIFICATION_A_ID);
|
allocation1ToOverlapA.setRole(TEST_QUALIFICATION_A_ID);
|
||||||
allocations.put("1>overlap-a", allocation1ToOverlapA);
|
allocations.put("1>overlap-a", allocation1ToOverlapA);
|
||||||
|
|
||||||
|
|
||||||
|
Allocation allocation1ToSpaceStation = new Allocation();
|
||||||
|
allocation1ToSpaceStation.setProject(allProjects.get("space-station"));
|
||||||
|
allocation1ToSpaceStation.setEmployeeId(TEST_EMPLOYEE_A_ID);
|
||||||
|
allocation1ToSpaceStation.setRole(TEST_QUALIFICATION_A_ID);
|
||||||
|
allocations.put("1>space-station", allocation1ToSpaceStation);
|
||||||
|
|
||||||
allocationRepository.saveAllAndFlush(allocations.values());
|
allocationRepository.saveAllAndFlush(allocations.values());
|
||||||
return allocations;
|
return allocations;
|
||||||
|
|
59
src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java
Normal file
59
src/test/java/de/hmmh/pmt/project/DeleteEmployeeTest.java
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package de.hmmh.pmt.project;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.RequestBuilder;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
|
import de.hmmh.pmt.IntegrationTest;
|
||||||
|
import de.hmmh.pmt.db.Allocation;
|
||||||
|
import de.hmmh.pmt.db.Project;
|
||||||
|
import de.hmmh.pmt.dtos.AddEmployeeDTO;
|
||||||
|
|
||||||
|
public class DeleteEmployeeTest extends IntegrationTest {
|
||||||
|
@Test
|
||||||
|
void shouldNotDeleteEmployeeWhenProjectIsNotFound() throws Exception {
|
||||||
|
mvc
|
||||||
|
.perform(delete(baseUri + "/project/1"))
|
||||||
|
.andExpect(status().isNotFound())
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotDeleteEmployeeWhenEmployeeIsNotAllocated() throws Exception {
|
||||||
|
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||||
|
.thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
|
||||||
|
|
||||||
|
Map<String, Project> allProjects = createTestProjectData();
|
||||||
|
|
||||||
|
this.mvc
|
||||||
|
.perform(getRequest(allProjects.get("research-lab").getId(), 0L))
|
||||||
|
.andExpect(status().isNotFound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void employeeSuccessfullyDeletedFromProject() throws Exception {
|
||||||
|
Map<String, Project> allProjects = createTestProjectData();
|
||||||
|
Map<String, Allocation> allAllocations = createTestAllocationData(allProjects);
|
||||||
|
Allocation allocation = allAllocations.get("1>space-station");
|
||||||
|
|
||||||
|
this.mvc
|
||||||
|
.perform(getRequest(allocation.getProjectId(),allocation.getEmployeeId()))
|
||||||
|
.andExpect(status().isNoContent());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBuilder getRequest(Long projectId, Long employeeId) {
|
||||||
|
return MockMvcRequestBuilders
|
||||||
|
.delete(baseUri + "/project/" + projectId + "/employee/" + employeeId);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue