Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.DispatcherTests/ServiceTests/WorkItemExecutionServiceTest.cs
T
2026-03-24 15:26:03 -05:00

102 lines
3.7 KiB
C#

using LCU.Api.Internal.Dispatcher.Services;
using LCU.Api.Internal.DispatcherLib.Repositories;
using LCU.Library.Internal.DispatcherModels.Enums;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests;
public class WorkItemExecutionServiceTest
{
private readonly WorkItemExecutionService _workItemExecutionService;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
private readonly Mock<IWorkItemExecutionRepository> _workItemExecutionRepository;
public WorkItemExecutionServiceTest()
{
_mockUnitOfWork = new Mock<IUnitOfWork>();
_workItemExecutionRepository = new Mock<IWorkItemExecutionRepository>();
_workItemExecutionService = new WorkItemExecutionService(_mockUnitOfWork.Object,
_workItemExecutionRepository.Object);
}
[Fact]
public async void GetByIdAsync_Success()
{
// Arrange
var workItemExecution = new WorkItemExecution()
{
WorkItemExecutionId = 1
};
_workItemExecutionRepository.Setup(repo => repo.FindByIdAsync(1)).ReturnsAsync(workItemExecution);
// Act
var result = await _workItemExecutionService.GetByIdAsync(1);
// Assert
Assert.NotNull(result);
Assert.Equal(1, result.WorkItemExecutionId);
_workItemExecutionRepository.Verify(x => x.FindByIdAsync(1), Times.Once);
}
[Fact]
public void GetAsync_Exception()
{
_workItemExecutionRepository.Setup(x => x.FindByIdAsync(1)).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _workItemExecutionService.GetByIdAsync(1));
_workItemExecutionRepository.Verify(x => x.FindByIdAsync(1), Times.Once);
}
[Fact]
public async void UpdateCompleteWorkItemExecutionWithoutSave_Success()
{
// Arrange
var workItemExecution = new WorkItemExecution()
{
WorkItemExecutionId = 1
};
var updatingWorkItemExecution = new WorkItemExecution()
{
WorkItemExecutionId = 1,
Status = StatusType.Completed
};
_workItemExecutionRepository.Setup(repo => repo.Update(updatingWorkItemExecution));
// Act
var result = await _workItemExecutionService.UpdateCompleteWorkItemExecutionWithoutSave(workItemExecution, StatusType.Completed);
// Assert
Assert.NotNull(result);
Assert.Equal(StatusType.Completed, result.Status);
_workItemExecutionRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Never);
_workItemExecutionRepository.Verify(x => x.Update(It.IsAny<WorkItemExecution>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
}
[Fact]
public async void UpdateCompleteWorkItemExecution_Success()
{
// Arrange
var workItemExecution = new WorkItemExecution()
{
WorkItemExecutionId = 1
};
var updatingWorkItemExecution = new WorkItemExecution()
{
WorkItemExecutionId = 1,
Status = StatusType.Completed
};
_workItemExecutionRepository.Setup(repo => repo.Update(updatingWorkItemExecution));
// Act
var result = await _workItemExecutionService.UpdateCompleteWorkItemExecution(workItemExecution, StatusType.Completed);
// Assert
Assert.NotNull(result);
Assert.Equal(StatusType.Completed, result.Status);
_workItemExecutionRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Never);
_workItemExecutionRepository.Verify(x => x.Update(It.IsAny<WorkItemExecution>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
}
}