Files
2026-04-20 14:47:24 -05:00

181 lines
6.5 KiB
C#

using System;
using System.Net.Http;
using LCU.API.Collections.RepoApi.Exceptions;
using LCU.API.Collections.RepoApi.Services;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Repositories;
using Moq;
using Xunit;
namespace LCU.API.Collections.RepoApiTests.Services
{
public class RefundStatusServiceTest
{
private IRefundStatusService _refundStatusService;
private Mock<IRefundStatusRepository> _refundStatusRepository;
private Mock<IUnitOfWork> _unitOfWork;
private Mock<IRepoAccountService> _repoAccountService;
private Mock<IEmailer> _emailer;
public RefundStatusServiceTest()
{
_refundStatusRepository = new Mock<IRefundStatusRepository>();
_unitOfWork = new Mock<IUnitOfWork>();
_repoAccountService = new Mock<IRepoAccountService>();
_emailer = new Mock<IEmailer>();
_refundStatusService = new RefundStatusService(
_refundStatusRepository.Object,
_unitOfWork.Object,
_repoAccountService.Object,
_emailer.Object);
}
[Fact]
public async void GetAsync_Ok()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ReturnsAsync(
new RefundStatus());
// Act
var result = await _refundStatusService.GetAsync(1);
// Assert
Assert.NotNull(result);
}
[Fact]
public async void GetAsync_Throws_NFE_When_RefundStatusRepository_Throws_Exception()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ThrowsAsync(
new Exception());
// Act / Assert
try
{
var result = await _refundStatusService.GetAsync(1);
Assert.True(false, "An Exception in the RefundStatusRepository should have triggered an Exception upward. No Exception triggered.");
}
catch (NotFoundException) { }
catch (Exception)
{
Assert.True(false, "A NotFoundException should be thrown when any Exception occurs in RefundStatusRepository. Another Exception was thrown instead.");
}
}
[Fact]
public async void GetAsync_Throws_NFE_When_RefundStatusRepository_Returns_Null()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ReturnsAsync((RefundStatus) null);
// Act / Assert
try
{
var result = await _refundStatusService.GetAsync(1);
Assert.True(false, "An Exception should have been thrown when the RefundStatusRepository returns a null value. No Exception triggered.");
}
catch (NotFoundException) { }
catch (Exception)
{
Assert.True(false, "A NotFoundException should be thrown when the RefundStatusRepository returns a null value. Another Exception was thrown instead.");
}
}
[Fact]
public async void SaveASync_Ok()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.AddAsync(It.IsAny<RefundStatus>())).ReturnsAsync(
new RefundStatus());
// Act
var result = await _refundStatusService.SaveAsync(new RefundStatus()
{
RepoId = 123
});
// Assert
Assert.NotNull(result);
}
[Fact]
public async void SaveAsynhc_Throws_Exception_When_RepoAccountService_Throws_Exception()
{
// Arrange
_repoAccountService.Setup(service => service.Get(It.IsAny<int>())).ThrowsAsync(
new Exception());
// Act / Assert
try
{
var result = await _refundStatusService.SaveAsync(new RefundStatus() { RepoId = 123 });
Assert.True(false, "An Exception in the RefundStatusRepository should have triggered an Exception upward. No Exception triggered.");
}
catch (Exception) { }
}
[Fact]
public async void UpdateAsync_Ok()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ReturnsAsync(
new RefundStatus());
_repoAccountService.Setup(service => service.Get(It.IsAny<int>())).ReturnsAsync(
new RepoAccount());
// Act
var result = await _refundStatusService.UpdateAsync(new RefundStatus()
{
RepoId = 123
});
// Assert
Assert.NotNull(result);
}
[Fact]
public async void UpdateAsync_With_Refunds_Received_Date_Sends_Email()
{
// Arrange
_refundStatusRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ReturnsAsync(
new RefundStatus());
_repoAccountService.Setup(service => service.Get(It.IsAny<int>())).ReturnsAsync(
new RepoAccount());
_emailer.Setup(emailer => emailer.SendSurplusDeficiencyLetterEmailAsync(It.IsAny<RepoAccount>())).ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
// Act
var result = await _refundStatusService.UpdateAsync(new RefundStatus()
{
RepoId = 123,
RefundsReceivedDate = DateTime.Now
});
// Assert
Assert.NotNull(result);
Assert.NotNull(result.RefundsReceivedDate);
_emailer.Verify(x => x.SendSurplusDeficiencyLetterEmailAsync(It.IsAny<RepoAccount>()), Times.Once);
}
[Fact]
public async void UpdateAsync_Returns_NFE_When_RefundStatusRepository_Throws_NFE()
{
// Act / Assert
try
{
await _refundStatusService.UpdateAsync(new RefundStatus()
{
RepoId = 123
});
Assert.True(false, "A Failure to Find a RefundStatus should have sent an Exception upward. No Exception triggered.");
}
catch (NotFoundException) { }
catch (Exception)
{
Assert.True(false, "A Failure to Find a RefundStatus should have trigerred a NotFoundException. Another Exception was thrown instead.");
}
}
}
}