Compare commits

...

4 commits

Author SHA1 Message Date
ccaf234d28
PMT-27-added Method to AllocationRepository
All checks were successful
Quality Check / Validate OAS (push) Successful in 33s
Quality Check / Linting (push) Successful in 1m9s
Quality Check / Static Analysis (push) Successful in 1m19s
Quality Check / Testing (push) Successful in 1m17s
Quality Check / Validate OAS (pull_request) Successful in 36s
Quality Check / Linting (pull_request) Successful in 1m12s
Quality Check / Static Analysis (pull_request) Successful in 1m18s
Quality Check / Testing (pull_request) Successful in 1m17s
2024-10-23 11:13:06 +02:00
2ca9cfbadb
PMT-27-code fix and cleanup Controller 2024-10-23 11:11:58 +02:00
8dff301457
PMT-27-added Tests for Responses 2024-10-23 11:10:40 +02:00
e6d3854fbd
PMT-27-added TestData 2024-10-23 11:09:41 +02:00
4 changed files with 71 additions and 17 deletions

View file

@ -145,25 +145,12 @@ public class ApiController implements DefaultApi {
@Override
public ResponseEntity<Void> removeEmployeeFromProject(Long id, Long employeeId){
if (!projectRepository.existsById(id)) {
return ResponseEntity.notFound().build();
Optional<Allocation> allocation = allocationRepository.findById(new AllocationId(id, employeeId));
if (allocation.isEmpty()){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
EmployeeResponseDTO employee;
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);
}
allocationRepository.delete(allocation.get());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

View file

@ -7,4 +7,5 @@ import java.util.List;
public interface AllocationRepository extends JpaRepository<Allocation, AllocationId> {
List<Allocation> findAllByEmployeeId(Long employeeId);
List<Allocation> findAllByProjectId(Long projectId);
}

View file

@ -165,6 +165,13 @@ public abstract class IntegrationTest {
allocation1ToOverlapA.setRole(TEST_QUALIFICATION_A_ID);
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());
return allocations;
}

View 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);
}
}