using System; using System.Threading.Tasks; using System.Collections.Generic; using Moq; using NUnit.Framework; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.API.Internal.DocumentIngestor.Services.IndexParserServices; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models.IndexFileDocuments; using LCU.Library.Internal.FileOperationsModels.Models; using LCU.Library.Internal.FileOperationsModels.Models.NameSettings; using LCU.Library.Internal.LCUServiceFactory.Configurations; namespace LCU.API.Internal.DocumentIngestorTests.Services.IndexParserServices; public class OsgDnaNoticesIndexServiceTest { private OsgDnaNoticesIndexService _osgDnaNoticesIndexService; private Mock _mockFileOperationsApiService; [SetUp] public void Setup() { _mockFileOperationsApiService = new Mock(); var serviceFactoryConfiguration = new ServicesFactoryConfiguration { ServiceFactoryRegistrations = new List { new() { ServiceInterfaceName = "IIndexParserService", ServiceRegistrations = new List { new() { ServiceName = "IndexParserService", ServiceId = "4" }, new() { ServiceName = "IndexParserService", ServiceId = "1" } } } } }; _osgDnaNoticesIndexService = new OsgDnaNoticesIndexService(_mockFileOperationsApiService.Object, serviceFactoryConfiguration); } [Test] public async Task HandleIndexRequest_IngestorRequest_Null_IngestorResult_Failure() { IngestorRequest request = null; var result = await _osgDnaNoticesIndexService.HandleIndexRequest(request); Assert.IsTrue(result.FatalFailure); _mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny()), Times.Never); } [Test] public async Task HandleIndexRequest_IngestorRequest_Success() { var rootPath = "\\Test\\"; var request = new IngestorRequest() { RootPath = rootPath, IndexFileName = "PDFXREF.TXT", IngestorType = IngestorTypes.OsgDnaNoticesDocument }; var fileContent = "00000001|LCL_LT_114_03|5651830147|5651830|20220823|1|1\n" + "00000002|LVI_LT_105_03|19619353841|8996218227|20220823|2|1\n"; var requestResponse = new RequestResponse() { IsSuccessful = true }; var dynamicFileRequest = new DynamicFileListRequest() { Path = rootPath, Filename = "replaceTag.pdf", DynamicFileSettings = new DynamicNameSetting() { RegexSettings = new List() { new RegexSetting() { Tag = "replaceTag", Regex = ".*" } } } }; _mockFileOperationsApiService.Setup(x => x.GetFileContent(It.IsAny())).ReturnsAsync(fileContent); var result = await _osgDnaNoticesIndexService.HandleIndexRequest(request); Assert.IsFalse(result.FatalFailure); Assert.AreEqual(2, result.ProcessingRequests.Count); _mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny()), Times.Once); } [Test] public async Task HandleIndexRequest_IngestorRequest_GetFileContent_ThrowsException_Failure() { var request = new IngestorRequest() { RootPath = "\\Test\\", IndexFileName = "PDFXREF.TXT", IngestorType = IngestorTypes.OsgDnaNoticesDocument }; _mockFileOperationsApiService.Setup(x => x.GetFileContent(It.IsAny())).ThrowsAsync(new Exception("Error")); var result = await _osgDnaNoticesIndexService.HandleIndexRequest(request); Assert.IsTrue(result.FatalFailure); _mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny()), Times.Once); } [Test] public void PrepareRunInfoAndProcessRequests_IngestorRunInfo_ProcessRequests_Created() { var request = new IngestorRequest() { RootPath = "\\Test\\", IndexFileName = "PDFXREF.TXT", IngestorType = IngestorTypes.OsgDnaNoticesDocument }; var documentMetaData = new List() { new OsgDnaNoticesDocument() { DocType = "TestDocType", AccountNumber = "1", MemberNumberOrUserId = "2", CreationDate = "20220823", StartingPage = "1", NumberOfPages = "3" } }; var ingestorResult = new IngestorResult(); var result = _osgDnaNoticesIndexService.PrepareRunInfoAndProcessRequests(request, documentMetaData, ingestorResult); Assert.IsFalse(result.FatalFailure); Assert.AreEqual(1, result.ProcessingRequests.Count); Assert.AreEqual("PDFXREF.TXT", result.IngestorRunInfo.IndexFileName); Assert.AreEqual(IngestorTypes.OsgDnaNoticesDocument, result.IngestorRunInfo.IngestorType); Assert.AreEqual("\\Test\\", result.IngestorRunInfo.IndexFilePath); } }