using System; using System.Collections.Generic; using System.Threading.Tasks; using Moq; using NUnit.Framework; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.Exceptions; using LCU.API.Internal.DocumentIngestor.Services.QueueServices; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestor.Services.IndexParserServices; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.Library.Internal.LCUServiceFactory.Services; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class IndexServiceTest { private IndexService _indexService; private Mock _mockQueueDocumentProcessing; private Mock _mockIngestorRunInfoService; private Mock _mockDocumentProcessingInfoService; private Mock _mockDocumentTypeRetrieverService; private Mock> _mockLcuServiceFactory; private IDictionary _handlerServicesByType; [SetUp] public void Setup() { _mockQueueDocumentProcessing = new Mock(); _mockIngestorRunInfoService = new Mock(); _mockDocumentProcessingInfoService = new Mock(); _mockDocumentTypeRetrieverService = new Mock(); _mockLcuServiceFactory = new Mock>(); _indexService = new IndexService(_mockQueueDocumentProcessing.Object, _mockIngestorRunInfoService.Object, _mockDocumentProcessingInfoService.Object, _mockDocumentTypeRetrieverService.Object, _mockLcuServiceFactory.Object); } [Test] public void HandleIngestion_Exception_RequestNull() { IngestorRequest request = null; Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); _mockLcuServiceFactory.Verify(x => x.GetService(It.IsAny()), Times.Never); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void HandleIngestion_Exception_Result_FatalFailure() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var result = new IngestorResult(true, "fail"); var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).ReturnsAsync(result); Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void HandleIngestion_Exception_FosTaxStatementIndexService_ExceptionThrown() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = new Guid(), IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void HandleIngestion_Exception_FosDnaDocumentIndexService_ExceptionThrown() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosDnaDocument, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = new Guid(), IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestion_Success_ThreeProcessRequest_Queued() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var guid = new Guid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = guid, IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var result = new IngestorResult(); result.ProcessingRequests = new List() { new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" }, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" }, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" } }; result.IngestorRunInfo = new IngestorRunInfo(Guid.NewGuid(), Convert.ToDateTime("1/1/2022"), IngestorTypes.FosTaxStatement, "\\test\\test\\", "Test.csv", 1); var documentCodeDetailInfo = new DocumentCodeDetailInfo() { DocumentBusinessName = "TestStatement", DocumentType = "TestDocumentType" }; var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).ReturnsAsync(result); _mockDocumentTypeRetrieverService.Setup(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny())) .ReturnsAsync(documentCodeDetailInfo); _mockIngestorRunInfoService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(result.IngestorRunInfo); var documentProcessingInfos = new List() { new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 1, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}" }, new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 2, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}", }, new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 3, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}", } }; _mockDocumentProcessingInfoService.Setup(x => x.GetByIngestorRunInfoIdAsync(It.IsAny())).ReturnsAsync(documentProcessingInfos); _mockDocumentProcessingInfoService.Setup(x => x.SaveRangeAsync(It.IsAny())); await _indexService.HandleIngestion(request); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockDocumentTypeRetrieverService.Verify(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny()), Times.Exactly(1)); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.SaveRangeAsync(It.IsAny()), Times.Exactly(1)); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Exactly(3)); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestion_ExceptionThrown_OnQueue() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var guid = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = guid, IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var result = new IngestorResult(); var processRequest1 = new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" }; var processRequest2 = new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" }; var processRequest3 = new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}", DocumentTypeDisplayName = "OneThousandNinetyNineInt" }; result.ProcessingRequests = new List() { processRequest1, processRequest2, processRequest3 }; result.IngestorRunInfo = new IngestorRunInfo(guid, Convert.ToDateTime("1/1/2022"), IngestorTypes.FosTaxStatement, "\\test\\test\\", "Test.csv", 1); var documentCodeDetailInfo = new DocumentCodeDetailInfo() { DocumentBusinessName = "TestStatement", DocumentType = "TestDocumentType" }; var documentProcessingInfos = new List() { new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 1, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}" }, new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 2, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}", }, new() { IngestorRunInfoId = guid, DocumentProcessingInfoId = 3, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}", } }; var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).ReturnsAsync(result); _mockDocumentTypeRetrieverService.Setup(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny())).ReturnsAsync(documentCodeDetailInfo); _mockDocumentProcessingInfoService.Setup(x => x.SaveRangeAsync(It.IsAny())); _mockDocumentProcessingInfoService.Setup(x => x.GetByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(documentProcessingInfos); _mockIngestorRunInfoService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(result.IngestorRunInfo); var documentProcessingInfo1 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = guid, StatusType = ProcessStatusTypes.Processing, DocumentValuePayload = "{jsonblah: hello1}", DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockQueueDocumentProcessing.Setup(x => x.QueueDocumentProcessingRequest(processRequest1)).ThrowsAsync(new Exception("Error")); var documentProcessingInfoUpdated1 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = guid, DocumentValuePayload = "{jsonblah: hello1}", StatusType = ProcessStatusTypes.CriticalFailure, DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockDocumentProcessingInfoService.Setup(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(documentProcessingInfoUpdated1); var documentProcessingInfo2 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 2, IngestorRunInfoId = guid, StatusType = ProcessStatusTypes.Processing, DocumentValuePayload = "{jsonblah: hello2}", DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockQueueDocumentProcessing.Setup(x => x.QueueDocumentProcessingRequest(processRequest2)).ThrowsAsync(new Exception("Error")); var documentProcessingInfoUpdated2 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 2, IngestorRunInfoId = guid, DocumentValuePayload = "{jsonblah: hello2}", StatusType = ProcessStatusTypes.CriticalFailure, DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockDocumentProcessingInfoService.Setup(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(documentProcessingInfoUpdated2); var documentProcessingInfo3 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 2, IngestorRunInfoId = guid, StatusType = ProcessStatusTypes.Processing, DocumentValuePayload = "{jsonblah: hello3}", DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockQueueDocumentProcessing.Setup(x => x.QueueDocumentProcessingRequest(processRequest3)).ThrowsAsync(new Exception("Error")); var documentProcessingInfoUpdated3 = new DocumentProcessingInfo() { DocumentProcessingInfoId = 3, IngestorRunInfoId = guid, DocumentValuePayload = "{jsonblah: hello3}", StatusType = ProcessStatusTypes.CriticalFailure, DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockDocumentProcessingInfoService.Setup(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(documentProcessingInfoUpdated3); await _indexService.HandleIngestion(request); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockDocumentTypeRetrieverService.Verify(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.SaveRangeAsync(It.IsAny()), Times.Once); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Exactly(3)); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(3)); } [Test] public void HandleIngestion_ExceptionThrown_DocumentProcessingInfo_SaveRangeAsync() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var guid = new Guid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = guid, IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var result = new IngestorResult(); var processRequest1 = new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}", DocumentTypeDisplayName = "1099-INT" }; result.ProcessingRequests = new List() { processRequest1, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}", DocumentTypeDisplayName = "1099-INT" }, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}", DocumentTypeDisplayName = "1099-INT" } }; result.IngestorRunInfo = new IngestorRunInfo(Guid.NewGuid(), Convert.ToDateTime("1/1/2022"), IngestorTypes.FosTaxStatement, "\\test\\test\\", "Test.csv", 1); var documentCodeDetailInfo = new DocumentCodeDetailInfo() { DocumentBusinessName = "TestStatement", DocumentType = "TestDocumentType" }; var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).ReturnsAsync(result); _mockDocumentTypeRetrieverService.Setup(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny())) .ReturnsAsync(documentCodeDetailInfo); _mockIngestorRunInfoService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(result.IngestorRunInfo); var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = 1, IngestorRunInfoId = Guid.NewGuid(), DocumentValuePayload = "{Test}", DocumentBusinessName = "1099-INT (interest income) (Tax)", DocumentType = "Statement" }; _mockDocumentProcessingInfoService.Setup(x => x.SaveRangeAsync(It.IsAny())).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockDocumentTypeRetrieverService.Verify(x => x.GetDocumentCodeDetailByDisplayName(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.SaveRangeAsync(It.IsAny()), Times.Once); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void HandleIngestion_ExceptionThrown_OnIngestorRunInfoSave() { var request = new IngestorRequest() { IngestorType = IngestorTypes.FosTaxStatement, RootPath = "\\test\\test", IndexFileName = "TestIndex.csv" }; var guid = new Guid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = guid, IndexFilePath = "\\test\\test\\TestIndex.csv", IndexTotal = 1 }; var result = new IngestorResult(); var processRequest1 = new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test1.pdf", DocumentValuePayload = "{jsonblah: hello1}" }; result.ProcessingRequests = new List() { processRequest1, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test2.pdf", DocumentValuePayload = "{jsonblah: hello2}" }, new ProcessingRequest() { IngestorRunInfoId = guid, IngestorRequest = request, DocumentFileName = "Test3.pdf", DocumentValuePayload = "{jsonblah: hello3}" } }; result.IngestorRunInfo = new IngestorRunInfo(Guid.NewGuid(), Convert.ToDateTime("1/1/2022"), IngestorTypes.FosTaxStatement, "\\test\\test\\", "Test.csv", 1); var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); _mockIngestorRunInfoService.Setup(x => x.SaveAsync(It.IsAny())).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _indexService.HandleIngestion(request)); indexParsingService.Verify(x => x.HandleIndexRequest(request), Times.Once); _mockIngestorRunInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); _mockQueueDocumentProcessing.Verify(x => x.QueueDocumentProcessingRequest(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [TestCase(IngestorTypes.FosTaxStatement, typeof(IIndexParserService))] [TestCase(IngestorTypes.FosDnaDocument, typeof(IIndexParserService))] [TestCase(IngestorTypes.FosLoanStatement, typeof(IIndexParserService))] [TestCase(IngestorTypes.XdiDnaNotice, typeof(IIndexParserService))] [TestCase(IngestorTypes.OsgDnaNoticesDocument, typeof(IIndexParserService))] [TestCase(IngestorTypes.XdiActionNotice, typeof(IIndexParserService))] [TestCase(IngestorTypes.OsgHistoricalDocument, typeof(IIndexParserService))] public async Task PerformIngestor_ByTypedRequest_HandledByMatchingService(IngestorTypes type, Type handlerType) { var request = new IngestorRequest { IngestorType = type }; var expectedResult = new IngestorResult(); var indexParsingService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(request.IngestorType)).Returns(indexParsingService.Object); indexParsingService.Setup(x => x.HandleIndexRequest(request)).ReturnsAsync(expectedResult); var result = await _indexService.PerformIngestor(request); Assert.AreSame(expectedResult, result); } }