"...assignment/individual/integration/OwnerEndpointTest.java" did not exist on "361a09b0827e04067bfbbb473ef5127823784378"
OwnerEndpointTest.java 2.71 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
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());
    }
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    @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());
    }
65 66
}