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 _refundStatusRepository; private Mock _unitOfWork; private Mock _repoAccountService; private Mock _emailer; public RefundStatusServiceTest() { _refundStatusRepository = new Mock(); _unitOfWork = new Mock(); _repoAccountService = new Mock(); _emailer = new Mock(); _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())).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())).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())).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())).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())).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())).ReturnsAsync( new RefundStatus()); _repoAccountService.Setup(service => service.Get(It.IsAny())).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())).ReturnsAsync( new RefundStatus()); _repoAccountService.Setup(service => service.Get(It.IsAny())).ReturnsAsync( new RepoAccount()); _emailer.Setup(emailer => emailer.SendSurplusDeficiencyLetterEmailAsync(It.IsAny())).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()), 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."); } } } }