OwnerEndpointTest.java 6 KB
Newer Older
1 2
package at.ac.tuwien.sepm.assignment.individual.integration;

3
import static at.ac.tuwien.sepm.assignment.individual.base.TestData.HORSE_URL;
4 5
import static org.junit.jupiter.api.Assertions.*;

6
import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto;
7
import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.OwnerDto;
8
import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
9 10 11 12 13
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;
14
import org.springframework.core.ParameterizedTypeReference;
15 16
import org.springframework.http.*;
import org.springframework.test.context.ActiveProfiles;
17
import org.springframework.web.client.HttpClientErrorException;
18 19
import org.springframework.web.client.RestTemplate;

20 21
import java.sql.Date;

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
@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());
    }
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

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

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    @Test
    @DisplayName("Deleting an existing owner without owners will return HTTP 204")
    public void deletingOwner_existingNoOwnersOwned_shouldReturnStatus204() {
        // Create the owner
        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);

        // Delete and test if deleted
        ResponseEntity res = REST_TEMPLATE
            .exchange(BASE_URL + port + OWNER_URL + '/' + response.getBody().getId(), HttpMethod.DELETE, null, new ParameterizedTypeReference<OwnerDto>() {});

        assertEquals(res.getStatusCode(), HttpStatus.NO_CONTENT);
    }

    @Test
    @DisplayName("Deleting an existing owner without horses will return HTTP 204")
    public void deletingOwner_existingNoHorsesOwned_shouldReturnStatus204() {
        // Create the owner
        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);

        // Delete and test if deleted
        ResponseEntity res = REST_TEMPLATE
            .exchange(BASE_URL + port + OWNER_URL + '/' + response.getBody().getId(), HttpMethod.DELETE, null, new ParameterizedTypeReference<OwnerDto>() {});

        assertEquals(res.getStatusCode(), HttpStatus.NO_CONTENT);
    }

    @Test
    @DisplayName("Deleting an nonexistent owner will return HTTP 404")
    public void deletingOwner_nonexistent_shouldReturnStatus404() {
        assertThrows(HttpClientErrorException.NotFound.class, () ->
            REST_TEMPLATE
                .exchange(BASE_URL + port + OWNER_URL + "/0", HttpMethod.DELETE, null, new ParameterizedTypeReference<OwnerDto>() {}));
    }

    @Test
    @DisplayName("Deleting an exiting owner with horses will return HTTP 403")
    public void deletingOwner_existingHorsesOwned_shouldReturnStatus403() {
        // Create the owner
        OwnerDto newOwner = new OwnerDto("Chad");

        HttpEntity<OwnerDto> request = new HttpEntity<>(newOwner);
        OwnerDto savedOwner = REST_TEMPLATE
            .exchange(BASE_URL + port + OWNER_URL, HttpMethod.POST, request, OwnerDto.class).getBody();

        // Create the horse
        HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", savedOwner.getId());

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

        assertThrows(HttpClientErrorException.Forbidden.class, () ->
            REST_TEMPLATE
                .exchange(BASE_URL + port + OWNER_URL + "/" + savedOwner.getId(), HttpMethod.DELETE, null, new ParameterizedTypeReference<OwnerDto>() {}));
    }
}