using ECMJobRunner.Application.Common.Dtos; using ECMJobRunner.Application.DEXJob.Queries; using ECMJobRunner.Domain.Entities; using ECMJobRunner.Domain.Interfaces; using FluentAssertions; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using Xunit; namespace ECMJobRunner.Tests.Application { public class GetProfileByIdQueryTests { private readonly Mock _mockProfileRepo; private readonly GetProfileByIdQueryHandler _handler; public GetProfileByIdQueryTests() { _mockProfileRepo = new Mock(); _handler = new GetProfileByIdQueryHandler(_mockProfileRepo.Object); } [Fact] public async Task Handle_WithValidProfileId_ReturnsProfile() { // Arrange var profileId = 123L; var profile = new CfgProfile { Id = profileId, Active = true, ProfileName = "Test Profile", TypeId = 2, Schedule = "0 30 4 ? * MON-SAT", Comment = "Test comment", AddedWho = "TestUser", AddedWhen = DateTime.Now, SqlJobs = new List { new ProfileSqlJob { Id = 1, ProfileId = profileId, Active = true, Sequence = 1, Name = "Job 1", SqlMainQuery = "SELECT 1", SqlCheckQuery = "SELECT COUNT(*) FROM Table1" } } }; _mockProfileRepo .Setup(r => r.GetByIdWithSqlJobsAsync(profileId, It.IsAny())) .ReturnsAsync(profile); var query = new GetProfileByIdQuery { ProfileId = profileId, IncludeSqlJobs = true }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result!.Id.Should().Be(profileId); result.ProfileName.Should().Be("Test Profile"); result.Active.Should().BeTrue(); result.TypeId.Should().Be(2); result.SqlJobs.Should().NotBeNull(); result.SqlJobs!.Count.Should().Be(1); result.SqlJobs[0].Name.Should().Be("Job 1"); } [Fact] public async Task Handle_WithNonExistentProfileId_ReturnsNull() { // Arrange var profileId = 999L; _mockProfileRepo .Setup(r => r.GetByIdWithSqlJobsAsync(profileId, It.IsAny())) .ReturnsAsync((CfgProfile?)null); var query = new GetProfileByIdQuery { ProfileId = profileId }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().BeNull(); } [Fact] public async Task Handle_WithIncludeSqlJobsFalse_DoesNotLoadSqlJobs() { // Arrange var profileId = 123L; var profile = new CfgProfile { Id = profileId, Active = true, ProfileName = "Test Profile", TypeId = 2, Schedule = "0 30 4 ? * MON-SAT", AddedWho = "TestUser", AddedWhen = DateTime.Now }; _mockProfileRepo .Setup(r => r.GetByIdAsync(profileId, It.IsAny())) .ReturnsAsync(profile); var query = new GetProfileByIdQuery { ProfileId = profileId, IncludeSqlJobs = false }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result!.SqlJobs.Should().BeNull(); _mockProfileRepo.Verify( r => r.GetByIdAsync(profileId, It.IsAny()), Times.Once); _mockProfileRepo.Verify( r => r.GetByIdWithSqlJobsAsync(It.IsAny(), It.IsAny()), Times.Never); } [Fact] public async Task Handle_MapsAllProperties_Correctly() { // Arrange var profileId = 123L; var addedWhen = new DateTime(2026, 1, 1, 10, 0, 0); var changedWhen = new DateTime(2026, 1, 2, 14, 30, 0); var profile = new CfgProfile { Id = profileId, Active = false, ProfileName = "Inactive Profile", TypeId = 3, Schedule = "0 0 12 * * ?", Comment = "Detailed comment", AddedWho = "Admin", AddedWhen = addedWhen, ChangedWho = "Editor", ChangedWhen = changedWhen }; _mockProfileRepo .Setup(r => r.GetByIdAsync(profileId, It.IsAny())) .ReturnsAsync(profile); var query = new GetProfileByIdQuery { ProfileId = profileId, IncludeSqlJobs = false }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result!.Id.Should().Be(profileId); result.Active.Should().BeFalse(); result.ProfileName.Should().Be("Inactive Profile"); result.TypeId.Should().Be(3); result.Schedule.Should().Be("0 0 12 * * ?"); result.Comment.Should().Be("Detailed comment"); result.AddedWho.Should().Be("Admin"); result.AddedWhen.Should().Be(addedWhen); result.ChangedWho.Should().Be("Editor"); result.ChangedWhen.Should().Be(changedWhen); } } public class GetAllActiveProfilesQueryTests { private readonly Mock _mockProfileRepo; private readonly GetAllActiveProfilesQueryHandler _handler; public GetAllActiveProfilesQueryTests() { _mockProfileRepo = new Mock(); _handler = new GetAllActiveProfilesQueryHandler(_mockProfileRepo.Object); } [Fact] public async Task Handle_ReturnsAllActiveProfiles() { // Arrange var profiles = new List { new CfgProfile { Id = 1, Active = true, ProfileName = "Profile 1", TypeId = 2, Schedule = "0 30 4 ? * MON-SAT", AddedWho = "User1", AddedWhen = DateTime.Now, SqlJobs = new List { new ProfileSqlJob { Id = 1, ProfileId = 1, Sequence = 1, Name = "Job 1" } } }, new CfgProfile { Id = 2, Active = true, ProfileName = "Profile 2", TypeId = 3, Schedule = "0 0 12 * * ?", AddedWho = "User2", AddedWhen = DateTime.Now, SqlJobs = new List { new ProfileSqlJob { Id = 2, ProfileId = 2, Sequence = 1, Name = "Job 2" } } } }; _mockProfileRepo .Setup(r => r.GetAllActiveWithSqlJobsAsync(It.IsAny())) .ReturnsAsync(profiles); var query = new GetAllActiveProfilesQuery { IncludeSqlJobs = true }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result.Count.Should().Be(2); result[0].ProfileName.Should().Be("Profile 1"); result[1].ProfileName.Should().Be("Profile 2"); result[0].SqlJobs.Should().NotBeNull(); result[0].SqlJobs!.Count.Should().Be(1); result[1].SqlJobs.Should().NotBeNull(); result[1].SqlJobs!.Count.Should().Be(1); } [Fact] public async Task Handle_WithNoActiveProfiles_ReturnsEmptyList() { // Arrange _mockProfileRepo .Setup(r => r.GetAllActiveWithSqlJobsAsync(It.IsAny())) .ReturnsAsync(new List()); var query = new GetAllActiveProfilesQuery(); // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result.Count.Should().Be(0); } [Fact] public async Task Handle_WithIncludeSqlJobsFalse_DoesNotLoadSqlJobs() { // Arrange var profiles = new List { new CfgProfile { Id = 1, Active = true, ProfileName = "Profile 1", TypeId = 2, Schedule = "0 30 4 ? * MON-SAT", AddedWho = "User1", AddedWhen = DateTime.Now } }; _mockProfileRepo .Setup(r => r.FindAsync(It.IsAny>>(), It.IsAny())) .ReturnsAsync(profiles); var query = new GetAllActiveProfilesQuery { IncludeSqlJobs = false }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result.Count.Should().Be(1); result[0].SqlJobs.Should().BeNull(); _mockProfileRepo.Verify( r => r.FindAsync(It.IsAny>>(), It.IsAny()), Times.Once); _mockProfileRepo.Verify( r => r.GetAllActiveWithSqlJobsAsync(It.IsAny()), Times.Never); } } }