PMT-16: Projekt Anlegen #12
1 changed files with 105 additions and 0 deletions
105
src/test/java/de/hmmh/pmt/project/CreateTest.java
Normal file
105
src/test/java/de/hmmh/pmt/project/CreateTest.java
Normal file
|
@ -0,0 +1,105 @@
|
|||
package de.hmmh.pmt.project;
|
||||
|
||||
import de.hmmh.pmt.IntegrationTest;
|
||||
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
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 java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class CreateTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void successfullyCreate() throws Exception {
|
||||
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
|
||||
.thenReturn(new EmployeeResponseDTO());
|
||||
|
||||
CreateProjectDTO createDTO = new CreateProjectDTO();
|
||||
createDTO.setName("Test");
|
||||
createDTO.setGoal("A Test Goal");
|
||||
createDTO.setCustomerId(10L);
|
||||
createDTO.setAdministratorId(10L);
|
||||
createDTO.setStart(LocalDateTime.of(2000, 1, 13, 12, 51));
|
||||
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
|
||||
.perform(requestBuilder)
|
||||
SZUT-Ole marked this conversation as resolved
Outdated
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.id").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCreateProjectWithSameName() throws Exception {
|
||||
|
||||
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));
|
||||
|
||||
RequestBuilder firstProjectRequestBuilder = createProjectRequestBuilder(createDTO);
|
||||
|
||||
this.mvc
|
||||
.perform(firstProjectRequestBuilder)
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.id").exists());
|
||||
|
||||
RequestBuilder secondProjectRequestBuilder = createProjectRequestBuilder(createDTO);
|
||||
SZUT-Ole marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Useless 2 Posts, nehmt einfach Einen Der Existierenden Datensätze und Versucht Ihn erneut zu schreiben Useless 2 Posts, nehmt einfach Einen Der Existierenden Datensätze und Versucht Ihn erneut zu schreiben
|
||||
|
||||
this.mvc
|
||||
.perform(secondProjectRequestBuilder)
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCreateProjectWhenAdministratorDoesNotExist() throws Exception {
|
||||
HttpClientErrorException httpClientErrorException = mock(HttpClientErrorException.class);
|
||||
SZUT-Ole marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Viel zu Komplex, Mein Ergebnis, wenn man die Spring Doc liest:
Viel zu Komplex, Mein Ergebnis, wenn man die Spring Doc liest:
```java
when(this.mockEmployeeApi.findById(Mockito.anyLong()))
.thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
``
|
||||
|
||||
when(httpClientErrorException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(404));
|
||||
|
||||
when(this.mockEmployeeApi.findById(1L))
|
||||
.thenThrow(httpClientErrorException);
|
||||
|
||||
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));
|
||||
|
||||
RequestBuilder projectRequestBuilder = createProjectRequestBuilder(createDTO);
|
||||
SZUT-Ole marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Wofür die Zwischen Variable, kann doch einfach Inline gemovt werden. Wofür die Zwischen Variable, kann doch einfach Inline gemovt werden.
|
||||
|
||||
this.mvc
|
||||
.perform(projectRequestBuilder)
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
private RequestBuilder createProjectRequestBuilder(CreateProjectDTO createDTO) throws Exception {
|
||||
SZUT-Ole marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Das Ganze Baute Euch den Inhalt einer Request zusammen, Würde es dementsprechend Das Ganze Baute Euch den Inhalt einer Request zusammen, Würde es dementsprechend
`getRequest()` nennen
|
||||
|
||||
return MockMvcRequestBuilders
|
||||
.post(baseUri + "/project")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.content(this.objectMapper.writeValueAsString(createDTO))
|
||||
.contentType(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue
Nutzt hier doch doirekt die Methode inline, also
.perform(getRequest(createDTO))