using System; using System.Threading.Tasks; using Moq; using NUnit.Framework; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.Exceptions; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestorLib.Repositories; namespace LCU.API.Internal.DocumentIngestorTests.Services; public class IngestorRunInfoServiceTest { private IngestorRunInfoService _ingestorRunInfoService; private Mock _unitOfWork; private Mock _mockIngestorRunInfoRepository; [SetUp] public void Setup() { _unitOfWork = new Mock(); _mockIngestorRunInfoRepository = new Mock(); _ingestorRunInfoService = new IngestorRunInfoService(_unitOfWork.Object, _mockIngestorRunInfoRepository.Object); } [Test] public async Task GetByIdAsync_IngestorRunInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ReturnsAsync(ingestorRunInfo); var result = await _ingestorRunInfoService.GetByIdAsync(ingestorRunInfoId); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByIdAsync_Exception() { var ingestorRunInfoId = Guid.NewGuid(); _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.GetByIdAsync(ingestorRunInfoId)); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public async Task SaveAsync_IngestorRunInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.AddAsync(ingestorRunInfo)).ReturnsAsync(ingestorRunInfo); var result = await _ingestorRunInfoService.SaveAsync(ingestorRunInfo); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); _mockIngestorRunInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public void SaveAsync_Exception() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.AddAsync(ingestorRunInfo)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.SaveAsync(ingestorRunInfo)); _mockIngestorRunInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public async Task Update_IngestorRunInfo_Returned() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ReturnsAsync(ingestorRunInfo); _mockIngestorRunInfoRepository.Setup(x => x.Update(ingestorRunInfo)); var result = await _ingestorRunInfoService.Update(ingestorRunInfo); Assert.IsNotNull(result); Assert.AreEqual(ingestorRunInfoId, result.IngestorRunInfoId); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public void Update_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.Update(ingestorRunInfo)); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); } [Test] public void Update_Update_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ReturnsAsync(ingestorRunInfo); _mockIngestorRunInfoRepository.Setup(x => x.Update(ingestorRunInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.Update(ingestorRunInfo)); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public async Task Delete_Success() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ReturnsAsync(ingestorRunInfo); _mockIngestorRunInfoRepository.Setup(x => x.Remove(ingestorRunInfo)); await _ingestorRunInfoService.Delete(ingestorRunInfoId); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } [Test] public void Delete_FindByIdAsync_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.Delete(ingestorRunInfoId)); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); } [Test] public void Delete_Remove_ThrowsException() { var ingestorRunInfoId = Guid.NewGuid(); var ingestorRunInfo = new IngestorRunInfo() { IngestorRunInfoId = ingestorRunInfoId }; _mockIngestorRunInfoRepository.Setup(x => x.FindByIdAsync(ingestorRunInfoId)).ReturnsAsync(ingestorRunInfo); _mockIngestorRunInfoRepository.Setup(x => x.Remove(ingestorRunInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _ingestorRunInfoService.Delete(ingestorRunInfoId)); _mockIngestorRunInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIngestorRunInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } }