using System; using Moq; using NUnit.Framework; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.Api.Internal.DispatcherSharedLib.Utilities; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.API.Internal.DocumentIngestor.Configurations; using LCU.Library.Internal.DispatcherModels.Models; using LCU.Library.Internal.Exceptions; using System.Collections.Generic; using System.Threading.Tasks; using LCU.Library.Internal.DispatcherModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Api.Internal.DispatcherSharedLib.BuilderServices; using LCU.Library.Internal.DispatcherModels.Enums; using LCU.Library.Internal.FileOperationsModels.Models; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class BatchIngestionServiceTest { private BatchIngestionService _batchIngestionService; private ApiConfiguration _apiConfiguration; private DispatcherJobCreationConfiguration _dispatcherJobCreationConfiguration; private Mock _mockFileOperationsApiService; private Mock _mockDispatcherRequestBuilder; [SetUp] public void Setup() { _apiConfiguration = new ApiConfiguration { TimeZone = "America/Chicago" }; _dispatcherJobCreationConfiguration = new DispatcherJobCreationConfiguration { DocumentIngestorDispatcherErrorRecipients = new List { "test@landmarkcu.com" }, JobTags = new List { new JobTag { JobTagId = 0, JobId = 0, TagId = 1 } } }; _mockFileOperationsApiService = new Mock(); _mockDispatcherRequestBuilder = new Mock(); _batchIngestionService = new BatchIngestionService ( _apiConfiguration, _dispatcherJobCreationConfiguration, _mockFileOperationsApiService.Object, _mockDispatcherRequestBuilder.Object ); } [Test] public void HandleBatchIngestion_ArgumentNullException_RequestNull() { BatchIngestionRequest request = null; Assert.ThrowsAsync(async () => await _batchIngestionService.HandleBatchIngestion(request)); _mockDispatcherRequestBuilder.Verify(x => x.CreateJob(It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicDirectoryList(It.IsAny()), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.CreateDirectory), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.IsAny()), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.DocumentIngestor), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.SendDispatcherRequest(null), Times.Never); } [Test] public void HandleBatchIngestion_ServiceException_BuildDynamicDirectoryList_ThrowsException() { var request = new BatchIngestionRequest { RootBasePath = "\\\\Tester\\path", ParentDynamicDirectory = "TestParentDynamicDirectory", IngestorType = IngestorTypes.FosTaxStatement }; _mockFileOperationsApiService.Setup(x => x.BuildDynamicDirectoryList(It.IsAny())).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _batchIngestionService.HandleBatchIngestion(request)); _mockDispatcherRequestBuilder.Verify(x => x.CreateJob(It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicDirectoryList(It.IsAny()), Times.Once); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.CreateDirectory), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.IsAny()), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.DocumentIngestor), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.SendDispatcherRequest(null), Times.Never); } [Test] public async Task HandleBatchIngestion_BuildDynamicFileList_ThrowsException() { var request = new BatchIngestionRequest { RootBasePath = "\\\\Tester\\path", ParentDynamicDirectory = "TestParentDynamicDirectory", IngestorType = IngestorTypes.FosTaxStatement }; var dynamicDirectoryListResult = new List { "TestParentDynamicDirectory1", "TestParentDynamicDirectory2" }; _mockFileOperationsApiService.Setup(x => x.BuildDynamicDirectoryList(It.IsAny())).ReturnsAsync(dynamicDirectoryListResult); _mockDispatcherRequestBuilder.Setup(x => x.CreateJob(It.IsAny())); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFile(It.IsAny())).ThrowsAsync(new Exception("Error")); await _batchIngestionService.HandleBatchIngestion(request); _mockFileOperationsApiService.Verify(x => x.BuildDynamicDirectoryList(It.IsAny()), Times.Once); _mockDispatcherRequestBuilder.Verify(x => x.CreateJob(It.IsAny()), Times.Exactly(2)); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.CreateDirectory), Times.Exactly(2)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFile(It.IsAny()), Times.Exactly(2)); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.DocumentIngestor), Times.Never); _mockDispatcherRequestBuilder.Verify(x => x.SendDispatcherRequest(null), Times.Never); } [Test] public async Task HandleBatchIngestion_Success() { var request = new BatchIngestionRequest { RootBasePath = "\\\\Tester\\path", ParentDynamicDirectory = "TestParentDynamicDirectory", DynamicIndexFileName = "TestIndexFile", IngestorType = IngestorTypes.FosTaxStatement, IngestionDate = Convert.ToDateTime("1/1/2022") }; var dynamicDirectoryListResult = new List { "TestParentDynamicDirectory1", "TestParentDynamicDirectory2" }; _mockFileOperationsApiService.Setup(x => x.BuildDynamicDirectoryList(It.IsAny())).ReturnsAsync(dynamicDirectoryListResult); _mockDispatcherRequestBuilder.Setup(x => x.CreateJob(It.IsAny())); var dynamicFileListRequest1 = new DynamicFileListRequest { Path = "\\\\Tester\\path\\01012022\\TestParentDynamicDirectory1\\", Filename = "TestIndexFile", DynamicFileSettings = null }; var dynamicFileResult1 = "TestIndexFile1"; _mockFileOperationsApiService.Setup(x => x.BuildDynamicFile(It.Is(x => x.Path == dynamicFileListRequest1.Path))).ReturnsAsync(dynamicFileResult1); var dynamicFileListRequest2 = new DynamicFileListRequest { Path = "\\\\Tester\\path\\01012022\\TestParentDynamicDirectory2\\", Filename = "TestIndexFile", DynamicFileSettings = null }; var dynamicFileResult2 = "TestIndexFile2"; _mockFileOperationsApiService.Setup(x => x.BuildDynamicFile(It.Is(x => x.Path == dynamicFileListRequest2.Path))).ReturnsAsync(dynamicFileResult2); await _batchIngestionService.HandleBatchIngestion(request); _mockFileOperationsApiService.Verify(x => x.BuildDynamicDirectoryList(It.IsAny()), Times.Once); _mockDispatcherRequestBuilder.Verify(x => x.CreateJob(It.IsAny()), Times.Exactly(2)); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.CreateDirectory), Times.Exactly(2)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFile(It.IsAny()), Times.Exactly(2)); _mockDispatcherRequestBuilder.Verify(x => x.AddWorkItem(It.IsAny(), It.IsAny(), ActionType.DocumentIngestor), Times.Exactly(2)); _mockDispatcherRequestBuilder.Verify(x => x.SendDispatcherRequest(null), Times.Exactly(2)); } }