OwnerEndpointTest.java 1.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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());
    }
}