using Heimdall.Application.Cameras.Commands.CreateCamera; using Heimdall.Application.Common; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.Cameras; public class CreateCameraHandlerTests { private readonly Site _existingSite = new("Test Site", "Paris", "Europe/Paris"); [Fact] public async Task Create_camera_should_return_success() { var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor([_existingSite])); context.Cameras.Returns(MockDbSetHelper.CreateFor()); var handler = new CreateCameraCommandHandler(context); var command = new CreateCameraCommand(_existingSite.Id, "Front Door", "rtsp://192.168.1.100/stream1", "Hikvision", "DS-2CD2T47G2-L"); var result = await handler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBeNull(); result.Data.Name.ShouldBe("Front Door"); result.Data.SiteId.ShouldBe(_existingSite.Id); } [Fact] public async Task Create_camera_nonexistent_site_should_return_not_found() { var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor([_existingSite])); var handler = new CreateCameraCommandHandler(context); var command = new CreateCameraCommand(Guid.NewGuid(), "Camera", null, null, null); var result = await handler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Create_camera_should_add_and_save() { var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor([_existingSite])); context.Cameras.Returns(MockDbSetHelper.CreateFor()); var handler = new CreateCameraCommandHandler(context); var command = new CreateCameraCommand(_existingSite.Id, "Back Yard", null, null, null); await handler.Handle(command, CancellationToken.None); await context.Received(1).SaveChangesAsync(Arg.Any()); } }