Files
monorepo/LCU.API.Internal.DocumentIngestor/LCU.API.Internal.DocumentIngestorTests/Services/DocumentProcessingInfoServiceTest.cs
T
2026-03-25 14:50:13 -05:00

957 lines
38 KiB
C#

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<IUnitOfWork> _unitOfWork;
private Mock<IDocumentProcessingInfoRepository> _mockDocumentProcessingInfoRepository;
private Mock<IProcessingLogService> _mockProcessingLogService;
[SetUp]
public void Setup()
{
_unitOfWork = new Mock<IUnitOfWork>();
_mockDocumentProcessingInfoRepository = new Mock<IDocumentProcessingInfoRepository>();
_mockProcessingLogService = new Mock<IProcessingLogService>();
_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<DocumentProcessingInfo>()
{
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<ServiceException>(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<int>()), Times.Once);
}
[Test]
public void GetByIdAsync_Exception()
{
var id = 1;
_mockDocumentProcessingInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _documentProcessingInfoService.GetByIdAsync(id));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
}
[Test]
public async Task GetByIngestorRunInfoIdAsync_DocumentProcessingInfos_Returned()
{
var ingestorRunInfoId = Guid.NewGuid();
var id = 1;
var documentProcessingInfos = new List<DocumentProcessingInfo>()
{
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<Guid>()), Times.Once);
}
[Test]
public void GetByIngestorRunInfoIdAsync_Exception()
{
var ingestorRunInfoId = Guid.NewGuid();
_mockDocumentProcessingInfoRepository.Setup(x => x.FindByIngestorRunInfoIdAsync(ingestorRunInfoId)).ThrowsAsync(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _documentProcessingInfoService.GetByIngestorRunInfoIdAsync(ingestorRunInfoId));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIngestorRunInfoIdAsync(It.IsAny<Guid>()), 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<DocumentProcessingInfo>()), 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<ServiceException>(async () => await _documentProcessingInfoService.SaveAsync(documentProcessingInfo));
_mockDocumentProcessingInfoRepository.Verify(x => x.AddAsync(It.IsAny<DocumentProcessingInfo>()), 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<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), 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<ServiceException>(async () => await _documentProcessingInfoService.Update(documentProcessingInfo));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), 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<ServiceException>(async () => await _documentProcessingInfoService.Update(documentProcessingInfo));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), 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<ProcessingLog>())).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<ProcessingLog>()), 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<ServiceException>(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<ProcessingLog>()), 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<ProcessingLog>())).ReturnsAsync(processingLog);
Assert.ThrowsAsync<ServiceException>(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<ProcessingLog>()), 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<ProcessingLog>())).ReturnsAsync(processingLog);
var logMessages = new List<string>()
{
"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<ProcessingLog>()), 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<string>()
{
"Account fail",
"Name fail"
};
Assert.ThrowsAsync<ServiceException>(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<ProcessingLog>()), 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<ProcessingLog>())).ReturnsAsync(processingLog);
var logMessages = new List<string>()
{
"Account fail",
"Name fail"
};
Assert.ThrowsAsync<ServiceException>(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<ProcessingLog>()), 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<ProcessingLog>())).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<ProcessingLog>()), 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<ServiceException>(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<ProcessingLog>()), 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<ProcessingLog>())).ReturnsAsync(processingLog);
Assert.ThrowsAsync<ServiceException>(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<ProcessingLog>()), 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<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny<DocumentProcessingInfo>()), 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<ServiceException>(async () => await _documentProcessingInfoService.Delete(id));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny<DocumentProcessingInfo>()), 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<ServiceException>(async () => await _documentProcessingInfoService.Delete(id));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Remove(It.IsAny<DocumentProcessingInfo>()), Times.Once);
}
[Test]
public async Task FilterPaged_Ok()
{
var pagedDocumentProcessingInfoResult = new PagedResult<DocumentProcessingInfo>();
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<ServiceException>(() => _documentProcessingInfoService.CountCompletedFirstPassByIngestorRunInfoIdAsync(runId));
}
[Test]
public async Task SetDocumentsToEntityUnknown_Success()
{
var ids = new List<int>() { 1, 2, 3 };
var documentProcessingInfos = new List<DocumentProcessingInfo>()
{
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<int>())).ReturnsAsync(documentProcessingInfos[0])
.ReturnsAsync(documentProcessingInfos[1])
.ReturnsAsync(documentProcessingInfos[2]);
_mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny<DocumentProcessingInfo>()));
_mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny<ProcessingLog>())).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<List<int>>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Exactly(3));
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Exactly(3));
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Exactly(3));
}
[Test]
public async Task SetDocumentsToEntityUnknown_ThrowsException_Failure()
{
var ids = new List<int>() { 1, 2, 3 };
var documentProcessingInfos = new List<DocumentProcessingInfo>()
{
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<int>())).ReturnsAsync(documentProcessingInfos[0])
.ReturnsAsync(documentProcessingInfos[1])
.ReturnsAsync(documentProcessingInfos[2]);
_mockDocumentProcessingInfoRepository.Setup(x => x.Update(It.IsAny<DocumentProcessingInfo>()));
_mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny<ProcessingLog>())).ReturnsAsync(() => processingLog);
_mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny<ProcessingLog>())).Throws(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _documentProcessingInfoService.SetDocumentsToEntityUnknown(request));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindListByIdAsync(It.IsAny<List<int>>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Never);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Never);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), 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<DocumentProcessingInfo>()));
_mockProcessingLogService.Setup(x => x.SaveAsync(It.IsAny<ProcessingLog>())).ReturnsAsync(() => processingLog);
var result = await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request);
Assert.IsNotNull(result);
Assert.AreEqual(result?.StatusType, request.ProcessStatus);
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Exactly(2));
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Once);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), 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<DocumentProcessingInfo>())).Throws(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Exactly(2));
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Once);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), 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<ServiceException>(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Never);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), 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<ServiceException>(async () => await _documentProcessingInfoService.UpdateDocumentProcessingInfoStatus(request));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockDocumentProcessingInfoRepository.Verify(x => x.Update(It.IsAny<DocumentProcessingInfo>()), Times.Never);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), 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<DocumentProcessingInfo>());
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<ServiceException>(() => _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<ProcessStatusTypes>(), null))
.ReturnsAsync(documentProcessingInfos).Verifiable();
var result = await _documentProcessingInfoService.GetByProcessStatusTypeAsync(processStatus);
Assert.AreSame(result, documentProcessingInfos);
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByProcessStatusTypeAsync(It.IsAny<ProcessStatusTypes>(), It.IsAny<bool?>()), Times.Once);
}
[Test]
public void GetByProcessStatusType_Failure_Exception()
{
_mockDocumentProcessingInfoRepository.Setup(x => x.FindByProcessStatusTypeAsync(It.IsAny<ProcessStatusTypes>(), null))
.ThrowsAsync(new Exception("Test"));
Assert.ThrowsAsync<ServiceException>(() =>
_documentProcessingInfoService.GetByProcessStatusTypeAsync(It.IsAny<ProcessStatusTypes>()));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindByProcessStatusTypeAsync(It.IsAny<ProcessStatusTypes>(), It.IsAny<bool?>()), 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<Guid>()))
.ReturnsAsync(documentProcessingInfos).Verifiable();
var result = await _documentProcessingInfoService.GetCompletedFirstPassByIngestorRunInfoIdAsync(ingestorRunInfoId);
Assert.AreSame(documentProcessingInfos, result);
_mockDocumentProcessingInfoRepository.Verify(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()), Times.Once);
}
[Test]
public void GetCompletedFirstPassByIngestorRunInfoIdAsync_Failure_Exception()
{
_mockDocumentProcessingInfoRepository.Setup(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()))
.ThrowsAsync(new Exception("Test"));
Assert.ThrowsAsync<ServiceException>(() =>
_documentProcessingInfoService.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()));
_mockDocumentProcessingInfoRepository.Verify(x => x.FindCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()), Times.Once);
}
[Test]
public async Task SaveRangeAsync_Success()
{
var documentProcessingInfos = GenerateDocumentProcessingInfos().ToArray();
await _documentProcessingInfoService.SaveRangeAsync(documentProcessingInfos);
_mockDocumentProcessingInfoRepository.Verify(x => x.AddRangeAsync(It.IsAny<DocumentProcessingInfo[]>()), Times.Once());
_unitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
}
[Test]
public void SaveRangeAsync_Failure_Exception()
{
_mockDocumentProcessingInfoRepository.Setup(x => x.AddRangeAsync(It.IsAny<DocumentProcessingInfo[]>()))
.ThrowsAsync(new Exception("Test"));
Assert.ThrowsAsync<ServiceException>(() =>
_documentProcessingInfoService.SaveRangeAsync(It.IsAny<DocumentProcessingInfo[]>()));
_mockDocumentProcessingInfoRepository.Verify(x => x.AddRangeAsync(It.IsAny<DocumentProcessingInfo[]>()), Times.Once);
_unitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
}
private List<DocumentProcessingInfo> GenerateDocumentProcessingInfos()
=> new()
{
GenerateDocumentProcessingInfo(),
GenerateDocumentProcessingInfo(),
GenerateDocumentProcessingInfo()
};
private DocumentProcessingInfo GenerateDocumentProcessingInfo(Guid? documentId = null)
=> new()
{
DocumentId = documentId ?? Guid.NewGuid()
};
}