PMT-16: Projekt Anlegen #12

Merged
SZUT-Ole merged 10 commits from story/PMT-16-projekt-anlegen into trunk 2024-10-15 08:15:23 +00:00
Showing only changes of commit a1710ef2fa - Show all commits

View file

@ -1,17 +1,21 @@
package de.hmmh.pmt.project; package de.hmmh.pmt.project;
import de.hmmh.pmt.IntegrationTest; import de.hmmh.pmt.IntegrationTest;
import de.hmmh.pmt.db.Project;
import de.hmmh.pmt.dtos.CreateProjectDTO; import de.hmmh.pmt.dtos.CreateProjectDTO;
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -33,14 +37,8 @@ public class CreateTest extends IntegrationTest {
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51)); createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42)); createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post(baseUri + "/project")
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(createDTO))
.contentType(MediaType.APPLICATION_JSON);
this.mvc this.mvc
.perform(requestBuilder) .perform(getRequest(createDTO))
.andExpect(status().isCreated()) .andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists()); .andExpect(jsonPath("$.id").exists());
} }
@ -48,36 +46,27 @@ public class CreateTest extends IntegrationTest {
@Test @Test
void shouldNotCreateProjectWithSameName() throws Exception { void shouldNotCreateProjectWithSameName() throws Exception {
Map<String, Project> allProjects = createTestProjectData();
Project spaceStation = allProjects.get("space-station");
CreateProjectDTO createDTO = new CreateProjectDTO(); CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test"); createDTO.setName(spaceStation.getName());
createDTO.setGoal("A Test Goal"); createDTO.setGoal(spaceStation.getGoal());
createDTO.setCustomerId(10L); createDTO.setCustomerId(spaceStation.getCustomerId());
createDTO.setAdministratorId(1L); createDTO.setAdministratorId(spaceStation.getAdministratorId());
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51)); createDTO.setStart(spaceStation.getStart());
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42)); createDTO.setPlannedEnd(spaceStation.getPlannedEnd());
RequestBuilder firstProjectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc this.mvc
.perform(firstProjectRequestBuilder) .perform(getRequest(createDTO))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists());
RequestBuilder secondProjectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc
.perform(secondProjectRequestBuilder)
.andExpect(status().isConflict()); .andExpect(status().isConflict());
} }
@Test @Test
void shouldNotCreateProjectWhenAdministratorDoesNotExist() throws Exception { void shouldNotCreateProjectWhenAdministratorDoesNotExist() throws Exception {
HttpClientErrorException httpClientErrorException = mock(HttpClientErrorException.class);
when(httpClientErrorException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(404)); when(this.mockEmployeeApi.findById(Mockito.anyLong()))
.thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
when(this.mockEmployeeApi.findById(1L))
.thenThrow(httpClientErrorException);
CreateProjectDTO createDTO = new CreateProjectDTO(); CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test"); createDTO.setName("Test");
@ -87,14 +76,66 @@ public class CreateTest extends IntegrationTest {
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51)); createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42)); createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
RequestBuilder projectRequestBuilder = createProjectRequestBuilder(createDTO);
this.mvc this.mvc
.perform(projectRequestBuilder) .perform(getRequest(createDTO))
.andExpect(status().isNotFound()); .andExpect(status().isNotFound());
} }
private RequestBuilder createProjectRequestBuilder(CreateProjectDTO createDTO) throws Exception { @Test
void shouldReturnUnavailableWhenEmployeeApiIsDown() throws Exception {
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
.thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(1L);
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
this.mvc
.perform(getRequest(createDTO))
.andExpect(status().isServiceUnavailable());
}
@Test
void shouldReturnInternalServerErrorOnApiClientCrash() throws Exception {
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
.thenThrow(new RestClientException("Api Client crash"));
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(1L);
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
this.mvc
.perform(getRequest(createDTO))
.andExpect(status().isInternalServerError());
}
@Test
void shouldReturnUnprocessableWhenDataIsInvalid() throws Exception {
CreateProjectDTO createDTO = new CreateProjectDTO();
createDTO.setName("Test");
createDTO.setGoal("A Test Goal");
createDTO.setCustomerId(10L);
createDTO.setAdministratorId(1L);
createDTO.setStart(LocalDateTime.of(2003, 1, 13, 12, 51));
createDTO.setPlannedEnd(LocalDateTime.of(2002, 3, 21, 11, 42));
this.mvc
.perform(getRequest(createDTO))
.andExpect(status().isUnprocessableEntity());
}
private RequestBuilder getRequest(CreateProjectDTO createDTO) throws Exception {
return MockMvcRequestBuilders return MockMvcRequestBuilders
.post(baseUri + "/project") .post(baseUri + "/project")