Files
monorepo/LCU.API.Internal.DocumentIngestor/LCU.API.Internal.DocumentIngestorTests/Services/IngestionOperationServices/PdfPartitionerCompletionServiceTest.cs
T
2026-03-25 14:50:13 -05:00

198 lines
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using LCU.Library.Internal.DocumentIngestorModels.Models;
using LCU.Library.Internal.LcuMassTransitModels;
using LCU.Library.Internal.DocumentIngestorModels.Entities;
using LCU.Library.Internal.DocumentIngestorModels.Enums;
using LCU.Library.Internal.Exceptions;
using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices;
using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices;
using LCU.API.Internal.DocumentIngestor.Configurations;
using LCU.API.Internal.DocumentIngestor.Services;
using LCU.API.Internal.DocumentIngestor.Services.QueueServices;
using LCU.API.Internal.DocumentIngestor.Services.PdfParserServices;
using LCU.Library.Internal.ApiResponse.Models;
using Newtonsoft.Json;
using LCU.Library.Internal.FileOperationsModels.Models.NameSettings;
namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices;
public class PdfPartitionerCompletionServiceTest
{
private PdfPartitionerCompletionService _pdfPartitionerCompletionService;
private Mock<IPdfPartitionerRunInfoService> _mockPdfPartitionerRunInfoService;
private Mock<IPdfPartitionerLogService> _mockPdfPartitionerLogService;
[SetUp]
public void Setup()
{
_mockPdfPartitionerRunInfoService = new Mock<IPdfPartitionerRunInfoService>();
_mockPdfPartitionerLogService = new Mock<IPdfPartitionerLogService>();
_pdfPartitionerCompletionService = new PdfPartitionerCompletionService(_mockPdfPartitionerRunInfoService.Object,
_mockPdfPartitionerLogService.Object);
}
[Test]
public void HandlePdfPartitionerCompletion_ArgumentNullException_ResponseNull()
{
Response response = null;
Assert.ThrowsAsync<ArgumentNullException>(async () => await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response));
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Never);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Never);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Never);
}
[Test]
public void HandlePdfPartitionerCompletion_ArgumentNullException_PdfPartitionerResultNull()
{
PdfPartitionerResult result = null;
var response = new Response
{
ResponsePayload = JsonConvert.SerializeObject(result)
};
Assert.ThrowsAsync<ArgumentNullException>(async () => await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response));
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Never);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Never);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Never);
}
[Test]
public void HandlePdfPartitionerCompletion_ServiceException_IncreaseCountsAndUpdate_ThrowException()
{
var pdfPartitionerRequest = new PdfPartitionerRequest
{
RootBasePath = "\\\\Tester\\path",
ParentDynamicDirectory = "TestParentDynamicDirectory",
PdfParserType = PdfParserTypes.FosLoanStatement,
DynamicIndexFileName = "TestDynamicIndexFileName.txt",
DynamicPdfName = "TestDynamicPdfname",
DynamicPdfSettings = new DynamicNameSetting()
};
var result = new PdfPartitionerResult(Guid.NewGuid(), pdfPartitionerRequest);
var response = new Response
{
ResponsePayload = JsonConvert.SerializeObject(result)
};
_mockPdfPartitionerRunInfoService.Setup(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>())).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response));
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Never);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Never);
}
[Test]
public void HandlePdfPartitionerCompletion_ServiceException_PdfPartitionerLogService_SaveListAsync_ThrowException()
{
var pdfPartitionerRequest = new PdfPartitionerRequest
{
RootBasePath = "\\\\Tester\\path",
ParentDynamicDirectory = "TestParentDynamicDirectory",
PdfParserType = PdfParserTypes.FosLoanStatement,
DynamicIndexFileName = "TestDynamicIndexFileName.txt",
DynamicPdfName = "TestDynamicPdfname",
DynamicPdfSettings = new DynamicNameSetting()
};
var result = new PdfPartitionerResult(Guid.NewGuid(), pdfPartitionerRequest);
var response = new Response
{
ResponsePayload = JsonConvert.SerializeObject(result)
};
var pdfPartitionerRunInfo = new PdfPartitionerRunInfo
{
PdfPartitionerRunInfoId = result.PdfPartitionerRunInfoId,
IndexTotal = 3,
SuccessCount = 1,
FailureCount = 1
};
_mockPdfPartitionerRunInfoService.Setup(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>())).ReturnsAsync(pdfPartitionerRunInfo);
_mockPdfPartitionerLogService.Setup(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>())).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response));
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Never);
}
[Test]
public void HandlePdfPartitionerCompletion_ServiceException_PdfPartitionerLogService_GetByPdfPartitionerRunInfoIdAsync_ThrowException()
{
var pdfPartitionerRequest = new PdfPartitionerRequest
{
RootBasePath = "\\\\Tester\\path",
ParentDynamicDirectory = "TestParentDynamicDirectory",
PdfParserType = PdfParserTypes.FosLoanStatement,
DynamicIndexFileName = "TestDynamicIndexFileName.txt",
DynamicPdfName = "TestDynamicPdfname",
DynamicPdfSettings = new DynamicNameSetting()
};
var result = new PdfPartitionerResult(Guid.NewGuid(), pdfPartitionerRequest);
var response = new Response
{
ResponsePayload = JsonConvert.SerializeObject(result)
};
var pdfPartitionerRunInfo = new PdfPartitionerRunInfo
{
PdfPartitionerRunInfoId = result.PdfPartitionerRunInfoId,
IndexTotal = 3,
SuccessCount = 2,
FailureCount = 1
};
_mockPdfPartitionerRunInfoService.Setup(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>())).ReturnsAsync(pdfPartitionerRunInfo);
_mockPdfPartitionerLogService.Setup(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()));
_mockPdfPartitionerLogService.Setup(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>())).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response));
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Once);
}
[Test]
public async Task HandlePdfPartitionerCompletion_Success()
{
var pdfPartitionerRequest = new PdfPartitionerRequest
{
RootBasePath = "\\\\Tester\\path",
ParentDynamicDirectory = "TestParentDynamicDirectory",
PdfParserType = PdfParserTypes.FosLoanStatement,
DynamicIndexFileName = "TestDynamicIndexFileName.txt",
DynamicPdfName = "TestDynamicPdfname",
DynamicPdfSettings = new DynamicNameSetting()
};
var result = new PdfPartitionerResult(Guid.NewGuid(), pdfPartitionerRequest);
result.FailedToQueueToParse = false;
var response = new Response
{
ResponsePayload = JsonConvert.SerializeObject(result),
ResponseStack = new List<string>
{
"Test ResponseStack"
}
};
var pdfPartitionerRunInfo = new PdfPartitionerRunInfo
{
PdfPartitionerRunInfoId = result.PdfPartitionerRunInfoId,
IndexTotal = 3,
SuccessCount = 2,
FailureCount = 1
};
_mockPdfPartitionerRunInfoService.Setup(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>())).ReturnsAsync(pdfPartitionerRunInfo);
_mockPdfPartitionerLogService.Setup(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()));
await _pdfPartitionerCompletionService.HandlePdfPartitionerCompletion(response);
_mockPdfPartitionerRunInfoService.Verify(x => x.IncreaseCountsAndUpdate(It.IsAny<Guid>(), It.IsAny<bool>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.SaveListAsync(It.IsAny<Guid>(), It.IsAny<List<string>>()), Times.Once);
_mockPdfPartitionerLogService.Verify(x => x.GetByPdfPartitionerRunInfoIdAsync(It.IsAny<Guid>()), Times.Once);
}
}