HorseEndpointTest.java 2.12 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 43 44 45 46 47
package at.ac.tuwien.sepm.assignment.individual.integration;

import static org.junit.jupiter.api.Assertions.*;

import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;

import java.sql.Date;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class HorseEndpointTest {
    private static final RestTemplate REST_TEMPLATE = new RestTemplate();
    private static final String BASE_URL = "http://localhost:";
    private static final String HORSE_URL = "/horses";

    @LocalServerPort
    private int port;

    @Test
    @DisplayName("Adding a new horse with the correct parameters will return HTTP 201 and the new HorseDto")
    public void addingNewHorse_correctParameters_shouldReturnStatus201AndHorse() {
        String birthday = "2020-01-01";
        HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf(birthday), null);

        HttpEntity<HorseDto> request = new HttpEntity<>(newHorse);
        ResponseEntity<HorseDto> response = REST_TEMPLATE
            .exchange(BASE_URL + port + HORSE_URL, HttpMethod.POST, request, HorseDto.class);

        // Compare everything except ids and timestamps
        assertEquals(response.getStatusCode(), HttpStatus.CREATED);
        assertEquals(newHorse.getName(), response.getBody().getName());
        assertEquals(newHorse.getDescription(), response.getBody().getDescription());
        assertEquals(newHorse.getScore(), response.getBody().getScore());
        assertEquals(newHorse.getBirthday().toString(), response.getBody().getBirthday().toString());
        assertEquals(newHorse.getOwner(), response.getBody().getOwner());
    }
}