2024-08-23 18:02:18 +02:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Json;
|
2025-06-12 18:22:37 +02:00
|
|
|
|
using Vegasco.Server.Api.Cars;
|
|
|
|
|
|
using Vegasco.Server.Api.Consumptions;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
|
|
|
|
|
[Collection(SharedTestCollection.Name)]
|
|
|
|
|
|
public class UpdateConsumptionTests : IAsyncLifetime
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly WebAppFactory _factory;
|
|
|
|
|
|
private readonly IServiceScope _scope;
|
|
|
|
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly CarFaker _carFaker = new();
|
|
|
|
|
|
private readonly ConsumptionFaker _consumptionFaker = new();
|
|
|
|
|
|
|
|
|
|
|
|
public UpdateConsumptionTests(WebAppFactory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
_factory = factory;
|
|
|
|
|
|
_scope = _factory.Services.CreateScope();
|
|
|
|
|
|
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task UpdateConsumption_ShouldCreateConsumption_WhenRequestIsValid()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
|
|
|
|
|
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{createdConsumption.Id}", updateConsumptionRequest);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
2025-06-12 17:43:22 +02:00
|
|
|
|
string content = await response.Content.ReadAsStringAsync();
|
2024-08-23 18:02:18 +02:00
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
|
|
var updatedConsumption = await response.Content.ReadFromJsonAsync<UpdateConsumption.Response>();
|
|
|
|
|
|
updatedConsumption.Should().BeEquivalentTo(updateConsumptionRequest, o => o.ExcludingMissingMembers());
|
|
|
|
|
|
|
|
|
|
|
|
_dbContext.Consumptions.Should().HaveCount(1)
|
|
|
|
|
|
.And.ContainEquivalentOf(updatedConsumption, o =>
|
|
|
|
|
|
o.ExcludingMissingMembers()
|
|
|
|
|
|
.Excluding(x => x!.Id)
|
|
|
|
|
|
.Excluding(x => x!.CarId));
|
|
|
|
|
|
|
|
|
|
|
|
Consumption singleConsumption = _dbContext.Consumptions.Single();
|
|
|
|
|
|
singleConsumption.Id.Value.Should().Be(updatedConsumption!.Id);
|
|
|
|
|
|
singleConsumption.CarId.Value.Should().Be(updatedConsumption.CarId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task UpdateConsumption_ShouldReturnValidationProblems_WhenRequestIsInvalid()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
|
|
|
|
|
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest() with { Distance = -42 };
|
|
|
|
|
|
var randomGuid = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{randomGuid}", updateConsumptionRequest);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
2025-06-12 17:43:22 +02:00
|
|
|
|
string content = await response.Content.ReadAsStringAsync();
|
2024-08-23 18:02:18 +02:00
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
|
|
|
|
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
|
|
|
|
|
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
|
|
|
|
|
x.Equals(nameof(updateConsumptionRequest.Distance), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
|
|
_dbContext.Consumptions.Should().NotContainEquivalentOf(updateConsumptionRequest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task UpdateConsumption_ShouldReturnNotFound_WhenConsumptionDoesNotExist()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
|
|
|
|
|
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest();
|
|
|
|
|
|
var randomGuid = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{randomGuid}", updateConsumptionRequest);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
2025-06-12 17:43:22 +02:00
|
|
|
|
string content = await response.Content.ReadAsStringAsync();
|
2024-08-23 18:02:18 +02:00
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
|
|
_dbContext.Consumptions.Should().NotContainEquivalentOf(updateConsumptionRequest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
|
|
|
|
|
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
|
|
|
|
|
return createdConsumption!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<CreateCar.Response> CreateCarAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
|
|
|
|
|
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
|
|
|
|
|
createCarResponse.EnsureSuccessStatusCode();
|
|
|
|
|
|
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
|
|
|
|
|
return createdCarResponse!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task InitializeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
FluentAssertionConfiguration.SetupGlobalConfig();
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
_scope.Dispose();
|
|
|
|
|
|
await _dbContext.DisposeAsync();
|
|
|
|
|
|
await _factory.ResetDatabaseAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|