HorseEndpointTest.java 5.47 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
import org.springframework.test.context.event.annotation.AfterTestMethod;
16
import org.springframework.test.context.jdbc.Sql;
17 18
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
19 20
import org.springframework.web.client.RestTemplate;

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class HorseEndpointTest {
27 28
    @Value("${spring.upload.path}")
    private String FILE_BASE_PATH;
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() {
39
        HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), 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 79 80 81 82 83 84 85 86 87 88 89
    @Test
    @DisplayName("Updating a horse with the correct parameters will return 200 and the new HorseDto")
    public void updatingNewHorse_correctParameters_shouldReturnStatus200AndHorse() {
        HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);

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

        // Update the horse
        newHorse.setId(response.getBody().getId());
        newHorse.setName("Katrina");
        newHorse.setDescription("Fast horse");
        newHorse.setScore((short) 3);
        newHorse.setBirthday(Date.valueOf("2005-01-01"));
        newHorse.setRace(ERace.MORGAN);
        newHorse.setImagePath("files/katrina.png");
        newHorse.setOwner(null);

        request = new HttpEntity<>(newHorse);
        response = REST_TEMPLATE
            .exchange(BASE_URL + port + HORSE_URL + '/' + newHorse.getId(), HttpMethod.PUT, request, HorseDto.class);

        // Compare everything except timestamps
        assertEquals(response.getStatusCode(), HttpStatus.OK);
        assertEquals(newHorse.getId(), response.getBody().getId());
        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.getImagePath(), response.getBody().getImagePath());
        assertEquals(newHorse.getOwner(), response.getBody().getOwner());
    }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    @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();
        }
    }
114
}