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.Attributes; using LCU.Library.DocumentType.Models.Requests.PrimaryOwner; using LCU.Library.DocumentType.Models.Requests.Generic; using LCU.Library.Internal.DocumentIngestorModels.Entities; 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 FosTaxStatementProcessingServiceTest { private FosTaxStatementProcessingService _fosTaxStatementProcessingService; private Mock _mockPrimaryOwnerDeterminingService; private Mock _mockFileOperationsApiService; private Mock _mockDocumentTypeUploaderApiService; [SetUp] public void Setup() { _mockPrimaryOwnerDeterminingService = 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 = "0" } } } } }; _fosTaxStatementProcessingService = new FosTaxStatementProcessingService(_mockPrimaryOwnerDeterminingService.Object, _mockFileOperationsApiService.Object, _mockDocumentTypeUploaderApiService.Object, serviceFactoryConfiguration); } [Test] public async Task ProcessRequest_ProcessingRequest_Null_ProcessResult_Failure() { ProcessingRequest request = null; var result = await _fosTaxStatementProcessingService.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); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_Null_ProcessResult_Failure() { var request = new ProcessingRequest { DocumentValuePayload = null }; var result = await _fosTaxStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsFalse(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.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_OneValidationFailures_Failure() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "0", FileName = "test", FullName = "test", PDF = "test", PrimaryTIN = "1" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument) }; var result = await _fosTaxStatementProcessingService.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.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); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_FiveValidationFailures_Failure() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "0", FileName = "", FullName = "", PDF = "", PrimaryTIN = "" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument) }; var result = await _fosTaxStatementProcessingService.ProcessRequest(request); Assert.IsFalse(result.Success); Assert.IsNull(result.EntityIsUnknown); Assert.AreEqual(5, 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); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_NumberIsNull_Failure() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-INT", FullName = "test", PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument) }; var primaryOwnerAttribute = ErrorCaptureResult.CreateErrorPayload("Error from unit test", null); _mockPrimaryOwnerDeterminingService.Setup(x => x.DeterminePrimaryOwnerAttribute(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(primaryOwnerAttribute); var result = await _fosTaxStatementProcessingService.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); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_NumberNotNull_Upload_Success() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-INT", FullName = "test", PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument), 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 _fosTaxStatementProcessingService.ProcessRequest(request); Assert.IsTrue(result.Success); _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_FosTaxStatementDocument_GenericDocument_NumberNotNull_Upload_Success() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-MISC", FullName = "test", PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument), IngestorRequest = new IngestorRequest { RootPath = "\\Tester\\" }, DocumentFileName = "test.pdf" }; var attribute = new GenericDocumentAttribute { EntityName = "test" }; var genericAttribute = ErrorCaptureResult.CreateSuccessPayload(attribute); var content = new byte[1024]; _mockFileOperationsApiService.Setup(x => x.GetContentBytes(It.IsAny(), It.IsAny())) .ReturnsAsync(content); var documentTypeArchiveDeleteInfo = new DocumentTypeArchiveDeleteInfo { DocumentTypeName = "OneThousandNinetyNineMiscelanneous", ArchiveDays = 2000, DeleteDays = 4000 }; _mockDocumentTypeUploaderApiService.Setup(x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny())); var result = await _fosTaxStatementProcessingService.ProcessRequest(request); Assert.IsTrue(result.Success); Assert.IsNull(result.EntityIsUnknown); _mockFileOperationsApiService.Verify(x => x.GetContentBytes(It.IsAny(), It.IsAny()), Times.Once); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.Is(request => request.GenericDocumentMetadata.Source == SourceTypes.FOS && request.GenericDocumentMetadata.DataSource == DataSourceTypes.DNA), It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_NumberNotNull_GetContentBytesThrowException_Failure() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-INT", FullName = "test", PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument), 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 _fosTaxStatementProcessingService.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); _mockDocumentTypeUploaderApiService.Verify( x => x.UploadGenericDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task ProcessRequest_FosTaxStatementDocument_NumberNotNull_UploadDocumentThrowException_Failure() { var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-INT", FullName = "test", PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument), 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 _fosTaxStatementProcessingService.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_FosTaxStatementDocument_GetCreatedDateFromDocumentType_Success() { var createdDate = DateTime.Now.Date; var fosTaxStatementDocument = new FosTaxStatementDocument { AccountNumber = "1", FileName = "1099-INT", FullName = "test", TaxYear = 2021, PDF = "test", PrimaryTIN = "11" }; var request = new ProcessingRequest { DocumentValuePayload = JsonConvert.SerializeObject(fosTaxStatementDocument), 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 builtRequest = await _fosTaxStatementProcessingService.BuildUploadRequest(request, fosTaxStatementDocument, attribute); Assert.AreEqual(createdDate, builtRequest.PrimaryOwnerDocumentMetadata.CreatedDate); _mockDocumentTypeUploaderApiService.Setup(x => x.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny())); var result = await _fosTaxStatementProcessingService.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); _mockDocumentTypeUploaderApiService.Verify(x => x.UploadPrimaryOwnerDocument(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } }