using System; using System.Threading.Tasks; using System.Collections.Generic; using Moq; using NUnit.Framework; using Newtonsoft.Json; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.API.Internal.DocumentIngestor.Services.QueueServices; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.ApiResponse.Models; using LCU.Library.Internal.DocumentIngestorModels.Constants; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.DocumentIngestorModels.Enums; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class CompletionServiceTest { private CompletionService _completionService; private Mock _mockIngestorRunInfoService; private Mock _mockDocumentProcessingInfoService; private readonly Mock _responseQueue = new(); [SetUp] public void Setup() { _mockIngestorRunInfoService = new Mock(); _mockDocumentProcessingInfoService = new Mock(); _completionService = new CompletionService(_mockIngestorRunInfoService.Object, _mockDocumentProcessingInfoService.Object, _responseQueue.Object); } [Test] public void HandleCompletion_Exception_ResponseNull() { Response request = null; Assert.ThrowsAsync(async () => await _completionService.HandleCompletion(null)); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void HandleCompletion_Exception_RequestNull() { var request = new Response(); Assert.ThrowsAsync(async () => await _completionService.HandleCompletion(new List() { request })); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleCompletion_Update_Success_Complete() { var documentProcessInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = Guid.NewGuid(), DocumentValuePayload = "{test}", StatusType = ProcessStatusTypes.Completed }; var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId, IndexFilePath = "\\Test\\", IndexTotal = 1 }; _mockIngestorRunInfoService.Setup(service => service.GetListByIdAsync(It.IsAny>())).ReturnsAsync(new List() { ingestorRunInfo }); var processResult = new ProcessResult() { Success = true, AttemptToArchive = false, }; var processingRequest = new ProcessingRequest() { IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId }; var completionRequest = new CompletionRequest() { ProcessingRequest = processingRequest, ProcessResult = processResult }; var response = new Response() { Successful = true, ResponsePayload = JsonConvert.SerializeObject(completionRequest) }; var completionProcessingData = new CompletionProcessingData() { DocumentProcessingInfo = documentProcessInfo, CompletionRequest = completionRequest }; _mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog( It.IsAny>())) .ReturnsAsync(new List() { completionProcessingData}); _mockDocumentProcessingInfoService.Setup(service => service.GetCompletedFirstPassByIngestorRunInfoIdAsync( It.IsAny())).ReturnsAsync( new List() { documentProcessInfo }); await _completionService.HandleCompletion(new List() { response }); _mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny>()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public async Task HandleCompletion_Update_Success_Incomplete() { var documentProcessInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = Guid.NewGuid(), DocumentValuePayload = "{test}", StatusType = ProcessStatusTypes.Completed }; var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId, IndexFilePath = "\\Test\\", IndexTotal = 5 }; _mockIngestorRunInfoService.Setup(service => service.GetListByIdAsync(It.IsAny>())).ReturnsAsync(new List() { ingestorRunInfo }); var processResult = new ProcessResult() { Success = true, AttemptToArchive = false, }; var processingRequest = new ProcessingRequest() { IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId }; var completionRequest = new CompletionRequest() { ProcessingRequest = processingRequest, ProcessResult = processResult }; var response = new Response() { Successful = true, ResponsePayload = JsonConvert.SerializeObject(completionRequest) }; var completionProcessingData = new CompletionProcessingData() { DocumentProcessingInfo = documentProcessInfo, CompletionRequest = completionRequest }; _mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog( It.IsAny>())) .ReturnsAsync(new List() { completionProcessingData }); _mockDocumentProcessingInfoService.Setup(service => service.GetCompletedFirstPassByIngestorRunInfoIdAsync( It.IsAny())).ReturnsAsync( new List() { documentProcessInfo }); await _completionService.HandleCompletion(new List() { response }); _mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny>()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny()), Times.Never); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public void HandleCompletion_UpdateRunInfoStatusById_ThrowsException() { var documentProcessInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = Guid.NewGuid(), DocumentValuePayload = "{test}", StatusType = ProcessStatusTypes.Completed }; var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = new Guid(), IndexFilePath = "\\Test\\", IndexTotal = 1 }; var processResult = new ProcessResult() { Success = true, AttemptToArchive = false, }; var processingRequest = new ProcessingRequest() { IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId }; var completionRequest = new CompletionRequest() { ProcessingRequest = processingRequest, ProcessResult = processResult }; var response = new Response() { Successful = true, ResponsePayload = JsonConvert.SerializeObject(completionRequest) }; var completionProcessingData = new CompletionProcessingData() { DocumentProcessingInfo = documentProcessInfo, CompletionRequest = completionRequest }; _mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog( It.IsAny>())) .ReturnsAsync(new List() { completionProcessingData }); _mockIngestorRunInfoService.Setup(x => x.GetListByIdAsync(It.IsAny>())).ThrowsAsync(new Exception("Test Exception")); Assert.ThrowsAsync(async () => await _completionService.HandleCompletion(new List() { response })); _mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny>()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); } }