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 _mockUnitOfWork; private readonly Mock _mockWorkItemRepository; private readonly Mock _mockWorkItemExecutionService; private readonly Mock _mockActionBuilderService; private readonly ConnectionConfiguration _connectionConfiguration; private readonly ReceiveEndpointConfigurations _receiveEndpointConfigurations; private readonly ApiConfiguration _apiConfiguration; public WorkItemServiceTest() { _mockUnitOfWork = new Mock(); _mockWorkItemRepository = new Mock(); _mockWorkItemExecutionService = new Mock(); _mockActionBuilderService = new Mock(); _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(async () => await _workItemService.GetByIdAsync(1)); _mockWorkItemRepository.Verify(x => x.FindByIdAsync(1), Times.Once); } [Fact] public async Task GetByJobIdAsync_Success() { // Arrange var workItems = new List() { 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(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()), Times.Never); _mockWorkItemRepository.Verify(x => x.Update(It.IsAny()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); } [Fact] public async Task DeleteList_Success() { // Arrange var workItem1Executions = new List() { new WorkItemExecution { WorkItemId = 1, WorkItemExecutionId = 1 }, new WorkItemExecution { WorkItemId = 1, WorkItemExecutionId = 2 } }; _mockWorkItemExecutionService.Setup(service => service.GetAllByWorkItemId(1)).ReturnsAsync(workItem1Executions); var workItem2Executions = new List() { new WorkItemExecution { WorkItemId = 2, WorkItemExecutionId = 3 }, new WorkItemExecution { WorkItemId = 2, WorkItemExecutionId = 4 } }; _mockWorkItemExecutionService.Setup(service => service.GetAllByWorkItemId(2)).ReturnsAsync(workItem2Executions); var deletingWorkItems = new List { new WorkItem { WorkItemId = 1, WorkItemExecutions = new List { new WorkItemExecution { WorkItemId = 1, WorkItemExecutionId = 1 }, new WorkItemExecution { WorkItemId = 1, WorkItemExecutionId = 2 } } }, new WorkItem { WorkItemId = 2, WorkItemExecutions = new List { new WorkItemExecution { WorkItemId = 2, WorkItemExecutionId = 3 }, new WorkItemExecution { WorkItemId = 2, WorkItemExecutionId = 4 } } } }; _mockWorkItemRepository.Setup(repo => repo.RemoveList(deletingWorkItems)); var startingDeletingWorkItems = new List { new WorkItem { WorkItemId = 1 }, new WorkItem { WorkItemId = 2 } }; // Act await _workItemService.DeleteList(startingDeletingWorkItems); // Assert _mockWorkItemExecutionService.Verify(x => x.GetAllByWorkItemId(It.Is(x => x == 1)), Times.Once); _mockWorkItemExecutionService.Verify(x => x.GetAllByWorkItemId(It.Is(x => x == 2)), Times.Once); _mockWorkItemRepository.Verify(x => x.RemoveList(It.IsAny>()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); } }