Files
2026-03-24 15:26:03 -05:00

226 lines
7.6 KiB
C#

using System.Collections.Generic;
using LCU.Library.Internal.LcuMassTransitModels;
using LCU.Api.Internal.Dispatcher.Services;
using LCU.Api.Internal.DispatcherLib.Repositories;
using LCU.Api.Internal.Dispatcher.Configurations;
using LCU.Api.Internal.DispatcherSharedLib.BuilderServices;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests;
public class WorkItemServiceTest
{
private readonly WorkItemService _workItemService;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
private readonly Mock<IWorkItemRepository> _mockWorkItemRepository;
private readonly Mock<IWorkItemExecutionService> _mockWorkItemExecutionService;
private readonly Mock<IActionBuilderService> _mockActionBuilderService;
private readonly ConnectionConfiguration _connectionConfiguration;
private readonly ReceiveEndpointConfigurations _receiveEndpointConfigurations;
private readonly ApiConfiguration _apiConfiguration;
public WorkItemServiceTest()
{
_mockUnitOfWork = new Mock<IUnitOfWork>();
_mockWorkItemRepository = new Mock<IWorkItemRepository>();
_mockWorkItemExecutionService = new Mock<IWorkItemExecutionService>();
_mockActionBuilderService = new Mock<IActionBuilderService>();
_connectionConfiguration = new ConnectionConfiguration();
_apiConfiguration = new ApiConfiguration()
{
NumberOfExecutionMessages = 20,
NumberOfWorkItemExecutions = 10
};
_receiveEndpointConfigurations = new ReceiveEndpointConfigurations
{
CompletionReceiveEndpoint = new ReceiveEndpointConfiguration()
};
_workItemService = new WorkItemService(_mockUnitOfWork.Object,
_mockWorkItemRepository.Object,
_mockWorkItemExecutionService.Object,
_mockActionBuilderService.Object,
_connectionConfiguration,
_receiveEndpointConfigurations,
_apiConfiguration);
}
[Fact]
public async Task GetByIdAsync_Success()
{
// Arrange
var workItem = new WorkItem()
{
WorkItemId = 1
};
_mockWorkItemRepository.Setup(repo => repo.FindByIdAsync(1)).ReturnsAsync(workItem);
// Act
var result = await _workItemService.GetByIdAsync(1);
// Assert
Assert.NotNull(result);
Assert.Equal(1, result.WorkItemId);
_mockWorkItemRepository.Verify(x => x.FindByIdAsync(1), Times.Once);
}
[Fact]
public void GetAsync_Exception()
{
_mockWorkItemRepository.Setup(x => x.FindByIdAsync(1)).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _workItemService.GetByIdAsync(1));
_mockWorkItemRepository.Verify(x => x.FindByIdAsync(1), Times.Once);
}
[Fact]
public async Task GetByJobIdAsync_Success()
{
// Arrange
var workItems = new List<WorkItem>()
{
new WorkItem()
{
WorkItemId = 1
}
};
_mockWorkItemRepository.Setup(repo => repo.FindByJobIdAsync(1)).ReturnsAsync(workItems);
// Act
var result = await _workItemService.GetByJobIdAsync(1);
// Assert
Assert.NotNull(result);
Assert.Equal(1, result.FirstOrDefault().WorkItemId);
_mockWorkItemRepository.Verify(x => x.FindByJobIdAsync(1), Times.Once);
}
[Fact]
public void GetByJobIdAsync_Exception()
{
_mockWorkItemRepository.Setup(x => x.FindByJobIdAsync(1)).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _workItemService.GetByJobIdAsync(1));
_mockWorkItemRepository.Verify(x => x.FindByJobIdAsync(1), Times.Once);
}
[Fact]
public async Task UpdateAsync_Success()
{
// Arrange
var workItem = new WorkItem()
{
WorkItemId = 1
};
var updatingWorkItem = new WorkItem()
{
WorkItemId = 1,
Name = "Test"
};
_mockWorkItemRepository.Setup(repo => repo.Update(updatingWorkItem));
// Act
var result = await _workItemService.UpdateAsync(updatingWorkItem);
// Assert
Assert.NotNull(result);
Assert.Equal("Test", result.Name);
_mockWorkItemRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Never);
_mockWorkItemRepository.Verify(x => x.Update(It.IsAny<WorkItem>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
}
[Fact]
public async Task DeleteList_Success()
{
// Arrange
var workItem1Executions = new List<WorkItemExecution>()
{
new WorkItemExecution
{
WorkItemId = 1,
WorkItemExecutionId = 1
},
new WorkItemExecution
{
WorkItemId = 1,
WorkItemExecutionId = 2
}
};
_mockWorkItemExecutionService.Setup(service => service.GetAllByWorkItemId(1)).ReturnsAsync(workItem1Executions);
var workItem2Executions = new List<WorkItemExecution>()
{
new WorkItemExecution
{
WorkItemId = 2,
WorkItemExecutionId = 3
},
new WorkItemExecution
{
WorkItemId = 2,
WorkItemExecutionId = 4
}
};
_mockWorkItemExecutionService.Setup(service => service.GetAllByWorkItemId(2)).ReturnsAsync(workItem2Executions);
var deletingWorkItems = new List<WorkItem>
{
new WorkItem
{
WorkItemId = 1,
WorkItemExecutions = new List<WorkItemExecution>
{
new WorkItemExecution
{
WorkItemId = 1,
WorkItemExecutionId = 1
},
new WorkItemExecution
{
WorkItemId = 1,
WorkItemExecutionId = 2
}
}
},
new WorkItem
{
WorkItemId = 2,
WorkItemExecutions = new List<WorkItemExecution>
{
new WorkItemExecution
{
WorkItemId = 2,
WorkItemExecutionId = 3
},
new WorkItemExecution
{
WorkItemId = 2,
WorkItemExecutionId = 4
}
}
}
};
_mockWorkItemRepository.Setup(repo => repo.RemoveList(deletingWorkItems));
var startingDeletingWorkItems = new List<WorkItem>
{
new WorkItem
{
WorkItemId = 1
},
new WorkItem
{
WorkItemId = 2
}
};
// Act
await _workItemService.DeleteList(startingDeletingWorkItems);
// Assert
_mockWorkItemExecutionService.Verify(x => x.GetAllByWorkItemId(It.Is<int>(x => x == 1)), Times.Once);
_mockWorkItemExecutionService.Verify(x => x.GetAllByWorkItemId(It.Is<int>(x => x == 2)), Times.Once);
_mockWorkItemRepository.Verify(x => x.RemoveList(It.IsAny<List<WorkItem>>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
}
}