using System; using System.Collections.Generic; using System.Threading.Tasks; using Moq; using NUnit.Framework; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.API.Internal.DocumentIngestor.Services; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.FileOperationsModels.Models; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class IngestionFolderCleanUpTest { private IngestionFolderCleanUpService _ingestionFolderCleanUpService; private Mock _mockFileOperationsApiService; private Mock _mockIngestorRunInfoService; private Mock _mockDocumentProcessingInfoService; [SetUp] public void Setup() { _mockFileOperationsApiService = new Mock(); _mockIngestorRunInfoService = new Mock(); _mockDocumentProcessingInfoService = new Mock(); _ingestionFolderCleanUpService = new IngestionFolderCleanUpService ( _mockFileOperationsApiService.Object, _mockIngestorRunInfoService.Object, _mockDocumentProcessingInfoService.Object ); } [Test] public async Task HandleIngestionFolderCleanUp_Success() { var now = DateTime.Now.ToString("yyyyMMdd"); // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{now}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path2\\{now}\\Path2\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path3\\{now}\\Path3\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 29), EndTimeStamp = new DateTime(2023, 03, 29), ArchiveTimeStamp = null, } }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; var successfulRequestResponse = new RequestResponse() { IsSuccessful = true, MessageStack = null, ReturnResponse = null }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockIngestorRunInfoService.Setup(x => x.Update(It.IsAny())).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ReturnsAsync(new List() { "" }).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny())).ReturnsAsync(successfulRequestResponse).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(successfulRequestResponse).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); } [Test] public async Task HandleIngestionFolderCleanUp_DocumentProcessingInfoCount_IndexTotal_Mismatch() { // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{DateTime.Now:yyyyMMdd}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, } }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_ArchivedFileCount_IndexTotal_Mismatch() { // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{DateTime.Now:yyyyMMdd}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf" }; var successfulRequestResponse = new RequestResponse() { IsSuccessful = true, MessageStack = null, ReturnResponse = null }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(5).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_ZipFile_NotFound() { // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{DateTime.Now.ToString("yyyyMMdd")}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ReturnsAsync(new List()).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_CreateArchiveDirectory_Exception() { var now = DateTime.Now.ToString("yyyyMMdd"); // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = $"\\\\zeus\\some\\path\\{now}\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{now}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path2\\{now}\\Path2\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; var successfulRequestResponse = new RequestResponse() { IsSuccessful = true, MessageStack = null, ReturnResponse = null }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ReturnsAsync(new List() { "" }).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny())).ThrowsAsync(new ServiceException("Exception from test")).Verifiable(); // Act Assert.ThrowsAsync(async () => await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request)); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Once); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Once); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_MoveZipFile_Exception() { var now = DateTime.Now.ToString("yyyyMMdd"); // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{now}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path2\\{now}\\Path2\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path3\\{now}\\Path3\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 29), EndTimeStamp = new DateTime(2023, 03, 29), ArchiveTimeStamp = null, } }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; var successfulRequestResponse = new RequestResponse() { IsSuccessful = true, MessageStack = null, ReturnResponse = null }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockIngestorRunInfoService.Setup(x => x.Update(It.IsAny())).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ReturnsAsync(new List() { "" }).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny())).ReturnsAsync(successfulRequestResponse).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(successfulRequestResponse).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny())).ThrowsAsync(new ServiceException("Exception from test")).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); } [Test] public async Task HandleIngestionFolderCleanUp_BuildDynamicFileList_Failure() { var now = DateTime.Now.ToString("yyyyMMdd"); // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{now}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path2\\{now}\\Path2\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path3\\{now}\\Path3\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 29), EndTimeStamp = new DateTime(2023, 03, 29), ArchiveTimeStamp = null, } }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockIngestorRunInfoService.Setup(x => x.Update(It.IsAny())).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ThrowsAsync(new ServiceException("Exception from test")).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_IncorrectFilePathFormat_Failure() { // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = "Path1", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = "Path2", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = "Path3", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 29), EndTimeStamp = new DateTime(2023, 03, 29), ArchiveTimeStamp = null, } }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockIngestorRunInfoService.Setup(x => x.Update(It.IsAny())).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Never); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleIngestionFolderCleanUp_DeleteUnzippedFolder_Exception() { var now = DateTime.Now.ToString("yyyyMMdd"); // Arrange var request = new IngestionFolderCleanUpRequest() { ArchiveDate = new DateTime(2023, 03, 30), ArchiveRootPath = "\\\\zeus\\some\\path\\", IngestionEndedDate = new DateTime(2023, 3, 30) }; var ingestorRunInfos = new List() { new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path1\\{now}\\Path1\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 27), EndTimeStamp = new DateTime(2023, 03, 27), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path2\\{now}\\Path2\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 28), EndTimeStamp = new DateTime(2023, 03, 28), ArchiveTimeStamp = null, }, new () { IngestorRunInfoId = Guid.NewGuid(), IngestorType = IngestorTypes.FosDnaDocument, IndexTotal = 5, IndexFilePath = $"Path3\\{now}\\Path3\\", IndexFileName = "Filename", StartTimeStamp = new DateTime(2023, 03, 29), EndTimeStamp = new DateTime(2023, 03, 29), ArchiveTimeStamp = null, } }; var docProcessingInfoFiles = new List() { "Filename1.pdf", "Filename2.pdf", "Filename3.pdf", "Filename4.pdf", "Filename5.pdf" }; var successfulRequestResponse = new RequestResponse() { IsSuccessful = true, MessageStack = null, ReturnResponse = null }; _mockIngestorRunInfoService.Setup(x => x.FindByIngestionEndDateNotArchived(It.IsAny())) .Returns(ingestorRunInfos).Verifiable(); _mockIngestorRunInfoService.Setup(x => x.Update(It.IsAny())).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(docProcessingInfoFiles.Count).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}")))).ReturnsAsync(docProcessingInfoFiles).Verifiable(); _mockFileOperationsApiService.Setup(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}")))).ReturnsAsync(new List() { "" }).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny())).ReturnsAsync(successfulRequestResponse).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny())).ThrowsAsync(new ServiceException("Exception from test")).Verifiable(); _mockFileOperationsApiService.Setup(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // Act await _ingestionFolderCleanUpService.HandleIngestionFolderCleanUp(request); // Assert _mockIngestorRunInfoService.Verify(x => x.FindByIngestionEndDateNotArchived(It.IsAny()), Times.Once); _mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockDocumentProcessingInfoService.Verify(x => x.CountCompletedByIngestorRunInfoIdAsync(It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ArchiveFiles}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.BuildDynamicFileList(It.Is(d => d.Filename.Equals("{ZipFileRegex}"))), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectCreateDirectory(It.IsAny(), It.IsAny()), Times.Once); _mockFileOperationsApiService.Verify(x => x.DirectDeleteDirectory(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); _mockFileOperationsApiService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(ingestorRunInfos.Count)); } }