using Heimdall.Application.Common; using Heimdall.Application.Vehicles.Commands.UpdateVehicle; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; using Microsoft.EntityFrameworkCore; namespace Heimdall.Application.Tests.Handlers.Vehicles; public class UpdateVehicleHandlerTests { [Fact] public async Task Update_vehicle_should_return_success() { var vehicle = new Vehicle("OLD-PLATE", null, "OldMake", "OldModel", "OldColor"); var context = Substitute.For(); context.Vehicles.Returns(MockDbSetHelper.CreateFor(new[] { vehicle })); context.CameraEvents.Returns(MockDbSetHelper.CreateFor()); var handler = new UpdateVehicleCommandHandler(context); var result = await handler.Handle( new UpdateVehicleCommand(vehicle.Id, "NEW-PLATE", "NewMake", "NewModel", "NewColor", null), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBeNull(); result.Data.LicensePlate.ShouldBe("NEW-PLATE"); result.Data.Make.ShouldBe("NewMake"); } [Fact] public async Task Update_vehicle_not_found_should_return_not_found() { var context = Substitute.For(); context.Vehicles.Returns(MockDbSetHelper.CreateFor()); var handler = new UpdateVehicleCommandHandler(context); var result = await handler.Handle( new UpdateVehicleCommand(Guid.NewGuid(), "X", null, null, null, null), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Update_vehicle_assign_person() { var vehicle = new Vehicle("PLATE-1"); var personId = Guid.NewGuid(); var context = Substitute.For(); context.Vehicles.Returns(MockDbSetHelper.CreateFor(new[] { vehicle })); context.CameraEvents.Returns(MockDbSetHelper.CreateFor()); var handler = new UpdateVehicleCommandHandler(context); var result = await handler.Handle( new UpdateVehicleCommand(vehicle.Id, null, null, null, null, personId), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data!.PersonId.ShouldBe(personId); } [Fact] public async Task Update_vehicle_unassign_person() { var personId = Guid.NewGuid(); var vehicle = new Vehicle("PLATE-2", personId); var context = Substitute.For(); context.Vehicles.Returns(MockDbSetHelper.CreateFor(new[] { vehicle })); context.CameraEvents.Returns(MockDbSetHelper.CreateFor()); var handler = new UpdateVehicleCommandHandler(context); var result = await handler.Handle( new UpdateVehicleCommand(vehicle.Id, null, null, null, null, null), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data!.PersonId.ShouldBeNull(); } }