OwnerEndpointTest.java 9.85 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
import org.springframework.web.client.RestTemplate;
19
import org.springframework.web.util.UriComponentsBuilder;
20

21
import java.sql.Date;
22
import java.util.List;
23

24 25 26 27 28 29 30 31 32 33 34 35 36
@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;


37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 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
    @Test
    @DisplayName("Searching for all owners with no filters, given two owners should return HTTP 200 and both ownerss")
    public void searchingOwners_noFiltersGivenTwoOwners_shouldReturnStatus200AndOwners() {
        // Create the owners
        OwnerDto newOwner = new OwnerDto("Chad");

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

        newOwner = new OwnerDto("Gigachad");

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

        // Get all owners and save in list
        // https://stackoverflow.com/a/31947188
        ResponseEntity<List<OwnerDto>> allOwners = REST_TEMPLATE
            .exchange(BASE_URL + port + OWNER_URL, HttpMethod.GET, null, new ParameterizedTypeReference<List<OwnerDto>>() {});

        assertEquals(allOwners.getStatusCode(), HttpStatus.OK);
        assertTrue(allOwners.getBody().contains(firstOwner.getBody()));
        assertTrue(allOwners.getBody().contains(secondOwner.getBody()));
    }

    @Test
    @DisplayName("Searching for all owners with correct filters, given two owners should return HTTP 200 and one owner")
    public void searchingOwners_withCorrectFiltersGivenTwoOwners_shouldReturnStatus200AndOwner() {
        // Create the owners
        OwnerDto newOwner = new OwnerDto("Chad");

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

        newOwner = new OwnerDto("Lad");

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

        // Get all owners and save in list
        // https://stackoverflow.com/a/25434451
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BASE_URL + port + OWNER_URL)
            .queryParam("name", "chad");

        ResponseEntity<List<OwnerDto>> allOwners = REST_TEMPLATE.exchange(
            builder.toUriString(),
            HttpMethod.GET,
            null,
            new ParameterizedTypeReference<List<OwnerDto>>() {}
        );

        assertEquals(allOwners.getStatusCode(), HttpStatus.OK);
        assertTrue(allOwners.getBody().contains(firstOwner.getBody()));
    }

    @Test
    @DisplayName("Searching for all owners with incorrect filters, given two owners should return HTTP 400")
    public void searchingOwners_withIncorrectFiltersGivenTwoOwners_shouldReturnStatus400() {
        // Create the owners
        OwnerDto newOwner = new OwnerDto("Chad");

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

        newOwner = new OwnerDto("Lad");

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

        // Get all owners and save in list
        // https://stackoverflow.com/a/25434451
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BASE_URL + port + OWNER_URL)
            .queryParam("name", "");

        assertThrows(HttpClientErrorException.BadRequest.class, () ->
            REST_TEMPLATE
                .exchange(builder.toUriString(), HttpMethod.GET, null, new ParameterizedTypeReference<OwnerDto>() {}));
    }

121 122 123 124 125 126 127 128 129 130 131 132 133
    @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());
    }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    @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>() {}));
    }
}