using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using Moq; using NUnit.Framework; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.Exceptions; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestorLib.Repositories; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.DocumentIngestorModels.Models.Pagination; namespace LCU.API.Internal.DocumentIngestorTests.Services; public class DocumentProcessingInfoServiceTest { private DocumentProcessingInfoService _documentProcessingInfoService; private Mock _unitOfWork; private Mock _mockDocumentProcessingInfoRepository; private Mock _mockProcessingLogService; [SetUp] public void Setup() { _unitOfWork = new Mock(); _mockDocumentProcessingInfoRepository = new Mock(); _mockProcessingLogService = new Mock(); _documentProcessingInfoService = new DocumentProcessingInfoService(_unitOfWork.Object, _mockDocumentProcessingInfoRepository.Object, _mockProcessingLogService.Object); } [Test] public async Task GetAsync_DocumentProcessingInfos_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfos = new List() { new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId } }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindAsync()).ReturnsAsync(documentProcessingInfos); var result = await _documentProcessingInfoService.GetAsync(); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockDocumentProcessingInfoRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public void GetAsync_Exception() { _mockDocumentProcessingInfoRepository.Setup(x => x.FindAsync()).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.GetAsync()); _mockDocumentProcessingInfoRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public async Task GetByIdAsync_DocumentProcessingInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); var result = await _documentProcessingInfoService.GetByIdAsync(id); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByIdAsync_Exception() { var id = 1; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.GetByIdAsync(id)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public async Task GetByIngestorRunInfoIdAsync_DocumentProcessingInfos_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfos = new List() { new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId } }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIngestorRunInfoIdAsync(ingestorRunInfoId)).ReturnsAsync(documentProcessingInfos); var result = await _documentProcessingInfoService.GetByIngestorRunInfoIdAsync(ingestorRunInfoId); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByIngestorRunInfoIdAsync_Exception() { var ingestorRunInfoId = Guid.NewGuid(); _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIngestorRunInfoIdAsync(ingestorRunInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.GetByIngestorRunInfoIdAsync(ingestorRunInfoId)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public async Task SaveAsync_DocumentProcessingInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.AddAsync(documentProcessingInfo)).ReturnsAsync(documentProcessingInfo); var result = await _documentProcessingInfoService.SaveAsync(documentProcessingInfo); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public void SaveAsync_Exception() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.AddAsync(documentProcessingInfo)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.SaveAsync(documentProcessingInfo)); _mockDocumentProcessingInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public async Task Update_DocumentProcessingInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)); var result = await _documentProcessingInfoService.Update(documentProcessingInfo); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public void Update_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.Update(documentProcessingInfo)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); } [Test] public void Update_Update_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.Update(documentProcessingInfo)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public async Task UpdateStatusAndAddLog_ById_SingleLog_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)); var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail", UserName = "User" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); var result = await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, "Account fail", "User"); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); } [Test] public void UpdateStatusAndAddLog_ById_SingleLog_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, "Account fail", "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public void UpdateStatusAndAddLog_ById_SingleLog_Update_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)).Throws(new Exception("Error")); ; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, "Account fail", "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task UpdateStatusAndAddLog_ById_TwoLog_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)); var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail", UserName = "User" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); var logMessages = new List() { "Account fail", "Name fail" }; var result = await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, logMessages, "User"); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Exactly(2)); } [Test] public void UpdateStatusAndAddLog_ById_TwoLog_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); var logMessages = new List() { "Account fail", "Name fail" }; Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, logMessages, "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public void UpdateStatusAndAddLog_ById_TwoLog_Update_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)).Throws(new Exception("Error")); ; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); var logMessages = new List() { "Account fail", "Name fail" }; Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(id, ProcessStatusTypes.DataMappingFailure, logMessages, "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task UpdateStatusAndAddLog_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)); var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail", UserName = "User" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); var result = await _documentProcessingInfoService.UpdateStatusAndAddLog(documentProcessingInfo, ProcessStatusTypes.DataMappingFailure, "Account fail", "User"); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); Assert.AreEqual(id, result.DocumentProcessingInfoId); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); } [Test] public void UpdateStatusAndAddLog_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(documentProcessingInfo, ProcessStatusTypes.DataMappingFailure, "Account fail", "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public void UpdateStatusAndAddLog_Update_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(documentProcessingInfo)).Throws(new Exception("Error")); ; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = id, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Account fail", UserName = "User" }; _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(processingLog); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateStatusAndAddLog(documentProcessingInfo, ProcessStatusTypes.DataMappingFailure, "Account fail", "User")); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(id), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(documentProcessingInfo), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task Delete_Success() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Remove(documentProcessingInfo)); await _documentProcessingInfoService.Delete(id); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } [Test] public void Delete_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.Delete(id)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); } [Test] public void Delete_Remove_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var id = 1; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = id, IngestorRunInfoId = ingestorRunInfoId }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Remove(documentProcessingInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.Delete(id)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } [Test] public async Task FilterPaged_Ok() { var pagedDocumentProcessingInfoResult = new PagedResult(); var filter = new Filter() { Page = 1, Limit = 2, StatusFilters = null, SortableFields = null, DocBusinessName = "EAgreement" }; _mockDocumentProcessingInfoRepository.Setup(repo => repo.FilteredListAsync(filter)) .ReturnsAsync(pagedDocumentProcessingInfoResult).Verifiable(); // Act var result = await _documentProcessingInfoService.GetFilteredDocumentProcessingInfo(filter); // Assert Assert.NotNull(result); Assert.AreEqual(pagedDocumentProcessingInfoResult, result); } [Test] public async Task CountCompletedFirstPassByIngestorRunInfoIdAsync_RepositoryReturnsCount_ReturnsCount() { var runId = Guid.NewGuid(); var count = DateTime.Now.Millisecond; _mockDocumentProcessingInfoRepository .Setup(r => r.CountCompletedFirstPassByIngestorRunInfoIdAsync(runId)) .ReturnsAsync(count); var actualCount = await _documentProcessingInfoService.CountCompletedFirstPassByIngestorRunInfoIdAsync(runId); Assert.AreEqual(count, actualCount); } [Test] public void CountCompletedFirstPassByIngestorRunInfoIdAsync_RepositoryThrows_ThrowsServiceException() { var runId = Guid.NewGuid(); _mockDocumentProcessingInfoRepository .Setup(r => r.CountCompletedFirstPassByIngestorRunInfoIdAsync(runId)) .ThrowsAsync(new Exception()); Assert.ThrowsAsync(() => _documentProcessingInfoService.CountCompletedFirstPassByIngestorRunInfoIdAsync(runId)); } [Test] public async Task SetDocumentsToEntityUnknown_Success() { var ids = new List() { 1, 2, 3 }; var documentProcessingInfos = new List() { new() { DocumentProcessingInfoId = ids[0], }, new() { DocumentProcessingInfoId = ids[1], }, new() { DocumentProcessingInfoId = ids[2], } }; var request = new EntityUnknownRequest() { UserName = "System", DocumentProcessingInfoIds = documentProcessingInfos.Select(x => x.DocumentProcessingInfoId).ToList() }; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = 1, LogMessage = "Log test", LogTimeStamp = DateTime.Now, UserName = "System" }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindListByIdAsync(ids)).ReturnsAsync(documentProcessingInfos); _mockDocumentProcessingInfoRepository.SetupSequence(x => x.FindByIdAsync(It.IsAny())).ReturnsAsync(documentProcessingInfos[0]) .ReturnsAsync(documentProcessingInfos[1]) .ReturnsAsync(documentProcessingInfos[2]); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny())); _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(() => processingLog); var result = await _documentProcessingInfoService.SetDocumentsToEntityUnknown(request); var trueCount = result.Where(x => x.EntityUnknown).ToList().Count(); Assert.IsNotNull(result); Assert.AreEqual(documentProcessingInfos.Count, result.Count()); Assert.AreEqual(documentProcessingInfos.Count(), trueCount); _mockDocumentProcessingInfoRepository.Verify(x => x.FindListByIdAsync(It.IsAny>()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Exactly(3)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Exactly(3)); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Exactly(3)); } [Test] public async Task SetDocumentsToEntityUnknown_ThrowsException_Failure() { var ids = new List() { 1, 2, 3 }; var documentProcessingInfos = new List() { new() { DocumentProcessingInfoId = ids[0], }, new() { DocumentProcessingInfoId = ids[1], }, new() { DocumentProcessingInfoId = ids[2], } }; var request = new EntityUnknownRequest() { UserName = "System", DocumentProcessingInfoIds = documentProcessingInfos.Select(x => x.DocumentProcessingInfoId).ToList() }; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = 1, LogMessage = "Log test", LogTimeStamp = DateTime.Now, UserName = "System" }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindListByIdAsync(ids)).ReturnsAsync(documentProcessingInfos); _mockDocumentProcessingInfoRepository.SetupSequence(x => x.FindByIdAsync(It.IsAny())).ReturnsAsync(documentProcessingInfos[0]) .ReturnsAsync(documentProcessingInfos[1]) .ReturnsAsync(documentProcessingInfos[2]); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny())); _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(() => processingLog); _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.SetDocumentsToEntityUnknown(request)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindListByIdAsync(It.IsAny>()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Never); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); } // [Test] public async Task UpdateDocumentProcessingInfoStatus_Success() { var documentProcessingInfoId = DateTime.Now.Millisecond; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId, StatusType = ProcessStatusTypes.ArchiveFailure }; var request = new DocumentProcessingInfoStatusRequest() { DocumentProcessingInfoId = documentProcessingInfoId, ProcessStatus = ProcessStatusTypes.Reprocessing, ReasonMessage = Guid.NewGuid().ToString(), Username = Guid.NewGuid().ToString() }; var processingLog = new ProcessingLog() { DocumentProcessingInfoId = documentProcessingInfoId, LogMessage = request.ReasonMessage, LogTimeStamp = DateTime.Now, UserName = request.Username }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(documentProcessingInfoId)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny())); _mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(() => processingLog); var result = await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request); Assert.IsNotNull(result); Assert.AreEqual(result?.StatusType, request.ProcessStatus); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Once); } [Test] public async Task UpdateDocumentProcessingInfoStatus_ThrowsException_Failure() { var documentProcessingInfoId = DateTime.Now.Millisecond; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId, StatusType = ProcessStatusTypes.ArchiveFailure }; var request = new DocumentProcessingInfoStatusRequest() { DocumentProcessingInfoId = documentProcessingInfoId, ProcessStatus = ProcessStatusTypes.Reprocessing, ReasonMessage = Guid.NewGuid().ToString(), Username = Guid.NewGuid().ToString() }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(documentProcessingInfoId)).ReturnsAsync(documentProcessingInfo); _mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny())).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Exactly(2)); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task UpdateDocumentProcessingInfoStatus_RejectedStatus_Failure() { var documentProcessingInfoId = DateTime.Now.Millisecond; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId, StatusType = ProcessStatusTypes.Completed }; var request = new DocumentProcessingInfoStatusRequest() { DocumentProcessingInfoId = documentProcessingInfoId, ProcessStatus = ProcessStatusTypes.Reprocessing, ReasonMessage = Guid.NewGuid().ToString(), Username = Guid.NewGuid().ToString() }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(documentProcessingInfoId)).ReturnsAsync(documentProcessingInfo); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task UpdateDocumentProcessingInfoStatus_SameStatus_Failure() { var documentProcessingInfoId = DateTime.Now.Millisecond; var documentProcessingInfo = new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId, StatusType = ProcessStatusTypes.Reprocessing }; var request = new DocumentProcessingInfoStatusRequest() { DocumentProcessingInfoId = documentProcessingInfoId, ProcessStatus = ProcessStatusTypes.Reprocessing, ReasonMessage = Guid.NewGuid().ToString(), Username = Guid.NewGuid().ToString() }; _mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(documentProcessingInfoId)).ReturnsAsync(documentProcessingInfo); Assert.ThrowsAsync(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request)); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task GetByDocumentIdsAsync_RepositoryReturnsInfoForId_InfoAtIdKey() { var ids = Enumerable.Repeat(Guid.NewGuid, 5) .Select(f => f()) .ToArray(); var info = new DocumentProcessingInfo { DocumentId = ids[2] }; _mockDocumentProcessingInfoRepository .Setup(dpir => dpir.FindByDocumentIdsAsync(ids)) .ReturnsAsync(new[] { info }); var infoById = await _documentProcessingInfoService.GetByDocumentIdsAsync(ids); Assert.AreSame(infoById[info.DocumentId.Value], info); } [Test] public async Task GetByDocumentIdsAsync_RepositoryDoesNotReturnInfoForId_NullAtIdKey() { var ids = Enumerable.Repeat(Guid.NewGuid, 5) .Select(f => f()) .ToArray(); var info = new DocumentProcessingInfo { DocumentId = ids[2] }; _mockDocumentProcessingInfoRepository .Setup(dpir => dpir.FindByDocumentIdsAsync(ids)) .ReturnsAsync(Array.Empty()); var infoById = await _documentProcessingInfoService.GetByDocumentIdsAsync(ids); Assert.IsNull(infoById[info.DocumentId.Value]); } [Test] public async Task GetByDocumentIdsAsync_RepositoryThrowsException_ThrowsServiceException() { var ids = Enumerable.Repeat(Guid.NewGuid, 5) .Select(f => f()) .ToArray(); _mockDocumentProcessingInfoRepository .Setup(dpir => dpir.FindByDocumentIdsAsync(ids)) .ThrowsAsync(new Exception()); Assert.ThrowsAsync(() => _documentProcessingInfoService.GetByDocumentIdsAsync(ids)); } [Test] public async Task GetByDocumentIdsAsync_AllCases_AllInputIdsInDictionaryKeys() { var ids = Enumerable.Repeat(Guid.NewGuid, 5) .Select(f => f()) .ToArray(); var info = new DocumentProcessingInfo { DocumentId = ids[2] }; var otherInfo = new DocumentProcessingInfo { DocumentId = ids[4] }; _mockDocumentProcessingInfoRepository .Setup(dpir => dpir.FindByDocumentIdsAsync(ids)) .ReturnsAsync(new[] { info, otherInfo }); var infoById = await _documentProcessingInfoService.GetByDocumentIdsAsync(ids); CollectionAssert.AreEquivalent(ids, infoById.Keys); } [Test] public async Task GetByProcessStatusTypeAsync_Success() { var processStatus = ProcessStatusTypes.Processing; var documentProcessingInfos = GenerateDocumentProcessingInfos(); documentProcessingInfos.ForEach(x => x.StatusType.Equals(processStatus)); _mockDocumentProcessingInfoRepository.Setup(x => x.FindByProcessStatusTypeAsync(It.IsAny(), null)) .ReturnsAsync(documentProcessingInfos).Verifiable(); var result = await _documentProcessingInfoService.GetByProcessStatusTypeAsync(processStatus); Assert.AreSame(result, documentProcessingInfos); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByProcessStatusTypeAsync(It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetByProcessStatusType_Failure_Exception() { _mockDocumentProcessingInfoRepository.Setup(x => x.FindByProcessStatusTypeAsync(It.IsAny(), null)) .ThrowsAsync(new Exception("Test")); Assert.ThrowsAsync(() => _documentProcessingInfoService.GetByProcessStatusTypeAsync(It.IsAny())); _mockDocumentProcessingInfoRepository.Verify(x => x.FindByProcessStatusTypeAsync(It.IsAny(), It.IsAny()), Times.Once); } [Test] public async Task GetCompletedFirstPassByIngestorRunInfoIdAsync_Success() { var ingestorRunInfoId = Guid.NewGuid(); var documentProcessingInfos = GenerateDocumentProcessingInfos(); documentProcessingInfos.ForEach(x => x.IngestorRunInfoId = ingestorRunInfoId); _mockDocumentProcessingInfoRepository.Setup(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny())) .ReturnsAsync(documentProcessingInfos).Verifiable(); var result = await _documentProcessingInfoService.GetCompletedFirstPassByIngestorRunInfoIdAsync(ingestorRunInfoId); Assert.AreSame(documentProcessingInfos, result); _mockDocumentProcessingInfoRepository.Verify(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public void GetCompletedFirstPassByIngestorRunInfoIdAsync_Failure_Exception() { _mockDocumentProcessingInfoRepository.Setup(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")); Assert.ThrowsAsync(() => _documentProcessingInfoService.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny())); _mockDocumentProcessingInfoRepository.Verify(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public async Task SaveRangeAsync_Success() { var documentProcessingInfos = GenerateDocumentProcessingInfos().ToArray(); await _documentProcessingInfoService.SaveRangeAsync(documentProcessingInfos); _mockDocumentProcessingInfoRepository.Verify(x => x.AddRangeAsync(It.IsAny()), Times.Once()); _unitOfWork.Verify(x => x.CompleteAsync(), Times.Once); } [Test] public void SaveRangeAsync_Failure_Exception() { _mockDocumentProcessingInfoRepository.Setup(x => x.AddRangeAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")); Assert.ThrowsAsync(() => _documentProcessingInfoService.SaveRangeAsync(It.IsAny())); _mockDocumentProcessingInfoRepository.Verify(x => x.AddRangeAsync(It.IsAny()), Times.Once); _unitOfWork.Verify(x => x.CompleteAsync(), Times.Never); } private List GenerateDocumentProcessingInfos() => new() { GenerateDocumentProcessingInfo(), GenerateDocumentProcessingInfo(), GenerateDocumentProcessingInfo() }; private DocumentProcessingInfo GenerateDocumentProcessingInfo(Guid? documentId = null) => new() { DocumentId = documentId ?? Guid.NewGuid() }; }