67 lines
2.7 KiB
Java
67 lines
2.7 KiB
Java
package at.ac.tuwien.sepm.assignment.individual.integration;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.OwnerDto;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.web.server.LocalServerPort;
|
|
import org.springframework.http.*;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
@ActiveProfiles("test")
|
|
public class OwnerEndpointTest {
|
|
@Value("${spring.upload.path}")
|
|
private String FILE_BASE_PATH;
|
|
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
|
|
private static final String BASE_URL = "http://localhost:";
|
|
private static final String OWNER_URL = "/owners";
|
|
|
|
@LocalServerPort
|
|
private int port;
|
|
|
|
|
|
@Test
|
|
@DisplayName("Adding a new owner with the correct parameters will return HTTP 201 and the new OwnerDto")
|
|
public void addingNewOwner_correctParameters_shouldReturnStatus201AndOwner() {
|
|
OwnerDto newOwner = new OwnerDto("Chad");
|
|
|
|
HttpEntity<OwnerDto> request = new HttpEntity<>(newOwner);
|
|
ResponseEntity<OwnerDto> response = REST_TEMPLATE
|
|
.exchange(BASE_URL + port + OWNER_URL, HttpMethod.POST, request, OwnerDto.class);
|
|
|
|
// Compare everything except ids and timestamps
|
|
assertEquals(response.getStatusCode(), HttpStatus.CREATED);
|
|
assertEquals(newOwner.getName(), response.getBody().getName());
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Updating a owner with the correct parameters will return 200 and the new OwnerDto")
|
|
public void updatingNewOwner_correctParameters_shouldReturnStatus200AndOwner() {
|
|
OwnerDto newOwner = new OwnerDto("Chad");
|
|
|
|
// Create a new owner
|
|
HttpEntity<OwnerDto> request = new HttpEntity<>(newOwner);
|
|
ResponseEntity<OwnerDto> response = REST_TEMPLATE
|
|
.exchange(BASE_URL + port + OWNER_URL, HttpMethod.POST, request, OwnerDto.class);
|
|
|
|
// Update the owner
|
|
newOwner.setId(response.getBody().getId());
|
|
newOwner.setName("Gigachad");
|
|
|
|
request = new HttpEntity<>(newOwner);
|
|
response = REST_TEMPLATE
|
|
.exchange(BASE_URL + port + OWNER_URL + '/' + newOwner.getId(), HttpMethod.PUT, request, OwnerDto.class);
|
|
|
|
// Compare everything except timestamps
|
|
assertEquals(response.getStatusCode(), HttpStatus.OK);
|
|
assertEquals(newOwner.getId(), response.getBody().getId());
|
|
assertEquals(newOwner.getName(), response.getBody().getName());
|
|
}
|
|
}
|
|
|