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; namespace LCU.API.Internal.DocumentIngestorTests.Services; public class PdfPartitionerLogServiceTest { private PdfPartitionerLogService _pdfPartitionerLogService; private Mock _unitOfWork; private Mock _mockIPdfPartitionerLogRepository; [SetUp] public void Setup() { _unitOfWork = new Mock(); _mockIPdfPartitionerLogRepository = new Mock(); _pdfPartitionerLogService = new PdfPartitionerLogService(_unitOfWork.Object, _mockIPdfPartitionerLogRepository.Object); } [Test] public async Task GetAsync_PdfPartitionerLogs_Returned() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLogs = new List() { new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId } }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindAsync()).ReturnsAsync(pdfPartitionerLogs); var result = await _pdfPartitionerLogService.GetAsync(); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockIPdfPartitionerLogRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public void GetAsync_Exception() { _mockIPdfPartitionerLogRepository.Setup(x => x.FindAsync()).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.GetAsync()); _mockIPdfPartitionerLogRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public async Task GetByIdAsync_PdfPartitionerLog_Returned() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByIdAsync(pdfPartitionerLogId)).ReturnsAsync(pdfPartitionerLog); var result = await _pdfPartitionerLogService.GetByIdAsync(pdfPartitionerLogId); Assert.IsNotNull(result); Assert.AreEqual(pdfPartitionerRunInfoId, result.PdfPartitionerRunInfoId); Assert.AreEqual(pdfPartitionerLogId, result.PdfPartitionerLogId); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByIdAsync_Exception() { var pdfPartitionerLogId = 2; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByIdAsync(pdfPartitionerLogId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.GetByIdAsync(pdfPartitionerLogId)); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public async Task GetByPdfPartitionerRunInfoIdAsync_PdfPartitionerLogs_Returned() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLogs = new List() { new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId } }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)).ReturnsAsync(pdfPartitionerLogs); var result = await _pdfPartitionerLogService.GetByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByPdfPartitionerRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByPdfPartitionerRunInfoIdAsync_Exception() { var pdfPartitionerRunInfoId = Guid.NewGuid(); _mockIPdfPartitionerLogRepository.Setup(x => x.FindByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.GetByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByPdfPartitionerRunInfoIdAsync(It.IsAny()), Times.Once); } [Test] public async Task SaveAsync_PdfPartitionerLog_Returned() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.AddAsync(pdfPartitionerLog)).ReturnsAsync(pdfPartitionerLog); var result = await _pdfPartitionerLogService.SaveAsync(pdfPartitionerLog); Assert.IsNotNull(result); Assert.AreEqual(pdfPartitionerRunInfoId, result.PdfPartitionerRunInfoId); Assert.AreEqual(pdfPartitionerLogId, result.PdfPartitionerLogId); _mockIPdfPartitionerLogRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public void SaveAsync_Exception() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.AddAsync(pdfPartitionerLog)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.SaveAsync(pdfPartitionerLog)); _mockIPdfPartitionerLogRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public async Task Delete_Success() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByIdAsync(pdfPartitionerLogId)).ReturnsAsync(pdfPartitionerLog); _mockIPdfPartitionerLogRepository.Setup(x => x.Remove(pdfPartitionerLog)); await _pdfPartitionerLogService.Delete(pdfPartitionerLogId); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIPdfPartitionerLogRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } [Test] public void Delete_FindByIdAsync_ThrowsException() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByIdAsync(pdfPartitionerLogId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.Delete(pdfPartitionerLogId)); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIPdfPartitionerLogRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); } [Test] public void Delete_Remove_ThrowsException() { var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogId = 2; var pdfPartitionerLog = new PdfPartitionerLog() { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockIPdfPartitionerLogRepository.Setup(x => x.FindByIdAsync(pdfPartitionerLogId)).ReturnsAsync(pdfPartitionerLog); _mockIPdfPartitionerLogRepository.Setup(x => x.Remove(pdfPartitionerLog)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _pdfPartitionerLogService.Delete(pdfPartitionerLogId)); _mockIPdfPartitionerLogRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockIPdfPartitionerLogRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } }