- Add AutoMapper profile for CfgProfile and ProfileSqlJob entity-to-DTO mappings - Consolidate GetProfileByIdQuery and GetAllActiveProfilesQuery into unified GetProfileQuery - Implement flexible filtering with nullable query options (Id, Active, TypeId, ProfileName) - Add IncludeSqlJobs option for optimized SQL job loading - Move CfgProfileDto to Common/Dtos for better architecture alignment - Add comprehensive unit tests for GetProfileQuery with multiple filter scenarios - Move ISQLExecutor interface from Domain to Application layer - Add GetByIdWithSqlJobsAsync and GetAllActiveWithSqlJobsAsync to repository
303 lines
10 KiB
C#
303 lines
10 KiB
C#
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<ICfgProfileRepository> _mockProfileRepo;
|
|
private readonly GetProfileByIdQueryHandler _handler;
|
|
|
|
public GetProfileByIdQueryTests()
|
|
{
|
|
_mockProfileRepo = new Mock<ICfgProfileRepository>();
|
|
_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<ProfileSqlJob>
|
|
{
|
|
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<CancellationToken>()))
|
|
.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<CancellationToken>()))
|
|
.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<CancellationToken>()))
|
|
.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<CancellationToken>()),
|
|
Times.Once);
|
|
_mockProfileRepo.Verify(
|
|
r => r.GetByIdWithSqlJobsAsync(It.IsAny<long>(), It.IsAny<CancellationToken>()),
|
|
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<CancellationToken>()))
|
|
.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<ICfgProfileRepository> _mockProfileRepo;
|
|
private readonly GetAllActiveProfilesQueryHandler _handler;
|
|
|
|
public GetAllActiveProfilesQueryTests()
|
|
{
|
|
_mockProfileRepo = new Mock<ICfgProfileRepository>();
|
|
_handler = new GetAllActiveProfilesQueryHandler(_mockProfileRepo.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ReturnsAllActiveProfiles()
|
|
{
|
|
// Arrange
|
|
var profiles = new List<CfgProfile>
|
|
{
|
|
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<ProfileSqlJob>
|
|
{
|
|
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<ProfileSqlJob>
|
|
{
|
|
new ProfileSqlJob { Id = 2, ProfileId = 2, Sequence = 1, Name = "Job 2" }
|
|
}
|
|
}
|
|
};
|
|
|
|
_mockProfileRepo
|
|
.Setup(r => r.GetAllActiveWithSqlJobsAsync(It.IsAny<CancellationToken>()))
|
|
.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<CancellationToken>()))
|
|
.ReturnsAsync(new List<CfgProfile>());
|
|
|
|
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<CfgProfile>
|
|
{
|
|
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<Expression<Func<CfgProfile, bool>>>(), It.IsAny<CancellationToken>()))
|
|
.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<Expression<Func<CfgProfile, bool>>>(), It.IsAny<CancellationToken>()),
|
|
Times.Once);
|
|
_mockProfileRepo.Verify(
|
|
r => r.GetAllActiveWithSqlJobsAsync(It.IsAny<CancellationToken>()),
|
|
Times.Never);
|
|
}
|
|
}
|
|
}
|