using System; using System.Collections.Generic; using System.Threading.Tasks; using NUnit.Framework; using Moq; using Newtonsoft.Json; using LCU.API.Internal.DocumentIngestor.Services.AttributeDeterminingServices; using LCU.API.Internal.DocumentIngestor.Services.DocumentProcessorServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.Library.DocumentType.Enums; using LCU.Library.DocumentType.Models.Requests.Account; using LCU.Library.DocumentType.Models.Requests.Attributes; using LCU.Library.DocumentType.Models.Requests.PrimaryOwner; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.DocumentIngestorModels.Models.IndexFileDocuments; using LCU.Library.Internal.LCUServiceFactory.Configurations; namespace LCU.API.Internal.DocumentIngestorTests.Services.DocumentProcessorServices; public class FosDnaDocumentProcessingServiceTest { private FosDnaDocumentProcessingService _fosDnaDocumentProcessingService; private Mock _mockPrimaryOwnerDeterminingService; private Mock _mockAccountOwnerDeterminingService; private Mock _mockFileOperationsApiService; private Mock _mockDocumentTypeUploaderApiService; [SetUp] public void Setup() { _mockPrimaryOwnerDeterminingService = new Mock(); _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 = "1" } } } } }; _fosDnaDocumentProcessingService = new FosDnaDocumentProcessingService(_mockPrimaryOwnerDeterminingService.Object, _mockAccountOwnerDeterminingService.Object, _mockFileOperationsApiService.Object, _mockDocumentTypeUploaderApiService.Object, serviceFactoryConfiguration); } [Test] public async Task ProcessRequest_ProcessingRequest_Null_ProcessResult_Failure() { ProcessingRequest request = null; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_Null_ProcessResult_Failure() { var request = new ProcessingRequest { DocumentValuePayload = null }; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_OneValidationFailures_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "0", CreationDate = "01232005", DocName = "Test.pdf", DocType = "LN_NENT", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument) }; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.AreEqual(result.ProcessStatusType, ProcessStatusTypes.DataMappingFailure); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_EntityUnknown_PsInfoChangeNotice_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "0", CreationDate = "01232005", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument) }; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.AreEqual(result.ProcessStatusType, ProcessStatusTypes.DataMappingFailure); Assert.IsTrue(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_ValidationFailures_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "0", CreationDate = "", DocName = "", DocType = "", EntityName = "" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument) }; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(3, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_NumberIsNull_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01232005", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument) }; var primaryOwnerAttribute = ErrorCaptureResult.CreateErrorPayload("Error", null); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_DataMappingFailure_CreationDate_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01T23T2005", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument) }; var primaryOwnerAttribute = ErrorCaptureResult.CreateErrorPayload("Error", null); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); Assert.AreEqual(ProcessStatusTypes.DataMappingFailure, result.ProcessStatusType); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocumentPrimaryOwner_NumberNotNull_Upload_Success() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "03072022", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new PrimaryOwnerAttribute { PersonNumber = "2" }; var primaryOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ReturnsAsync(content); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny())); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsTrue(result.Success); Assert.IsNull(result.EntityIsUnknown); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.Is(request => request.PrimaryOwnerDocumentMetadata.Source == SourceTypes.FOS && request.PrimaryOwnerDocumentMetadata.DataSource == DataSourceTypes.DNA), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosDnaDocumentAccountOwner_NumberNotNull_Upload_Success() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01232025", DocName = "Test.pdf", DocType = "LN_NENT", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), 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 _fosDnaDocumentProcessingService.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.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosDnaDocument_NumberNotNull_GetContentBytesThrowException_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01232005", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new PrimaryOwnerAttribute { PersonNumber = "2" }; var primaryOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("Error")); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosDnaDocument_NumberNotNull_UploadDocumentThrowException_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01232005", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new PrimaryOwnerAttribute { PersonNumber = "2" }; var primaryOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ReturnsAsync(content); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("Error")); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosDnaDocument_SkipAccountNumberPsInfoChangeNoticeOverride_Success() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "", CreationDate = "03072022", DocName = "Test.pdf", DocType = "PS_INFOCHANGE_NO", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf", OwnerOverrideInfo = new() { EntityType = EntityTypes.Person, EntityId = "123" } }; var attribute = new PrimaryOwnerAttribute { PersonNumber = "2" }; var primaryOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ReturnsAsync(content); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny())); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsTrue(result.Success); Assert.IsNull(result.EntityIsUnknown); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.Is(request => request.PrimaryOwnerDocumentMetadata.Source == SourceTypes.FOS && request.PrimaryOwnerDocumentMetadata.DataSource == DataSourceTypes.DNA), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosDnaDocument_SkipAccountNumberPsInfoChangeNoticeOverride_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "", CreationDate = "03072022", DocName = "Test.pdf", DocType = "LN_NENT", EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf", OwnerOverrideInfo = new() { EntityType = EntityTypes.Person, EntityId = "123" } }; var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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_FosDnaDocument_GetValueFromName_ExceptionThrown_Failure() { var fosDnaDocument = new FosDnaDocument { AccountNumber = "1", CreationDate = "01232005", DocName = "Test.pdf", DocType = Guid.NewGuid().ToString(), EntityName = "Bill B Billson" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosDnaDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new PrimaryOwnerAttribute { PersonNumber = "2" }; var primaryOwnerAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())).ReturnsAsync(content); var result = await _fosDnaDocumentProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(result.EntityIsUnknown); Assert.AreEqual(1, result.MessageStack.Count); _mockPrimaryOwnerDeterminingService.Verify(x => x.DeterminePrimaryOwnerAttribute(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } }