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 OsgHistoricalDocumentPdfServiceTest { private OsgHistoricalDocumentPdfService _osgHistoricalDocumentPdfService; 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 = "6" } } } } }; _osgHistoricalDocumentPdfService = new OsgHistoricalDocumentPdfService(_mockLcuServiceFactory.Object, _mockFileOperationsApiService.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.OsgHistoricalDocument)).Returns(indexService.Object); indexService.Setup(x => x.GetFileContentAndParseRecords(It.IsAny())).ThrowsAsync(new Exception("Errors")); Assert.ThrowsAsync(async () => await _osgHistoricalDocumentPdfService.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 OsgHistoricalDocument { DocType = "TestDocType", EntityName = "Jim Jones", MemberNumberOrUserId = "2", CreationDate = "20220823", StartingPage = "1", NumberOfPages = "3" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.OsgHistoricalDocument)).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 _osgHistoricalDocumentPdfService.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 OsgHistoricalDocument { DocType = "TestDocType", EntityName = "Jan James", MemberNumberOrUserId = "2", CreationDate = "20220823", StartingPage = "1", NumberOfPages = "3" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.OsgHistoricalDocument)).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 _osgHistoricalDocumentPdfService.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 OsgHistoricalDocument { DocType = "TestDocType", EntityName = "Jeff June", MemberNumberOrUserId = "2", CreationDate = "20220823", StartingPage = "1", NumberOfPages = "3" } }; var indexService = new Mock(); _mockLcuServiceFactory.Setup(x => x.GetService(IngestorTypes.OsgHistoricalDocument)).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 _osgHistoricalDocumentPdfService.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); } }