Files
2026-03-25 14:50:13 -05:00

153 lines
4.8 KiB
C#

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 OsgHistoricalDocumentIndexServiceTest
{
private OsgHistoricalDocumentIndexService _osgHistoricalDocumentIndexService;
private Mock<IFileOperationsApiService> _mockFileOperationsApiService;
[SetUp]
public void Setup()
{
_mockFileOperationsApiService = new Mock<IFileOperationsApiService>();
var serviceFactoryConfiguration = new ServicesFactoryConfiguration
{
ServiceFactoryRegistrations = new List<ServiceFactoryRegistration>
{
new()
{
ServiceInterfaceName = "IIndexParserService",
ServiceRegistrations = new List<ServiceRegistration>
{
new()
{
ServiceName = "IndexParserService",
ServiceId = "4"
},
new()
{
ServiceName = "IndexParserService",
ServiceId = "6"
}
}
}
}
};
_osgHistoricalDocumentIndexService = new OsgHistoricalDocumentIndexService(_mockFileOperationsApiService.Object,
serviceFactoryConfiguration);
}
[Test]
public async Task HandleIndexRequest_IngestorRequest_Null_IngestorResult_Failure()
{
IngestorRequest request = null;
var result = await _osgHistoricalDocumentIndexService.HandleIndexRequest(request);
Assert.IsTrue(result.FatalFailure);
_mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny<string>()), 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<RegexSetting>()
{
new RegexSetting()
{
Tag = "replaceTag",
Regex = ".*"
}
}
}
};
_mockFileOperationsApiService.Setup(x => x.GetFileContent(It.IsAny<string>())).ReturnsAsync(fileContent);
var result = await _osgHistoricalDocumentIndexService.HandleIndexRequest(request);
Assert.IsFalse(result.FatalFailure);
Assert.AreEqual(2, result.ProcessingRequests.Count);
_mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny<string>()), Times.Once);
}
[Test]
public async Task HandleIndexRequest_IngestorRequest_GetFileContent_ThrowsException_Failure()
{
var request = new IngestorRequest()
{
RootPath = "\\Test\\",
IndexFileName = "PDFXREF.TXT",
IngestorType = IngestorTypes.OsgHistoricalDocument
};
_mockFileOperationsApiService.Setup(x => x.GetFileContent(It.IsAny<string>())).ThrowsAsync(new Exception("Error"));
var result = await _osgHistoricalDocumentIndexService.HandleIndexRequest(request);
Assert.IsTrue(result.FatalFailure);
_mockFileOperationsApiService.Verify(x => x.GetFileContent(It.IsAny<string>()), Times.Once);
}
[Test]
public void PrepareRunInfoAndProcessRequests_IngestorRunInfo_ProcessRequests_Created()
{
var request = new IngestorRequest()
{
RootPath = "\\Test\\",
IndexFileName = "PDFXREF.TXT",
IngestorType = IngestorTypes.OsgHistoricalDocument
};
var documentMetaData = new List<OsgHistoricalDocument>()
{
new OsgHistoricalDocument()
{
DocType = "TestDocType",
EntityName = "John Jim",
MemberNumberOrUserId = "2",
CreationDate = "20220823",
StartingPage = "1",
NumberOfPages = "3"
}
};
var ingestorResult = new IngestorResult();
var result = _osgHistoricalDocumentIndexService.PrepareRunInfoAndProcessRequests(request, documentMetaData, ingestorResult);
Assert.IsFalse(result.FatalFailure);
Assert.AreEqual(1, result.ProcessingRequests.Count);
Assert.AreEqual("PDFXREF.TXT", result.IngestorRunInfo.IndexFileName);
Assert.AreEqual(IngestorTypes.OsgHistoricalDocument, result.IngestorRunInfo.IngestorType);
Assert.AreEqual("\\Test\\", result.IngestorRunInfo.IndexFilePath);
}
}