using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; 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.DocumentIngestorModels.Enums; using LCU.Library.Internal.LCUServiceFactory.Configurations; namespace LCU.API.Internal.DocumentIngestorTests.Services.DocumentProcessorServices; public class XdiDnaNoticeDocumentProcessingServiceTest { private XdiDnaNoticeDocumentProcessingService _xdiDnaNoticeDocumentProcessingService; 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 = "2" } } } } }; _xdiDnaNoticeDocumentProcessingService = new XdiDnaNoticeDocumentProcessingService(_mockAccountOwnerDeterminingService.Object, _mockFileOperationsApiService.Object, _mockDocumentTypeUploaderApiService.Object, serviceFactoryConfiguration); } [Test] public async Task ProcessRequest_XdiDnaNoticeDocumentAccountOwner_NumberNotNull_Upload_Success() { var xdiDnaDocument = new XdiDnaNoticesDocument { AccountNumber = "1", CreationDate = "01/23/2025", PDF = "Test.pdf", DocumentType = "DNA_22 Day Past Due Letter", PageCount = 2, EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(xdiDnaDocument), 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); var 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 _xdiDnaNoticeDocumentProcessingService.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.XDI && request.AccountDocumentMetadata.DataSource == DataSourceTypes.DNA), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_XdiDnaNoticeDocument_AccountOwnerNotDetermined_Failure() { var xdiDnaNoticeDocument = new XdiDnaNoticesDocument { AccountNumber = "1", CreationDate = "01/23/2025", PDF = "Test.pdf", DocumentType = "DNA_32 Day Past Due Letter", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(xdiDnaNoticeDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountAttribute = ErrorCaptureResult.CreateErrorPayload("Error from test", attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountAttribute); var result = await _xdiDnaNoticeDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); Assert.AreEqual(ProcessStatusTypes.DataMappingFailure, result.ProcessStatusType); _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_XdiDnaNoticeDocument_NumberNotNull_GetContentBytesThrowException_Failure() { var xdiDnaNoticeDocument = new XdiDnaNoticesDocument { AccountNumber = "1", CreationDate = "01/23/2025", PDF = "Test.pdf", DocumentType = "DNA_32 Day Past Due Letter", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(xdiDnaNoticeDocument), 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 _xdiDnaNoticeDocumentProcessingService.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_XdiDnaNoticeDocument_NumberNotNull_UploadDocumentThrowException_Failure() { var xdiDnaNoticeDocument = new XdiDnaNoticesDocument { AccountNumber = "1", CreationDate = "01/23/2025", PDF = "Test.pdf", DocumentType = "ThirtyTwoDayPastDueLetter", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(xdiDnaNoticeDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new AccountAttribute { PersonNumbers = new List { "2", "3", "4" } }; var accountOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockAccountOwnerDeterminingService.Setup(x => x.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(accountOwnerAttribute); var 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 _xdiDnaNoticeDocumentProcessingService.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_XdiDnaNoticeDocument_GetValueFromName_ExceptionThrown_Failure() { var xdiDnaDocument = new XdiDnaNoticesDocument { AccountNumber = "1", CreationDate = "01/23/2005", PDF = "Test.pdf", DocumentType = "DNA_32 Day Past Due Letter Incorrect", EntityName = "Test Entity" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(xdiDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var result = await _xdiDnaNoticeDocumentProcessingService.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); } [Test] public async Task ProcessRequest_ValidDate_ParsesDateCorrectly() { var date = DateTime.Now; DateTime? parsedCreationTime = null; var request = new ProcessingRequest { DocumentValuePayload = @"{ ""CreationDate"":""" + $"{date:MM/dd/yyyy}" + @""", ""AccountNumber"":""10"", ""LoanNumber"":""1"", ""EntityName"":""1"", ""PageCount"":1, ""DocumentType"":""" + typeof(AccountDocumentTypes).GetFields().First(f => f.GetCustomAttribute() is not null).GetCustomAttribute().Name + @""", ""PDF"":""1"" }" }; _mockAccountOwnerDeterminingService .Setup(aods => aods.DetermineAccountOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Callback((_, _, creationTime, _) => parsedCreationTime = creationTime); await _xdiDnaNoticeDocumentProcessingService.ProcessRequest(request); Assert.IsNotNull(parsedCreationTime); Assert.AreEqual(date.Month, parsedCreationTime.Value.Month); Assert.AreEqual(date.Day, parsedCreationTime.Value.Day); Assert.AreEqual(date.Year, parsedCreationTime.Value.Year); } }