using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.LoanRateModels.Entities; using LCU.API.Internal.LoanRateLib.Repositories; using LCU.API.Internal.LoanRate.Services; namespace LCU.API.Internal.LoanRateTests.ServiceTests; public class RateEvaluationServiceTest { private readonly Mock _mockUnitOfWork; private readonly Mock _mockRateEvaluationRepository; private readonly Mock _mockRateService; private readonly IRateEvaluationService _service; public RateEvaluationServiceTest() { _mockRateEvaluationRepository = new Mock(); _mockUnitOfWork = new Mock(); _mockRateService = new Mock(); _service = new RateEvaluationService(_mockUnitOfWork.Object, _mockRateEvaluationRepository.Object, _mockRateService.Object); } [Fact] public async void RateEvaluation_GetAsync_Success() { var rateEvaluations = new List { new() { RateEvaluationId = 1 }, new() { RateEvaluationId = 2 }, new() { RateEvaluationId = 3 } }; _mockRateEvaluationRepository.Setup(x => x.FindAsync()) .ReturnsAsync(rateEvaluations).Verifiable(); var result = (await _service.GetAsync()).ToList(); Assert.NotNull(result); Assert.Equal(rateEvaluations.First().RateEvaluationId, result.First().RateEvaluationId); Assert.Equal(rateEvaluations.Count, result.Count); _mockRateEvaluationRepository.Verify(); } [Fact] public async void RateEvaluation_GetAsync_Success_NotFound() { List? rateEvaluations = null; _mockRateEvaluationRepository.Setup(x => x.FindAsync()) .ReturnsAsync(rateEvaluations).Verifiable(); var result = await _service.GetAsync(); Assert.NotNull(result); Assert.Empty(result); _mockRateEvaluationRepository.Verify(); } [Fact] public void RateEvaluation_GetAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.FindAsync()) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.GetAsync()); _mockRateEvaluationRepository.Verify(x => x.FindAsync(), Times.Once); } [Fact] public async void RateEvaluation_GetByIdAsync_Success() { var rateEvaluationId = 1; var rateEvaluation = new RateEvaluation { RateEvaluationId = rateEvaluationId }; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId)) .ReturnsAsync(rateEvaluation).Verifiable(); var result = await _service.GetByIdAsync(rateEvaluationId); Assert.NotNull(result); Assert.Equal(rateEvaluation.RateEvaluationId, result.RateEvaluationId); _mockRateEvaluationRepository.Verify(); } [Fact] public async void RateEvaluation_GetByIdAsync_Success_NotFound() { var rateEvaluationId = 1; RateEvaluation? rateEvaluation = null; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId)) .ReturnsAsync(rateEvaluation).Verifiable(); var result = await _service.GetByIdAsync(rateEvaluationId); Assert.Null(result); _mockRateEvaluationRepository.Verify(); } [Fact] public void RateEvaluation_GetByIdAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.GetByIdAsync(It.IsAny())); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Fact] public async void RateEvaluation_GetRangeByIdAsync_Success() { var rateEvaluations = new List { new() { RateEvaluationId = 1 }, new() { RateEvaluationId = 2 }, new() { RateEvaluationId = 3 } }; var ids = rateEvaluations.Select(x => x.RateEvaluationId).ToList(); _mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(ids)) .ReturnsAsync(rateEvaluations).Verifiable(); var result = (await _service.GetRangeByIdAsync(ids)).ToList(); Assert.NotNull(result); Assert.Equal(rateEvaluations.First().RateEvaluationId, result.First().RateEvaluationId); Assert.Equal(rateEvaluations.Count, result.Count); _mockRateEvaluationRepository.Verify(); } [Fact] public async void RateEvaluation_GetRangeByIdAsync_Success_NotFound() { List? rateEvaluations = null; var ids = new List { 1,2,3 }; _mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(ids)) .ReturnsAsync(rateEvaluations).Verifiable(); var result = await _service.GetRangeByIdAsync(ids); Assert.NotNull(result); Assert.Empty(result); _mockRateEvaluationRepository.Verify(); } [Fact] public void RateEvaluation_GetRangeByIdAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(It.IsAny>())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.GetRangeByIdAsync(It.IsAny>())); _mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny>()), Times.Once); } [Fact] public async void RateEvaluation_SaveAsync_Success() { var rateEvaluation = new RateEvaluation { RateId = 1, }; var rateEvaluationReturned = new RateEvaluation { RateEvaluationId = 1, RateId = 2 }; _mockRateEvaluationRepository.Setup(x => x.AddAsync(rateEvaluation)) .ReturnsAsync(rateEvaluationReturned).Verifiable(); var result = await _service.SaveAsync(rateEvaluation); Assert.NotNull(result); Assert.Equal(rateEvaluationReturned.RateEvaluationId, result.RateEvaluationId); Assert.Equal(rateEvaluationReturned.RateId, result.RateId); _mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_SaveAsync_Failure_ReturnedNull() { var rateEvaluation = new RateEvaluation { RateId = 1 }; RateEvaluation? rateEvaluationReturned = null; _mockRateEvaluationRepository.Setup(x => x.AddAsync(rateEvaluation)) .ReturnsAsync(rateEvaluationReturned).Verifiable(); Assert.ThrowsAsync(async () => await _service.SaveAsync(rateEvaluation)); _mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public void RateEvaluation_SaveAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.AddAsync(It.IsAny())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.SaveAsync(It.IsAny())); _mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_SaveRangeAsync_Success() { var rateEvaluations = new List { new() { RateId = 1 }, new() { RateId = 2 }, new() { RateId = 3 } }; _mockRateEvaluationRepository.Setup(x => x.AddRangeAsync(rateEvaluations)).Verifiable(); await _service.SaveRangeAsync(rateEvaluations); _mockRateEvaluationRepository.Verify(x => x.AddRangeAsync(It.IsAny>()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_SaveRangeAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.AddRangeAsync(It.IsAny>())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.SaveRangeAsync(It.IsAny>())); _mockRateEvaluationRepository.Verify(x => x.AddRangeAsync(It.IsAny>()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_UpdateAsync_Success() { var rateEvaluation = new RateEvaluation { RateEvaluationId = 1, RateId = 1 }; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluation.RateEvaluationId)) .ReturnsAsync(rateEvaluation).Verifiable(); var result = await _service.UpdateAsync(rateEvaluation); Assert.NotNull(result); Assert.Equal(rateEvaluation.RateEvaluationId, result.RateEvaluationId); Assert.Equal(rateEvaluation.RateId, result.RateId); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once()); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_UpdateAsync_Failure_ReturnedNull() { var rateEvaluation = new RateEvaluation { RateId = 1 }; RateEvaluation? rateEvaluationReturned = null; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluation.RateEvaluationId)) .ReturnsAsync(rateEvaluationReturned).Verifiable(); Assert.ThrowsAsync(async () => await _service.UpdateAsync(rateEvaluation)); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public void RateEvaluation_UpdateAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.UpdateAsync(It.IsAny())); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_UpdateRangeAsync_Success() { var rateEvaluations = new List { new() { RateEvaluationId = 1, RateId = 1 }, new() { RateEvaluationId = 2, RateId = 2 }, new() { RateEvaluationId = 3, RateId = 3 } }; _mockRateEvaluationRepository.Setup(x => x.UpdateRange(rateEvaluations)).Verifiable(); await _service.UpdateRangeAsync(rateEvaluations); _mockRateEvaluationRepository.Verify(x => x.UpdateRange(It.IsAny>()), Times.Once()); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_UpdateRangeAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.UpdateRange(It.IsAny>())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.UpdateRangeAsync(It.IsAny>())); _mockRateEvaluationRepository.Verify(x => x.UpdateRange(It.IsAny>()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_DeleteAsync_Success() { var rateEvaluationId = 1; var rateEvaluation = new RateEvaluation { RateEvaluationId = rateEvaluationId, RateId = 1 }; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId)) .ReturnsAsync(rateEvaluation).Verifiable(); _mockRateEvaluationRepository.Setup(x => x.Remove(rateEvaluation)).Verifiable(); await _service.DeleteAsync(rateEvaluationId); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once()); _mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny()), Times.Once()); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_DeleteAsync_Failure_ReturnedNull() { var rateEvaluationId = 1; RateEvaluation? rateEvaluation = null; _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId)) .ReturnsAsync(rateEvaluation).Verifiable(); Assert.ThrowsAsync(async () => await _service.DeleteAsync(rateEvaluationId)); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once()); _mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public void RateEvaluation_DeleteAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.DeleteAsync(It.IsAny())); _mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_DeleteRangeAsync_Success() { var rateEvaluationIds = new List { 1, 2, 3 }; var rateEvaluations = new List { new() { RateEvaluationId = rateEvaluationIds[0], RateId = 1 }, new() { RateEvaluationId = rateEvaluationIds[1], RateId = 2 }, new() { RateEvaluationId = rateEvaluationIds[2], RateId = 3 } }; _mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(rateEvaluationIds)).ReturnsAsync(rateEvaluations).Verifiable(); _mockRateEvaluationRepository.Setup(x => x.RemoveRange(rateEvaluations)).Verifiable(); await _service.DeleteRangeAsync(rateEvaluationIds); _mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny>()), Times.Once()); _mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny>()), Times.Once()); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once); _mockRateService.Verify(x => x.ClearCache(), Times.Once); } [Fact] public void RateEvaluation_DeleteRangeAsync_Failure_ReturnedNull() { var rateEvaluationIds = new List { 1, 2, 3 }; List? rateEvaluations = null; _mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(rateEvaluationIds)) .ReturnsAsync(rateEvaluations).Verifiable(); Assert.ThrowsAsync(async () => await _service.DeleteRangeAsync(rateEvaluationIds)); _mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny>()), Times.Once()); _mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny>()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public void RateEvaluation_DeleteRangeAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.RemoveRange(It.IsAny>())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.DeleteRangeAsync(It.IsAny>())); _mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny>()), Times.Once()); _mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny>()), Times.Never); _mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); _mockRateService.Verify(x => x.ClearCache(), Times.Never); } [Fact] public async void RateEvaluation_CountByEvaluationDetailIdAsync_Success() { var evaluationDetailId = DateTime.Now.Millisecond; var count = DateTime.Now.Minute; _mockRateEvaluationRepository.Setup(x => x.CountByEvaluationDetailIdAsync(evaluationDetailId)).ReturnsAsync(count).Verifiable(); await _service.CountByEvaluationDetailIdAsync(evaluationDetailId); _mockRateEvaluationRepository.Verify(x => x.CountByEvaluationDetailIdAsync(It.IsAny()), Times.Once()); } [Fact] public void RateEvaluation_CountByEvaluationDetailIdAsync_Failure_Exception() { _mockRateEvaluationRepository.Setup(x => x.CountByEvaluationDetailIdAsync(It.IsAny())) .Throws(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await _service.CountByEvaluationDetailIdAsync(It.IsAny())); _mockRateEvaluationRepository.Verify(x => x.CountByEvaluationDetailIdAsync(It.IsAny()), Times.Once()); } }