2024-08-17 16:38:40 +02:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Json;
|
2025-06-12 18:22:37 +02:00
|
|
|
|
using Vegasco.Server.Api.Cars;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
[Collection(SharedTestCollection.Name)]
|
|
|
|
|
|
public class GetCarsTests : IAsyncLifetime
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly WebAppFactory _factory;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly CarFaker _carFaker = new();
|
|
|
|
|
|
|
|
|
|
|
|
public GetCarsTests(WebAppFactory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
_factory = factory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task GetCars_ShouldReturnEmptyList_WhenNoEntriesExist()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2024-08-25 13:39:00 +02:00
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
2024-08-25 13:39:00 +02:00
|
|
|
|
var apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
|
|
|
|
|
|
apiResponse!.Cars.Should().BeEmpty();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task GetCars_ShouldReturnEntries_WhenEntriesExist()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
List<CreateCar.Response> createdCars = [];
|
|
|
|
|
|
|
|
|
|
|
|
const int numberOfCars = 5;
|
|
|
|
|
|
for (var i = 0; i < numberOfCars; i++)
|
|
|
|
|
|
{
|
2024-08-25 13:39:00 +02:00
|
|
|
|
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
|
|
|
|
|
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
createCarResponse.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
|
|
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
|
|
|
|
|
createdCars.Add(createdCar!);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2024-08-25 13:39:00 +02:00
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
2024-08-25 13:39:00 +02:00
|
|
|
|
var apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
|
|
|
|
|
|
apiResponse!.Cars.Should().BeEquivalentTo(createdCars);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task InitializeAsync() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await _factory.ResetDatabaseAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|