using System; using System.Collections.Generic; using Moq; using NUnit.Framework; using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.DocumentIngestorModels.Models.IndexFileDocuments; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.API.Internal.DocumentIngestor.Services.IndexParserServices; using LCU.API.Internal.DocumentIngestor.Services.PdfParserServices; using System.Threading.Tasks; using System.Linq; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.FileOperationsModels.Models; using LCU.Library.Internal.LCUServiceFactory.Configurations; using LCU.Library.Internal.LCUServiceFactory.Services; namespace LCU.API.Internal.DocumentIngestorTests.Services.PdfParserServices; public class FosLoanStatementPdfServiceTest { private FosLoanStatementPdfService _fosLoanStatementPdfService; private Mock> _mockLcuServiceFactory; private Mock _mockFileOperationsApiService; [SetUp] public void Setup() { _mockLcuServiceFactory = new Mock>(); _mockFileOperationsApiService = new Mock(); var serviceFactoryConfiguration = new ServicesFactoryConfiguration { ServiceFactoryRegistrations = new List { new() { ServiceInterfaceName = "IPdfIndexParserService", ServiceRegistrations = new List { new() { ServiceName = "PdfIndexParserService", ServiceId = "0" }, new() { ServiceName = "PdfIndexParserService", ServiceId = "3" } } } } }; _fosLoanStatementPdfService = new FosLoanStatementPdfService(_mockFileOperationsApiService.Object, _mockLcuServiceFactory.Object, serviceFactoryConfiguration); } [Test] public void DetermineBatchParsePdfRequest_GetFileContentAndParseRecords_ThrowException_Failure() { var rootPath = "\\\\Test\\root\\Path\\"; var indexFile = "TestIndexFile"; var pdfName = "TestPdfName"; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.FosLoanStatement)).Returns(indexService.Object); indexService.Setup(x => x.GetFileContentAndParseRecords(It.IsAny())).ThrowsAsync(new Exception("Errors")); Assert.ThrowsAsync(async () => await _fosLoanStatementPdfService.DetermineBatchParsePdfRequest(rootPath, indexFile, pdfName)); indexService.Verify(x => x.GetFileContentAndParseRecords(It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny()), Times.Never); indexService.Verify(x => x.CreateParsedPdfName(It.IsAny()), Times.Never); } [Test] public void DetermineBatchParsePdfRequest_ValidatePdfPageExists_ThrowException_Failure() { var rootPath = "\\\\Test\\root\\Path\\"; var indexFile = "TestIndexFile"; var pdfName = "TestPdfName"; var records = new List { new FosLoanStatement { DocType = "TestDocType", AccountNumber = "1", EntityName = "Test EntityName", EmailAddress = "Test EmailAddress", CreationDate = "1/1/2022", StartingPage = "1", NumberOfPages = "3", DocName = "Test DocName" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.FosLoanStatement)).Returns(indexService.Object); indexService.Setup(x => x.GetFileContentAndParseRecords(It.IsAny())).ReturnsAsync(records); _mockFileOperationsApiService.Setup(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("Errors")); Assert.ThrowsAsync(async () => await _fosLoanStatementPdfService.DetermineBatchParsePdfRequest(rootPath, indexFile, pdfName)); indexService.Verify(x => x.GetFileContentAndParseRecords(It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny()), Times.Once); indexService.Verify(x => x.CreateParsedPdfName(It.IsAny()), Times.Never); } [Test] public void DetermineBatchParsePdfRequest_CreateParsedPdfName_ThrowException_Failure() { var rootPath = "\\\\Test\\root\\Path\\"; var indexFile = "TestIndexFile"; var pdfName = "TestPdfName"; var records = new List { new FosLoanStatement { DocType = "TestDocType", AccountNumber = "1", EntityName = "Test EntityName", EmailAddress = "Test EmailAddress", CreationDate = "1/1/2022", StartingPage = "1", NumberOfPages = "3", DocName = "Test DocName" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.FosLoanStatement)).Returns(indexService.Object); indexService.Setup(x => x.GetFileContentAndParseRecords(It.IsAny())).ReturnsAsync(records); var requestResponse = new RequestResponse { IsSuccessful = true }; _mockFileOperationsApiService.Setup(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny())).ReturnsAsync(requestResponse); indexService.Setup(x => x.CreateParsedPdfName(It.IsAny())).Throws(new Exception("Errors")); Assert.ThrowsAsync(async () => await _fosLoanStatementPdfService.DetermineBatchParsePdfRequest(rootPath, indexFile, pdfName)); indexService.Verify(x => x.GetFileContentAndParseRecords(It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny()), Times.Once); indexService.Verify(x => x.CreateParsedPdfName(It.IsAny()), Times.Once); } [Test] public async Task DetermineBatchParsePdfRequest_Success() { var rootPath = "\\\\Test\\root\\Path\\"; var indexFile = "TestIndexFile"; var pdfName = "TestPdfName"; var records = new List { new FosLoanStatement { DocType = "TestDocType", AccountNumber = "1", EntityName = "Test EntityName", EmailAddress = "Test EmailAddress", CreationDate = "1/1/2022", StartingPage = "1", NumberOfPages = "3", DocName = "Test DocName" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.FosLoanStatement)).Returns(indexService.Object); indexService.Setup(x => x.GetFileContentAndParseRecords(It.IsAny())).ReturnsAsync(records); var requestResponse = new RequestResponse { IsSuccessful = true }; _mockFileOperationsApiService.Setup(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny())).ReturnsAsync(requestResponse); var parsedPdfName = "TestPdfName_Test EntityName_1"; indexService.Setup(x => x.CreateParsedPdfName(It.IsAny())).Returns(parsedPdfName); var batchParsePdfRequest = await _fosLoanStatementPdfService.DetermineBatchParsePdfRequest(rootPath, indexFile, pdfName); Assert.AreEqual(1, batchParsePdfRequest.BatchPdfs.Count); var batchPdf = batchParsePdfRequest.BatchPdfs.FirstOrDefault(); Assert.IsNotNull(batchPdf); Assert.AreEqual($"{parsedPdfName}.pdf", batchPdf?.ParsedPdfname); Assert.AreEqual(1, batchPdf?.PageStart); Assert.AreEqual(3, batchPdf?.PageStop); indexService.Verify(x => x.GetFileContentAndParseRecords(It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.ValidatePdfPageExists(It.IsAny(), It.IsAny()), Times.Once); indexService.Verify(x => x.CreateParsedPdfName(It.IsAny()), Times.Once); } }