HorseEndpointTest.java 3.64 KB
Newer Older
1 2 3 4 5
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;
6
import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
7 8
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
9
import org.springframework.beans.factory.annotation.Value;
10 11
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
12 13
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.*;
14
import org.springframework.test.context.ActiveProfiles;
15 16 17
import org.springframework.test.context.event.annotation.AfterTestMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
18 19
import org.springframework.web.client.RestTemplate;

20
import java.io.File;
21 22 23 24 25
import java.sql.Date;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class HorseEndpointTest {
26 27
    @Value("${spring.upload.path}")
    private String FILE_BASE_PATH;
28 29 30 31 32 33 34 35 36 37 38
    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";
39
        HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf(birthday), ERace.APPALOOSA, "files/test.png", null);
40 41 42 43 44 45 46 47 48 49 50

        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());
51
        assertEquals(newHorse.getImagePath(), response.getBody().getImagePath());
52 53
        assertEquals(newHorse.getOwner(), response.getBody().getOwner());
    }
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    @Test
    @DisplayName("Uploading an image in the correct format will return HTTP 201")
    public void addingNewImage_correctFormat_shouldReturnStatus201() {
        try {
            // More information:
            // https://www.baeldung.com/spring-rest-template-multipart-upload
            // https://github.com/spring-guides/gs-uploading-files/blob/master/complete/src/test/java/com/example/uploadingfiles/FileUploadIntegrationTests.java

            // Get the file
            ClassPathResource image = new ClassPathResource("horse.jpg", getClass());

            // Set the body
            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            body.add("file", image);

            // Create the HTTP Entity
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body);

            ResponseEntity<String> response = REST_TEMPLATE.postForEntity(BASE_URL + port + HORSE_URL + "/upload", request, String.class);
            assertEquals(HttpStatus.CREATED, response.getStatusCode());
        } finally {
            new File(FILE_BASE_PATH + "horse.jpg").delete();
        }
    }
79
}