using System; using System.Collections.Generic; using System.Threading.Tasks; using NUnit.Framework; using Moq; using Newtonsoft.Json; using LCU.Library.DocumentType.Enums; using LCU.Library.DocumentType.Models.Requests.Account; using LCU.Library.DocumentType.Models.Requests.Attributes; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.DocumentIngestorModels.Models.IndexFileDocuments; using LCU.API.Internal.DocumentIngestor.Services.AttributeDeterminingServices; using LCU.API.Internal.DocumentIngestor.Services.DocumentProcessorServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.Library.Internal.LCUServiceFactory.Configurations; namespace LCU.API.Internal.DocumentIngestorTests.Services.DocumentProcessorServices; public class FosLoanStatementProcessingServiceTest { private FosLoanStatementProcessingService _fosLoanStatementProcessingService; private Mock _mockAccountOwnerDeterminingService; private Mock _mockFileOperationsApiService; private Mock _mockDocumentTypeUploaderApiService; [SetUp] public void Setup() { _mockAccountOwnerDeterminingService = new Mock(); _mockFileOperationsApiService = new Mock(); _mockDocumentTypeUploaderApiService = new Mock(); var serviceFactoryConfiguration = new ServicesFactoryConfiguration { ServiceFactoryRegistrations = new List { new() { ServiceInterfaceName = "IIndexParserService", ServiceRegistrations = new List { new() { ServiceName = "IndexParserService", ServiceId = "4" }, new() { ServiceName = "IndexParserService", ServiceId = "3" } } } } }; _fosLoanStatementProcessingService = new FosLoanStatementProcessingService(_mockAccountOwnerDeterminingService.Object, _mockFileOperationsApiService.Object, _mockDocumentTypeUploaderApiService.Object, serviceFactoryConfiguration); } [Test] public async Task ProcessRequest_ProcessingRequest_Null_ProcessResult_Failure() { ProcessingRequest request = null; var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_Null_ProcessResult_Failure() { var request = new ProcessingRequest { DocumentValuePayload = null }; var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_OneValidationFailures_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "0", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement) }; var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_FiveValidationFailures_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "0", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement) }; var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_NumberIsNull_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "111111", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement) }; AccountAttribute attribute = null; var accountAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_NumberNotNull_Upload_Success() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "111111", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); byte[] content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())) .ReturnsAsync(content); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny())); var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsTrue(result.Success); Assert.IsNull(result.EntityIsUnknown); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Once); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.Is(request => request.AccountDocumentMetadata.Source == SourceTypes.FOS && request.AccountDocumentMetadata.DataSource == DataSourceTypes.DNA), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosLoanStatement_NumberNotNull_GetContentBtyesThrowException_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "111111", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error")); var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Once); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosLoanStatement_NumberNotNull_UploadDocumentThrowException_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "111111", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = "Mortgage Statement" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); byte[] content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ReturnsAsync(content); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("Error")); var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Once); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosLoanStatement_GetValueFromName_ExceptionThrown_Failure() { var FosLoanStatement = new FosLoanStatement { AccountNumber = "111111", CreationDate = "20230401", DocName = "test.pdf", EmailAddress = "test@test.com", EntityName = "Test Tester", StartingPage = "2", NumberOfPages = "3", DocType = Guid.NewGuid().ToString() }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(FosLoanStatement), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); byte[] content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())) .ReturnsAsync(content); var result = await _fosLoanStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockAccountOwnerDeterminingService.Verify(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadAccountDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } }