82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using Heimdall.Application.Agents.Queries.GetAllAgents;
|
|
using Heimdall.Application.Common;
|
|
using Heimdall.Application.Tests.Common;
|
|
using Heimdall.Domain.Entities;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Agents;
|
|
|
|
public class GetAllAgentsHandlerTests
|
|
{
|
|
private readonly List<Agent> _agents;
|
|
|
|
public GetAllAgentsHandlerTests()
|
|
{
|
|
_agents =
|
|
[
|
|
new Agent(Guid.NewGuid(), "Charlie", "charlie-host", "1.0.0"),
|
|
new Agent(Guid.NewGuid(), "Alpha", "alpha-host", "2.0.0"),
|
|
new Agent(Guid.NewGuid(), "Bravo", "bravo-host", "1.5.0"),
|
|
];
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_all_agents_should_return_all_agents()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>(_agents));
|
|
var handler = new GetAllAgentsHandler(context);
|
|
var query = new GetAllAgentsQuery(Page: 1, PageSize: 20);
|
|
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
result.Data.ShouldNotBeNull();
|
|
result.Data.TotalCount.ShouldBe(3);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_all_agents_should_order_by_name()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>(_agents));
|
|
var handler = new GetAllAgentsHandler(context);
|
|
var query = new GetAllAgentsQuery(Page: 1, PageSize: 20);
|
|
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
result.Data!.Items[0].Name.ShouldBe("Alpha");
|
|
result.Data.Items[1].Name.ShouldBe("Bravo");
|
|
result.Data.Items[2].Name.ShouldBe("Charlie");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_all_agents_should_respect_pagination()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>(_agents));
|
|
var handler = new GetAllAgentsHandler(context);
|
|
var query = new GetAllAgentsQuery(Page: 1, PageSize: 2);
|
|
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
result.Data!.Items.Count.ShouldBe(2);
|
|
result.Data.TotalCount.ShouldBe(3);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_all_agents_empty_result_should_return_empty_list()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>());
|
|
var handler = new GetAllAgentsHandler(context);
|
|
var query = new GetAllAgentsQuery(Page: 1, PageSize: 20);
|
|
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
result.Data!.Items.ShouldBeEmpty();
|
|
result.Data.TotalCount.ShouldBe(0);
|
|
}
|
|
}
|